Skip to content

Commit

Permalink
Improve library menu button
Browse files Browse the repository at this point in the history
The color of the menu button for library entries now adjusts after the
thumbnail image of the game, to make a better contrast
  • Loading branch information
axtloss authored and mirkobrombin committed Jul 27, 2022
1 parent f6c51ac commit a6a6ca2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
9 changes: 9 additions & 0 deletions com.usebottles.bottles.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ cleanup-commands:
modules:
# Python modules
# ----------------------------------------------------------------------------
- name: python-pillow
buildsystem: simple
build-commands:
- python3 setup.py install --prefix=/app --root=/
sources:
- type: archive
url: https://github.com/python-pillow/Pillow/archive/refs/tags/9.2.0.tar.gz
sha256: 95836f00972dbf724bf1270178683a0ac4ea23c6c3a980858fc9f2f9456e32ef

- name: PyYAML
buildsystem: simple
build-commands:
Expand Down
6 changes: 3 additions & 3 deletions src/ui/library-entry.ui
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@
<property name="width-request">256</property>
<property name="height-request">384</property>
<child type="overlay">
<object class="GtkMenuButton">
<object class="GtkMenuButton" id="btn_menu">
<property name="halign">start</property>
<property name="valign">start</property>
<property name="margin-top">5</property>
<property name="margin-start">5</property>
<property name="popover">pop_context</property>
<property name="icon-name">view-more-symbolic</property>
<style>
<class name="osd"/>
<class name="circular"/>
<class name="flat"/>
</style>
</object>
</child>
Expand Down Expand Up @@ -153,3 +152,4 @@
</style>
</template>
</interface>

15 changes: 14 additions & 1 deletion src/views/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ class LibraryView(Adw.Bin):
scroll_window = Gtk.Template.Child()
main_flow = Gtk.Template.Child()
status_page = Gtk.Template.Child()
style_provider = Gtk.CssProvider()
# endregion

def __init__(self, window, **kwargs):
super().__init__(**kwargs)
self.window = window
self.css = b""
self.update()

def update(self):
Expand All @@ -58,5 +60,16 @@ def remove_entry(self, uuid):
library_manager.remove_from_library(uuid)
self.update()

def add_css_entry(self, entry, color):
gtk_context = self.get_style_context()
Gtk.StyleContext.add_class(entry.btn_menu.get_style_context(), entry.entry['name']+"_menu_button")
self.css = self.css+b"\n"+b"."+bytes(entry.entry["name"], 'utf-8')+b"_menu_button { color: rgba("+bytes(str(color), 'utf-8')+b","+bytes(str(color), 'utf-8')+b","+bytes(str(color), 'utf-8')+b", 255); }"
self.style_provider.load_from_data(self.css)
Gtk.StyleContext.add_provider(
entry.btn_menu.get_style_context(),
self.style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_USER
)

def go_back(self, widget=False):
self.window.main_leaf.navigate(Adw.NavigationDirection.BACK)
self.window.main_leaf.navigate(Adw.NavigationDirection.BACK)
13 changes: 13 additions & 0 deletions src/widgets/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import logging
import os
import math
from PIL import Image
from datetime import datetime
from gettext import gettext as _
from gi.repository import Gtk, Gdk, GLib, GdkPixbuf, Adw
Expand All @@ -43,6 +45,7 @@ class LibraryEntry(Gtk.Box):
label_no_cover = Gtk.Template.Child()
img_cover = Gtk.Template.Child()
img_icon = Gtk.Template.Child()
btn_menu = Gtk.Template.Child()

# endregion

Expand Down Expand Up @@ -79,6 +82,7 @@ def __init__(self, library, uuid, entry, *args, **kwargs):
#self.img_cover.set_paintable(texture)
self.img_cover.set_visible(True)
self.label_no_cover.set_visible(False)
self.__calculate_button_color(path=path)

self.btn_run.connect("clicked", self.run_executable)
self.btn_launch_steam.connect("clicked", self.run_steam)
Expand Down Expand Up @@ -147,6 +151,15 @@ def set_watcher(result=False, error=False):
def __remove_entry(self, widget):
self.library.remove_entry(self.uuid)

def __calculate_button_color(self, path):
image = Image.open(path)
image = image.crop((0, 0, 47, 58))
image.thumbnail((150, 150))
palette = image.convert('P', palette=Image.ADAPTIVE, colors=1).getpalette()
rgb = (255-palette[0], 255-palette[1], 255-palette[2])
button_color = math.floor(0.299*rgb[0])+math.floor(0.587*rgb[1])+math.floor(0.144*rgb[2])
self.library.add_css_entry(entry=self, color=button_color)

def run_executable(self, widget, with_terminal=False):
executor = WineExecutor(
self.config,
Expand Down

0 comments on commit a6a6ca2

Please sign in to comment.