Skip to content

Commit

Permalink
WIP: Make mouse svg follow dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
A6GibKm committed Aug 5, 2022
1 parent 405adc1 commit b21e473
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion piper/mousemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@

gi.require_version("Gdk", "3.0")
gi.require_version("Gtk", "3.0")
gi.require_version("Handy", "1")
gi.require_version("Rsvg", "2.0")
from gi.repository import Gdk, GLib, Gtk, GObject, Rsvg # noqa
from gi.repository import Gdk, GLib, Gtk, GObject, Handy, Rsvg # noqa

"""This module contains the MouseMap widget (and its helper class
_MouseMapChild), which is central to the button and LED configuration stack
pages. The MouseMap widget draws the device SVG in the center and lays out a
bunch of child widgets relative to the leaders in the device SVG."""

# FIXME set_stylesheet to something that actually makes the content dark.
dark_stylesheet = b"svg {fill: red}"


class _MouseMapChild:
# A helper class to manage children and their properties.
Expand Down Expand Up @@ -89,13 +93,19 @@ def __init__(self, layer, ratbagd_device, spacing=10, *args, **kwargs):
has no image registered.
@raises GLib.Error when the SVG cannot be loaded.
"""
manager = Handy.StyleManager.get_default()
manager.connect("notify::dark", self._on_dark_changed)

if layer is None:
raise ValueError("Layer cannot be None")
if ratbagd_device is None:
raise ValueError("Device cannot be None")
try:
svg_bytes = get_svg(ratbagd_device.model)
self._handle = Rsvg.Handle.new_from_data(svg_bytes)
if manager.get_dark():
self._handle.set_stylesheet(dark_stylesheet)

self._svg_data = etree.fromstring(svg_bytes)
except FileNotFoundError:
raise ValueError("Device has no image or its path is invalid")
Expand Down Expand Up @@ -404,3 +414,11 @@ def _draw_device(self, cr):
for child in self._children:
self._handle.render_cairo_sub(cr, id=child.svg_path)
self._handle.render_cairo_sub(cr, id=child.svg_leader)

def _on_dark_changed(self, manager, _pspec):
if manager.get_dark():
self._handle.set_stylesheet(dark_stylesheet)
else:
self._handle.set_stylesheet(b"")

self.queue_draw()

0 comments on commit b21e473

Please sign in to comment.