-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
196 lines (162 loc) · 6.03 KB
/
Copy pathmain.py
File metadata and controls
196 lines (162 loc) · 6.03 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
"""Live Translator — Real-time system audio translation for macOS."""
import sys
import os
# Add src to path (use realpath to resolve symlinks and relative paths)
_BASE = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, os.path.join(_BASE, "src"))
import objc
from AppKit import (
NSApplication,
NSApp,
NSStatusBar,
NSMenu,
NSMenuItem,
NSVariableStatusItemLength,
NSObject,
NSApplicationActivationPolicyAccessory,
)
import threading
from overlay import TranslationOverlay
from pipeline import TranslationPipeline
from config import load_config, save_config
class AppDelegate(NSObject):
def init(self):
self = objc.super(AppDelegate, self).init()
if self is None:
return None
self._overlay = None
self._pipeline = None
self._config = load_config()
return self
def applicationDidFinishLaunching_(self, notification):
# Hide from dock, show only in menu bar
NSApp.setActivationPolicy_(NSApplicationActivationPolicyAccessory)
self._overlay = TranslationOverlay(
on_lang_change=self._on_lang_change,
on_settings_change=self._on_settings_change,
on_clear=self._on_clear,
on_close=self._on_close,
on_toggle_tts=self._on_toggle_tts,
)
self._setup_status_bar()
self._overlay.setup()
self._overlay.show()
threading.Thread(target=self._start_pipeline, daemon=True).start()
def _start_pipeline(self):
try:
self._pipeline = TranslationPipeline(
self._overlay, config=self._config
)
self._pipeline.start()
except Exception as e:
print(f"[Main] Pipeline error: {e}")
def _setup_status_bar(self):
self._status_item = NSStatusBar.systemStatusBar().statusItemWithLength_(
NSVariableStatusItemLength
)
self._status_item.setTitle_("🌐")
menu = NSMenu.alloc().init()
toggle = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
"Pause", "toggleCapture:", "t"
)
toggle.setTarget_(self)
menu.addItem_(toggle)
show_hide = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
"Show/Hide Window", "toggleOverlay:", "o"
)
show_hide.setTarget_(self)
menu.addItem_(show_hide)
menu.addItem_(NSMenuItem.separatorItem())
quit_item = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
"Quit", "quitApp:", "q"
)
quit_item.setTarget_(self)
menu.addItem_(quit_item)
self._status_item.setMenu_(menu)
def _on_lang_change(self, source_locale, target_lang):
self._config["source_locale"] = source_locale
self._config["target_lang"] = target_lang
save_config(self._config)
if self._pipeline:
threading.Thread(
target=self._pipeline.change_languages,
args=(source_locale, target_lang),
daemon=True,
).start()
def _on_settings_change(self, config):
self._config = config
if self._pipeline:
self._pipeline.update_config(config)
def _on_clear(self):
if self._pipeline:
self._pipeline.clear()
def _on_close(self):
if self._pipeline:
self._pipeline.stop()
NSApp.terminate_(None)
def _on_toggle_tts(self):
if self._pipeline:
on = self._pipeline.toggle_tts()
self._overlay.set_tts_state(on)
@objc.IBAction
def toggleCapture_(self, sender):
if self._pipeline and self._pipeline.is_running:
self._pipeline.stop()
sender.setTitle_("Resume")
self._status_item.setTitle_("⏸")
else:
if self._pipeline:
threading.Thread(target=self._pipeline.start, daemon=True).start()
sender.setTitle_("Pause")
self._status_item.setTitle_("🌐")
@objc.IBAction
def toggleOverlay_(self, sender):
if self._overlay.is_visible():
self._overlay.hide()
else:
self._overlay.show()
@objc.IBAction
def quitApp_(self, sender):
if self._pipeline:
self._pipeline.stop()
NSApp.terminate_(None)
def main():
import os, ctypes, ctypes.util
# 1. Set process name BEFORE anything else
try:
libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library("c"))
libc.setprogname(b"Live Translator")
except Exception:
pass
# 2. Point NSBundle to our .app so macOS reads its Info.plist
from Foundation import NSBundle
res_dir = os.path.dirname(os.path.realpath(__file__))
if "/Contents/Resources" in res_dir:
bundle_path = res_dir.split("/Contents/Resources")[0]
our_bundle = NSBundle.bundleWithPath_(bundle_path)
if our_bundle:
our_bundle.load()
# Patch mainBundle's infoDictionary
our_info = our_bundle.infoDictionary()
main_info = NSBundle.mainBundle().infoDictionary()
if our_info and main_info:
main_info["CFBundleName"] = our_info.get("CFBundleName", "Live Translator")
main_info["CFBundleDisplayName"] = our_info.get("CFBundleDisplayName", "Live Translator")
main_info["CFBundleIdentifier"] = our_info.get("CFBundleIdentifier", "com.livetranslator.app")
main_info["CFBundleIconFile"] = our_info.get("CFBundleIconFile", "AppIcon")
# 3. Create NSApplication
app = NSApplication.sharedApplication()
# 4. Set dock icon
from AppKit import NSImage
icon_path = os.path.join(res_dir, "AppIcon.icns")
if os.path.exists(icon_path):
app.setApplicationIconImage_(NSImage.alloc().initWithContentsOfFile_(icon_path))
delegate = AppDelegate.alloc().init()
app.setDelegate_(delegate)
print("=" * 50)
print(" Live Translator")
print(" Menu bar: 🌐")
print("=" * 50)
app.run()
if __name__ == "__main__":
main()