From: Christian Heller Date: Mon, 30 Sep 2024 11:31:40 +0000 (+0200) Subject: Minor refactoring. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/decks/%27%29;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20chunks.push%28escapeHTML%28span%5B2%5D%29%29;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20chunks.push%28%27?a=commitdiff_plain;h=0bfeeba8ecc9cfebdb07d717c7a0b6b518b44fc2;p=stable_plom Minor refactoring. --- diff --git a/browser.py b/browser.py index 410fe3d..30acecc 100755 --- a/browser.py +++ b/browser.py @@ -53,6 +53,16 @@ button.slot { """ +def _add_button(parent, label, on_click=None, checkbox=False): + """Helper to add Gtk.Button or .CheckButton to parent.""" + btn = (Gtk.CheckButton(label=label) if checkbox + else Gtk.Button(label=label)) + if on_click: + btn.connect('toggled' if checkbox else 'clicked', on_click) + parent.append(btn) + return btn + + class Sorter(GObject.GObject): """Sort order box representation of sorting attribute.""" widget: Gtk.Box @@ -130,7 +140,7 @@ class TableConfig(): list_item.get_parent().grab_focus() self._last_selected = list_item - def on_by_1st_sorter_toggle(btn): + def on_by_1st_toggle(btn): self._btn_per_row.set_sensitive(not btn.props.active) self._btn_show_dirs.set_sensitive(not btn.props.active) if btn.props.active: @@ -148,22 +158,16 @@ class TableConfig(): factory.connect('bind', bind_sort_order_item) self.view = Gtk.ListView(model=self._selection, factory=factory) - self._btn_show_dirs = Gtk.CheckButton(label='show') - self._btn_recurse_dirs = Gtk.CheckButton(label='recurse') - self._btn_reload_dir = Gtk.Button(label='reload') - self._btn_by_1st_sorter = Gtk.CheckButton(label='by 1st sorter') - self._btn_by_1st_sorter.connect('toggled', on_by_1st_sorter_toggle) - self._btn_per_row = Gtk.SpinButton.new_with_range(1, 9, 1) - dirs_box = Gtk.Box(orientation=OR_H) dirs_box.append(Gtk.Label(label='directories:')) - dirs_box.append(self._btn_show_dirs) - dirs_box.append(self._btn_recurse_dirs) - dirs_box.append(self._btn_reload_dir) + self._btn_show_dirs = _add_button(dirs_box, 'show', checkbox=True) + self._btn_recurse = _add_button(dirs_box, 'recurse', checkbox=True) + self._btn_reload_dir = _add_button(dirs_box, 'reload') per_row_box = Gtk.Box(orientation=OR_H) per_row_box.append(Gtk.Label(label='cols/row:')) - per_row_box.append(self._btn_by_1st_sorter) + _add_button(per_row_box, 'by 1st sorter', on_by_1st_toggle, True) + self._btn_per_row = Gtk.SpinButton.new_with_range(1, 9, 1) per_row_box.append(self._btn_per_row) self.box = Gtk.Box(orientation=OR_V) @@ -199,7 +203,7 @@ class TableConfig(): """Connect to Gallery interfaces where necessary.""" self._gallery_update = on_update self._btn_show_dirs.connect('toggled', toggle_showdirs) - self._btn_recurse_dirs.connect('toggled', toggle_recurse) + self._btn_recurse.connect('toggled', toggle_recurse) self._btn_reload_dir.connect('clicked', reload) self._gallery_set_by_1st_sorter = set_by_1st_sorter self._gallery_items_attrs_full = items_attrs_full @@ -752,17 +756,10 @@ class MainWindow(Gtk.Window): def init_navbar(): - def add_button(label_, on_click, checkbox=False): - btn = (Gtk.CheckButton(label=label_) if checkbox - else Gtk.Button(label=label_)) - btn.connect('toggled' if checkbox else 'clicked', on_click) - navbar.append(btn) - return btn - navbar = Gtk.Box(orientation=OR_H) self.counter = Gtk.Label() navbar.append(self.counter) - add_button('sidebar', lambda _: self.toggle_side_box()) + _add_button(navbar, 'sidebar', lambda _: self.toggle_side_box()) return navbar def init_metadata_box():