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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
"""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"<span {' '.join(attrs)}>{line}</span>"
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)
|