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