From: Christian Heller Date: Mon, 11 Nov 2024 17:12:17 +0000 (+0100) Subject: Browser: Further refactor filtering code. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/static/tasks?a=commitdiff_plain;h=cd91c931c4ad30d95c408facc2edac41eae7280c;p=stable_plom Browser: Further refactor filtering code. --- diff --git a/browser.py b/browser.py index df219d2..dd84621 100755 --- a/browser.py +++ b/browser.py @@ -170,11 +170,11 @@ class SorterAndFilterer(GObject.GObject): def __init__(self, name: str) -> None: super().__init__() self.name = name + self.filter = '' def setup_on_bind(self, widget: Gtk.Box, on_filter_activate: Callable, - filter_text: str, vals: AttrValsByVisibility, ) -> None: """Set up SorterAndFilterer label, values listing, filter entry.""" @@ -192,15 +192,24 @@ class SorterAndFilterer(GObject.GObject): self.widget.values.set_text(', '.join(vals_listed)) self.widget.values.set_use_markup(True) # filter input - filter_buffer = self.widget.filter.get_buffer() - filter_buffer.set_text(filter_text, -1) - self.widget.filter.connect('activate', on_filter_activate) + + def filter_activate() -> None: + self.widget.filter_input.remove_css_class('temp') + self.filter = self.widget.filter_input.get_buffer().get_text() + on_filter_activate() + + filter_buffer = self.widget.filter_input.get_buffer() + filter_buffer.set_text(self.filter, -1) # triggers 'temp' class set, + self.widget.filter_input.remove_css_class('temp') # that's why … + self.widget.filter_input.connect( + 'activate', + lambda _: filter_activate()) filter_buffer.connect( 'inserted_text', - lambda a, b, c, d: self.widget.filter.add_css_class('temp')) + lambda a, b, c, d: self.widget.filter_input.add_css_class('temp')) filter_buffer.connect( 'deleted_text', - lambda a, b, c: self.widget.filter.add_css_class('temp')) + lambda a, b, c: self.widget.filter_input.add_css_class('temp')) class SorterAndFiltererOrder: @@ -208,7 +217,6 @@ class SorterAndFiltererOrder: def __init__(self, as_list: list[SorterAndFilterer]) -> None: self._list = as_list - self.filter_inputs: FilterInputs = {} def __eq__(self, other) -> bool: return self._list == other._list @@ -466,28 +474,20 @@ class GalleryConfig(): visible=False, max_width_chars=35, wrap=True, wrap_mode=Pango.WrapMode.WORD_CHAR) item_widget.label = Gtk.Label(hexpand=True) - item_widget.filter = Gtk.Entry(placeholder_text='filter?') + item_widget.filter_input = Gtk.Entry(placeholder_text='filter?') hbox = Gtk.Box(orientation=OR_H) hbox.append(item_widget.label) - hbox.append(item_widget.filter) + hbox.append(item_widget.filter_input) item_widget.append(hbox) item_widget.append(item_widget.values) list_item.set_child(item_widget) def bind_sorter_list_item(_, list_item: SorterAndFilterer) -> None: - - def on_filter_activate(entry: Gtk.Box) -> None: - entry.remove_css_class('temp') - text: str = entry.get_buffer().get_text() - if '' != text.rstrip(): - self.order.filter_inputs[sorter.name] = text - elif sorter.name in self.order.filter_inputs: - del self.order.filter_inputs[sorter.name] - self._filter_inputs_changed = True - + def on_filter_activate(): + self._filter_changed = True sorter: SorterAndFilterer = list_item.props.item - sorter.setup_on_bind(list_item.props.child, on_filter_activate, - self.order.filter_inputs.get(sorter.name, ''), + sorter.setup_on_bind(list_item.props.child, + on_filter_activate, self._gallery_items_attrs[sorter.name]) def select_sort_order(_a, _b, _c) -> None: @@ -508,7 +508,7 @@ class GalleryConfig(): self.order.sync_from(self._tmp_order) self._tmp_order = None self._gallery_request_update(build_grid=True) - if self._filter_inputs_changed: + if self._filter_changed: self._gallery_request_update(build_grid=True) self._gallery_update_settings( per_row=self._btn_per_row.get_value_as_int(), @@ -517,14 +517,14 @@ class GalleryConfig(): recurse_dirs=self._btn_recurse.get_active()) self._gallery_request_update(select=True) self._set_recurse_changed = False - self._filter_inputs_changed = False + self._filter_changed = False def full_reload() -> None: apply_config() self._gallery_request_update(load=True) self._btn_apply.set_sensitive(True) - self._filter_inputs_changed = False + self._filter_changed = False self._set_recurse_changed = False self._last_selected: Optional[Gtk.Widget] = None @@ -914,16 +914,16 @@ class Gallery: return False return True - if attr_name not in self._sort_order.filter_inputs: - return True if val is None: return False - pattern = self._sort_order.filter_inputs[attr_name] - if attr_name in number_attributes: - if not passes_number_filter(attr_name, pattern, val): + for filterer in [f for f in self._sort_order + if f.name == attr_name]: + if attr_name in number_attributes: + if not passes_number_filter(attr_name, filterer.filter, + val): + return False + elif not re_search(filterer.filter, val): return False - elif not re_search(pattern, val): - return False return True def update_items_attrs() -> None: @@ -934,7 +934,7 @@ class Gallery: for attr_name, vals in basic_items_attrs.items(): items_attrs[attr_name] = {'incl': [], 'excl': []} for v in vals: - k = ('incl' if passes_filter(attr_name, v) else 'excl') + k = 'incl' if passes_filter(attr_name, v) else 'excl' items_attrs[attr_name][k] += [v] return items_attrs