-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBadges.py
More file actions
238 lines (186 loc) · 8.41 KB
/
Badges.py
File metadata and controls
238 lines (186 loc) · 8.41 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
# -*- coding: utf-8 -*-
import threading
import tkinter as tk
from PIL import ImageTk
import win32gui
from utils import resource_path
HEIGHT_OFFSET = 4
class Badges(threading.Thread):
def __init__(self):
super().__init__(daemon=True)
self.root = None
self.container = None
self.photo_refs = []
self._ready = False
self.trans_color = '#abcdef'
self.offset_x = 0
self.offset_y = 0
self.orientation = 'horizontal'
self.current_images = []
self.taskbar_height = 48 - HEIGHT_OFFSET
self.is_fit_mode = False
def run(self):
self.root = tk.Tk()
self.root.title('Badge')
# 背景と透過
self.root.config(bg=self.trans_color)
self.root.wm_attributes('-transparentcolor', self.trans_color)
# 枠なし・最前面・タスクバーアイコン非表示
self.root.overrideredirect(True)
self.root.protocol('WM_DELETE_WINDOW', self.toggle_title)
self.root.attributes('-topmost', True)
self.root.resizable(False, False)
# self.root.attributes('-toolwindow', True)
# マウスイベント
self.root.bind('<Button-1>', self.start_drag)
self.root.bind('<B1-Motion>', self.drag_window)
# キーボードイベント
self.root.bind('f', lambda _: self.toggle_fit())
# アイコン設定
self.root.iconbitmap(resource_path('Assets/sample.ico'))
self.root.withdraw()
self.container = tk.Frame(self.root, bg=self.trans_color)
self.container.pack(padx=0, pady=0)
self._keep_on_top_loop()
self._ready = True
self.root.mainloop()
def toggle_title(self):
is_hidden = self.root.overrideredirect()
self.root.overrideredirect(not is_hidden)
self.root.update()
self.root.after(0, self._clamp_position)
def toggle_orientation(self):
orientation = 'vertical' if self.orientation == 'horizontal' else 'horizontal'
self.update(self.current_images, orientation=orientation)
def update(self, pil_images, orientation=None):
"""表示状態にかかわらず、containerの中身を最新にする"""
if not self._ready:
return
if pil_images is not None:
self.current_images = pil_images
# 指定があれば更新、なければ現在の状態を維持
if orientation:
self.orientation = orientation
# 現在のタスクバーの高さを取得
hwnd = win32gui.FindWindow('Shell_TrayWnd', None)
rect = win32gui.GetWindowRect(hwnd)
self.taskbar_height = rect[3] - rect[1] - HEIGHT_OFFSET
def _do_update():
for widget in self.container.winfo_children():
widget.destroy()
self.photo_refs.clear()
# メインの並び方向
main_side = tk.LEFT if self.orientation == 'horizontal' else tk.TOP
for item in self.current_images:
if not isinstance(item, list):
# --- 単体画像の場合 ---
self._create_image_label(self.container, item, main_side)
else:
# --- リストの場合:横並びのグループ(Frame)を作る ---
group_frame = tk.Frame(self.container, bg=self.trans_color)
# 縦並び(TOP)の時は、横方向(X)に最大まで広げる
if self.orientation == 'vertical':
group_frame.pack(side=main_side, fill=tk.X, padx=0, pady=0)
else:
group_frame.pack(side=main_side, padx=0, pady=0)
# グループ内の画像を配置
for img_obj in item:
# expand=True を入れることで、親Frameの幅の中で均等にスペースを分け合う
self._create_image_label(group_frame, img_obj, tk.LEFT, expand=True)
self.root.geometry('')
self.root.update_idletasks()
self.root.after(0, self._clamp_position)
if self.root.state() == 'normal':
self._force_topmost()
self.root.after(0, _do_update)
def _create_image_label(self, parent, img_obj, side, expand=False):
"""画像を生成してイベントをバインドする共通処理"""
w, h = nw, nh = img_obj.size
if self.is_fit_mode:
nw, nh = int(w * (self.taskbar_height / h)), self.taskbar_height
tk_img = ImageTk.PhotoImage(img_obj.resize((nw, nh)))
self.photo_refs.append(tk_img)
label = tk.Label(parent, image=tk_img, bg=self.trans_color)
# どの画像をクリックしてもウィンドウ全体を操作できるようにバインド
label.bind('<Button-1>', self.start_drag)
label.bind('<B1-Motion>', self.drag_window)
label.bind('<Button-3>', lambda e: self.toggle_orientation())
label.pack(side=side, padx=0, pady=0, expand=expand)
return label
def _clamp_position(self):
if not self.root:
return
self.root.update_idletasks()
self.root.update()
geom = self.root.geometry()
parts = geom.replace('x', '+').split('+')
curr_x = int(parts[2])
curr_y = int(parts[3])
ww = self.root.winfo_width()
wh = self.root.winfo_height()
if not self.root.overrideredirect():
border_w = self.root.winfo_rootx() - curr_x
title_h = self.root.winfo_rooty() - curr_y
full_ww = ww + (border_w * 2)
full_wh = wh + title_h + border_w
else:
full_ww = ww
full_wh = wh
sw = self.root.winfo_screenwidth()
sh = self.root.winfo_screenheight()
nx = max(0, min(curr_x, sw - full_ww))
ny = max(0, min(curr_y, sh - full_wh))
self.root.geometry(f'+{nx}+{ny}')
# --- 2. 表示状態だけを切り替えるメソッド ---
def set_visible(self, visible: bool):
"""ウィンドウの表示・非表示だけを制御する"""
if not self._ready:
return
def _do_toggle():
if visible:
self.root.deiconify()
self._force_topmost()
else:
self.root.withdraw()
self.root.after(0, _do_toggle)
def _keep_on_top_loop(self):
"""ウィンドウが表示されている間、0.1秒ごとに最前面を強制する"""
if self.root:
# ウィンドウが表示状態(normal)の時だけ実行
if self.root.state() == 'normal':
# lift() と topmost 再設定の合わせ技
self.root.lift()
self.root.attributes('-topmost', True)
# 100ms後に自分を再帰的に呼び出す
self.root.after(100, self._keep_on_top_loop)
def _force_topmost(self):
"""即座に最前面へ引き上げる(クリック時用)"""
if self.root:
self.root.attributes('-topmost', False)
self.root.attributes('-topmost', True)
self.root.lift()
def start_drag(self, event):
self.offset_x = event.x
self.offset_y = event.y
self._force_topmost()
def drag_window(self, event):
if self.root:
# 1. マウスの動きから計算上の新しい位置を出す
new_x = self.root.winfo_x() + (event.x - self.offset_x)
new_y = self.root.winfo_y() + (event.y - self.offset_y)
# 2. 画面の幅と高さを取得
screen_w = self.root.winfo_screenwidth()
screen_h = self.root.winfo_screenheight()
# 3. ウィンドウ自体の幅と高さを取得
win_w = self.root.winfo_width()
win_h = self.root.winfo_height()
# 4. 座標を画面内に収める (クランプ処理)
# 左端(0) と 右端(画面幅 - ウィンドウ幅) の間に収める
new_x = max(0, min(new_x, screen_w - win_w))
# 上端(0) と 下端(画面高 - ウィンドウ高) の間に収める
new_y = max(0, min(new_y, screen_h - win_h))
# 5. 制限された座標を適用
self.root.geometry(f'+{new_x}+{new_y}')
def toggle_fit(self):
self.is_fit_mode = not self.is_fit_mode
self.update(self.current_images, orientation=self.orientation)