Skip to content

Commit

Permalink
still trying to fix the gfile monitoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
maoschanz committed Mar 24, 2024
1 parent 859b9db commit 13581ad
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
35 changes: 28 additions & 7 deletions src/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
21 changes: 7 additions & 14 deletions src/saving_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 13581ad

Please sign in to comment.