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 • 10.33 kiB
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
gi.require_version("Gtk4LayerShell", "1.0")
33
34
from gi.repository import Gtk, GLib, Gtk4LayerShell, Gio, Gdk, Pango
35
import subprocess
36
37
38
LOGOUT_BUTTON_SIZE = 125
39
LOGOUT_BUTTON_MARGIN = 10
40
41
def create_logout_ui_button(icon_name, label_text):
42
layout = Gtk.Box()
43
image = Gtk.Image()
44
label = Gtk.Label()
45
button = Gtk.Button()
46
button.set_size_request(LOGOUT_BUTTON_SIZE, LOGOUT_BUTTON_SIZE)
47
image.set_from_icon_name(icon_name)
48
label.set_text(label_text)
49
layout.set_orientation(Gtk.Orientation.VERTICAL)
50
layout.set_halign(Gtk.Align.CENTER)
51
layout.append(image)
52
image.set_icon_size(Gtk.IconSize.LARGE)
53
image.set_vexpand(True)
54
layout.append(label)
55
button.set_child(layout)
56
return button
57
58
class WayfireLogoutUI(Gtk.Window):
59
60
def __init__(self):
61
super().__init__()
62
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
# Make surfaces layer shell
98
Gtk4LayerShell.init_for_window(self)
99
Gtk4LayerShell.set_namespace(self, "com.roundabout_host.panorama.logout")
100
Gtk4LayerShell.set_layer(self, Gtk4LayerShell.Layer.OVERLAY)
101
102
Gtk4LayerShell.set_anchor(self, Gtk4LayerShell.Edge.TOP, True)
103
Gtk4LayerShell.set_anchor(self, Gtk4LayerShell.Edge.BOTTOM, True)
104
Gtk4LayerShell.set_anchor(self, Gtk4LayerShell.Edge.LEFT, True)
105
Gtk4LayerShell.set_anchor(self, Gtk4LayerShell.Edge.RIGHT, True)
106
main_layout.set_valign(Gtk.Align.CENTER)
107
hbox.set_center_widget(main_layout)
108
hbox.set_hexpand(True)
109
hbox.set_vexpand(True)
110
self.set_child(hbox)
111
self.get_style_context().add_class("logout")
112
display = self.get_display()
113
css_provider = Gtk.CssProvider()
114
css_provider.load_from_data("window.logout { background-color: rgba(0, 0, 0, 0.5); }")
115
Gtk.StyleContext.add_provider_for_display(display, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)
116
117
def on_suspend_click(self, button):
118
GLib.spawn_command_line_async("systemctl suspend")
119
120
def on_hibernate_click(self, button):
121
GLib.spawn_command_line_async("systemctl hibernate")
122
123
def on_switchuser_click(self, button):
124
GLib.spawn_command_line_async("dm-tool switch-to-greeter")
125
126
def on_logout_click(self, button):
127
GLib.spawn_command_line_async("wayland-logout")
128
129
def on_reboot_click(self, button):
130
GLib.spawn_command_line_async("systemctl reboot")
131
132
def on_shutdown_click(self, button):
133
GLib.spawn_command_line_async("systemctl poweroff")
134
135
def on_cancel_click(self, button):
136
self.hide()
137
138
class MenuItemButton(Gtk.Button):
139
140
def __init__(self, name, desc, exe):
141
super().__init__()
142
self.name = name
143
self.desc = desc
144
self.exe = exe
145
146
class SoreausMenu(panorama_panel.Applet):
147
name = "Soreau's menu"
148
description = "Flowbox app menu"
149
150
def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None):
151
super().__init__()
152
153
self.button = Gtk.MenuButton()
154
155
self.menu_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
156
image = Gtk.Image.new_from_icon_name("wayfire")
157
image.set_icon_size(Gtk.IconSize.LARGE)
158
self.append(self.button)
159
self.button.set_child(image)
160
self.popover = Gtk.Popover()
161
self.popover.set_parent(self)
162
self.flowbox = Gtk.FlowBox()
163
self.flowbox.set_valign(Gtk.Align.START);
164
self.flowbox.set_homogeneous(True);
165
self.flowbox_item_focus_signal = self.flowbox.connect("selected-children-changed", self.on_flowbox_item_focus)
166
self.app_buttons = []
167
self.populate_menu_entries()
168
self.scrolled_window = Gtk.ScrolledWindow()
169
self.scrolled_window.set_size_request(-1, 350)
170
self.scrolled_window.set_child(self.flowbox)
171
self.logout_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
172
self.logout_button = Gtk.Button()
173
self.logout_button.set_margin_top(5)
174
self.logout_button.set_margin_bottom(5)
175
self.logout_button.set_margin_start(5)
176
self.logout_button.set_margin_end(5)
177
self.logout_button.set_child(Gtk.Image.new_from_icon_name("system-shutdown"))
178
self.logout_button.connect("clicked", self.on_logout_button_clicked)
179
self.logout_box.set_halign(Gtk.Align.END)
180
self.logout_box.append(self.logout_button)
181
self.search_entry = Gtk.SearchEntry()
182
self.search_entry.connect("search-changed", self.on_search_changed)
183
self.menu_box.append(self.search_entry)
184
self.menu_box.append(self.scrolled_window)
185
self.menu_box.append(self.logout_box)
186
self.popover.set_child(self.menu_box)
187
self.popover.set_size_request(300, 400)
188
self.popover.connect("show", self.on_popover_popup)
189
self.button.set_popover(self.popover)
190
self.logout_ui = WayfireLogoutUI()
191
192
def on_search_changed(self, search_entry):
193
text = search_entry.get_text().lower()
194
self.flowbox.remove_all()
195
for button in self.app_buttons:
196
if (button.name and text in button.name.lower()) or \
197
(button.desc and text in button.desc.lower()) or \
198
(button.exe and text in button.exe.lower()):
199
self.flowbox.append(button)
200
201
def on_logout_button_clicked(self, button):
202
self.logout_ui.present()
203
self.popover.popdown()
204
205
def on_flowbox_item_focus(self, flowbox):
206
selected_children = flowbox.get_selected_children()
207
if len(selected_children) >= 1:
208
selected_children[0].get_child().grab_focus()
209
210
def on_popover_popup(self, parent):
211
for child in self.flowbox.get_selected_children():
212
self.flowbox.unselect_child(child)
213
self.search_entry.set_text("")
214
self.popover.popup()
215
216
def app_button_clicked(self, app_button):
217
subprocess.Popen(app_button.command, start_new_session=True)
218
self.popover.popdown()
219
220
def populate_menu_entries(self):
221
app_infos = Gio.AppInfo.get_all() # Get all registered applications
222
app_infos = sorted(app_infos, key=lambda obj: obj.get_display_name().lower())
223
224
for app_info in app_infos:
225
app_categories = app_info.get_categories()
226
if app_categories == None:
227
continue
228
app_name = app_info.get_display_name()
229
command = app_info.get_executable()
230
app_button = MenuItemButton(app_name, app_info.get_description(), command)
231
app_button.command = command
232
app_button.set_tooltip_text(app_name)
233
app_label = Gtk.Label(label=app_name)
234
app_label.set_ellipsize(Pango.EllipsizeMode.END)
235
app_label.set_max_width_chars(7)
236
app_button_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
237
app_button_box.append(app_label)
238
app_button.set_child(app_button_box)
239
240
image = Gtk.Image()
241
image.set_icon_size(Gtk.IconSize.LARGE)
242
icon = app_info.get_icon()
243
if icon:
244
icon_name = icon.to_string()
245
if not Gtk.IconTheme.get_for_display(Gdk.Display.get_default()).has_icon(icon_name):
246
continue
247
if icon_name[0] == '/':
248
image.set_from_file(icon_name)
249
else:
250
image.set_from_icon_name(icon_name)
251
else:
252
if not Gtk.IconTheme.get_for_display(Gdk.Display.get_default()).has_icon(app_name.lower()):
253
continue
254
image.set_from_icon_name(app_name.lower())
255
256
self.flowbox.append(app_button)
257
app_button.connect("clicked", self.app_button_clicked)
258
app_button_box.prepend(image)
259
self.app_buttons.append(app_button)
260