From e6739c3087268c228805569b6d3b067a3d21e985 Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Sun, 12 Nov 2023 23:25:46 -0600 Subject: [PATCH] Wrapped async in daemon thread for icon loading --- src/solarfm/app.py | 5 +- src/solarfm/core/mixins/ui/grid_mixin.py | 53 ++++++------------- .../core/widgets/files_view/grid_mixin.py | 52 ++++++------------ 3 files changed, 33 insertions(+), 77 deletions(-) diff --git a/src/solarfm/app.py b/src/solarfm/app.py index 209e350..471b8d9 100644 --- a/src/solarfm/app.py +++ b/src/solarfm/app.py @@ -15,6 +15,7 @@ class AppLaunchException(Exception): ... + class Application(IPCServer): """ docstring for Application. """ @@ -30,7 +31,7 @@ class Application(IPCServer): message = f"FILE|{arg}" self.send_ipc_message(message) - raise AppLaunchException(f"{app_name} IPC Server Exists: Will send path(s) to it and close...") + raise AppLaunchException(f"{app_name} IPC Server Exists: Have sent path(s) to it and closing...") self.setup_debug_hook() Window(args, unknownargs) @@ -56,4 +57,4 @@ class Application(IPCServer): ) except ValueError: # Typically: ValueError: signal only works in main thread - ... + ... \ No newline at end of file diff --git a/src/solarfm/core/mixins/ui/grid_mixin.py b/src/solarfm/core/mixins/ui/grid_mixin.py index 31881ef..937802c 100644 --- a/src/solarfm/core/mixins/ui/grid_mixin.py +++ b/src/solarfm/core/mixins/ui/grid_mixin.py @@ -26,59 +26,36 @@ class GridMixin: store.append([None, file[0]]) Gtk.main_iteration() - # for i, file in enumerate(files): - # self.create_icon(i, tab, store, dir, file[0]) - - if use_generator: - # NOTE: tab > icon > _get_system_thumbnail_gtk_thread must not be used - # as the attempted promotion back to gtk threading stalls the generator. (We're already in main gtk thread) - for i, icon in enumerate( self.create_icons_generator(tab, dir, files) ): - self.load_icon(i, store, icon) - else: - try: - loop = asyncio.get_running_loop() - except RuntimeError: - loop = None - - if loop and loop.is_running(): - loop.create_task( self.create_icons(tab, store, dir, files) ) - else: - asyncio.run( self.create_icons(tab, store, dir, files) ) + self.generate_icons(tab, store, dir, files) # NOTE: Not likely called often from here but it could be useful if save_state and not trace_debug: self.fm_controller.save_state() + @daemon_threaded + def generate_icons(self, tab, store, dir, files): + try: + loop = asyncio.get_running_loop() + except RuntimeError: + loop = None + + if loop and loop.is_running(): + loop.create_task( self.create_icons(tab, store, dir, files) ) + else: + asyncio.run( self.create_icons(tab, store, dir, files) ) + async def create_icons(self, tab, store, dir, files): tasks = [self.update_store(i, store, dir, tab, file[0]) for i, file in enumerate(files)] await asyncio.gather(*tasks) async def load_icon(self, i, store, icon): - self.update_store(i, store, icon) + GLib.idle_add(self.update_store, i, store, icon ) async def update_store(self, i, store, dir, tab, file): icon = tab.create_icon(dir, file) itr = store.get_iter(i) store.set_value(itr, 0, icon) - def create_icons_generator(self, tab, dir, files): - for file in files: - icon = tab.create_icon(dir, file[0]) - yield icon - - # @daemon_threaded - # def create_icon(self, i, tab, store, dir, file): - # icon = tab.create_icon(dir, file) - # GLib.idle_add(self.update_store, *(i, store, icon,)) - # - # @daemon_threaded - # def load_icon(self, i, store, icon): - # GLib.idle_add(self.update_store, *(i, store, icon,)) - - # def update_store(self, i, store, icon): - # itr = store.get_iter(i) - # store.set_value(itr, 0, icon) - def create_tab_widget(self, tab): return TabHeaderWidget(tab, self.close_tab) @@ -144,4 +121,4 @@ class GridMixin: icon_grid = obj.get_children()[0] name = icon_grid.get_name() if name == _name: - return icon_grid + return icon_grid \ No newline at end of file diff --git a/src/solarfm/core/widgets/files_view/grid_mixin.py b/src/solarfm/core/widgets/files_view/grid_mixin.py index 0b02715..11ca4f6 100644 --- a/src/solarfm/core/widgets/files_view/grid_mixin.py +++ b/src/solarfm/core/widgets/files_view/grid_mixin.py @@ -26,58 +26,36 @@ class GridMixin: store.append([None, file[0]]) Gtk.main_iteration() - if use_generator: - # NOTE: tab > icon > _get_system_thumbnail_gtk_thread must not be used - # as the attempted promotion back to gtk threading stalls the generator. (We're already in main gtk thread) - for i, icon in enumerate( self.create_icons_generator(tab, dir, files) ): - self.load_icon(i, store, icon) - else: - # for i, file in enumerate(files): - # self.create_icon(i, tab, store, dir, file[0]) - try: - loop = asyncio.get_running_loop() - except RuntimeError: - loop = None - - if loop and loop.is_running(): - loop.create_task( self.create_icons(tab, store, dir, files) ) - else: - asyncio.run( self.create_icons(tab, store, dir, files) ) + self.generate_icons(tab, store, dir, files) # NOTE: Not likely called often from here but it could be useful if save_state and not trace_debug: self.fm_controller.save_state() + @daemon_threaded + def generate_icons(self, tab, store, dir, files): + try: + loop = asyncio.get_running_loop() + except RuntimeError: + loop = None + + if loop and loop.is_running(): + loop.create_task( self.create_icons(tab, store, dir, files) ) + else: + asyncio.run( self.create_icons(tab, store, dir, files) ) + async def create_icons(self, tab, store, dir, files): tasks = [self.update_store(i, store, dir, tab, file[0]) for i, file in enumerate(files)] await asyncio.gather(*tasks) async def load_icon(self, i, store, icon): - self.update_store(i, store, icon) + GLib.idle_add(self.update_store, i, store, icon ) async def update_store(self, i, store, dir, tab, file): icon = tab.create_icon(dir, file) itr = store.get_iter(i) store.set_value(itr, 0, icon) - def create_icons_generator(self, tab, dir, files): - for file in files: - icon = tab.create_icon(dir, file[0]) - yield icon - - # @daemon_threaded - # def create_icon(self, i, tab, store, dir, file): - # icon = tab.create_icon(dir, file) - # GLib.idle_add(self.update_store, *(i, store, icon,)) - # - # @daemon_threaded - # def load_icon(self, i, store, icon): - # GLib.idle_add(self.update_store, *(i, store, icon,)) - # - # def update_store(self, i, store, icon): - # itr = store.get_iter(i) - # store.set_value(itr, 0, icon) - def create_tab_widget(self, tab): return TabHeaderWidget(tab, self.close_tab) @@ -136,4 +114,4 @@ class GridMixin: store = icon_grid.get_model() tab_label = notebook.get_tab_label(obj).get_children()[0] - return store, tab_label + return store, tab_label \ No newline at end of file