-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu_bar_controller.py
More file actions
300 lines (260 loc) · 9.82 KB
/
menu_bar_controller.py
File metadata and controls
300 lines (260 loc) · 9.82 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python3
"""
Menu Bar Controller for PosturePal
Provides easy access to posture detection controls from the macOS menu bar
"""
import os
import sys
import subprocess
import json
import time
import re
from datetime import datetime, timedelta
# Add the current directory to Python path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
try:
import rumps
except ImportError:
print("rumps not found. Installing...")
subprocess.run([sys.executable, "-m", "pip", "install", "rumps"])
import rumps
CONFIG_FILE = "config.json"
def load_config():
"""Load configuration from file"""
if os.path.exists(CONFIG_FILE):
try:
with open(CONFIG_FILE, 'r') as f:
return json.load(f)
except Exception as e:
print(f"Error loading config: {e}")
return {}
else:
return {}
def is_posture_detection_running():
"""Check if posture detection is running"""
try:
result = subprocess.run(['pgrep', '-f', 'pose_webcam.py'],
capture_output=True, text=True)
return result.returncode == 0
except:
return False
def get_sitting_time():
"""Get current sitting time from status file"""
try:
if not os.path.exists('posture_status.json'):
return None, None, None
with open('posture_status.json', 'r') as f:
status = json.load(f)
sitting_elapsed = status.get('sitting_elapsed', 0)
is_sitting = status.get('is_sitting', False)
window_visible = status.get('window_visible', False)
if is_sitting and sitting_elapsed > 0:
# Convert seconds to formatted time
minutes = int(sitting_elapsed // 60)
seconds = int(sitting_elapsed % 60)
time_str = f"{minutes:02d}:{seconds:02d}"
return sitting_elapsed, time_str, window_visible
return None, None, window_visible
except:
return None, None, None
def toggle_camera_window():
"""Toggle camera window visibility"""
try:
# Send a signal to the posture detection process to toggle window
# We'll use a simple file-based approach
toggle_file = "toggle_window.txt"
with open(toggle_file, 'w') as f:
f.write(str(time.time()))
# Give it a moment to process
time.sleep(0.1)
# Check if file was processed (deleted)
if not os.path.exists(toggle_file):
return True
else:
# Clean up if not processed
try:
os.remove(toggle_file)
except:
pass
return False
except:
return False
def start_posture_detection():
"""Start posture detection"""
try:
result = subprocess.run(['python3', 'config_manager.py', '--start'],
capture_output=True, text=True)
return result.returncode == 0
except Exception as e:
print(f"Error starting posture detection: {e}")
return False
def stop_posture_detection():
"""Stop posture detection"""
try:
result = subprocess.run(['python3', 'config_manager.py', '--stop'],
capture_output=True, text=True)
return result.returncode == 0
except Exception as e:
print(f"Error stopping posture detection: {e}")
return False
class PosturePalMenuBar(rumps.App):
def __init__(self):
super().__init__("PosturePal", quit_button=None)
self.config = load_config()
self.setup_menu()
self.sitting_time = None
self.sitting_start_time = None
def setup_menu(self):
"""Setup the menu bar items"""
# Status indicator
self.status_item = rumps.MenuItem("Status: Checking...")
# Timer display
self.timer_item = rumps.MenuItem("Timer: --:--")
# Control buttons
self.start_item = rumps.MenuItem("Start Posture Detection", callback=self.start_detection)
self.stop_item = rumps.MenuItem("Stop Posture Detection", callback=self.stop_detection)
# Camera window control
self.camera_item = rumps.MenuItem("Show Camera Window", callback=self.toggle_camera)
# Settings
self.settings_item = rumps.MenuItem("Open Settings", callback=self.open_settings)
# Calibration
self.calibrate_item = rumps.MenuItem("Run Calibration", callback=self.run_calibration)
# Quit
self.quit_item = rumps.MenuItem("Quit PosturePal", callback=self.quit_app)
# Add all items to the menu
self.menu = [
self.status_item,
self.timer_item,
None, # Separator
self.start_item,
self.stop_item,
self.camera_item,
None, # Separator
self.settings_item,
self.calibrate_item,
None, # Separator
self.quit_item
]
# Start status update timer
self.timer = rumps.Timer(self.update_status, 2) # Update every 2 seconds
self.timer.start()
def update_status(self, _):
"""Update the status indicator"""
if is_posture_detection_running():
self.status_item.title = "Status: 🟢 Running"
self.start_item.set_callback(None) # Disable start
self.stop_item.set_callback(self.stop_detection) # Enable stop
# Update timer and camera window status
sitting_elapsed, time_str, window_visible = get_sitting_time()
if time_str:
self.timer_item.title = f"Timer: {time_str}"
else:
self.timer_item.title = "Timer: --:--"
# Update camera window button
if window_visible:
self.camera_item.title = "Hide Camera Window"
else:
self.camera_item.title = "Show Camera Window"
else:
self.status_item.title = "Status: 🔴 Stopped"
self.start_item.set_callback(self.start_detection) # Enable start
self.stop_item.set_callback(None) # Disable stop
self.timer_item.title = "Timer: --:--"
self.camera_item.title = "Show Camera Window"
def start_detection(self, _):
"""Start posture detection"""
if start_posture_detection():
rumps.notification(
title="PosturePal",
subtitle="Posture Detection Started",
message="Posture detection is now running in the background."
)
else:
rumps.notification(
title="PosturePal",
subtitle="Error",
message="Failed to start posture detection. Check the logs."
)
def stop_detection(self, _):
"""Stop posture detection"""
if stop_posture_detection():
rumps.notification(
title="PosturePal",
subtitle="Posture Detection Stopped",
message="Posture detection has been stopped."
)
else:
rumps.notification(
title="PosturePal",
subtitle="Error",
message="Failed to stop posture detection."
)
def open_settings(self, _):
"""Open the settings GUI"""
try:
subprocess.Popen([sys.executable, 'run_gui.py'])
except Exception as e:
rumps.notification(
title="PosturePal",
subtitle="Error",
message=f"Failed to open settings: {e}"
)
def toggle_camera(self, _):
"""Toggle camera window visibility"""
if is_posture_detection_running():
if toggle_camera_window():
rumps.notification(
title="PosturePal",
subtitle="Camera Window",
message="Camera window visibility toggled."
)
else:
rumps.notification(
title="PosturePal",
subtitle="Error",
message="Failed to toggle camera window."
)
else:
rumps.notification(
title="PosturePal",
subtitle="Not Running",
message="Posture detection must be running to toggle camera window."
)
def run_calibration(self, _):
"""Run calibration mode"""
try:
config = load_config()
camera_index = config.get('camera_index', 0)
subprocess.Popen([sys.executable, 'pose_webcam.py', '--calibrate', '--camera-index', str(camera_index)])
except Exception as e:
rumps.notification(
title="PosturePal",
subtitle="Error",
message=f"Failed to start calibration: {e}"
)
def quit_app(self, _):
"""Quit the menu bar app and stop posture detection"""
# Stop posture detection before quitting
stop_posture_detection()
rumps.quit_application()
def main():
"""Main function"""
# Hide dock icon for menu bar app on macOS
import os
import sys
# Set environment variable to hide dock icon
os.environ['PYTHON_DISABLE_DOCK_ICON'] = '1'
# Additional macOS-specific dock hiding
if sys.platform == 'darwin':
try:
import AppKit
# Hide dock icon
AppKit.NSApplication.sharedApplication()
AppKit.NSApp.setActivationPolicy_(AppKit.NSApplicationActivationPolicyAccessory)
except ImportError:
# AppKit not available, use alternative method
pass
app = PosturePalMenuBar()
app.run()
if __name__ == "__main__":
main()