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