Skip to content

Commit 13581ad

Browse files
committed
still trying to fix the gfile monitoring code
1 parent 859b9db commit 13581ad

2 files changed

Lines changed: 35 additions & 21 deletions

File tree

src/image.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ def reload_from_disk(self):
226226
return
227227
disk_pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.get_file_path())
228228
self._load_pixbuf_common(disk_pixbuf)
229-
self.window.update_picture_title()
230229
self.use_stable_pixbuf()
231230
self.update()
232231
self.remember_current_state()
232+
self.window.update_picture_title()
233233

234234
def try_load_file(self, gfile):
235235
try:
@@ -250,6 +250,12 @@ def try_load_file(self, gfile):
250250
self._update_can_reload_action()
251251

252252
def _connect_gfile_monitoring(self):
253+
if self._gfile_monitor:
254+
# Probably no need to reconnect it again.
255+
# XXX est-il vraiment bon ? on devrait le déconnecter, le refaire ?
256+
# Tout cela n'est pas débuggable parce que le fichier que je traque
257+
# est le proxy de la sandbox (/run/user/1000/...)
258+
self._gfile_monitor = None
253259
flags = Gio.FileMonitorFlags.WATCH_MOUNTS
254260
self._gfile_monitor = self.gfile.monitor(flags)
255261
self._gfile_monitor.connect('changed', self.reveal_reload_message)
@@ -258,13 +264,29 @@ def disable_monitoring(self):
258264
self._monitoring_disabled = True
259265

260266
def enable_monitoring(self):
267+
"""This should only be called initially, or asynchronously, or in case
268+
of an exception"""
261269
self._monitoring_disabled = False
262270

263271
def reveal_reload_message(self, *args):
272+
"""This method is called when the file changed, which is async: we can't
273+
enable or disable the monitoring in the DrSavingManager's method because
274+
saving the pixbuf takes longer to trigger the 'changed' signal on the
275+
monitor than it takes to run the end of the saving process."""
276+
# I'm not sure this lock is 100% correct because i'm monitoring the
277+
# portal proxy file when testing with flatpak.
264278
if self._monitoring_disabled:
265-
# I'm not sure this lock is 100% correct because i'm monitoring the
266-
# portal proxy file when testing with flatpak. Better than nothing.
279+
# The idea is that the message banner is temporarily disabled until
280+
# the monitor sends its next "changed" event, which we expect (it's
281+
# the one from our own saving process).
267282
if args[3] != Gio.FileMonitorEvent.CHANGED:
283+
try:
284+
self.reload_from_disk()
285+
except Exception as ex:
286+
print(e)
287+
# Context: an error message
288+
self._window.reveal_message(_("Failed to reload %s") % \
289+
self.gfile.get_path())
268290
self.enable_monitoring()
269291
return
270292
self._update_can_reload_action()
@@ -556,12 +578,11 @@ def on_enter_image(self, *args):
556578
def on_leave_image(self, *args):
557579
self.window.set_cursor(False)
558580

559-
def post_save(self, gfile):
581+
def pre_save(self, gfile):
582+
"""The gfile is set before saving, because reveal_reload_message expects
583+
self.gfile to be correct."""
560584
self.gfile = gfile
561585
self._connect_gfile_monitoring()
562-
self.use_stable_pixbuf()
563-
self.update()
564-
self.reload_from_disk()
565586

566587
def set_surface_as_stable_pixbuf(self):
567588
w = self.surface.get_width()

src/saving_manager.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ def save_current_image(self, is_export, to_new, selection_only, allow_alpha):
8585
# Context: an error message
8686
self._window.reveal_message(_("Failed to replace transparency"))
8787

88-
# The "reload?" message shouldn't be shown, i force the reload later
89-
image.lock_monitoring(False)
88+
if not is_export:
89+
image.pre_save(gfile)
90+
# The "reload?" message shouldn't be shown, i force the reload later
91+
image.disable_monitoring()
9092

9193
try:
9294
# 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):
98100
image.enable_monitoring()
99101
return False
100102

101-
# Reset the file monitoring flag
102-
image.lock_monitoring(True)
103-
104-
if not is_export:
105-
# Update the image and the window objects
106-
try:
107-
image.post_save(gfile)
108-
except Exception as e:
109-
print(e)
110-
# Context: an error message
111-
self._window.reveal_message(_("Failed to reload %s") % file_path)
112-
return False
103+
# The file monitoring flag shall reset itself asynchronously.
104+
# If it doesn't, i don't care that much tbh it's just a message for a
105+
# smoother UX.
113106

114107
# everything went fine, return true
115108
return True

0 commit comments

Comments
 (0)