From 13581adc83c4a4eb6620b6fcd89d86760292dbfe Mon Sep 17 00:00:00 2001 From: RomainFT Date: Sun, 24 Mar 2024 20:57:20 +0100 Subject: [PATCH] still trying to fix the gfile monitoring code --- src/image.py | 35 ++++++++++++++++++++++++++++------- src/saving_manager.py | 21 +++++++-------------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/image.py b/src/image.py index 2e4bee6e..8d36f3b0 100644 --- a/src/image.py +++ b/src/image.py @@ -226,10 +226,10 @@ def reload_from_disk(self): return disk_pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.get_file_path()) self._load_pixbuf_common(disk_pixbuf) - self.window.update_picture_title() self.use_stable_pixbuf() self.update() self.remember_current_state() + self.window.update_picture_title() def try_load_file(self, gfile): try: @@ -250,6 +250,12 @@ def try_load_file(self, gfile): self._update_can_reload_action() def _connect_gfile_monitoring(self): + if self._gfile_monitor: + # Probably no need to reconnect it again. + # XXX est-il vraiment bon ? on devrait le déconnecter, le refaire ? + # Tout cela n'est pas débuggable parce que le fichier que je traque + # est le proxy de la sandbox (/run/user/1000/...) + self._gfile_monitor = None flags = Gio.FileMonitorFlags.WATCH_MOUNTS self._gfile_monitor = self.gfile.monitor(flags) self._gfile_monitor.connect('changed', self.reveal_reload_message) @@ -258,13 +264,29 @@ def disable_monitoring(self): self._monitoring_disabled = True def enable_monitoring(self): + """This should only be called initially, or asynchronously, or in case + of an exception""" self._monitoring_disabled = False def reveal_reload_message(self, *args): + """This method is called when the file changed, which is async: we can't + enable or disable the monitoring in the DrSavingManager's method because + saving the pixbuf takes longer to trigger the 'changed' signal on the + monitor than it takes to run the end of the saving process.""" + # I'm not sure this lock is 100% correct because i'm monitoring the + # portal proxy file when testing with flatpak. if self._monitoring_disabled: - # I'm not sure this lock is 100% correct because i'm monitoring the - # portal proxy file when testing with flatpak. Better than nothing. + # The idea is that the message banner is temporarily disabled until + # the monitor sends its next "changed" event, which we expect (it's + # the one from our own saving process). if args[3] != Gio.FileMonitorEvent.CHANGED: + try: + self.reload_from_disk() + except Exception as ex: + print(e) + # Context: an error message + self._window.reveal_message(_("Failed to reload %s") % \ + self.gfile.get_path()) self.enable_monitoring() return self._update_can_reload_action() @@ -556,12 +578,11 @@ def on_enter_image(self, *args): def on_leave_image(self, *args): self.window.set_cursor(False) - def post_save(self, gfile): + def pre_save(self, gfile): + """The gfile is set before saving, because reveal_reload_message expects + self.gfile to be correct.""" self.gfile = gfile self._connect_gfile_monitoring() - self.use_stable_pixbuf() - self.update() - self.reload_from_disk() def set_surface_as_stable_pixbuf(self): w = self.surface.get_width() diff --git a/src/saving_manager.py b/src/saving_manager.py index 938fec2f..e16d0881 100644 --- a/src/saving_manager.py +++ b/src/saving_manager.py @@ -85,8 +85,10 @@ def save_current_image(self, is_export, to_new, selection_only, allow_alpha): # Context: an error message self._window.reveal_message(_("Failed to replace transparency")) - # The "reload?" message shouldn't be shown, i force the reload later - image.lock_monitoring(False) + if not is_export: + image.pre_save(gfile) + # The "reload?" message shouldn't be shown, i force the reload later + image.disable_monitoring() try: # Actually save the pixbuf to the given file path @@ -98,18 +100,9 @@ def save_current_image(self, is_export, to_new, selection_only, allow_alpha): image.enable_monitoring() return False - # Reset the file monitoring flag - image.lock_monitoring(True) - - if not is_export: - # Update the image and the window objects - try: - image.post_save(gfile) - except Exception as e: - print(e) - # Context: an error message - self._window.reveal_message(_("Failed to reload %s") % file_path) - return False + # The file monitoring flag shall reset itself asynchronously. + # If it doesn't, i don't care that much tbh it's just a message for a + # smoother UX. # everything went fine, return true return True