"""Splash window for game launch output. Provides a ``SplashWindow`` class that displays a modal window with live stdout/stderr output during game launch, and can show error information (exit code, accumulated stderr) when the process fails. """ from __future__ import annotations from collections.abc import Callable import gi gi.require_version("Gtk", "3.0") from gi.repository import Gtk, GLib # type: ignore from fungus.constants import fungus_png class SplashWindow: """A splash window showing live game output and error information.""" def __init__(self, title: str, on_close: Callable[[], None]) -> None: """Create the splash window. Parameters ---------- title : str Window title (usually the game name). on_close : callable Callback invoked when the user clicks the Close button. """ self._on_close = on_close self._window = Gtk.Window(title=title) self._buffer = None self._text_view = None self._scrolled = None self._close_button = None self._build() # ── UI construction ─────────────────────────────────────────────────── def _build(self) -> None: """Build the splash window UI.""" self._window.set_decorated(True) self._window.set_resizable(True) self._window.set_default_size(840, 400) self._window.set_icon_from_file(fungus_png) frame = Gtk.Frame() frame.set_label_align(0.5, 0.5) frame.set_shadow_type(Gtk.ShadowType.ETCHED_IN) grid = Gtk.Grid() grid.set_hexpand(True) grid.set_vexpand(True) grid.set_row_spacing(10) frame.add(grid) # Live output text view (hidden until output arrives) self._buffer = Gtk.TextBuffer() self._text_view = Gtk.TextView(buffer=self._buffer) self._text_view.set_editable(False) self._text_view.set_cursor_visible(False) self._text_view.set_monospace(True) self._text_view.set_margin_top(5) self._text_view.set_margin_bottom(5) self._text_view.set_margin_start(10) self._text_view.set_margin_end(10) self._text_view.set_wrap_mode(Gtk.WrapMode.WORD_CHAR) self._text_view.get_style_context().add_class("output-view") self._scrolled = Gtk.ScrolledWindow() self._scrolled.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) self._scrolled.set_min_content_height(120) self._scrolled.set_hexpand(True) self._scrolled.set_vexpand(True) self._scrolled.set_visible(False) self._scrolled.add(self._text_view) grid.attach(self._scrolled, 0, 1, 1, 1) # Close button (hidden until error) self._close_button = Gtk.Button(label="Close") self._close_button.set_sensitive(False) self._close_button.set_size_request(150, -1) self._close_button.connect("clicked", self._on_close_clicked) grid.attach(self._close_button, 0, 2, 1, 1) self._window.add(frame) # Handle window manager close (X button) self._window.connect("delete_event", self._on_delete_event) self._window.show_all() # ── Public API ──────────────────────────────────────────────────────── def append_output( self, line: str, *, color: str | None = None, bold: bool = False, ) -> None: """Append a line of output to the text view. Parameters ---------- line : str Text line to append. color : str, optional Pango color name (e.g. ``"red"``, ``"#888888"``). bold : bool, default False Whether to render the line in bold. """ if self._buffer is None or self._text_view is None: return # Wrap in Pango markup if styling is requested. if color or bold: attrs: list[str] = [] if color: attrs.append(f"foreground=\"{color}\"") if bold: attrs.append("weight=bold") markup = f"{line}" else: markup = line end = self._buffer.get_end_iter() self._buffer.insert_markup(end, markup + "\n", -1) self._text_view.scroll_to_iter(end, 0.0, True, 0.0, 1.0) # Reveal output view on first output if self._scrolled is not None: self._scrolled.set_visible(True) def show_error(self, exit_code: int) -> None: """Re-show the splash after a failed process exit. The live stream already displayed stderr, so we just update the title and reveal the Close button. Parameters ---------- exit_code : int Non-zero process exit code. """ assert self._window is not None # Reveal the window if it was hidden when the game started self._window.show_all() # Update window title with exit code current_title = self._window.get_title() or "" self._window.set_title(f"{current_title} — exited with code {exit_code}") # Reveal output view and close button if self._scrolled is not None: self._scrolled.set_visible(True) if self._close_button is not None: self._close_button.set_visible(True) self._close_button.set_sensitive(True) def close(self) -> None: """Hide the splash window. Called when the game has started — the window is kept alive so output can continue to be written and it can be shown again if the game exits with an error. """ if self._window is not None: self._window.hide() def _destroy(self) -> None: """Destroy the splash window and invoke the close callback. Called during final cleanup (process exit). The callback is cleared after the first invocation to prevent an infinite loop. """ if self._window is not None: self._window.destroy() # type: ignore[union-attr] # Clear the callback so repeated _destroy() calls are no-ops. self._on_close() self._on_close = lambda: None def show(self) -> None: """Show the splash window (no-op if already visible).""" if self._window is not None: self._window.show_all() def hide(self) -> None: """Hide the splash window.""" if self._window is not None: self._window.hide() def destroy(self) -> None: """Destroy the window and release all references.""" if self._window is not None: self._window.destroy() # type: ignore[union-attr] # ── Internal ────────────────────────────────────────────────────────── def _on_delete_event(self, _widget: Gtk.Widget, _event: object) -> bool: """Handle window manager close (X button). Returns ``True`` to prevent further destruction (we handle it ourselves). Quits GTK in error mode, hides the splash otherwise. """ if self._close_button is not None and self._close_button.get_visible(): GLib.idle_add(Gtk.main_quit) self.close() return True # prevent default destruction def _on_close_clicked(self, _widget: Gtk.Widget) -> None: """Handle the Close button click. When the Close button is visible (error mode), quit GTK — the game process is already dead and there is nothing left to do. """ self._on_delete_event(None, None)