-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
242 lines (197 loc) · 10.3 KB
/
main.py
File metadata and controls
242 lines (197 loc) · 10.3 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
import ctypes
import subprocess
import sys
from PyQt6.QtCore import Qt, QTime
from PyQt6.QtGui import QColor, QIcon, QKeySequence, QPalette, QShortcut
from PyQt6.QtWidgets import (QApplication, QButtonGroup, QDoubleSpinBox,
QHBoxLayout, QLabel, QMainWindow, QMessageBox,
QPushButton, QRadioButton, QTimeEdit, QVBoxLayout,
QWidget)
if sys.platform == 'win32':
# Hide console window
kernel32 = ctypes.WinDLL('kernel32')
user32 = ctypes.WinDLL('user32')
SW_HIDE = 0
hWnd = kernel32.GetConsoleWindow()
if hWnd:
user32.ShowWindow(hWnd, SW_HIDE)
class LifeControlButtonApp(QMainWindow):
def __init__(self):
super().__init__()
# Set application icon
self.setWindowIcon(QIcon('icon.png'))
self.setFixedSize(500, 400)
# Set the theme based on Atom One Dark colours
self.set_theme()
# Initialize main UI
self.init_ui()
# Add Enter key shortcut
self.shortcut = QShortcut(QKeySequence(Qt.Key.Key_Return), self)
self.shortcut.activated.connect(self.execute_shutdown)
# Add Enter key shortcut for numpad Enter as well
self.shortcut_numpad = QShortcut(QKeySequence(Qt.Key.Key_Enter), self)
self.shortcut_numpad.activated.connect(self.execute_shutdown)
def set_theme(self):
self.setWindowTitle("Life Control Button")
palette = QPalette()
palette.setColor(QPalette.ColorRole.Window, QColor("#282c34"))
palette.setColor(QPalette.ColorRole.WindowText, QColor("#abb2bf"))
palette.setColor(QPalette.ColorRole.Base, QColor("#21252b"))
palette.setColor(QPalette.ColorRole.AlternateBase, QColor("#282c34"))
palette.setColor(QPalette.ColorRole.ToolTipBase, QColor("#abb2bf"))
palette.setColor(QPalette.ColorRole.ToolTipText, QColor("#abb2bf"))
palette.setColor(QPalette.ColorRole.Text, QColor("#abb2bf"))
palette.setColor(QPalette.ColorRole.Highlight, QColor("#222e4d"))
palette.setColor(QPalette.ColorRole.HighlightedText, QColor("#abb2bf"))
self.setPalette(palette)
# Regular Windows title bar
self.setWindowFlags(Qt.WindowType.Window)
def init_ui(self):
central_widget = QWidget()
layout = QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
# Title
title_label = QLabel("Liberation, not limitation")
title_label.setStyleSheet("padding: 0; color: #abb2bf; font-family: ubuntu; font-size: 32px; margin: 30px 0 20px 0;")
title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(title_label)
# Shutdown mode selection
mode_layout = QVBoxLayout()
self.radio_at_time = QRadioButton("Turn PC off at a specific time")
self.radio_at_time.setStyleSheet(
"QRadioButton {padding: 0; margin: 5px 0px 5px 112px; color: #abb2bf; font-family: ubuntu; font-size: 14px; spacing: 10px;} "
"QRadioButton::indicator {width: 14px; height: 14px; border: 1px solid #abb2bf; background: #21252b; } "
"QRadioButton::indicator:checked { background-color: #abb2bf; } "
"QRadioButton:hover {color: #dcdfe4;}"
"QRadioButton:focus {outline: none;}"
)
self.radio_after_time = QRadioButton("Turn PC off after a specific duration")
self.radio_after_time.setStyleSheet(
"QRadioButton { padding: 0; margin: 5px 0px 5px 112px; color: #abb2bf; font-family: ubuntu; font-size: 14px; spacing: 10px;} "
"QRadioButton::indicator {padding: 0; width: 14px; height: 14px; border: 1px solid #abb2bf; background: #21252b; } "
"QRadioButton::indicator:checked {padding: 0; background-color: #abb2bf; } "
"QRadioButton:hover {color: #dcdfe4;}"
"QRadioButton:focus {outline: none;}"
)
self.radio_at_time.setChecked(True)
self.mode_group = QButtonGroup()
self.mode_group.addButton(self.radio_at_time)
self.mode_group.addButton(self.radio_after_time)
self.radio_at_time.toggled.connect(self.update_input_visibility)
mode_layout.addWidget(self.radio_at_time)
mode_layout.addWidget(self.radio_after_time)
layout.addLayout(mode_layout)
# Set Time option
set_time_layout = QHBoxLayout()
set_time_label = QLabel("Turn PC off at:")
set_time_label.setStyleSheet("margin: 0px 0px 0px 100px; color: #abb2bf; font-family: ubuntu; font-size: 14px;")
self.time_edit = QTimeEdit()
self.time_edit.setAlignment(Qt.AlignmentFlag.AlignCenter) # Center text
self.time_edit.setStyleSheet(
"border: none; margin: 0px 157px 0 4px; background-color: #21252b; color: #abb2bf; font-family: ubuntu; font-size: 14px; selection-background-color: #6d798f;"
)
self.time_edit.setDisplayFormat("HH:mm")
self.time_edit.setButtonSymbols(QDoubleSpinBox.ButtonSymbols.NoButtons)
current_time = QTime.currentTime()
hours = (current_time.hour() + 2) % 24 # Add 2 hours and wrap around at 24
self.time_edit.setTime(QTime(hours, 0))
set_time_layout.addWidget(set_time_label)
set_time_layout.addWidget(self.time_edit)
self.set_time_widget = QWidget()
self.set_time_widget.setLayout(set_time_layout)
layout.addWidget(self.set_time_widget)
# Turn off after n time option
after_time_layout = QHBoxLayout()
after_time_label = QLabel("Turn PC off after:")
after_time_label.setStyleSheet("margin: 0 0 0 100px; color: #abb2bf; font-family: ubuntu; font-size: 14px;")
self.time_value_spinbox = QDoubleSpinBox()
self.time_value_spinbox.setAlignment(Qt.AlignmentFlag.AlignCenter) # Center text
self.time_value_spinbox.setStyleSheet(
"border: none; margin: 0 25px 0 25px; background-color: #21252b; color: #abb2bf; font-family: ubuntu; font-size: 14px; selection-background-color: #6d798f;"
)
self.time_value_spinbox.setRange(0.1, 24) # Max 24 hours, minimum 0.1
self.time_value_spinbox.setDecimals(2)
self.time_value_spinbox.setValue(1) # Default value 1
self.time_value_spinbox.setButtonSymbols(QDoubleSpinBox.ButtonSymbols.NoButtons)
self.time_unit_label = QLabel("hours")
self.time_unit_label.setStyleSheet("font-family: ubuntu; font-size: 14px; color: #abb2bf;")
after_time_layout.addWidget(after_time_label)
after_time_layout.addWidget(self.time_value_spinbox)
after_time_layout.addWidget(self.time_unit_label)
self.after_time_widget = QWidget()
self.after_time_widget.setLayout(after_time_layout)
layout.addWidget(self.after_time_widget)
# Initially hide "after time" option
self.after_time_widget.hide()
# "Get Life Control" Button
self.control_button = QPushButton("Get Life Control") # Make it an instance variable
self.control_button.setStyleSheet(
"QPushButton {margin: auto; width: 100px; height: 30; background-color: #21252b; color: #abb2bf; font-family: ubuntu; font-size: 20px; padding: 10px; border: none;}"
"QPushButton:hover {background-color: #3b4252; color: #ffffff;}"
"QPushButton:focus {background-color: #333946; color: #ffffff; outline: none;}"
)
self.control_button.clicked.connect(self.execute_shutdown)
self.control_button.setDefault(True) # Make it the default button
layout.addWidget(self.control_button)
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
def update_input_visibility(self):
if self.radio_at_time.isChecked():
self.set_time_widget.show()
self.after_time_widget.hide()
else:
self.set_time_widget.hide()
self.after_time_widget.show()
def execute_shutdown_command(self, seconds):
"""Execute shutdown command and return True if successful"""
try:
result = subprocess.run(['powershell.exe', 'shutdown', '/s', '/t', str(seconds)],
creationflags=subprocess.CREATE_NO_WINDOW,
shell=True,
capture_output=True)
return result.returncode == 0
except Exception as e:
QMessageBox.critical(self, "Error", f"Failed to schedule shutdown: {str(e)}")
return False
def execute_shutdown(self):
if self.radio_at_time.isChecked():
self.set_shutdown_time()
elif self.radio_after_time.isChecked():
self.set_shutdown_after()
def set_shutdown_time(self):
target_time = self.time_edit.time()
now = QTime.currentTime()
seconds_until_shutdown = now.secsTo(target_time)
if seconds_until_shutdown <= 0:
seconds_until_shutdown += 86400 # Adjust for next day
if self.execute_shutdown_command(seconds_until_shutdown):
self.close() # Close the app if shutdown was scheduled successfully
def set_shutdown_after(self):
time_value = self.time_value_spinbox.value()
seconds = int(time_value * 3600)
if self.execute_shutdown_command(seconds):
self.close() # Close the app if shutdown was scheduled successfully
def center_window_on_primary_monitor(self):
# Get the primary screen (focused monitor)
screen = QApplication.primaryScreen()
if not screen:
return # Safety check in case no primary screen is available
# Get the available geometry of the primary screen
screen_geometry = screen.availableGeometry()
# Calculate the center of the screen
screen_center = screen_geometry.center()
# Adjust position relative to the window's frame geometry
frame_geometry = self.frameGeometry()
frame_geometry.moveCenter(screen_center)
# Move the window to the calculated position
self.move(frame_geometry.topLeft())
if __name__ == "__main__":
app = QApplication(sys.argv)
# Set application-wide icon
app.setWindowIcon(QIcon('icon.png'))
main_window = LifeControlButtonApp()
main_window.resize(500, 400)
main_window.center_window_on_primary_monitor()
main_window.show()
sys.exit(app.exec())