Totally unnecessary algorithm

This commit is contained in:
kramo
2023-04-07 23:54:42 +02:00
parent 9986e35365
commit a9e67595f7
2 changed files with 55 additions and 12 deletions

View File

@@ -129,10 +129,6 @@ template CartridgesWindow : Adw.ApplicationWindow {
wrap: true; wrap: true;
wrap-mode: word_char; wrap-mode: word_char;
natural-wrap-mode: word; natural-wrap-mode: word;
styles [
"dim-label",
]
} }
Label overview_last_played { Label overview_last_played {
margin-top: 6; margin-top: 6;
@@ -141,10 +137,6 @@ template CartridgesWindow : Adw.ApplicationWindow {
wrap: true; wrap: true;
wrap-mode: word_char; wrap-mode: word_char;
natural-wrap-mode: word; natural-wrap-mode: word;
styles [
"dim-label",
]
} }
Box { Box {
hexpand: true; hexpand: true;
@@ -187,7 +179,6 @@ template CartridgesWindow : Adw.ApplicationWindow {
] ]
Picture overview_blurred_cover { Picture overview_blurred_cover {
opacity: 0.2;
can-shrink: true; can-shrink: true;
keep-aspect-ratio: false; keep-aspect-ratio: false;
hexpand: true; hexpand: true;

View File

@@ -19,6 +19,7 @@
import datetime import datetime
import os import os
import struct
from pathlib import Path from pathlib import Path
from gi.repository import Adw, GdkPixbuf, Gio, GLib, Gtk from gi.repository import Adw, GdkPixbuf, Gio, GLib, Gtk
@@ -96,6 +97,7 @@ class CartridgesWindow(Adw.ApplicationWindow):
self.pixbufs = {} self.pixbufs = {}
self.active_game_id = None self.active_game_id = None
self.loading = None self.loading = None
self.scaled_pixbuf = None
self.overview.set_measure_overlay(self.overview_box, True) self.overview.set_measure_overlay(self.overview_box, True)
self.overview.set_clip_overlay(self.overview_box, False) self.overview.set_clip_overlay(self.overview_box, False)
@@ -127,6 +129,13 @@ class CartridgesWindow(Adw.ApplicationWindow):
back_mouse_button.connect("pressed", self.on_go_back_action) back_mouse_button.connect("pressed", self.on_go_back_action)
self.add_controller(back_mouse_button) self.add_controller(back_mouse_button)
Adw.StyleManager.get_default().connect(
"notify::dark", self.set_overview_opacity
)
Adw.StyleManager.get_default().connect(
"notify::high-contrast", self.set_overview_opacity
)
def update_games(self, games): def update_games(self, games):
current_games = get_games(self) current_games = get_games(self)
@@ -270,9 +279,11 @@ class CartridgesWindow(Adw.ApplicationWindow):
pixbuf = current_game.pixbuf pixbuf = current_game.pixbuf
self.overview_cover.set_pixbuf(pixbuf) self.overview_cover.set_pixbuf(pixbuf)
self.overview_blurred_cover.set_pixbuf(
pixbuf.scale_simple(2, 3, GdkPixbuf.InterpType.BILINEAR) self.scaled_pixbuf = pixbuf.scale_simple(2, 3, GdkPixbuf.InterpType.BILINEAR)
) self.overview_blurred_cover.set_pixbuf(self.scaled_pixbuf)
self.set_overview_opacity()
self.overview_title.set_label(current_game.name) self.overview_title.set_label(current_game.name)
self.overview_header_bar_title.set_title(current_game.name) self.overview_header_bar_title.set_title(current_game.name)
date = self.get_time(current_game.added) date = self.get_time(current_game.added)
@@ -290,6 +301,47 @@ class CartridgesWindow(Adw.ApplicationWindow):
_("Last played: {}").format(last_played_date) _("Last played: {}").format(last_played_date)
) )
def set_overview_opacity(self, _widget=None, _unused=None):
if self.stack.get_visible_child() == self.overview:
style_manager = Adw.StyleManager.get_default()
if style_manager.get_high_contrast():
self.overview_blurred_cover.set_opacity(0)
return
if not style_manager.get_system_supports_color_schemes():
self.overview_blurred_cover.set_opacity(0.2)
return
pixels = self.scaled_pixbuf.get_pixels()
channels = self.scaled_pixbuf.get_n_channels()
colors = set()
for index in range(6):
colors.add(struct.unpack_from("BBBB", pixels, offset=index * channels))
dark_theme = style_manager.get_dark()
luminances = []
for red, green, blue, alpha in colors:
# https://en.wikipedia.org/wiki/Relative_luminance
luminance = red * 0.2126 + green * 0.7152 + blue * 0.0722
if dark_theme:
luminances.append((luminance * alpha) / 255**2)
else:
luminances.append(1 - (alpha / 255) * (1 - luminance / 255))
if dark_theme:
self.overview_blurred_cover.set_opacity(
1.3 - (sum(luminances) / len(luminances) + max(luminances)) / 2
)
else:
self.overview_blurred_cover.set_opacity(
0.2 + (sum(luminances) / len(luminances) + min(luminances)) / 2
)
def a_z_sort(self, child1, child2): def a_z_sort(self, child1, child2):
name1 = child1.get_first_child().name.lower() name1 = child1.get_first_child().name.lower()
name2 = child2.get_first_child().name.lower() name2 = child2.get_first_child().name.lower()