From: Christian Heller Date: Sat, 24 Aug 2024 06:31:25 +0000 (+0200) Subject: Add directory reload button, preserve displayed item on all re-sortings. X-Git-Url: https://plomlompom.com/repos/do_day?a=commitdiff_plain;h=f3a727c0447d8a9d5487870b9df2ae486c6cd6c6;p=stable_plom Add directory reload button, preserve displayed item on all re-sortings. --- diff --git a/browser.py b/browser.py index 3d43d05..651e1e5 100755 --- a/browser.py +++ b/browser.py @@ -52,15 +52,19 @@ class Window(Gtk.ApplicationWindow): def __init__(self, **kwargs): super().__init__(**kwargs) - box_buttons = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) - for args in (('first', lambda _: self.move_selection(None, 0)), - ('prev', lambda _: self.move_selection(-1, None)), - ('next', lambda _: self.move_selection(1, None)), - ('last', lambda _: self.move_selection(None, -1))): - btn = Gtk.Button(label=args[0]) - btn.connect('clicked', args[1]) + def add_nav_button(label_, inc_, pos_): + btn = Gtk.Button(label=label_) + btn.connect('clicked', lambda _: self.move_selection(inc_, pos_)) box_buttons.append(btn) + box_buttons = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) + btn = Gtk.Button(label='reload') + btn.connect('clicked', lambda _: self.reload_dir()) + box_buttons.append(btn) + for label, inc, pos in (('first', None, 0), ('prev', -1, None), + ('next', 1, None), ('last', None, -1)): + add_nav_button(label, inc, pos) + self.metadata = Gtk.Label() self.label_nothing_to_show = Gtk.Label(label='nothing to show') @@ -85,8 +89,8 @@ class Window(Gtk.ApplicationWindow): dir_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) dir_box.append(self.sorter) - img_dir_absolute = abspath(IMG_DIR) - self.dir = Gio.File.new_for_path(img_dir_absolute) + self.img_dir_absolute = abspath(IMG_DIR) + self.dir = Gio.File.new_for_path(self.img_dir_absolute) self.list_store = Gio.ListStore(item_type=FileItem) self.selection = Gtk.SingleSelection.new(self.list_store) factory = Gtk.SignalListItemFactory() @@ -103,29 +107,8 @@ class Window(Gtk.ApplicationWindow): box_outer.append(self.viewer) self.props.child = box_outer - if not path_exists(CACHE_PATH): - with open(CACHE_PATH, 'w', encoding='utf8') as f: - json_dump({}, f) - with open(CACHE_PATH, 'r', encoding='utf8') as f: - cache = json_load(f) - self.max_index = 0 self.item = None - self.selection.connect('selection-changed', self.update_selected) - query_attrs = 'standard::name,standard::content-type,time::*' - enumerator = self.dir.enumerate_children( - query_attrs, Gio.FileQueryInfoFlags.NONE, None) - self.unsorted = [] - for info in [info for info in enumerator - if info.get_content_type().startswith('image/')]: - item = FileItem(img_dir_absolute, info, cache) - self.unsorted += [item] - with ExifToolHelper() as et: - for item in [item for item in self.unsorted if '' == item.model]: - item.set_metadata(et, cache) - self.max_index = len(self.unsorted) - 1 - self.sort('name') - with open(CACHE_PATH, 'w', encoding='utf8') as f: - json_dump(cache, f) + self.reload_dir() def on_sort_select(self, button): self.sort(button.props.label) @@ -136,15 +119,20 @@ class Window(Gtk.ApplicationWindow): self.unsorted.sort(key=lambda i: getattr(i, attr_name)) for file_item in self.unsorted: self.list_store.append(file_item) - self.update_selected() + if self.item: + for pos, item in enumerate(self.list_store): + if item.full_path == self.item.full_path: + self.selection.set_selected(pos) + else: + self.update_selected() def update_selected(self, *_args): self.item = self.selection.props.selected_item - self.reload() + self.reload_image() self.selector.scroll_to(self.selection.props.selected, Gtk.ListScrollFlags.NONE, None) - def reload(self): + def reload_image(self): self.viewer.remove(self.viewer.get_last_child()) if self.item: params_strs = [f'{k}: ' + str(getattr(self.item, k.lower())) @@ -168,6 +156,34 @@ class Window(Gtk.ApplicationWindow): or (-1 == increment and cur_index > 0): self.selection.props.selected = cur_index + increment + def reload_dir(self): + old_item_path = self.item.full_path if self.item else '' + if not path_exists(CACHE_PATH): + with open(CACHE_PATH, 'w', encoding='utf8') as f: + json_dump({}, f) + with open(CACHE_PATH, 'r', encoding='utf8') as f: + cache = json_load(f) + self.max_index = 0 + self.item = None + self.selection.connect('selection-changed', self.update_selected) + query_attrs = 'standard::name,standard::content-type,time::*' + enumerator = self.dir.enumerate_children( + query_attrs, Gio.FileQueryInfoFlags.NONE, None) + self.unsorted = [] + for info in [info for info in enumerator + if info.get_content_type().startswith('image/')]: + item = FileItem(self.img_dir_absolute, info, cache) + if old_item_path == item.full_path: + self.item = item + self.unsorted += [item] + with ExifToolHelper() as et: + for item in [item for item in self.unsorted if '' == item.model]: + item.set_metadata(et, cache) + self.max_index = len(self.unsorted) - 1 + self.sort('name') + with open(CACHE_PATH, 'w', encoding='utf8') as f: + json_dump(cache, f) + def on_activate(app_): win = Window(application=app_)