#!/usr/bin/python3 """GTK-dependent utility functions. Pure utility functions live in ``fungus.utils_pure`` and are re-exported here for backward compatibility. """ import os import re import gi gi.require_version('Pango', '1.0') from gi.repository import Pango # type: ignore from fungus.logger import get_logger log = get_logger(__name__) # Re-export pure utilities so existing imports from fungus.utils continue # to work without breaking. from fungus.utils_pure import ( # noqa: F401 format_title, has_proton_installed, is_executable_in_whitelist, is_safe_prefix_path, is_valid_prefix_path, _resolve_runner_to_directory, ) def _validate_text(entry): """Remove non-Latin alphabetic characters from a GTK Entry widget.""" import unicodedata text = entry.get_text() for i, c in enumerate(text): if c.isalpha() and "LATIN" not in unicodedata.name(c, ""): new_text = text[:i] + text[i+1:] entry.set_text(new_text) entry.set_position(i) break def populate_combobox_with_runners(combobox): """Populate a GTK ComboBoxText with available Proton/compatibility tool runners. Scans ``/Steam/compatibilitytools.d/`` for directories, sorts them by natural version ordering (newest first), and sets the first item as active. The cell renderer is configured for ellipsiation. """ from fungus.constants import share_dir runner_path = f'{share_dir}/Steam/compatibilitytools.d/' versions = [] try: if os.path.exists(runner_path): for entry in os.listdir(runner_path): entry_path = os.path.join(runner_path, entry) if os.path.isdir(entry_path) and entry != "LegacyRuntime": versions.append(entry) def version_key(v): parts = re.split(r'(\d+)', v) return [int(p) if p.isdigit() else p for p in parts] versions.sort(key=version_key, reverse=True) for version in versions: combobox.append_text(version) except Exception as e: log.error("Error accessing the directory: %s", e) if versions: combobox.set_active(0) cell_renderer = combobox.get_cells()[0] cell_renderer.set_property("ellipsize", Pango.EllipsizeMode.END) cell_renderer.set_property("max-width-chars", 20) def pick_folder(title: str, transient_for, current_folder: str | None = None) -> str | None: """Open a GTK folder picker and return the selected path (or ``None``). Parameters ---------- title : str Dialog window title. transient_for : Gtk.Window Transient parent window. current_folder : str, optional Folder to open by default. Falls back to ``~/`` if omitted or the path does not exist. """ from gi.repository import Gtk # type: ignore chooser = Gtk.FileChooserNative( title=title, transient_for=transient_for, action=Gtk.FileChooserAction.SELECT_FOLDER, accept_label="Open", cancel_label="Cancel", ) if current_folder and os.path.isdir(current_folder): chooser.set_current_folder(current_folder) else: chooser.set_current_folder(os.path.expanduser("~")) response = chooser.run() if response == Gtk.ResponseType.ACCEPT: result = chooser.get_filename() else: result = None chooser.destroy() return result class EntryTooltipMixin: """Mixin providing standard ``query-tooltip`` handlers for GTK widgets. ``on_entry_query_tooltip`` — displays the entry's current text when non-empty, otherwise falls back to the entry's ``tooltip-text`` property. ``on_list_query_tooltip`` — shows a tooltip with the cell value when hovering over a TreeView row. Accepts a single *liststore* to resolve the cell from. """ def on_entry_query_tooltip(self, widget, x, y, keyboard_mode, tooltip): current_text = widget.get_text() if current_text.strip(): tooltip.set_text(current_text) else: tooltip.set_text(widget.get_tooltip_text()) return True def on_list_query_tooltip(self, widget, x, y, keyboard_mode, tooltip, liststore): """Show a tooltip with the cell value when hovering over a treeview row. Parameters ---------- widget : Gtk.TreeView The treeview that emitted the signal. x, y : int The cursor position inside *widget*. keyboard_mode : bool Whether the tooltip was triggered by the keyboard. tooltip : Gtk.Tooltip The tooltip widget to populate. liststore : Gtk.ListStore The liststore to resolve the cell from. """ result = widget.get_path_at_pos(x, y) if result is not None: path, *_ = result try: tree_iter = liststore.get_iter(path) except ValueError: return False value = liststore.get_value(tree_iter, 0) if value.strip(): tooltip.set_text(value) return True return False