#!/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()