1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
"""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 # 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)
self._window.show_all()
# ── Public API ────────────────────────────────────────────────────────
def append_output(self, line: str) -> None:
"""Append a line of output to the text view.
Parameters
----------
line : str
Text line to append.
"""
if self._buffer is None or self._text_view is None:
return
end = self._buffer.get_end_iter()
self._buffer.insert(end, line + "\n")
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, stderr_lines: list[str]) -> None:
"""Show error information in the splash window.
Parameters
----------
exit_code : int
Non-zero process exit code.
stderr_lines : list[str]
Accumulated stderr output.
"""
assert self._window is not None
# 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}")
# Show accumulated stderr in the text view
if stderr_lines and self._buffer is not None:
error_text = "\n".join(stderr_lines)
self.append_output(f"\n--- stderr ({len(stderr_lines)} lines) ---")
self.append_output(error_text)
# 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:
"""Destroy the splash window and invoke the close callback.
The callback is cleared after the first invocation to prevent
an infinite loop: ``close()`` → ``_on_close()`` → ``idle_add(close)``
→ ``close()`` → …
"""
if self._window is not None:
self._window.destroy() # type: ignore[union-attr]
# Clear the callback so repeated close() 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_close_clicked(self, _widget: Gtk.Widget) -> None:
"""Handle the Close button click."""
self.close()
|