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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
#!/usr/bin/python3
import subprocess
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk # type: ignore
from fungus.constants import fungus_png
def show_warning_dialog(
parent: Gtk.Window,
title: str,
secondary: str | None = None,
confirm: bool = False,
) -> bool | None:
"""Show a modal warning/confirmation dialog.
Parameters
----------
parent : Gtk.Window
Transient parent window.
title : str
Primary message text.
secondary : str, optional
Secondary message text (shown below *title*). Ignored when
*confirm* is ``True``.
confirm : bool
When ``True`` the dialog shows *Yes* / *No* buttons and
returns ``True`` if the user clicked *Yes*, ``False`` if
*No*, and ``None`` on window-close.
When ``False`` (default) the dialog shows an *Ok* button
and returns ``None``.
Returns
-------
bool or None
"""
if confirm:
message_type = Gtk.MessageType.QUESTION
buttons_type = Gtk.ButtonsType.YES_NO
else:
message_type = Gtk.MessageType.WARNING
buttons_type = Gtk.ButtonsType.OK
dialog = Gtk.MessageDialog(
parent=parent,
flags=Gtk.DialogFlags.MODAL,
message_type=message_type,
buttons=buttons_type,
text=title,
)
if secondary and not confirm:
dialog.format_secondary_text(secondary)
dialog.set_resizable(False)
dialog.set_icon_from_file(fungus_png)
response = dialog.run()
dialog.destroy()
if confirm:
return response == Gtk.ResponseType.YES
return None
class SandboxWarningDialog(Gtk.MessageDialog):
"""Warn the user that the game executable's directory is not in the sandbox whitelist."""
def __init__(self, exec_path: str, game_dirs: list[str]):
secondary = (
'The executable "{exec_path}" is outside the sandbox whitelist. '
'The game may fail to launch or may not access its required files.\n\n'
'Whitelisted directories: {game_dirs}\n\n'
'Please add the game directory to Settings → Game Directories.'
).format(
exec_path=exec_path,
game_dirs=", ".join(game_dirs),
)
super().__init__(
parent=None,
flags=Gtk.DialogFlags.MODAL,
message_type=Gtk.MessageType.WARNING,
buttons=Gtk.ButtonsType.OK,
text="Executable directory not in sandbox whitelist",
)
self.format_secondary_text(secondary)
self.set_resizable(False)
self.set_icon_from_file(fungus_png)
subprocess.Popen(["canberra-gtk-play", "-i", "dialog-warning"])
self.run()
self.destroy()
class GameDirectoriesNotConfiguredDialog(Gtk.MessageDialog):
"""Warn the user that game-directories is empty and offer to open Settings."""
def __init__(self, parent: Gtk.Window):
secondary = (
"The game-directories setting is currently empty. "
"Without at least one directory configured, the sandboxed "
"game environment will have no access to your game files.\n\n"
"Please add at least one game directory in Settings before launching."
)
super().__init__(
parent=parent,
flags=Gtk.DialogFlags.MODAL,
message_type=Gtk.MessageType.WARNING,
buttons=Gtk.ButtonsType.NONE,
text="Game Directories Not Configured",
)
self.format_secondary_text(secondary)
self.set_resizable(False)
self.set_icon_from_file(fungus_png)
self.add_button("Open Settings", Gtk.ResponseType.YES)
self.add_button("Cancel", Gtk.ResponseType.CANCEL)
self.set_default_response(Gtk.ResponseType.YES)
self.result = self.run()
self.destroy()
class UmuLauncherNotInstalledDialog(Gtk.MessageDialog):
"""Warn the user that UMU-Launcher is not installed and offer to open Settings."""
def __init__(self, parent: Gtk.Window):
secondary = (
"UMU-Launcher is required to launch games but is not currently installed. "
"Please install it via Settings → UMU-Launcher before launching."
)
super().__init__(
parent=parent,
flags=Gtk.DialogFlags.MODAL,
message_type=Gtk.MessageType.WARNING,
buttons=Gtk.ButtonsType.NONE,
text="UMU-Launcher Not Installed",
)
self.format_secondary_text(secondary)
self.set_resizable(False)
self.set_icon_from_file(fungus_png)
self.add_button("Open Settings", Gtk.ResponseType.YES)
self.add_button("Cancel", Gtk.ResponseType.CANCEL)
self.set_default_response(Gtk.ResponseType.YES)
self.result = self.run()
self.destroy()
def create_error_dialog(
title: str,
primary: str,
secondary: str | None = None,
error_text: str | None = None,
*,
quit_app: bool = False,
) -> None:
"""Build and display a modal error dialog.
Parameters
----------
title : str
Dialog window title.
primary : str
Primary message (always shown, centred, line-wrapped).
secondary : str, optional
Secondary message shown below *primary*. Ignored when ``None``.
error_text : str, optional
Raw error output shown in a scrollable text view below the labels.
Ignored when ``None`` or empty.
quit_app : bool, default False
If ``True``, call ``Gtk.main_quit()`` and ``sys.exit()`` after
the dialog is dismissed.
"""
from gi.repository import Gtk # type: ignore
import sys
from fungus.constants import fungus_png
dialog = Gtk.Dialog(title=title)
dialog.set_resizable(False)
dialog.set_icon_from_file(fungus_png)
content_area = dialog.get_content_area()
content_area.set_border_width(0)
content_area.set_halign(Gtk.Align.CENTER)
content_area.set_valign(Gtk.Align.CENTER)
content_area.set_vexpand(True)
content_area.set_hexpand(True)
box_top = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
box_top.set_margin_start(20)
box_top.set_margin_end(20)
box_top.set_margin_top(20)
box_top.set_margin_bottom(20)
label = Gtk.Label(label=primary)
label.set_halign(Gtk.Align.CENTER)
label.set_line_wrap(True)
label.set_max_width_chars(60)
box_top.pack_start(label, True, True, 0)
if secondary:
label2 = Gtk.Label(label=secondary)
label2.set_halign(Gtk.Align.CENTER)
box_top.pack_start(label2, True, True, 0)
if error_text:
scrolled = Gtk.ScrolledWindow()
scrolled.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
scrolled.set_min_content_height(120)
text_view = Gtk.TextView()
text_view.set_editable(False)
text_view.set_cursor_visible(False)
buffer = text_view.get_buffer()
buffer.set_text(error_text)
scrolled.add(text_view)
box_top.pack_start(scrolled, True, True, 0)
button_ok = Gtk.Button(label="Ok")
button_ok.set_size_request(150, -1)
button_ok.connect("clicked", lambda w: dialog.response(Gtk.ResponseType.OK))
box_bottom = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=10)
box_bottom.set_margin_start(10)
box_bottom.set_margin_end(10)
box_bottom.set_margin_bottom(10)
box_bottom.pack_start(button_ok, True, True, 0)
content_area.add(box_top)
content_area.add(box_bottom)
dialog.show_all()
dialog.run()
dialog.destroy()
if quit_app:
Gtk.main_quit()
sys.exit()
|