-
Notifications
You must be signed in to change notification settings - Fork 0
/
brollin_talon.py
116 lines (87 loc) · 3.08 KB
/
brollin_talon.py
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
from typing import Dict
from talon import Module, Context, ui, ctrl, canvas, screen, actions, clip, noise
from talon.skia import Paint, Image
import json
mod = Module()
ctx = Context()
def testing_pop(active):
print("Heard pop")
def testing_hiss(active):
print("Heard hiss")
# noise.register("pop", testing_pop)
# noise.register("hiss", testing_hiss)
class BrollinOverlay:
talon_awake = True
screen = ui.screens()[0]
overlay_text = ""
def __init__(self) -> None:
self.mcanvas = canvas.Canvas.from_screen(self.screen)
self.mcanvas.register("draw", self.draw)
self.mcanvas.freeze()
def draw(self, canvas):
canvas.paint.textsize = 16
canvas.paint.text_align = canvas.paint.TextAlign.CENTER
text_rectangle = canvas.paint.measure_text(self.overlay_text)[1].inset(-4)
bar_rectangle = ui.Rect(
self.screen.width / 3,
0,
self.screen.width / 3,
text_rectangle.height - 4 if len(self.overlay_text) else 7,
)
def draw_bar():
canvas.paint.color = "008f00" if self.talon_awake else "8f0000"
canvas.paint.style = Paint.Style.FILL
canvas.draw_rect(bar_rectangle)
def draw_text():
if not len(self.overlay_text):
return
canvas.paint.color = "ffffff"
canvas.draw_text(
self.overlay_text,
self.screen.width / 2,
text_rectangle.height * 0.65,
)
draw_bar()
draw_text()
def redraw(self):
self.mcanvas.unregister("draw", self.draw)
self.mcanvas.register("draw", self.draw)
self.mcanvas.freeze()
def set_awake(self, awake=True):
self.talon_awake = awake
self.redraw()
def set_overlay_text(self, new_text: str):
self.overlay_text = new_text
self.redraw()
brollin_overlay = BrollinOverlay()
@mod.action_class
class BrollinActions:
def copy_command_id():
"""Copy the command id of the focused menu item"""
actions.key("tab:2 enter")
actions.sleep("750ms")
json_text = actions.edit.selected_text()
command_id = json.loads(json_text)["command"]
actions.app.tab_close()
clip.set_text(command_id)
def show_talon_overlay(toggle: int):
"""Show an overlay indicating whether talon is awake"""
brollin_overlay.set_awake(True if toggle == 1 else False)
def toggle_talon():
"""Toggle talon on or off"""
if not actions.speech.enabled():
actions.user.wake_talon()
else:
actions.user.sleep_talon()
def sleep_talon():
"""Turn talon off"""
actions.speech.disable()
actions.user.show_talon_overlay(0)
def wake_talon():
"""Turn talon on"""
actions.speech.enable()
actions.user.show_talon_overlay(1)
def open_file_in_vscode(path: str):
"""Open a given file path with VSCode"""
command = "open -a 'Visual Studio Code' " + path
actions.user.system_command_nb(command)