-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaskbarDate.py
More file actions
178 lines (152 loc) · 5.31 KB
/
taskbarDate.py
File metadata and controls
178 lines (152 loc) · 5.31 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
from PySide6.QtWidgets import (
QApplication, QLabel, QWidget, QMenu, QInputDialog, QDialog, QSpinBox,
QPushButton, QHBoxLayout, QVBoxLayout, QGridLayout
)
from PySide6.QtCore import Qt, QTimer, QDate, QPoint
from PySide6.QtGui import QPainter, QColor, QFont
import os
import sys
import keyboard
import threading
import time
def get_app_dir():
if getattr(sys, 'frozen', False):
# Running as PyInstaller EXE
return os.path.dirname(sys.executable)
else:
# Running as a script
return os.path.dirname(os.path.abspath(__file__))
SETTINGS_FILE = "taskbarDate-settings.txt"
WINDOW_WIDTH = 80
WINDOW_HEIGHT = 30
def load_settings(screen_width, screen_height):
settings = {
"x": screen_width - WINDOW_WIDTH - 20,
"y": screen_height - WINDOW_HEIGHT,
}
if os.path.exists(os.path.join(get_app_dir(),SETTINGS_FILE)):
try:
with open(os.path.join(get_app_dir(),SETTINGS_FILE), "r") as f:
for line in f:
key, value = line.strip().split("=")
if key in ("x", "y"):
settings[key] = int(value)
except Exception:
pass
return settings
def save_settings(settings):
with open(os.path.join(get_app_dir(),SETTINGS_FILE), "w") as f:
f.write(f"x={settings['x']}\n")
f.write(f"y={settings['y']}\n")
class PositionDialog(QDialog):
def __init__(self, parent, x, y, max_x, max_y):
super().__init__(parent)
self.setWindowTitle("Set Window Position")
self.setModal(True)
self.setFixedSize(220, 120)
self.dragging = False
self.offset = QPoint()
self.max_x = max_x
self.max_y = max_y
self.x_spin = QSpinBox(self)
self.x_spin.setRange(0, max_x)
self.x_spin.setValue(x)
self.y_spin = QSpinBox(self)
self.y_spin.setRange(0, max_y)
self.y_spin.setValue(y)
self.ok_btn = QPushButton("OK", self)
self.cancel_btn = QPushButton("Cancel", self)
layout = QGridLayout()
layout.addWidget(QLabel("X:"), 0, 0)
layout.addWidget(self.x_spin, 0, 1)
layout.addWidget(QLabel("Y:"), 1, 0)
layout.addWidget(self.y_spin, 1, 1)
btn_layout = QHBoxLayout()
btn_layout.addWidget(self.ok_btn)
btn_layout.addWidget(self.cancel_btn)
layout.addLayout(btn_layout, 2, 0, 1, 2)
self.setLayout(layout)
self.x_spin.valueChanged.connect(self.preview_move)
self.y_spin.valueChanged.connect(self.preview_move)
self.ok_btn.clicked.connect(self.accept)
self.cancel_btn.clicked.connect(self.reject)
def preview_move(self):
x = self.x_spin.value()
y = self.y_spin.value()
self.parent().move(x, y)
class TransparentWindow(QWidget):
def __init__(self):
super().__init__()
screen = QApplication.primaryScreen().geometry()
self.screen_width = screen.width()
self.screen_height = screen.height()
self.settings = load_settings(self.screen_width, self.screen_height)
self.setGeometry(self.settings["x"], self.settings["y"], WINDOW_WIDTH, WINDOW_HEIGHT)
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Tool)
self.setAttribute(Qt.WA_TranslucentBackground, True)
self.setAttribute(Qt.WA_ShowWithoutActivating, True)
self.label = QLabel(self)
self.label.setFont(QFont("DejaVu Sans Mono", 9))
self.label.setStyleSheet("color: white; background: transparent;")
self.label.setAlignment(Qt.AlignCenter)
self.label.setGeometry(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)
self.update_label()
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_label)
self.timer.start(60000) # TODO: set back to 60000 for minute updates
#self.top_timer = QTimer(self)
#self.top_timer.timeout.connect(self.bring_to_front)
#self.top_timer.start(10000) # bring back to front timer
def bring_to_front(self):
self.setAttribute(Qt.WA_ShowWithoutActivating, True)
self.raise_()
self.show()
def update_label(self):
today = QDate.currentDate()
self.label.setText(today.toString("dd.MM.yyyy"))
def paintEvent(self, event):
painter = QPainter(self)
painter.fillRect(self.rect(), QColor(0, 0, 0, 1)) # QColor(0, 0, 0, 60) to see the box
def contextMenuEvent(self, event):
menu = QMenu(self)
menu.addAction("Set Position", self.set_position_dialog)
menu.addSeparator()
menu.addAction("Exit", QApplication.quit)
menu.exec_(event.globalPos())
def set_position_dialog(self):
max_x = self.screen_width - WINDOW_WIDTH
max_y = self.screen_height - WINDOW_HEIGHT
dlg = PositionDialog(self, self.x(), self.y(), max_x, max_y)
old_x, old_y = self.x(), self.y()
if dlg.exec_():
x = dlg.x_spin.value()
y = dlg.y_spin.value()
self.move(x, y)
self.settings["x"] = x
self.settings["y"] = y
save_settings(self.settings)
else:
# Restore old position if cancelled
self.move(old_x, old_y)
def moveEvent(self, event):
# Clamp position within screen bounds
x = min(max(0, self.x()), self.screen_width - WINDOW_WIDTH)
y = min(max(0, self.y()), self.screen_height - WINDOW_HEIGHT)
if (x, y) != (self.x(), self.y()):
self.move(x, y)
self.settings["x"] = x
self.settings["y"] = y
save_settings(self.settings)
if __name__ == "__main__":
#app = QApplication(sys.argv)
#window = TransparentWindow()
#window.show()
#sys.exit(app.exec_())
app = QApplication(sys.argv)
window = TransparentWindow()
window.show()
def bring_to_front_on_hotkey():
keyboard.add_hotkey('windows', lambda: (time.sleep(.2), window.raise_(), window.show()))
keyboard.wait() # Blocks forever, but in a thread
threading.Thread(target=bring_to_front_on_hotkey, daemon=True).start()
sys.exit(app.exec_())