__init__.py
Python script, ASCII text executable
1""" 2The MIT License (MIT) 3 4Copyright (c) 2025 Scott Moreau <oreaus@gmail.com>, modified by <root@roundabout-host.com> 5 6Permission is hereby granted, free of charge, to any person obtaining a copy 7of this software and associated documentation files (the "Software"), to deal 8in the Software without restriction, including without limitation the rights 9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10copies of the Software, and to permit persons to whom the Software is 11furnished to do so, subject to the following conditions: 12 13The above copyright notice and this permission notice shall be included in 14all copies or substantial portions of the Software. 15 16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22SOFTWARE. 23""" 24 25import os 26from pathlib import Path 27import locale 28import panorama_panel 29 30import gi 31gi.require_version("Gtk", "4.0") 32gi.require_version("Gtk4LayerShell", "1.0") 33 34from gi.repository import Gtk, GLib, Gtk4LayerShell, Gio, Gdk, Pango 35import subprocess 36 37 38LOGOUT_BUTTON_SIZE = 125 39LOGOUT_BUTTON_MARGIN = 10 40 41def create_logout_ui_button(icon_name, label_text): 42layout = Gtk.Box() 43image = Gtk.Image() 44label = Gtk.Label() 45button = Gtk.Button() 46button.set_size_request(LOGOUT_BUTTON_SIZE, LOGOUT_BUTTON_SIZE) 47image.set_from_icon_name(icon_name) 48label.set_text(label_text) 49layout.set_orientation(Gtk.Orientation.VERTICAL) 50layout.set_halign(Gtk.Align.CENTER) 51layout.append(image) 52image.set_icon_size(Gtk.IconSize.LARGE) 53image.set_vexpand(True) 54layout.append(label) 55button.set_child(layout) 56return button 57 58class WayfireLogoutUI(Gtk.Window): 59 60def __init__(self): 61super().__init__() 62 63hbox = Gtk.CenterBox() 64main_layout = Gtk.Grid() 65suspend = create_logout_ui_button("emblem-synchronizing", "Suspend") 66suspend.connect("clicked", self.on_suspend_click) 67main_layout.attach(suspend, 0, 0, 1, 1) 68 69hibernate = create_logout_ui_button("weather-clear-night", "Hibernate") 70hibernate.connect("clicked", self.on_hibernate_click) 71main_layout.attach(hibernate, 1, 0, 1, 1) 72 73switchuser = create_logout_ui_button("system-users", "Switch User") 74switchuser.connect("clicked", self.on_switchuser_click) 75main_layout.attach(switchuser, 2, 0, 1, 1) 76 77logout = create_logout_ui_button("system-log-out", "Log Out") 78logout.connect("clicked", self.on_logout_click) 79main_layout.attach(logout, 0, 1, 1, 1) 80 81reboot = create_logout_ui_button("system-reboot", "Reboot") 82reboot.connect("clicked", self.on_reboot_click) 83main_layout.attach(reboot, 1, 1, 1, 1) 84 85shutdown = create_logout_ui_button("system-shutdown", "Shut Down") 86shutdown.connect("clicked", self.on_shutdown_click) 87main_layout.attach(shutdown, 2, 1, 1, 1) 88 89cancel_button = Gtk.Button() 90cancel_button.set_size_request(100, 50) 91cancel_button.set_label("Cancel") 92main_layout.attach(cancel_button, 1, 2, 1, 1) 93cancel_button.connect("clicked", self.on_cancel_click) 94 95main_layout.set_row_spacing(LOGOUT_BUTTON_MARGIN) 96main_layout.set_column_spacing(LOGOUT_BUTTON_MARGIN) 97# Make surfaces layer shell 98Gtk4LayerShell.init_for_window(self) 99Gtk4LayerShell.set_namespace(self, "com.roundabout_host.panorama.logout") 100Gtk4LayerShell.set_layer(self, Gtk4LayerShell.Layer.OVERLAY) 101 102Gtk4LayerShell.set_anchor(self, Gtk4LayerShell.Edge.TOP, True) 103Gtk4LayerShell.set_anchor(self, Gtk4LayerShell.Edge.BOTTOM, True) 104Gtk4LayerShell.set_anchor(self, Gtk4LayerShell.Edge.LEFT, True) 105Gtk4LayerShell.set_anchor(self, Gtk4LayerShell.Edge.RIGHT, True) 106main_layout.set_valign(Gtk.Align.CENTER) 107hbox.set_center_widget(main_layout) 108hbox.set_hexpand(True) 109hbox.set_vexpand(True) 110self.set_child(hbox) 111self.get_style_context().add_class("logout") 112display = self.get_display() 113css_provider = Gtk.CssProvider() 114css_provider.load_from_data("window.logout { background-color: rgba(0, 0, 0, 0.5); }") 115Gtk.StyleContext.add_provider_for_display(display, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER) 116 117def on_suspend_click(self, button): 118GLib.spawn_command_line_async("systemctl suspend") 119 120def on_hibernate_click(self, button): 121GLib.spawn_command_line_async("systemctl hibernate") 122 123def on_switchuser_click(self, button): 124GLib.spawn_command_line_async("dm-tool switch-to-greeter") 125 126def on_logout_click(self, button): 127GLib.spawn_command_line_async("wayland-logout") 128 129def on_reboot_click(self, button): 130GLib.spawn_command_line_async("systemctl reboot") 131 132def on_shutdown_click(self, button): 133GLib.spawn_command_line_async("systemctl poweroff") 134 135def on_cancel_click(self, button): 136self.hide() 137 138class MenuItemButton(Gtk.Button): 139 140def __init__(self, name, desc, exe): 141super().__init__() 142self.name = name 143self.desc = desc 144self.exe = exe 145 146class SoreausMenu(panorama_panel.Applet): 147name = "Soreau's menu" 148description = "Flowbox app menu" 149 150def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None): 151super().__init__() 152 153self.button = Gtk.MenuButton() 154 155self.menu_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) 156image = Gtk.Image.new_from_icon_name("wayfire") 157image.set_icon_size(Gtk.IconSize.LARGE) 158self.append(self.button) 159self.button.set_child(image) 160self.popover = Gtk.Popover() 161self.popover.set_parent(self) 162self.flowbox = Gtk.FlowBox() 163self.flowbox.set_valign(Gtk.Align.START) 164self.flowbox.set_homogeneous(True) 165self.flowbox_item_focus_signal = self.flowbox.connect("selected-children-changed", self.on_flowbox_item_focus) 166self.app_buttons = [] 167self.scrolled_window = Gtk.ScrolledWindow() 168self.scrolled_window.set_size_request(-1, 360) 169self.scrolled_window.set_child(self.flowbox) 170self.logout_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) 171self.logout_button = Gtk.Button() 172self.logout_button.set_margin_top(6) 173self.logout_button.set_margin_bottom(6) 174self.logout_button.set_margin_start(6) 175self.logout_button.set_margin_end(6) 176self.logout_button.set_child(Gtk.Image.new_from_icon_name("system-shutdown")) 177self.logout_button.connect("clicked", self.on_logout_button_clicked) 178self.logout_box.set_halign(Gtk.Align.END) 179self.logout_box.append(self.logout_button) 180self.search_entry = Gtk.SearchEntry() 181self.search_entry.connect("search-changed", self.on_search_changed) 182self.search_entry.connect("activate", self.click_first_button) 183self.menu_box.append(self.search_entry) 184self.menu_box.append(self.scrolled_window) 185self.menu_box.append(self.logout_box) 186self.popover.set_child(self.menu_box) 187self.popover.set_size_request(400, 400) 188self.popover.connect("show", self.on_popover_popup) 189self.button.set_popover(self.popover) 190self.logout_ui = WayfireLogoutUI() 191self.populate_menu_entries() 192 193def click_first_button(self, search_entry): 194flowbox_child = self.flowbox.get_first_child() 195if flowbox_child is not None: 196button = flowbox_child.get_first_child() 197button.emit("clicked") 198 199def on_search_changed(self, search_entry): 200self.flowbox.remove_all() 201self.populate_menu_entries() 202 203def on_logout_button_clicked(self, button): 204self.logout_ui.present() 205self.popover.popdown() 206 207def on_flowbox_item_focus(self, flowbox): 208selected_children = flowbox.get_selected_children() 209if len(selected_children) >= 1: 210selected_children[0].get_child().grab_focus() 211 212def on_popover_popup(self, parent): 213for child in self.flowbox.get_selected_children(): 214self.flowbox.unselect_child(child) 215self.search_entry.set_text("") 216self.popover.popup() 217 218def app_button_clicked(self, app_button): 219subprocess.Popen(app_button.command, start_new_session=True) 220self.popover.popdown() 221 222def populate_menu_entries(self): 223if self.search_entry.get_text(): 224desktop_names = Gio.DesktopAppInfo.search(self.search_entry.get_text()) 225 226app_infos = [] 227 228for sublist in desktop_names: 229for desktop_name in sublist: 230try: 231app_infos.append(Gio.DesktopAppInfo.new(desktop_name)) 232except TypeError: 233# The constructor may return a C NULL if the file is unable to be launched, 234# so skip the app 235pass 236else: 237app_infos = Gio.AppInfo.get_all() 238app_infos.sort(key=lambda app_info: app_info.get_display_name()) 239 240for app_info in app_infos: 241app_categories = app_info.get_categories() 242if app_categories == None: 243continue 244app_name = app_info.get_display_name() 245command = app_info.get_executable() 246app_button = MenuItemButton(app_name, app_info.get_description(), command) 247app_button.command = command 248app_button.set_tooltip_text(app_name) 249app_label = Gtk.Label(label=app_name) 250app_label.set_ellipsize(Pango.EllipsizeMode.END) 251app_label.set_max_width_chars(7) 252app_button_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) 253app_button_box.append(app_label) 254app_button.set_child(app_button_box) 255app_button.set_has_frame(False) 256 257image = Gtk.Image() 258image.set_icon_size(Gtk.IconSize.LARGE) 259icon = app_info.get_icon() 260if icon: 261icon_name = icon.to_string() 262if not Gtk.IconTheme.get_for_display(Gdk.Display.get_default()).has_icon(icon_name): 263continue 264if icon_name[0] == '/': 265image.set_from_file(icon_name) 266else: 267image.set_from_icon_name(icon_name) 268else: 269if not Gtk.IconTheme.get_for_display(Gdk.Display.get_default()).has_icon(app_name.lower()): 270continue 271image.set_from_icon_name(app_name.lower()) 272 273self.flowbox.append(app_button) 274app_button.connect("clicked", self.app_button_clicked) 275app_button_box.prepend(image) 276self.app_buttons.append(app_button) 277