home · contact · privacy
In browser.py, ensure focus on selected item after row resizing.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 9 Sep 2024 10:20:25 +0000 (12:20 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 9 Sep 2024 10:20:25 +0000 (12:20 +0200)
browser.py

index c8d5d305e687c26515f0842c862c937f87255a0d..a6f864bc2d3581424c99ba1d7c93dc7692e6ae12 100755 (executable)
@@ -260,8 +260,6 @@ class MainWindow(Gtk.Window):
             slot.append(slot.content)
             return slot
 
-        self.gallery.set_min_children_per_line(self.per_row)
-        self.gallery.set_max_children_per_line(self.per_row)
         self.gallery.bind_model(self.gallery_selection, init_gallery_slot)
         if sort:
             self.gallery_store.sort(sorter)
@@ -282,12 +280,15 @@ class MainWindow(Gtk.Window):
             to_select.activate()
         self.update_gallery_view()
 
-    def update_gallery_view(self):
+    def update_gallery_view(self, refocus=False):
         """Load/unload gallery's file images based on viewport visibility."""
+        self.gallery.set_min_children_per_line(self.per_row)
+        self.gallery.set_max_children_per_line(self.per_row)
         viewport = self.gallery.get_parent().get_parent()
         vp_height = viewport.get_height()
         vp_width = viewport.get_width()
-        vp_top = viewport.get_vadjustment().get_value()
+        vp_scroll = viewport.get_vadjustment()
+        vp_top = vp_scroll.get_value()
         vp_bottom = vp_top + vp_height
         margin = 6
         max_slot_width = vp_width / self.per_row - margin
@@ -311,6 +312,18 @@ class MainWindow(Gtk.Window):
                 slot.content = Gtk.Label(label='?')
                 slot.append(slot.content)
             slot.content.set_size_request(slot_size, slot_size)
+        if refocus:
+            for c in self.gallery.get_selected_children():
+                for i in range(self.gallery_store_filtered.get_n_items()):
+                    if c == self.gallery.get_child_at_index(i):
+                        slot = self.gallery.get_child_at_index(i).props.child
+                        slot_top = (i // self.per_row) * (slot_size + margin)
+                        slot_bottom = slot_top + slot_size
+                        if slot_top < vp_top:
+                            vp_scroll.set_value(slot_top)
+                        elif slot_bottom > vp_bottom:
+                            vp_scroll.set_value(slot_bottom - vp_height)
+                        vp_scroll.emit('value-changed')
         # # DEBUG: mere statistics
         # from datetime import datetime
         # n_imgs, n_buttons, n_others = 0, 0, 0
@@ -430,8 +443,7 @@ class MainWindow(Gtk.Window):
     def toggle_side_box(self):
         """Toggle window sidebox visible/invisible."""
         self.side_box.props.visible = not self.side_box.get_visible()
-        self.update_gallery(self.gallery_selection.props.selected_item,
-                            sort=False)
+        self.update_gallery_view()
 
     def reset_include_dirs(self, button):
         """By button's .active, in-/exclude directories from gallery view."""
@@ -442,8 +454,7 @@ class MainWindow(Gtk.Window):
         """Change by increment how many items max to display in gallery row."""
         if self.per_row + increment > 0:
             self.per_row += increment
-            self.update_gallery(self.gallery_selection.props.selected_item,
-                                sort=False)
+            self.update_gallery_view(refocus=True)
 
     def reset_recurse(self, button):
         """By button's .active, de-/activate recursion on image collection."""