-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpybye.py
More file actions
executable file
·347 lines (299 loc) · 12.2 KB
/
Copy pathpybye.py
File metadata and controls
executable file
·347 lines (299 loc) · 12.2 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/bin/python3
from subprocess import check_output, CalledProcessError
from os import path, mkdir
from configparser import ConfigParser
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk
PATH_TO_CONFIG = path.expanduser("~") + "/.config/PyBye/"
PATH_TO_CSS = path.dirname(path.abspath(__file__)) + "/gtk_style.css"
config = ConfigParser()
# Configuration to write to the configfile.
config.add_section("Commands")
config.set("Commands", "button_one_command", "shutdown now")
config.set("Commands", "button_two_command", "reboot")
config.set("Commands", "button_three_command", "systemctl suspend")
config.set("Commands", "button_four_command", "dm-tool lock")
config.set("Commands", "button_five_command", "gnome-session-quit --force")
config.set("Commands", "button_six_command", "systemctl hibernate")
config.add_section("Icons")
config.set("Icons", "button_one_icon", "system-shutdown-symbolic")
config.set("Icons", "button_two_icon", "system-reboot-symbolic")
config.set("Icons", "button_three_icon", "system-suspend-symbolic")
config.set("Icons", "button_four_icon", "system-lock-screen-symbolic")
config.set("Icons", "button_five_icon", "system-log-out-symbolic")
config.set("Icons", "button_six_icon", "system-hibernate-symbolic")
config.add_section("Size")
config.set("Size", "Width", "1920")
config.set("Size", "Height", "1080")
config.set("Size", "border_width", "480")
config.set("Size", "icon_size", "6")
config.set("Size", "dialog_height", "120")
config.set("Size", "dialog_width", "150")
config.add_section("Options")
config.set("Options", "ask_for_confirmation", "True")
config.set("Options", "space_between_buttons_and_text", "20")
config.set("Options", "space_between_buttons", "20")
config.set("Options", "fullscreen_mode", "True")
config.add_section("Text")
config.set("Text", "button_one", "Shutdown")
config.set("Text", "button_two", "Reboot")
config.set("Text", "button_three", "Suspend")
config.set("Text", "button_four", "Lock screen")
config.set("Text", "button_five", "Log-Out")
config.set("Text", "button_six", "Hibernate")
config.add_section("Colors")
config.set("Colors", "background_color", "rgba(40,40,40, 0.6)")
config.set("Colors", "button_background", "rgba(40,40,40, 0.1)")
config.set("Colors", "button_hover", "rgba(235, 219, 178, 0.461)")
config.set("Colors", "button_shadow", "antiquewhite")
config.set("Colors", "button_activate", "antiquewhite")
config.set("Colors", "text_color", "#ebdbb2")
config.set("Colors", "text_shadow", "#3c3836")
# Writes configuration to the file if the file does not exist yet.
# Also creates a folder if it doesn't exist yet.
if not path.exists(PATH_TO_CONFIG + "pybye.conf"):
if not path.exists(PATH_TO_CONFIG):
mkdir(PATH_TO_CONFIG)
with open(PATH_TO_CONFIG + "pybye.conf", "w") as conf:
config.write(conf)
config.read(PATH_TO_CONFIG + "pybye.conf")
# Variables for icons.
button_one_icon = config["Icons"]["button_one_icon"]
button_two_icon = config["Icons"]["button_two_icon"]
button_three_icon = config["Icons"]["button_three_icon"]
button_four_icon = config["Icons"]["button_four_icon"]
button_five_icon = config["Icons"]["button_five_icon"]
button_six_icon = config["Icons"]["button_six_icon"]
# Variables for commands.
button_one_command = config["Commands"]["button_one_command"]
button_two_command = config["Commands"]["button_two_command"]
button_three_command = config["Commands"]["button_three_command"]
button_four_command = config["Commands"]["button_four_command"]
button_five_command = config["Commands"]["button_five_command"]
button_six_command = config["Commands"]["button_six_command"]
# Variables for size.
width = int(config["Size"]["Width"])
height = int(config["Size"]["Height"])
border_width = int(config["Size"]["Border_width"])
icon_size = int(config["Size"]["icon_size"])
dialog_width = int(config["Size"]["dialog_width"])
dialog_height = int(config["Size"]["dialog_height"])
# Variables for options.
confirmation = config["Options"]["ask_for_confirmation"].capitalize()
row_spacing = int(config["Options"]["space_between_buttons_and_text"])
column_spacing = int(config["Options"]["space_between_buttons"])
fullscreen_mode = config["Options"]["fullscreen_mode"].capitalize()
# Variables for text.
button_one = config["Text"]["button_one"]
button_two = config["Text"]["button_two"]
button_three = config["Text"]["button_three"]
button_four = config["Text"]["button_four"]
button_five = config["Text"]["button_five"]
button_six = config["Text"]["button_six"]
# Variables for colors.
background_color = config["Colors"]["background_color"]
button_background = config["Colors"]["button_background"]
button_hover = config["Colors"]["button_hover"]
button_shadow = config["Colors"]["button_shadow"]
button_activate = config["Colors"]["button_activate"]
text_color = config["Colors"]["text_color"]
text_shadow = config["Colors"]["text_shadow"]
lines_list = [
"@define-color bg-color ",
"@define-color button-bg-color ",
"@define-color bg-hover ",
"@define-color box-shadow ",
"@define-color button-active-bg-color ",
"@define-color text-color ",
"@define-color text-shadow ",
]
vars_list = [
background_color,
button_background,
button_hover,
button_shadow,
button_activate,
text_color,
text_shadow,
]
def save_colors(n):
""""Rewrites the variables inside gtk_style.css
to change colors if the values are different."""
with open(PATH_TO_CSS, "r") as css_file:
if n == 7:
return
css_file.seek(0)
lines = css_file.readlines()
if lines[n] == lines_list[n] + vars_list[n] + ";\n":
save_colors(n+1)
else:
lines[n] = lines_list[n] + vars_list[n] + ";\n"
with open(PATH_TO_CSS, "w") as file:
file.writelines(lines)
save_colors(n+1)
save_colors(0)
def switch_to_watch_cursor():
"""Changes cursor to 'loading' state."""
watch_cursor = Gdk.Cursor(Gdk.CursorType.WATCH)
win.get_window().set_cursor(watch_cursor)
def switch_to_arrow_cursor():
"""Changes cursor back to normal."""
arrow_cursor = Gdk.Cursor(Gdk.CursorType.ARROW)
win.get_window().set_cursor(arrow_cursor)
def run_command(shell_command):
"""Runs a command and displays an error if there is one."""
try:
check_output((shell_command), shell=True)
except CalledProcessError as error:
dialog = Gtk.MessageDialog(
transient_for=MainWindow(),
flags=0,
message_type=Gtk.MessageType.ERROR,
buttons=Gtk.ButtonsType.CLOSE,
text="There's been an error while running the command.",
)
dialog.format_secondary_text(
str(error)
)
response = dialog.run()
dialog.destroy()
if response == Gtk.ResponseType.CLOSE:
switch_to_arrow_cursor()
else:
switch_to_arrow_cursor()
Gtk.main_quit()
def run_dialog(button_name, button_command):
"""Shows the dialog window for the clicked button.
Takes two args: button_name and button command."""
global status
status = button_name
dialog = ConfirmAction(MainWindow())
response = dialog.run()
if response == Gtk.ResponseType.YES:
run_command(button_command)
switch_to_arrow_cursor()
dialog.hide()
class ConfirmAction(Gtk.Dialog):
def __init__(self, parent):
super().__init__(title=status,
transient_for=parent,
modal=True,
name="dialog"
)
self.set_default_size(dialog_width, dialog_height)
self.set_resizable(False)
self.connect("key-press-event", self.on_escape_pressed)
self.add_button(Gtk.STOCK_NO, Gtk.ResponseType.CANCEL)
self.add_button(Gtk.STOCK_YES, Gtk.ResponseType.YES)
label = Gtk.Label(label=f"\n\nDo you want to {status.lower()}?")
box = self.get_content_area()
box.add(label)
self.show_all()
def on_escape_pressed(self, widget, event):
pressed_key = Gdk.keyval_name(event.keyval)
if pressed_key == "Escape":
switch_to_arrow_cursor()
self.hide()
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self,
title="PyBye",
name="main_window"
)
Gtk.Window.set_default_size(self, width, height)
self.set_border_width(border_width)
self.set_decorated(False)
self.connect("key-press-event", self.on_key_pressed)
# Get CSS style for GTK+3 from a file.
style_provider = Gtk.CssProvider()
style_provider.load_from_path(PATH_TO_CSS)
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(),
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
buttons = {
1: [button_one, button_one_icon, self.on_button1_clicked],
2: [button_two, button_two_icon, self.on_button2_clicked],
3: [button_three, button_three_icon, self.on_button3_clicked],
4: [button_four, button_four_icon, self.on_button4_clicked],
5: [button_five, button_five_icon, self.on_button5_clicked],
6: [button_six, button_six_icon, self.on_button6_clicked],
}
grid = Gtk.Grid()
def buttons_and_labels(n):
"""Creates 5 buttons and 5 labels under them
with specified text, icons,
icon size and corresponding commands."""
label = Gtk.Label(label=buttons[n][0])
label.set_selectable(False)
button = Gtk.Button.new_from_icon_name(icon_name=buttons[n][1],
size=icon_size
)
button.connect("clicked", buttons[n][2])
grid.add(button)
grid.attach_next_to(label, button,
Gtk.PositionType.BOTTOM,
1, 1)
if n == 6:
return
buttons_and_labels(n+1)
buttons_and_labels(1)
grid.set_row_spacing(row_spacing)
grid.set_row_homogeneous(False)
grid.set_column_homogeneous(True)
grid.set_column_spacing(column_spacing)
self.add(grid)
# Button functions.
def on_button1_clicked(self, widget):
switch_to_watch_cursor()
if confirmation == "True":
run_dialog(button_one, button_one_command)
elif confirmation == "False":
run_command(button_one_command)
def on_button2_clicked(self, widget):
switch_to_watch_cursor()
if confirmation == "True":
run_dialog(button_two, button_two_command)
elif confirmation == "False":
run_command(button_two_command)
def on_button3_clicked(self, widget):
switch_to_watch_cursor()
if confirmation == "True":
run_dialog(button_three, button_three_command)
elif confirmation == "False":
run_command(button_three_command)
def on_button4_clicked(self, widget):
switch_to_watch_cursor()
if confirmation == "True":
run_dialog(button_four, button_four_command)
elif confirmation == "False":
run_command(button_four_command)
def on_button5_clicked(self, widget):
switch_to_watch_cursor()
if confirmation == "True":
run_dialog(button_five, button_five_command)
elif confirmation == "False":
run_command(button_five_command)
def on_button6_clicked(self, widget):
switch_to_watch_cursor()
if confirmation == "True":
run_dialog(button_six, button_six_command)
elif confirmation == "False":
run_command(button_six_command)
# Key press function.
def on_key_pressed(self, widget, event):
pressed_key = Gdk.keyval_name(event.keyval)
if pressed_key == "Escape":
Gtk.main_quit()
if __name__ == "__main__":
win = MainWindow()
screen = win.get_screen()
visual = screen.get_rgba_visual()
win.set_visual(visual)
win.connect("destroy", Gtk.main_quit)
win.show_all()
if fullscreen_mode == "True":
win.fullscreen()
Gtk.main()