From 97e9c6b35ab447aa43d84327360c024072959c50 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Tue, 10 Sep 2024 05:42:37 +0200
Subject: [PATCH] Make and display sort/filter reconfigurations temporary
 unless explicitly activated.

---
 browser.py | 82 ++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 58 insertions(+), 24 deletions(-)

diff --git a/browser.py b/browser.py
index 7201241..8c258a3 100755
--- a/browser.py
+++ b/browser.py
@@ -30,6 +30,9 @@ flowboxchild:selected { outline: none; box-shadow: none; background: green; }
 flowboxchild:hover{ outline: none; box-shadow: none; background: yellow; }
 flowboxchild:active { outline: none; box-shadow: none; background: red; }
 """
+CSS = """
+.temp { background: #ffaaaa; }
+"""
 
 
 class SortLabelItem(GObject.GObject):
@@ -105,6 +108,7 @@ class MainWindow(Gtk.Window):
     sort_selection: Gtk.SingleSelection
     prev_key: list
     filter_inputs = dict
+    button_activate_sort: Gtk.Button
 
     def __init__(self, app, **kwargs):
         super().__init__(**kwargs)
@@ -175,7 +179,7 @@ class MainWindow(Gtk.Window):
             factory = Gtk.SignalListItemFactory()
 
             def setup_sort_order_item(_, item):
-                box = Gtk.Box(orientation=OR_H, halign=Gtk.Align.END)
+                box = Gtk.Box(orientation=OR_H)
                 box.append(Gtk.Label(hexpand=True))
                 box.append(Gtk.Entry.new())
                 box.get_last_child().props.placeholder_text = 'filter?'
@@ -184,6 +188,7 @@ class MainWindow(Gtk.Window):
             def bind_sort_order_item(_, item):
 
                 def on_filter_enter(entry):
+                    entry.remove_css_class('temp')
                     text = entry.get_buffer().get_text()
                     if '' != text.rstrip():
                         self.filter_inputs[item.props.item.name] = text
@@ -191,13 +196,21 @@ class MainWindow(Gtk.Window):
                         del self.filter_inputs[item.props.item.name]
                     self.update_gallery()
 
+                item.props.item.box = item
                 label = f'{item.props.item.name} '
                 item.props.item.filterer = item.props.child.get_last_child()
                 item.props.child.get_first_child().set_text(label)
                 filter_entry = item.props.child.get_last_child()
                 filter_text = self.filter_inputs.get(item.props.item.name, '')
-                filter_entry.get_buffer().set_text(filter_text, -1)
+                filter_buffer = filter_entry.get_buffer()
+                filter_buffer.set_text(filter_text, -1)
                 filter_entry.connect('activate', on_filter_enter)
+                filter_buffer.connect(
+                        'inserted_text',
+                        lambda a, b, c, d: filter_entry.add_css_class('temp'))
+                filter_buffer.connect(
+                        'deleted_text',
+                        lambda a, b, c: filter_entry.add_css_class('temp'))
 
             factory.connect('setup', setup_sort_order_item)
             factory.connect('bind', bind_sort_order_item)
@@ -205,6 +218,11 @@ class MainWindow(Gtk.Window):
             sort_box = Gtk.Box(orientation=OR_V)
             sort_box.append(Gtk.Label(label='** sort order **'))
             sort_box.append(selector)
+            self.button_activate_sort = Gtk.Button(label='activate')
+            self.button_activate_sort.props.sensitive = False
+            self.button_activate_sort.connect(
+                    'clicked', lambda _: self.activate_sort_order())
+            sort_box.append(self.button_activate_sort)
             self.filter_inputs = {}
             return sort_box
 
@@ -231,6 +249,13 @@ class MainWindow(Gtk.Window):
         self.block_file_selection_updates = False
         self.force_width, self.force_height = 0, 0
 
+        css_provider = Gtk.CssProvider()
+        # css_provider.load_from_data(DEBUGGING_CSS)
+        css_provider.load_from_data(CSS)
+        Gtk.StyleContext.add_provider_for_display(
+                self.get_display(), css_provider,
+                Gtk.STYLE_PROVIDER_PRIORITY_USER)
+
         viewer = Gtk.Box(orientation=OR_V)
         self.navbar = init_navbar()
         viewer.append(self.navbar)
@@ -248,16 +273,9 @@ class MainWindow(Gtk.Window):
         init_key_control()
         init_gallery_content()
         self.load_directory(update_gallery=False)
-        self.update_sort_order(update_gallery=False)
+        self.update_sort_order_box(self.sort_order)
         GLib.idle_add(self.update_gallery)
 
-        # # useful for debugging
-        # css_provider = Gtk.CssProvider()
-        # css_provider.load_from_data(DEBUGGING_CSS)
-        # Gtk.StyleContext.add_provider_for_display(
-        #         self.get_display(), css_provider,
-        #         Gtk.STYLE_PROVIDER_PRIORITY_USER)
-
     def on_resize(self):
         """Adapt .force_(width|height) to new .default_(width|height)"""
         if self.get_width() > 0:  # so we don't call this on initial resize
@@ -438,15 +456,23 @@ class MainWindow(Gtk.Window):
         sync_fbox_selection_to_gallery_selection()
         update_metadata_on_file()
 
-    def update_sort_order(self, cur_selection=0, update_gallery=True):
+    def update_sort_order_box(self, sort_order_source, cur_selection=0):
         """Rebuild self.sort_store from self.sort_order."""
         self.sort_store.remove_all()
-        for s in self.sort_order:
+        for s in sort_order_source:
             self.sort_store.append(SortLabelItem(s))
         self.sort_selection.props.selected = cur_selection
+
+    def activate_sort_order(self):
+        """Write sort order box order into self.sort_order, mark finalized."""
+        self.sort_order = []
+        for i in range(self.sort_store.get_n_items()):
+            sort_item = self.sort_store.get_item(i)
+            sort_item.box.props.child.remove_css_class('temp')
+            self.sort_order += [sort_item.name]
+        self.button_activate_sort.props.sensitive = False
         old_selection = self.gallery_selection.props.selected_item
-        if update_gallery:
-            self.update_gallery(old_selection)
+        self.update_gallery(old_selection)
 
     # navbar callables
 
@@ -602,22 +628,30 @@ class MainWindow(Gtk.Window):
     # movement
 
     def move_sort(self, direction):
-        """In sort order list, move selected item up (-1) or down (+1)."""
+        """Move selected item in sort order view, ensure temporary state."""
+        tmp_sort_order = []
+        for i in range(self.sort_store.get_n_items()):
+            tmp_sort_order += [self.sort_store.get_item(i).name]
         cur_idx = self.sort_selection.props.selected
-        selected = self.sort_order[cur_idx]
+        selected = tmp_sort_order[cur_idx]
         if direction == -1 and cur_idx > 0:
             prev_i = cur_idx - 1
-            old_prev = self.sort_order[prev_i]
-            self.sort_order[prev_i] = selected
-            self.sort_order[cur_idx] = old_prev
-        elif direction == 1 and cur_idx < (len(self.sort_order) - 1):
+            old_prev = tmp_sort_order[prev_i]
+            tmp_sort_order[prev_i] = selected
+            tmp_sort_order[cur_idx] = old_prev
+        elif direction == 1 and cur_idx < (len(tmp_sort_order) - 1):
             next_i = cur_idx + 1
-            old_next = self.sort_order[next_i]
-            self.sort_order[next_i] = selected
-            self.sort_order[cur_idx] = old_next
+            old_next = tmp_sort_order[next_i]
+            tmp_sort_order[next_i] = selected
+            tmp_sort_order[cur_idx] = old_next
         else:
             return
-        self.update_sort_order(cur_idx + direction)
+        self.update_sort_order_box(tmp_sort_order, cur_idx + direction)
+        self.sort_selection.props.selected = cur_idx + direction
+        for i in range(self.sort_store.get_n_items()):
+            sort_item = self.sort_store.get_item(i)
+            sort_item.box.props.child.add_css_class('temp')
+        self.button_activate_sort.props.sensitive = True
 
     def move_selection_in_sort_order(self, direction):
         """Move sort order selection by direction (-1 or +1)."""
-- 
2.30.2