-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick-power.py
More file actions
146 lines (120 loc) · 4.98 KB
/
Copy pathquick-power.py
File metadata and controls
146 lines (120 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GLib
import subprocess
import sys
class QuickPower(Gtk.Window):
def __init__(self):
super().__init__(type=Gtk.WindowType.TOPLEVEL)
self.set_decorated(False)
self.set_keep_above(True)
self.set_type_hint(Gdk.WindowTypeHint.POPUP_MENU)
self.set_skip_taskbar_hint(True)
self.set_skip_pager_hint(True)
self.set_app_paintable(True)
self.set_position(Gtk.WindowPosition.CENTER)
screen = self.get_screen()
visual = screen.get_rgba_visual()
if visual and screen.is_composited():
self.set_visual(visual)
self.setup_css()
self.connect("key-press-event", self.on_key_press)
self.connect("focus-out-event", self.on_focus_out)
self.connect("map-event", self.on_map)
self.connect("unmap-event", self.on_unmap)
self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
self.connect("button-press-event", self.on_button_press)
main_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=20)
main_box.set_name("power_box")
main_box.set_margin_top(50)
main_box.set_margin_bottom(50)
main_box.set_margin_start(60)
main_box.set_margin_end(60)
lbl_title = Gtk.Label(label="Menú de Energía")
lbl_title.set_markup("<span weight='bold' foreground='#fafafa' size='x-large'>Opciones del Sistema</span>")
main_box.pack_start(lbl_title, False, False, 10)
grid = Gtk.Grid(column_spacing=20, row_spacing=20)
grid.set_halign(Gtk.Align.CENTER)
btn_power = self.make_action_btn("system-shutdown-symbolic", "Apagar", "systemctl poweroff")
btn_restart = self.make_action_btn("view-refresh-symbolic", "Reiniciar", "systemctl reboot")
btn_logout = self.make_action_btn("system-log-out-symbolic", "Cerrar Sesión", "pkill xfce4-session")
btn_user = self.make_action_btn("system-users-symbolic", "Cambiar Perfil", "dm-tool switch-to-greeter")
grid.attach(btn_power, 0, 0, 1, 1)
grid.attach(btn_restart, 1, 0, 1, 1)
grid.attach(btn_logout, 0, 1, 1, 1)
grid.attach(btn_user, 1, 1, 1, 1)
main_box.pack_start(grid, False, False, 0)
self.add(main_box)
self.show_all()
# Grab focus
self.present()
def make_action_btn(self, icon_name, label_text, cmd):
btn = Gtk.Button()
btn.set_name("power_btn")
btn.set_can_focus(False)
btn.set_size_request(140, 140) # 1:1 aspect ratio
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
vbox.set_valign(Gtk.Align.CENTER)
icon = Gtk.Image.new_from_icon_name(icon_name, Gtk.IconSize.DIALOG)
icon.set_pixel_size(48)
lbl = Gtk.Label(label=label_text)
vbox.pack_start(icon, False, False, 0)
vbox.pack_start(lbl, False, False, 0)
btn.add(vbox)
btn.connect("clicked", lambda w: self.run_command(cmd))
return btn
def run_command(self, cmd):
subprocess.Popen(cmd, shell=True)
Gtk.main_quit()
def on_focus_out(self, widget, event):
Gtk.main_quit()
return False
def on_key_press(self, widget, event):
if event.keyval == Gdk.KEY_Escape:
Gtk.main_quit()
return False
def setup_css(self):
css = b"""
window { background-color: transparent; }
#power_box {
background-color: #18181b;
border-radius: 24px;
border: 1px solid rgba(255, 255, 255, 0.1);
padding: 30px;
}
#power_btn {
background-color: #27272a;
border-radius: 16px;
border: 1px solid transparent;
color: #fafafa;
}
#power_btn:hover {
background-color: rgba(255, 255, 255, 0.15);
border: 1px solid rgba(255, 255, 255, 0.3);
transition: all 0.2s ease;
}
"""
provider = Gtk.CssProvider()
provider.load_from_data(css)
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(),
provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
def on_map(self, widget, event):
seat = Gdk.Display.get_default().get_default_seat()
seat.grab(self.get_window(), Gdk.SeatCapabilities.ALL_POINTING, True, None, None, None)
return False
def on_unmap(self, widget, event):
seat = Gdk.Display.get_default().get_default_seat()
seat.ungrab()
return False
def on_button_press(self, widget, event):
width, height = self.get_size()
if event.x < 0 or event.x > width or event.y < 0 or event.y > height:
Gtk.main_quit()
return True
return False
if __name__ == '__main__':
win = QuickPower()
Gtk.main()