-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.py
More file actions
213 lines (187 loc) · 6.78 KB
/
Copy pathui.py
File metadata and controls
213 lines (187 loc) · 6.78 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
import tkinter as tk
import time
import subprocess
from sprite import Sprite
SPRITE_RENDER_SIZE = 24
TEXT_FONT = ("DejaVu Sans", 10)
ENTRY_FONT = ("DejaVu Sans", 10)
PLACEHOLDER = "ask me anything..."
BAR_BG = "#1e1e2e"
BAR_W = 900
BAR_H = 30
TEXT_DISPLAY_SECS = 120 # Hvor lenge respons vises før feltet resettes
class PixelAssistantUI:
def __init__(self, on_submit):
self.on_submit = on_submit
self.root = tk.Tk(className="PixelAssistant")
self.root.withdraw()
self.root.title("pixel-assistant")
screen_w = self.root.winfo_screenwidth()
screen_h = self.root.winfo_screenheight()
x = (screen_w - BAR_W) // 2
y = screen_h - BAR_H
self.root.geometry(f"{BAR_W}x{BAR_H}+{x}+{y}")
self.root.configure(bg=BAR_BG)
self.root.attributes("-alpha", 0.88)
self.sprite = Sprite("idle")
self.state = "idle"
self.response_text = ""
self.last_response_time = 0
self._build_ui()
self.root.deiconify()
self.root.after(200, self._apply_hints)
self.root.after(50, self._loop)
def _apply_hints(self):
# La set-hints.sh håndtere xprop, strut, og posisjon — den kjøres
# fra start.sh som fallback etter at vinduet er mappet.
pass
def _build_ui(self):
container = tk.Frame(self.root, bg=BAR_BG)
container.pack(fill=tk.BOTH, expand=True, padx=4, pady=0)
# Mikrofonknapp — hold inne for å ta opp, slipp for å transkribere og sende
self.mic_btn = tk.Label(
container,
text="🎤",
font=("DejaVu Sans", 12),
fg="#585b70",
bg=BAR_BG,
padx=4,
cursor="hand2",
)
self.mic_btn.pack(side=tk.LEFT, padx=(0, 3))
self.mic_btn.bind("<ButtonPress-1>", self._on_mic_press)
self.mic_btn.bind("<ButtonRelease-1>", self._on_mic_release)
self.entry = tk.Entry(
container,
font=ENTRY_FONT,
bg="#1e1e2e",
fg="#585b70",
insertbackground="#cdd6f4",
relief=tk.FLAT,
bd=0,
highlightthickness=0,
takefocus=1,
)
self.entry.insert(0, PLACEHOLDER)
self.entry.bind("<FocusIn>", self._on_entry_focus)
self.entry.bind("<FocusOut>", self._on_entry_blur)
self.entry.bind("<Return>", self._on_submit)
self.entry.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(0, 3))
self.canvas = tk.Canvas(
container,
width=SPRITE_RENDER_SIZE + 4,
height=SPRITE_RENDER_SIZE + 2,
bg=BAR_BG,
highlightthickness=0,
)
self.canvas.pack(side=tk.LEFT, padx=1)
self.output = tk.Entry(
container,
font=TEXT_FONT,
fg="#cdd6f4",
bg="#1e1e2e",
relief=tk.FLAT,
bd=0,
highlightthickness=0,
state="readonly",
readonlybackground="#1e1e2e",
)
self.output.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(3, 0))
def _on_entry_focus(self, event):
if self.entry.get() == PLACEHOLDER:
self.entry.delete(0, tk.END)
self.entry.config(fg="#cdd6f4")
def _on_entry_blur(self, event):
if not self.entry.get().strip():
self.entry.delete(0, tk.END)
self.entry.insert(0, PLACEHOLDER)
self.entry.config(fg="#585b70")
def _on_submit(self, event):
text = self.entry.get().strip()
if not text or text == PLACEHOLDER:
return
self.entry.delete(0, tk.END)
self.on_submit(text)
def _on_mic_press(self, event):
self.mic_btn.config(bg="#f38ba8") # rød bakgrunn = opptak
self.entry.focus_force()
subprocess.Popen(
["dictation", "start"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
)
def _on_mic_release(self, event):
self.mic_btn.config(bg=BAR_BG) # tilbake til standard
subprocess.Popen(
["dictation", "stop"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
)
# Gjentatt fokus så xdotool får limt inn teksten, deretter auto-submit
self.entry.focus_force()
self.root.after(400, self.entry.focus_force)
self.root.after(800, self.entry.focus_force)
self.root.after(1200, self.entry.focus_force)
self.root.after(1500, self._submit_entry)
def _submit_entry(self):
text = self.entry.get().strip()
if text and text != PLACEHOLDER:
self.entry.delete(0, tk.END)
self.on_submit(text)
def _set_output(self, text):
self.output.configure(state="normal")
self.output.delete(0, tk.END)
self.output.insert(0, text)
self.output.configure(state="readonly")
self.output.icursor(tk.END)
def set_state(self, state, response=""):
self.sprite.set_state(state)
self.state = state
if state == "think":
self._set_output("⏳")
elif state == "done" and response:
self.response_text = response
self._set_output(response)
self.last_response_time = time.monotonic()
elif state == "alert" and response:
self._set_output(response)
elif state == "listen":
self._set_output("")
elif state == "idle":
self._set_output("")
def append_text(self, delta):
self.response_text += delta
self._set_output(self.response_text)
def set_text(self, text):
self._set_output(text)
def _render_sprite(self):
self.canvas.delete("all")
frame = self.sprite.get_frame()
if not frame:
return
cw = SPRITE_RENDER_SIZE + 4
ch = SPRITE_RENDER_SIZE + 2
ox = (cw - 24) // 2
oy = (ch - 24) // 2
for row_idx, row in enumerate(frame):
for col_idx, px in enumerate(row):
if len(px) < 4 or px[3] < 200:
continue
r, g, b = px[0], px[1], px[2]
color = f"#{r:02x}{g:02x}{b:02x}"
x1 = ox + col_idx
y1 = oy + row_idx
self.canvas.create_rectangle(
x1, y1, x1 + 1, y1 + 1,
fill=color, outline="", width=0,
)
def _loop(self):
now = time.monotonic()
self._render_sprite()
if self.state == "done" and self.response_text:
elapsed = now - self.last_response_time
if elapsed > TEXT_DISPLAY_SECS:
self.set_state("idle")
self.root.update_idletasks()
self.root.update()
self.root.after(50, self._loop)
def run(self):
self.root.mainloop()