[gnome-music/wip/mschraal/rework-art-widget: 19/26] artstack: Move dark mode switch to CoverPaintable



commit 3ba011c967d35386f1f5dd6ccc32bf72df86cc98
Author: Marinus Schraal <mschraal gnome org>
Date:   Sat Apr 2 11:33:25 2022 +0200

    artstack: Move dark mode switch to CoverPaintable

 gnomemusic/coverpaintable.py   | 21 +++++++++++++++------
 gnomemusic/widgets/artstack.py | 27 ++++-----------------------
 2 files changed, 19 insertions(+), 29 deletions(-)
---
diff --git a/gnomemusic/coverpaintable.py b/gnomemusic/coverpaintable.py
index 540f2371d..e5a64df45 100644
--- a/gnomemusic/coverpaintable.py
+++ b/gnomemusic/coverpaintable.py
@@ -26,7 +26,7 @@ from __future__ import annotations
 
 import gi
 gi.require_versions({"Gdk": "4.0", "Gtk": "4.0", "Gsk": "4.0"})
-from gi.repository import Gsk, Gtk, GObject, Graphene, Gdk
+from gi.repository import Adw, Gsk, Gtk, GObject, Graphene, Gdk
 
 from gnomemusic.utils import ArtSize, DefaultIconType
 
@@ -43,7 +43,7 @@ class CoverPaintable(GObject.GObject, Gdk.Paintable):
     def __init__(
             self, art_size: ArtSize, widget: Gtk.Widget,
             icon_type: DefaultIconType = DefaultIconType.ALBUM,
-            texture: Gdk.Texture = None, dark: bool = False) -> None:
+            texture: Gdk.Texture = None) -> None:
         """Initiliaze CoverPaintable
 
         :param ArtSize art_size: Size of the cover
@@ -51,18 +51,19 @@ class CoverPaintable(GObject.GObject, Gdk.Paintable):
         :param DefaultIconType icon_type: Type of cover
         :param Gdk.Texture texture: Texture to use or None for
             placeholder
-        :param bool dark: Dark mode
         """
         super().__init__()
 
         self._art_size = art_size
-        self._dark = dark
         self._icon_theme = Gtk.IconTheme.new().get_for_display(
             widget.get_display())
         self._icon_type = icon_type
+        self._style_manager = Adw.StyleManager.get_default()
         self._texture = texture
         self._widget = widget
 
+        self._style_manager.connect("notify::dark", self._on_dark_changed)
+
     def do_snapshot(self, snapshot: Gtk.Snapshot, w: int, h: int) -> None:
         if self._icon_type == DefaultIconType.ARTIST:
             radius = 90.0
@@ -95,7 +96,7 @@ class CoverPaintable(GObject.GObject, Gdk.Paintable):
                 self._widget.props.scale_factor, 0, 0)
 
             bg_color = Gdk.RGBA(1, 1, 1, 1)
-            if self._dark:
+            if self._style_manager.props.dark:
                 bg_color = Gdk.RGBA(0.3, 0.3, 0.3, 1)
 
             snapshot.append_color(bg_color, Graphene.Rect().init(0, 0, w, h))
@@ -108,8 +109,16 @@ class CoverPaintable(GObject.GObject, Gdk.Paintable):
 
         snapshot.pop()
 
+    def _on_dark_changed(
+            self, style_manager: Adw.StyleManager,
+            pspec: GObject.ParamSpecBoolean) -> None:
+        if self._texture is not None:
+            return
+
+        self.invalidate_contents()
+
     def do_get_flags(self) -> Gdk.PaintableFlags:
-        return Gdk.PaintableFlags.SIZE | Gdk.PaintableFlags.CONTENTS
+        return Gdk.PaintableFlags.SIZE
 
     def do_get_intrinsic_height(self) -> int:
         return self._art_size.height
diff --git a/gnomemusic/widgets/artstack.py b/gnomemusic/widgets/artstack.py
index ac78baf3a..93a6aa89c 100644
--- a/gnomemusic/widgets/artstack.py
+++ b/gnomemusic/widgets/artstack.py
@@ -26,7 +26,7 @@ from __future__ import annotations
 from typing import Optional, Union
 import typing
 
-from gi.repository import Adw, GObject, Gtk
+from gi.repository import GObject, Gtk
 
 from gnomemusic.asyncqueue import AsyncQueue
 from gnomemusic.coverpaintable import CoverPaintable
@@ -76,9 +76,6 @@ class ArtStack(Gtk.Stack):
 
         self.props.size = size
 
-        Adw.StyleManager.get_default().connect(
-            "notify::dark", self._on_dark_changed)
-
     @GObject.Property(type=object, flags=GObject.ParamFlags.READWRITE)
     def size(self) -> ArtSize:
         """Size of the cover
@@ -121,9 +118,8 @@ class ArtStack(Gtk.Stack):
         """
         self._art_type = value
 
-        dark = Adw.StyleManager.get_default().props.dark
         default_icon = CoverPaintable(
-            self._size, self, icon_type=self._art_type, dark=dark)
+            self._size, self, icon_type=self._art_type)
         self._cover.props.paintable = default_icon
 
     @GObject.Property(type=object, default=None)
@@ -135,9 +131,8 @@ class ArtStack(Gtk.Stack):
         if coreobject is self._coreobject:
             return
 
-        dark = Adw.StyleManager.get_default().props.dark
         default_icon = CoverPaintable(
-            self._size, self, icon_type=self._art_type, dark=dark)
+            self._size, self, icon_type=self._art_type)
         self._cover.props.paintable = default_icon
 
         if self._thumbnail_id != 0:
@@ -151,19 +146,6 @@ class ArtStack(Gtk.Stack):
         if self._coreobject.props.thumbnail is not None:
             self._on_thumbnail_changed(self._coreobject, None)
 
-    def _on_dark_changed(
-            self, style_manager: Adw.StyleManager,
-            pspec: GObject.ParamSpecBoolean) -> None:
-        if self._coreobject is None:
-            return
-
-        if self._coreobject.props.thumbnail != "generic":
-            return
-
-        dark = Adw.StyleManager.get_default().props.dark
-        default_icon = CoverPaintable(self._size, self, dark=dark)
-        self._cover.props.paintable = default_icon
-
     def _on_thumbnail_changed(
             self, coreobject: CoreObject,
             uri: GObject.ParamSpecString) -> None:
@@ -173,9 +155,8 @@ class ArtStack(Gtk.Stack):
             self._art_loading_id = 0
 
         if thumbnail_uri == "generic":
-            dark = Adw.StyleManager.get_default().props.dark
             default_icon = CoverPaintable(
-                self._size, self, icon_type=self._art_type, dark=dark)
+                self._size, self, icon_type=self._art_type)
             self._cover.props.paintable = default_icon
             return
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]