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
|
#!/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 ``<share_dir>/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
|