about summary refs log tree commit diff stats
path: root/fungus
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-07-05 22:18:57 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-07-05 22:18:57 +0300
commit30dff8577cf7b5d2d7f76e1ba465c6c3c7bd4e3e (patch)
treea82e729480232cb9fba500c2861b55c3951c0e65 /fungus
parentf8205cbfa571e5e4c2e57654580b46bec1b88c48 (diff)
downloadfungus-launcher-dev.tar.gz
fungus-launcher-dev.tar.xz
initialize prefix asynchronously when adding new game as to not block the UI dev
Diffstat (limited to 'fungus')
-rw-r--r--fungus/add_game_dialog.py53
-rw-r--r--fungus/main_window.py49
2 files changed, 89 insertions, 13 deletions
diff --git a/fungus/add_game_dialog.py b/fungus/add_game_dialog.py
index 25e12bf..0cb2597 100644
--- a/fungus/add_game_dialog.py
+++ b/fungus/add_game_dialog.py
@@ -126,7 +126,15 @@ class AddGame(EntryTooltipMixin, Gtk.Dialog):
         self.grid_shortcut_icon.set_margin_start(10)
         self.grid_shortcut_icon.set_margin_end(10)
         self.grid_shortcut_icon.set_margin_top(10)
-        self.grid_shortcut_icon.set_margin_bottom(10)
+        self.grid_shortcut_icon.set_margin_bottom(5)
+
+        # Status label for background prefix initialisation
+        self._prefix_status_label = Gtk.Label(label="")
+        self._prefix_status_label.set_visible(False)
+        self._prefix_status_label.set_halign(Gtk.Align.START)
+        self._prefix_status_label.set_markup("<i>Click OK to create prefix</i>")
+        self._prefix_status_label.set_margin_start(10)
+        self._prefix_status_label.set_margin_bottom(10)
 
         self.grid_protonfix = Gtk.Grid()
         self.grid_protonfix.set_row_spacing(10)
@@ -168,6 +176,12 @@ class AddGame(EntryTooltipMixin, Gtk.Dialog):
         .combobox {
             border: 1px solid red;
         }
+        .prefix-status-ready {
+            color: green;
+        }
+        .prefix-status-error {
+            color: red;
+        }
         """
         css_provider.load_from_data(css.encode('utf-8'))
         Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(), css_provider,
@@ -392,6 +406,7 @@ class AddGame(EntryTooltipMixin, Gtk.Dialog):
         page1.add(self.grid_prefix)
         page1.add(self.grid_runner)
         page1.add(self.grid_shortcut_icon)
+        page1.add(self._prefix_status_label)
 
         self.grid_protonfix.attach(self.label_protonfix, 0, 0, 1, 1)
         self.grid_protonfix.attach(self.entry_protonfix, 0, 1, 3, 1)
@@ -933,6 +948,42 @@ class AddGame(EntryTooltipMixin, Gtk.Dialog):
 
         return True
 
+    # ── Background prefix initialisation ─────────────────────────────
+
+    def _set_prefix_status(self, message: str, success: bool | None) -> None:
+        """Update the prefix-status label and the OK button state.
+
+        Parameters
+        ----------
+        message : str
+            Status text to display.
+        success : bool or None
+            ``True`` = ready, ``False`` = failed, ``None`` = in progress.
+        """
+        if not hasattr(self, "_prefix_status_label"):
+            return
+
+        self._prefix_status_label.set_visible(True)
+        self._prefix_status_label.set_text(message)
+        if success is True:
+            self._prefix_status_label.get_style_context().add_class("prefix-status-ready")
+            self._prefix_status_label.get_style_context().remove_class("prefix-status-error")
+            self.button_ok.set_sensitive(True)
+        elif success is False:
+            self._prefix_status_label.get_style_context().add_class("prefix-status-error")
+            self._prefix_status_label.get_style_context().remove_class("prefix-status-ready")
+            self.button_ok.set_sensitive(True)  # allow user to close and retry
+        else:
+            self._prefix_status_label.get_style_context().remove_class("prefix-status-ready")
+            self._prefix_status_label.get_style_context().remove_class("prefix-status-error")
+            self.button_ok.set_sensitive(False)
+
+    def _close_after_prefix(self, response_id: int) -> None:
+        """Close the dialog after prefix initialisation finishes."""
+        if hasattr(self, "blocking_window"):
+            GLib.idle_add(self.blocking_window.destroy)
+        GLib.idle_add(self.response, response_id)
+
     # ── Environment tab handlers ─────────────────────────────────────
 
     def populate_environment_from_game(self, game):
diff --git a/fungus/main_window.py b/fungus/main_window.py
index 75cb76b..122473b 100644
--- a/fungus/main_window.py
+++ b/fungus/main_window.py
@@ -6,6 +6,7 @@ import os
 import shutil
 import subprocess
 import sys
+import threading
 
 import gi
 gi.require_version('Gtk', '3.0')
@@ -698,6 +699,7 @@ class Main(Gtk.ApplicationWindow):
 
         if game:
             edit_game_dialog = AddGame(self, file_path, is_editing=True, dispatcher=self.dispatcher)
+            edit_game_dialog._prefix_status_label.set_visible(False)
             edit_game_dialog.connect("response", self.on_edit_dialog_response, edit_game_dialog, game)
 
             model_runner = edit_game_dialog.combobox_runner.get_model()
@@ -854,24 +856,47 @@ class Main(Gtk.ApplicationWindow):
             if prefix:
                 if not os.path.isdir(prefix):
                     os.makedirs(prefix, exist_ok=True)
-                    # Initialize the Wine prefix via UMU
+                    # Initialize the Wine prefix via UMU (background thread)
                     gameid = protonfix if protonfix else title_formatted
                     if gameid and runner:
                         env = os.environ.copy()
                         env["GAMEID"] = gameid
                         env["WINEPREFIX"] = prefix
                         env["PROTONPATH"] = runner
-                        try:
-                            subprocess.run(
-                                [umu_run, "wineprefixcreate", "/usr/bin/true"],
-                                env=env,
-                                check=True,
-                                capture_output=True,
-                            )
-                        except subprocess.CalledProcessError as e:
-                            log.error("Prefix initialization failed for '%s': %s", title, e.stderr.decode())
-                    else:
-                        log.warning("Skipping prefix init for '%s' (GAMEID or runner missing)", title)
+
+                        umu_run_path = umu_run
+                        umu_env = env
+                        umu_title = title
+                        umu_dispatcher = self.dispatcher
+
+                        def _init_prefix():
+                            try:
+                                subprocess.run(
+                                    [umu_run_path, "wineprefixcreate", "/usr/bin/true"],
+                                    env=umu_env,
+                                    check=True,
+                                    capture_output=True,
+                                )
+                                log.info("Prefix initialized for '%s'", umu_title)
+                                umu_dispatcher.submit(
+                                    add_game_dialog._set_prefix_status,
+                                    "Prefix ready",
+                                    True,
+                                )
+                            except subprocess.CalledProcessError as e:
+                                log.error("Prefix initialization failed for '%s': %s", umu_title, e.stderr.decode())
+                                umu_dispatcher.submit(
+                                    add_game_dialog._set_prefix_status,
+                                    "Prefix init failed",
+                                    False,
+                                )
+                            finally:
+                                umu_dispatcher.submit(add_game_dialog._close_after_prefix, response_id)
+
+                        add_game_dialog._set_prefix_status("Creating prefix…", None)
+                        threading.Thread(target=_init_prefix, daemon=True).start()
+                        return True  # defer dialog close
+                log.warning("Skipping prefix init for '%s' (GAMEID or runner missing)", title)
 
             if self.library.exists(title_formatted):
                 self.show_warning_dialog(