# Copyright 2022 (c) Anna Schumaker. """A custom Gtk.Dialog for showing Settings.""" from gi.repository import Gtk from gi.repository import Adw from .. import db from .. import entry from .. import factory class ValueRow(factory.ListRow): """A Row for displaying settings values.""" def do_bind(self) -> None: """Bind a db.Setting to this Row.""" if isinstance(self.item.value, bool): self.child = Gtk.Switch(halign=Gtk.Align.START) self.bind_and_set_property("value", "active", bidirectional=True) elif isinstance(self.item.value, str): self.child = entry.String(has_frame=False) self.bind_and_set_property("value", "value", bidirectional=True) elif isinstance(self.item.value, int): self.child = entry.Integer(has_frame=False) self.bind_and_set_property("value", "value", bidirectional=True) elif isinstance(self.item.value, float): self.child = entry.Float(has_frame=False) self.bind_and_set_property("value", "value", bidirectional=True) class Window(Adw.Window): """A custom window that displays the current settings.""" def __init__(self, sql: db.Connection): """Initialize the Settings window.""" super().__init__(default_width=500, default_height=500, title="Emmental Settings", icon_name="settings", hide_on_close=True, content=Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)) self._search = entry.Filter(what="settings") self._header = Gtk.HeaderBar(title_widget=self._search) self._selection = Gtk.NoSelection(model=sql.settings) self._view = Gtk.ColumnView(model=self._selection, show_row_separators=True) self._scroll = Gtk.ScrolledWindow(child=self._view, vexpand=True) self._scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC) self.__append_column(factory.InscriptionFactory("key"), "Key", width=400) self.__append_column(factory.Factory(row_type=ValueRow), "Value", width=100) self.get_content().append(self._header) self.get_content().append(self._scroll) if __debug__: self.add_css_class("devel") self._search.connect("search-changed", self.__filter) def __append_column(self, factory: factory.Factory, title: str, *, width: int) -> None: self._view.append_column(Gtk.ColumnViewColumn(factory=factory, title=title, fixed_width=width)) def __filter(self, entry: entry.Filter) -> None: self._selection.get_model().filter(entry.get_query()) class Row(Adw.ActionRow): """An Adw.ActionRow for opening the Settings Window.""" def __init__(self, sql: db.Connection): """Initialize our settings ActionRow.""" super().__init__(activatable=True, title="Edit Settings", subtitle="Open the settings editor (debug only)") self._prefix = Gtk.Image(icon_name="settings-symbolic") self._window = Window(sql) self.connect("activated", self.__on_activated) self.add_prefix(self._prefix) def __on_activated(self, row: Adw.ActionRow) -> None: self.get_ancestor(Gtk.Popover).popdown() self._window.present()