| 14 | |
| 15 | @Gtk.Template(resource_path=f"{RES_PATH}/widgets/theme_switcher.ui") |
| 16 | class ThemeSwitcher(Gtk.Box): |
| 17 | __gtype_name__ = "ThemeSwitcher" |
| 18 | |
| 19 | # Properties |
| 20 | show_system: bool = GObject.property(type=bool, default=True) # type: ignore |
| 21 | color_scheme: str = "light" |
| 22 | |
| 23 | # Child widgets |
| 24 | system: Gtk.CheckButton = Gtk.Template.Child() # type: ignore |
| 25 | light: Gtk.CheckButton = Gtk.Template.Child() # type: ignore |
| 26 | dark: Gtk.CheckButton = Gtk.Template.Child() # type: ignore |
| 27 | |
| 28 | @GObject.Property(type=str) |
| 29 | def selected_color_scheme(self) -> str: # type: ignore |
| 30 | """Read-write integer property.""" |
| 31 | |
| 32 | return self.color_scheme |
| 33 | |
| 34 | @selected_color_scheme.setter |
| 35 | def selected_color_scheme(self, color_scheme: str): |
| 36 | self.color_scheme = color_scheme |
| 37 | |
| 38 | if color_scheme == "auto": |
| 39 | self.system.props.active = True |
| 40 | self.style_manager.props.color_scheme = Adw.ColorScheme.PREFER_LIGHT |
| 41 | if color_scheme == "light": |
| 42 | self.light.props.active = True |
| 43 | self.style_manager.props.color_scheme = Adw.ColorScheme.FORCE_LIGHT |
| 44 | if color_scheme == "dark": |
| 45 | self.dark.props.active = True |
| 46 | self.style_manager.props.color_scheme = Adw.ColorScheme.FORCE_DARK |
| 47 | |
| 48 | def __init__(self, **kwargs): |
| 49 | super().__init__(**kwargs) |
| 50 | |
| 51 | self.style_manager = Adw.StyleManager.get_default() |
| 52 | |
| 53 | self.color_scheme = Settings.get().color_scheme |
| 54 | |
| 55 | Settings.get().bind("color-scheme", self, "selected_color_scheme", Gio.SettingsBindFlags.DEFAULT) |
| 56 | |
| 57 | self.style_manager.bind_property( |
| 58 | "system-supports-color-schemes", self, "show_system", GObject.BindingFlags.SYNC_CREATE |
| 59 | ) |
| 60 | |
| 61 | @Gtk.Template.Callback() |
| 62 | def _on_color_scheme_changed(self, _widget, _paramspec): |
| 63 | """Called on (self.system, self.light, self.dark)::notify::active signal""" |
| 64 | if self.system.props.active: |
| 65 | self.selected_color_scheme = "auto" |
| 66 | if self.light.props.active: |
| 67 | self.selected_color_scheme = "light" |
| 68 | if self.dark.props.active: |
| 69 | self.selected_color_scheme = "dark" |