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 • 9.66 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
locale.bindtextdomain("panorama-app-menu", module_directory / "locale")
33
_ = lambda x: locale.dgettext("panorama-app-menu", x)
34
35
36
CATEGORY_MAPPINGS = {
37
"Utility": {"menu_name": _("Accessories"), "icon": "applications-accessories"},
38
"Development": {"menu_name": _("Programming"), "icon": "applications-development"},
39
"Game": {"menu_name": _("Games"), "icon": "applications-games"},
40
"Graphics": {"menu_name": _("Graphics"), "icon": "applications-graphics"},
41
"Network": {"menu_name": _("Network"), "icon": "applications-internet"},
42
"AudioVideo": {"menu_name": _("Multimedia"), "icon": "applications-multimedia"},
43
"Office": {"menu_name": _("Office"), "icon": "applications-office"},
44
"Science": {"menu_name": _("Science"), "icon": "applications-science"},
45
"Education": {"menu_name": _("Education"), "icon": "applications-education"},
46
"System": {"menu_name": _("System"), "icon": "applications-system"},
47
"Settings": {"menu_name": _("Settings"), "icon": "preferences-desktop"},
48
"Other": {"menu_name": _("Other"), "icon": "applications-other"},
49
}
50
51
custom_css = """
52
.no-menu-item-padding:dir(ltr) {
53
padding-left: 0;
54
}
55
56
.no-menu-item-padding:dir(rtl) {
57
padding-right: 0;
58
}
59
"""
60
61
css_provider = Gtk.CssProvider()
62
css_provider.load_from_data(custom_css)
63
Gtk.StyleContext.add_provider_for_display(
64
Gdk.Display.get_default(),
65
css_provider,
66
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
67
)
68
69
70
def force_visible_on_visible_notify(widget, *args):
71
if not widget.get_visible():
72
widget.set_visible(True)
73
74
75
def add_icons_to_menu(popover: Gtk.PopoverMenu):
76
section = popover.get_child().get_first_child().get_first_child().get_first_child()
77
while section is not None:
78
child = section.get_first_child().get_first_child()
79
80
while child is not None:
81
gutter_box: Gtk.Box = child.get_first_child()
82
if isinstance(gutter_box.get_next_sibling(), Gtk.Image):
83
# For some reason GTK creates images but they're hidden?
84
image: Gtk.Image = gutter_box.get_next_sibling()
85
label: Gtk.Label = image.get_next_sibling()
86
87
image.unparent()
88
gutter_box.unparent()
89
gutter_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
90
gutter_box.insert_before(child, label)
91
gutter_box.append(image)
92
image.set_icon_size(Gtk.IconSize.LARGE)
93
image.set_margin_start(8)
94
image.set_margin_end(8)
95
96
child.add_css_class("no-menu-item-padding")
97
98
# Push the arrow to the left
99
label.set_halign(Gtk.Align.FILL)
100
label.set_hexpand(True)
101
label.set_xalign(0)
102
103
# GTK pushes its stance on icons so hard it makes them invisible multiple times;
104
# force it visible
105
image.set_visible(True)
106
image.connect("notify::visible", force_visible_on_visible_notify)
107
108
# Find the submenu if there is one
109
subchild = child.get_first_child()
110
submenu = None
111
while subchild is not None:
112
if isinstance(subchild, Gtk.PopoverMenu):
113
submenu = subchild
114
subchild = subchild.get_next_sibling()
115
116
# Recursive
117
if submenu is not None:
118
add_icons_to_menu(submenu)
119
120
child = child.get_next_sibling()
121
122
section = section.get_next_sibling()
123
124
125
class AppMenu(panorama_panel.Applet):
126
name = _("App menu")
127
description = _("Show apps installed on your system, grouped by category")
128
129
def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None):
130
super().__init__(orientation=orientation, config=config)
131
if config is None:
132
config = {}
133
self.category_mappings = config.get("category_mappings", CATEGORY_MAPPINGS)
134
self.trigger_name = config.get("trigger_name", "app-menu")
135
self.button = Gtk.MenuButton()
136
self.button.set_has_frame(False) # flat look
137
self.icon = Gtk.Image.new_from_icon_name("start-here")
138
self.button.set_child(self.icon)
139
self.apps_by_id: dict[int, Gio.AppInfo] = {}
140
# Wait for the widget to be in a layer-shell window before doing this
141
self.connect("realize", lambda *args: self.add_trigger_to_app())
142
143
self.menu = Gio.Menu()
144
self.popover = Gtk.PopoverMenu.new_from_model_full(self.menu, Gtk.PopoverMenuFlags.NESTED)
145
self.popover.set_has_arrow(False)
146
panorama_panel.track_popover(self.popover)
147
self.button.set_popover(self.popover)
148
149
self.generate_app_menu()
150
151
self.append(self.button)
152
153
self.context_menu = self.make_context_menu()
154
panorama_panel.track_popover(self.context_menu)
155
156
right_click_controller = Gtk.GestureClick()
157
right_click_controller.set_button(3)
158
right_click_controller.connect("pressed", self.show_context_menu)
159
160
self.add_controller(right_click_controller)
161
162
action_group = Gio.SimpleActionGroup()
163
options_action = Gio.SimpleAction.new("options", None)
164
options_action.connect("activate", self.show_options)
165
action_group.add_action(options_action)
166
options_action = Gio.SimpleAction.new("launch-app", GLib.VariantType.new("s"))
167
options_action.connect("activate", self.launch_app)
168
action_group.add_action(options_action)
169
self.insert_action_group("applet", action_group)
170
171
self.options_window = None
172
173
def add_trigger_to_app(self):
174
app: Gtk.Application = self.get_root().get_application()
175
action = Gio.SimpleAction.new(self.trigger_name, None)
176
action.connect("activate", lambda *args: self.button.popup())
177
app.add_action(action)
178
179
def launch_app(self, action, id: GLib.Variant):
180
app = self.apps_by_id[int(id.get_string())]
181
app.launch()
182
183
def make_context_menu(self):
184
menu = Gio.Menu()
185
menu.append(_("Menu _options"), "applet.options")
186
context_menu = Gtk.PopoverMenu.new_from_model(menu)
187
context_menu.set_has_arrow(False)
188
context_menu.set_parent(self)
189
context_menu.set_halign(Gtk.Align.START)
190
context_menu.set_flags(Gtk.PopoverMenuFlags.NESTED)
191
return context_menu
192
193
def show_context_menu(self, gesture, n_presses, x, y):
194
rect = Gdk.Rectangle()
195
rect.x = int(x)
196
rect.y = int(y)
197
rect.width = 1
198
rect.height = 1
199
200
self.context_menu.set_pointing_to(rect)
201
self.context_menu.popup()
202
203
def show_options(self, _0=None, _1=None):
204
...
205
206
def shutdown(self):
207
app: Gtk.Application = self.get_root().get_application()
208
app.remove_action(self.trigger_name)
209
210
def get_config(self):
211
return {"category_mappings": self.category_mappings, "trigger_name": self.trigger_name}
212
213
def set_panel_position(self, position):
214
self.popover.set_position(panorama_panel.OPPOSITE_POSITION[position])
215
self.button.set_direction(panorama_panel.POSITION_TO_ARROW[panorama_panel.OPPOSITE_POSITION[position]])
216
217
def generate_app_menu(self):
218
self.menu.remove_all()
219
self.apps_by_id = {}
220
221
all_apps = Gio.AppInfo.get_all()
222
apps_by_category: dict[str, list[Gio.AppInfo]] = {}
223
for category, info in self.category_mappings.items():
224
apps_by_category[category] = []
225
226
for app in all_apps:
227
category_found = False
228
if isinstance(app, Gio.DesktopAppInfo):
229
if app.get_categories() is not None:
230
categories = app.get_categories().split(";")
231
for category in categories:
232
if category in apps_by_category:
233
apps_by_category[category].append(app)
234
category_found = True
235
236
if not category_found:
237
apps_by_category["Other"].append(app)
238
239
for category, info in self.category_mappings.items():
240
if apps_by_category[category]:
241
item = Gio.MenuItem.new(info["menu_name"])
242
item.set_icon(Gio.ThemedIcon.new(info["icon"]))
243
submenu = Gio.Menu()
244
245
for app in apps_by_category[category]:
246
if isinstance(app, Gio.DesktopAppInfo) and (app.get_is_hidden() or app.get_nodisplay()):
247
continue
248
249
subitem = Gio.MenuItem.new(app.get_display_name(), f"applet.launch-app::{id(app)}")
250
subitem.set_icon(app.get_icon() or Gio.ThemedIcon.new("image-missing"))
251
self.apps_by_id[id(app)] = app
252
submenu.append_item(subitem)
253
254
item.set_submenu(submenu)
255
self.menu.append_item(item)
256
257
add_icons_to_menu(self.popover)
258