home · contact · privacy
Browser: Reduce proliferation of SorterAndFiltererOrders.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 11 Nov 2024 00:15:01 +0000 (01:15 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 11 Nov 2024 00:15:01 +0000 (01:15 +0100)
browser.py

index dbbff1d5f511685d52a7704e054278c77f200055..eeb78f14116d797023f42666cc9303582b9b8a83 100755 (executable)
@@ -454,6 +454,7 @@ class GalleryConfig():
                  items_attrs: ItemsAttrs,
                  ) -> None:
         self.order = sort_order
+        self._tmp_order: Optional[SorterAndFiltererOrder] = None
         self._gallery_request_update = request_update
         self._gallery_update_settings = update_settings
         self._gallery_items_attrs = items_attrs
@@ -502,14 +503,14 @@ class GalleryConfig():
                 self._btn_show_dirs.set_active(False)
 
         def apply_config() -> None:
-            self.order.update_from_store(self._store)
-            for sorter in self.order:
-                sorter.widget.remove_css_class('temp')
+            if self._tmp_order:
+                self.order.sync_from(self._tmp_order)
+                self._tmp_order = None
+                self._gallery_request_update(build_grid=True)
             self._gallery_update_settings(
                     per_row=self._btn_per_row.get_value_as_int(),
                     by_1st=self._btn_by_1st.get_active(),
                     show_dirs=self._btn_show_dirs.get_active(),
-                    sort_order=self.order.copy(),
                     filter_inputs=self.filter_inputs.copy(),
                     recurse_dirs=self._btn_recurse.get_active())
             self._gallery_request_update(select=True)
@@ -586,31 +587,27 @@ class GalleryConfig():
 
     def move_sorter(self, direction: int) -> None:
         """Move selected item in sort order view, ensure temporary state."""
-        tmp_sort_order = SorterAndFiltererOrder.from_store(self._store)
+        tmp_order = self._tmp_order if self._tmp_order else self.order.copy()
         cur_idx = self._sort_sel.props.selected
         if direction == -1 and cur_idx > 0:
-            tmp_sort_order.switch_at(cur_idx, forward=False)
-        elif direction == 1 and cur_idx < (len(tmp_sort_order) - 1):
-            tmp_sort_order.switch_at(cur_idx, forward=True)
+            tmp_order.switch_at(cur_idx, forward=False)
+        elif direction == 1 and cur_idx < (len(tmp_order) - 1):
+            tmp_order.switch_at(cur_idx, forward=True)
         else:  # to catch movement beyond limits
             return
-        self.update_box(tmp_sort_order, cur_idx + direction)
+        if not self._tmp_order:
+            self._tmp_order = tmp_order
+            for sorter in self._tmp_order:
+                sorter.widget.add_css_class('temp')
+        self.update_box(cur_idx + direction)
         self._sort_sel.props.selected = cur_idx + direction
         for i in range(self._store.get_n_items()):
             sort_item: SorterAndFilterer = self._store.get_item(i)
             sort_item.widget.add_css_class('temp')
 
-    def sync_sorter(self, order: SorterAndFiltererOrder) -> None:
-        """Update .order and its dependencies from order."""
-        self.order.sync_from(order)
-        self.update_box()
-
-    def update_box(self,
-                   alt_order: Optional[SorterAndFiltererOrder] = None,
-                   cur_selection: int = 0
-                   ) -> None:
+    def update_box(self, cur_selection: int = 0) -> None:
         """Rebuild sorter listing in box from .order, or alt_order if set."""
-        sort_order = alt_order if alt_order else self.order
+        sort_order = self._tmp_order if self._tmp_order else self.order
         sort_order.into_store(self._store)
         self._sort_sel.props.selected = cur_selection
 
@@ -656,9 +653,9 @@ class VerticalLabel(Gtk.DrawingArea):
 class Gallery:
     """Representation of GalleryItems below a directory."""
     update_config_box: Callable
-    sync_sorter: Callable
 
     def __init__(self,
+                 sort_order: SorterAndFiltererOrder,
                  on_hit_item: Callable,
                  on_selection_change: Callable,
                  bookmarks_db: BookmarksDb,
@@ -667,7 +664,7 @@ class Gallery:
         self._on_hit_item = on_hit_item
         self._on_selection_change = on_selection_change
         self._bookmarks_db, self._cache_db = bookmarks_db, cache_db
-        self._sort_order = SorterAndFiltererOrder([])
+        self._sort_order = sort_order
         self._filter_inputs: FilterInputs = {}
         self._img_dir_path = ''
 
@@ -741,7 +738,6 @@ class Gallery:
                         show_dirs: Optional[bool] = None,
                         recurse_dirs: Optional[bool] = None,
                         img_dir_path: Optional[str] = None,
-                        sort_order: Optional[SorterAndFiltererOrder] = None,
                         filter_inputs: Optional[FilterInputs] = None
                         ) -> None:
         """Set Gallery setup fields, request appropriate updates."""
@@ -750,7 +746,6 @@ class Gallery:
                                (show_dirs, '_show_dirs'),
                                (recurse_dirs, '_recurse_dirs'),
                                (img_dir_path, '_img_dir_path'),
-                               (sort_order, '_sort_order'),
                                (filter_inputs, '_filter_inputs')]:
             if val is not None and getattr(self, attr_name) != val:
                 setattr(self, attr_name, val)
@@ -959,7 +954,6 @@ class Gallery:
                 for category in ('incl', 'semi', 'excl'):
                     final_values[category].sort()
                 self.items_attrs[attr_name] = final_values
-            self.sync_sorter(self._sort_order)
 
         def filter_entries(items_attrs: ItemsAttrs) -> list[GalleryItem]:
             entries_filtered: list[GalleryItem] = []
@@ -1239,6 +1233,7 @@ class MainWindow(Gtk.Window):
         super().__init__(**kwargs)
         self.app = app
         self.gallery = Gallery(
+                sort_order=self.app.sort_order,
                 on_hit_item=self.hit_gallery_item,
                 on_selection_change=self.update_metadata_on_gallery_selection,
                 bookmarks_db=self.app.bookmarks_db,
@@ -1251,7 +1246,6 @@ class MainWindow(Gtk.Window):
                 update_settings=self.gallery.update_settings,
                 items_attrs=self.gallery.items_attrs)
         self.gallery.update_config_box = self.conf.update_box
-        self.gallery.sync_sorter = self.conf.sync_sorter
         metadata_textview = Gtk.TextView(wrap_mode=Gtk.WrapMode.WORD_CHAR,
                                          editable=False)
         self.metadata = metadata_textview.get_buffer()
@@ -1298,7 +1292,6 @@ class MainWindow(Gtk.Window):
         # only now we're ready for actually running the gallery
         GLib.idle_add(lambda: self.gallery.update_settings(
             img_dir_path=self.app.img_dir_absolute,
-            sort_order=self.conf.order.copy(),
             filter_inputs=self.conf.filter_inputs.copy()))
 
     def on_focus_change(self) -> None: