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 • 8.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, GioUnix, 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
locale.bindtextdomain("panorama-app-menu", module_directory / "locale")
68
_ = lambda x: locale.dgettext("panorama-app-menu", x)
69
70
71
class AppMenu(panorama_panel.Applet):
72
name = _("App menu")
73
description = _("Show apps installed on your system, grouped by category")
74
icon = Gio.ThemedIcon.new("start-here")
75
76
def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None):
77
super().__init__(orientation=orientation, config=config)
78
locale.bindtextdomain("panorama-app-menu", module_directory / "locale")
79
_ = lambda x: locale.dgettext("panorama-app-menu", x)
80
if config is None:
81
config = {}
82
83
self.auto_refresh = config.get("auto_refresh", True)
84
self.category_mappings = config.get("category_mappings", CATEGORY_MAPPINGS)
85
self.trigger_name = config.get("trigger_name", "app-menu")
86
self.icon_name = config.get("icon_name", "start-here-symbolic")
87
88
self.button = Gtk.MenuButton()
89
self.button.set_has_frame(False) # flat look
90
self.icon = Gtk.Image.new_from_icon_name(self.icon_name)
91
self.icon.set_pixel_size(config.get("icon_size", 24))
92
self.button.set_child(self.icon)
93
self.apps_by_id: dict[int, Gio.AppInfo] = {}
94
# Wait for the widget to be in a layer-shell window before doing this
95
self.connect("realize", lambda *args: self.add_trigger_to_app())
96
97
self.menu = Gio.Menu()
98
self.popover = Gtk.PopoverMenu.new_from_model_full(self.menu, Gtk.PopoverMenuFlags.NESTED)
99
self.popover.set_has_arrow(False)
100
panorama_panel.track_popover(self.popover)
101
self.button.set_popover(self.popover)
102
103
self.generate_app_menu()
104
105
self.append(self.button)
106
107
self.context_menu = self.make_context_menu()
108
panorama_panel.track_popover(self.context_menu)
109
110
right_click_controller = Gtk.GestureClick()
111
right_click_controller.set_button(3)
112
right_click_controller.connect("pressed", self.show_context_menu)
113
114
self.add_controller(right_click_controller)
115
116
action_group = Gio.SimpleActionGroup()
117
options_action = Gio.SimpleAction.new("options", None)
118
options_action.connect("activate", self.show_options)
119
action_group.add_action(options_action)
120
options_action = Gio.SimpleAction.new("launch-app", GLib.VariantType.new("s"))
121
options_action.connect("activate", self.launch_app)
122
action_group.add_action(options_action)
123
self.insert_action_group("applet", action_group)
124
125
if self.auto_refresh:
126
self.app_info_monitor = Gio.AppInfoMonitor.get()
127
self.app_info_monitor.connect("changed", self.generate_app_menu)
128
129
self.options_window = None
130
131
def add_trigger_to_app(self):
132
app: Gtk.Application = self.get_root().get_application()
133
action = Gio.SimpleAction.new(self.trigger_name, None)
134
action.connect("activate", lambda *args: self.button.popup())
135
app.add_action(action)
136
137
def launch_app(self, action, id: GLib.Variant):
138
app = self.apps_by_id[int(id.get_string())]
139
app.launch()
140
141
def make_context_menu(self):
142
menu = Gio.Menu()
143
menu.append(_("Menu _options"), "applet.options")
144
context_menu = Gtk.PopoverMenu.new_from_model(menu)
145
context_menu.set_has_arrow(False)
146
context_menu.set_parent(self)
147
context_menu.set_halign(Gtk.Align.START)
148
context_menu.set_flags(Gtk.PopoverMenuFlags.NESTED)
149
return context_menu
150
151
def show_context_menu(self, gesture, n_presses, x, y):
152
rect = Gdk.Rectangle()
153
rect.x = int(x)
154
rect.y = int(y)
155
rect.width = 1
156
rect.height = 1
157
158
self.context_menu.set_pointing_to(rect)
159
self.context_menu.popup()
160
161
def show_options(self, _0=None, _1=None):
162
...
163
164
def shutdown(self, app: Gtk.Application):
165
app.remove_action(self.trigger_name)
166
167
def get_config(self):
168
return {
169
"category_mappings": self.category_mappings,
170
"trigger_name": self.trigger_name,
171
"icon_name": self.icon_name,
172
"icon_size": self.icon.get_pixel_size(),
173
}
174
175
def set_panel_position(self, position):
176
self.popover.set_position(panorama_panel.OPPOSITE_POSITION[position])
177
self.button.set_direction(panorama_panel.POSITION_TO_ARROW[panorama_panel.OPPOSITE_POSITION[position]])
178
179
def generate_app_menu(self, app_info_monitor=None):
180
self.menu.remove_all()
181
self.apps_by_id = {}
182
183
all_apps = Gio.AppInfo.get_all()
184
apps_by_category: dict[str, list[Gio.AppInfo]] = {}
185
for category, info in self.category_mappings.items():
186
apps_by_category[category] = []
187
188
for app in all_apps:
189
category_found = False
190
if isinstance(app, GioUnix.DesktopAppInfo):
191
if app.get_categories() is not None:
192
categories = app.get_categories().split(";")
193
for category in categories:
194
if category in apps_by_category:
195
apps_by_category[category].append(app)
196
category_found = True
197
198
if not category_found:
199
apps_by_category["Other"].append(app)
200
201
for apps in apps_by_category.values():
202
apps.sort(key=lambda app: app.get_display_name())
203
204
for category, info in self.category_mappings.items():
205
if apps_by_category[category]:
206
item = Gio.MenuItem.new(_(info["menu_name"]))
207
item.set_icon(Gio.ThemedIcon.new(info["icon"]))
208
submenu = Gio.Menu()
209
210
for app in apps_by_category[category]:
211
if isinstance(app, GioUnix.DesktopAppInfo) and (app.get_is_hidden() or app.get_nodisplay()):
212
continue
213
214
subitem = Gio.MenuItem.new(app.get_display_name(), f"applet.launch-app::{id(app)}")
215
subitem.set_icon(app.get_icon() or Gio.ThemedIcon.new("image-missing"))
216
self.apps_by_id[id(app)] = app
217
submenu.append_item(subitem)
218
219
item.set_submenu(submenu)
220
self.menu.append_item(item)
221
222
panorama_panel.add_icons_to_menu(self.popover)
223