By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 __init__.py

View raw Download
text/plain • 10.37 kiB
Python script, ASCII text executable
        
            
1
"""
2
MATE-like app menu applet for the Panorama panel.
3
Copyright 2025, roundabout-host.com <vlad@roundabout-host.com>
4
5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public Licence as published by
7
the Free Software Foundation, either version 3 of the Licence, or
8
(at your option) any later version.
9
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public Licence for more details.
14
15
You should have received a copy of the GNU General Public Licence
16
along with this program. If not, see <https://www.gnu.org/licenses/>.
17
"""
18
19
import os
20
from pathlib import Path
21
import locale
22
import panorama_panel
23
24
import gi
25
gi.require_version("Gtk", "4.0")
26
27
from gi.repository import Gtk, GLib, Gio, Gdk
28
29
30
module_directory = Path(__file__).resolve().parent
31
32
33
CATEGORY_MAPPINGS = {
34
"Utility": {"menu_name": "Accessories", "icon": "applications-accessories"},
35
"Development": {"menu_name": "Programming", "icon": "applications-development"},
36
"Game": {"menu_name": "Games", "icon": "applications-games"},
37
"Graphics": {"menu_name": "Graphics", "icon": "applications-graphics"},
38
"Network": {"menu_name": "Network", "icon": "applications-internet"},
39
"AudioVideo": {"menu_name": "Multimedia", "icon": "applications-multimedia"},
40
"Office": {"menu_name": "Office", "icon": "applications-office"},
41
"Science": {"menu_name": "Science", "icon": "applications-science"},
42
"Education": {"menu_name": "Education", "icon": "applications-education"},
43
"System": {"menu_name": "System", "icon": "applications-system"},
44
"Settings": {"menu_name": "Settings", "icon": "preferences-desktop"},
45
"Other": {"menu_name": "Other", "icon": "applications-other"},
46
}
47
48
custom_css = """
49
.no-menu-item-padding:dir(ltr) {
50
padding-left: 0;
51
}
52
53
.no-menu-item-padding:dir(rtl) {
54
padding-right: 0;
55
}
56
"""
57
58
css_provider = Gtk.CssProvider()
59
css_provider.load_from_data(custom_css)
60
Gtk.StyleContext.add_provider_for_display(
61
Gdk.Display.get_default(),
62
css_provider,
63
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
64
)
65
66
67
def force_visible_on_visible_notify(widget, *args):
68
if not widget.get_visible():
69
widget.set_visible(True)
70
71
72
def add_icons_to_menu(popover: Gtk.PopoverMenu):
73
section = popover.get_child().get_first_child().get_first_child().get_first_child()
74
while section is not None:
75
child = section.get_first_child().get_first_child()
76
77
while child is not None:
78
gutter_box: Gtk.Box = child.get_first_child()
79
if isinstance(gutter_box.get_next_sibling(), Gtk.Image):
80
# For some reason GTK creates images but they're hidden?
81
image: Gtk.Image = gutter_box.get_next_sibling()
82
label: Gtk.Label = image.get_next_sibling()
83
84
image.unparent()
85
gutter_box.unparent()
86
gutter_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
87
gutter_box.insert_before(child, label)
88
gutter_box.append(image)
89
image.set_icon_size(Gtk.IconSize.LARGE)
90
image.set_margin_start(8)
91
image.set_margin_end(8)
92
93
child.add_css_class("no-menu-item-padding")
94
95
# Push the arrow to the left
96
label.set_halign(Gtk.Align.FILL)
97
label.set_hexpand(True)
98
label.set_xalign(0)
99
100
# GTK pushes its stance on icons so hard it makes them invisible multiple times;
101
# force it visible
102
image.set_visible(True)
103
image.connect("notify::visible", force_visible_on_visible_notify)
104
105
# Find the submenu if there is one
106
subchild = child.get_first_child()
107
submenu = None
108
while subchild is not None:
109
if isinstance(subchild, Gtk.PopoverMenu):
110
submenu = subchild
111
subchild = subchild.get_next_sibling()
112
113
# Recursive
114
if submenu is not None:
115
add_icons_to_menu(submenu)
116
117
child = child.get_next_sibling()
118
119
section = section.get_next_sibling()
120
121
122
locale.bindtextdomain("panorama-app-menu", module_directory / "locale")
123
_ = lambda x: locale.dgettext("panorama-app-menu", x)
124
125
126
class AppMenu(panorama_panel.Applet):
127
name = _("App menu")
128
description = _("Show apps installed on your system, grouped by category")
129
130
def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None):
131
super().__init__(orientation=orientation, config=config)
132
locale.bindtextdomain("panorama-app-menu", module_directory / "locale")
133
_ = lambda x: locale.dgettext("panorama-app-menu", x)
134
if config is None:
135
config = {}
136
137
self.auto_refresh = config.get("auto_refresh", True)
138
self.category_mappings = config.get("category_mappings", CATEGORY_MAPPINGS)
139
self.trigger_name = config.get("trigger_name", "app-menu")
140
self.icon_name = config.get("icon_name", "start-here-symbolic")
141
142
self.button = Gtk.MenuButton()
143
self.button.set_has_frame(False) # flat look
144
self.icon = Gtk.Image.new_from_icon_name(self.icon_name)
145
self.icon.set_pixel_size(config.get("icon_size", 24))
146
self.button.set_child(self.icon)
147
self.apps_by_id: dict[int, Gio.AppInfo] = {}
148
# Wait for the widget to be in a layer-shell window before doing this
149
self.connect("realize", lambda *args: self.add_trigger_to_app())
150
151
self.menu = Gio.Menu()
152
self.popover = Gtk.PopoverMenu.new_from_model_full(self.menu, Gtk.PopoverMenuFlags.NESTED)
153
self.popover.set_has_arrow(False)
154
panorama_panel.track_popover(self.popover)
155
self.button.set_popover(self.popover)
156
157
self.generate_app_menu()
158
159
self.append(self.button)
160
161
self.context_menu = self.make_context_menu()
162
panorama_panel.track_popover(self.context_menu)
163
164
right_click_controller = Gtk.GestureClick()
165
right_click_controller.set_button(3)
166
right_click_controller.connect("pressed", self.show_context_menu)
167
168
self.add_controller(right_click_controller)
169
170
action_group = Gio.SimpleActionGroup()
171
options_action = Gio.SimpleAction.new("options", None)
172
options_action.connect("activate", self.show_options)
173
action_group.add_action(options_action)
174
options_action = Gio.SimpleAction.new("launch-app", GLib.VariantType.new("s"))
175
options_action.connect("activate", self.launch_app)
176
action_group.add_action(options_action)
177
self.insert_action_group("applet", action_group)
178
179
if self.auto_refresh:
180
self.app_info_monitor = Gio.AppInfoMonitor.get()
181
self.app_info_monitor.connect("changed", self.generate_app_menu)
182
183
self.options_window = None
184
185
def add_trigger_to_app(self):
186
app: Gtk.Application = self.get_root().get_application()
187
action = Gio.SimpleAction.new(self.trigger_name, None)
188
action.connect("activate", lambda *args: self.button.popup())
189
app.add_action(action)
190
191
def launch_app(self, action, id: GLib.Variant):
192
app = self.apps_by_id[int(id.get_string())]
193
app.launch()
194
195
def make_context_menu(self):
196
menu = Gio.Menu()
197
menu.append(_("Menu _options"), "applet.options")
198
context_menu = Gtk.PopoverMenu.new_from_model(menu)
199
context_menu.set_has_arrow(False)
200
context_menu.set_parent(self)
201
context_menu.set_halign(Gtk.Align.START)
202
context_menu.set_flags(Gtk.PopoverMenuFlags.NESTED)
203
return context_menu
204
205
def show_context_menu(self, gesture, n_presses, x, y):
206
rect = Gdk.Rectangle()
207
rect.x = int(x)
208
rect.y = int(y)
209
rect.width = 1
210
rect.height = 1
211
212
self.context_menu.set_pointing_to(rect)
213
self.context_menu.popup()
214
215
def show_options(self, _0=None, _1=None):
216
...
217
218
def shutdown(self):
219
app: Gtk.Application = self.get_root().get_application()
220
app.remove_action(self.trigger_name)
221
222
def get_config(self):
223
return {
224
"category_mappings": self.category_mappings,
225
"trigger_name": self.trigger_name,
226
"icon_name": self.icon_name,
227
"icon_size": self.icon_size,
228
}
229
230
def set_panel_position(self, position):
231
self.popover.set_position(panorama_panel.OPPOSITE_POSITION[position])
232
self.button.set_direction(panorama_panel.POSITION_TO_ARROW[panorama_panel.OPPOSITE_POSITION[position]])
233
234
def generate_app_menu(self, app_info_monitor=None):
235
self.menu.remove_all()
236
self.apps_by_id = {}
237
238
all_apps = Gio.AppInfo.get_all()
239
apps_by_category: dict[str, list[Gio.AppInfo]] = {}
240
for category, info in self.category_mappings.items():
241
apps_by_category[category] = []
242
243
for app in all_apps:
244
category_found = False
245
if isinstance(app, Gio.DesktopAppInfo):
246
if app.get_categories() is not None:
247
categories = app.get_categories().split(";")
248
for category in categories:
249
if category in apps_by_category:
250
apps_by_category[category].append(app)
251
category_found = True
252
253
if not category_found:
254
apps_by_category["Other"].append(app)
255
256
for apps in apps_by_category.values():
257
apps.sort(key=lambda app: app.get_display_name())
258
259
for category, info in self.category_mappings.items():
260
if apps_by_category[category]:
261
item = Gio.MenuItem.new(_(info["menu_name"]))
262
item.set_icon(Gio.ThemedIcon.new(info["icon"]))
263
submenu = Gio.Menu()
264
265
for app in apps_by_category[category]:
266
if isinstance(app, Gio.DesktopAppInfo) and (app.get_is_hidden() or app.get_nodisplay()):
267
continue
268
269
subitem = Gio.MenuItem.new(app.get_display_name(), f"applet.launch-app::{id(app)}")
270
subitem.set_icon(app.get_icon() or Gio.ThemedIcon.new("image-missing"))
271
self.apps_by_id[id(app)] = app
272
submenu.append_item(subitem)
273
274
item.set_submenu(submenu)
275
self.menu.append_item(item)
276
277
add_icons_to_menu(self.popover)
278