This commit is contained in:
Mikan
2026-03-09 22:05:48 +03:00
parent c635b0261b
commit 01cf0f87d0
5 changed files with 282 additions and 28 deletions

View File

@@ -1,4 +1,6 @@
"""Time Tracker package."""
"""Entry point for running the time tracker as a module."""
from .main import main
if __name__ == "__main__":
main()

View File

@@ -51,16 +51,28 @@ except ImportError:
return 0
class ConfigWrapper:
"""Wrapper to allow attribute access to config dict"""
def __init__(self, config_dict: Dict):
self.__dict__.update(config_dict)
def update(self, new_config: Dict):
"""Update the config and underlying dict"""
self.__dict__.update(new_config)
class WorkTracker:
def __init__(self):
self.config = load_config()
raw_config = load_config()
self.config = ConfigWrapper(raw_config)
self.session_manager = SessionManager("tasks")
self.active_window = ""
self.window_classification = "neutral"
self.last_activity = time.time()
self.session_locked = False
self.is_tracking = self.config.get("tracking_enabled", True)
self.current_task_id = self.config.get("current_task_id")
self.is_tracking = self.config.tracking_enabled
self.current_task_id = self.config.current_task_id
# Start monitoring in background
self.start_monitoring()
@@ -119,7 +131,7 @@ class WorkTracker:
def classify_window(self, title: str) -> str:
"""Classify window as work, distraction, or neutral"""
title_lower = title.lower()
rules = self.config.get("window_rules", {})
rules = self.config.window_rules # Access through wrapper
# Check distractions first
for keyword in rules.get("distraction", []):
@@ -150,7 +162,6 @@ class WorkTracker:
# Check if tracking is enabled
if not self.is_tracking:
return False
# Check if session is locked
if self.session_locked:
return False
@@ -186,16 +197,19 @@ class WorkTracker:
def toggle_tracking(self):
"""Toggle tracking on/off"""
self.is_tracking = not self.is_tracking
self.config["tracking_enabled"] = self.is_tracking
save_config(self.config)
self.config.tracking_enabled = self.is_tracking
# Update raw config too
raw_config = {key: value for key, value in vars(self.config).items() if not key.startswith('_')}
save_config(raw_config)
def create_task(self, task_id: str):
"""Create a new task and start tracking it"""
project_id = task_id.split('-')[0] if '-' in task_id else task_id
self.session_manager.create_session(task_id, project_id)
self.current_task_id = task_id
self.config["current_task_id"] = task_id
save_config(self.config)
self.config.current_task_id = task_id
raw_config = {key: value for key, value in vars(self.config).items() if not key.startswith('_')}
save_config(raw_config)
def switch_task(self, task_id: str):
"""Switch to an existing task"""
@@ -207,8 +221,9 @@ class WorkTracker:
project_id = task_id.split('-')[0] if '-' in task_id else task_id
self.session_manager.create_session(task_id, project_id)
self.current_task_id = task_id
self.config["current_task_id"] = task_id
save_config(self.config)
self.config.current_task_id = task_id
raw_config = {key: value for key, value in vars(self.config).items() if not key.startswith('_')}
save_config(raw_config)
def get_available_tasks(self) -> List[str]:
"""Get list of all available tasks"""

View File

@@ -18,11 +18,26 @@ def register_session_listener(callback: Callable[[str], None]) -> None:
"""Register a callback for Windows session lock/unlock events"""
def session_event_handler(hwnd, msg, wparam, lparam):
if msg == win32con.WM_WTSSESSION_CHANGE:
if wparam == win32con.WTS_SESSION_LOCK:
callback("locked")
elif wparam == win32con.WTS_SESSION_UNLOCK:
callback("unlocked")
# Check if the constant exists before using it
try:
if msg == win32con.WM_WTSSESSION_CHANGE:
if wparam == getattr(win32con, 'WTS_SESSION_LOCK', 0x1):
callback("locked")
elif wparam == getattr(win32con, 'WTS_SESSION_UNLOCK', 0x2):
callback("unlocked")
except AttributeError:
# Some versions of pywin32 might not have these constants
# Use numeric values as fallback
WM_WTSSESSION_CHANGE = 0x02B1
WTS_SESSION_LOCK = 0x1
WTS_SESSION_UNLOCK = 0x2
if msg == WM_WTSSESSION_CHANGE:
if wparam == WTS_SESSION_LOCK:
callback("locked")
elif wparam == WTS_SESSION_UNLOCK:
callback("unlocked")
return win32gui.DefWindowProc(hwnd, msg, wparam, lparam)
def run_message_loop():
@@ -39,17 +54,17 @@ def register_session_listener(callback: Callable[[str], None]) -> None:
# Corrected CreateWindow call with all required parameters
hwnd = win32gui.CreateWindow(
wndclass.lpszClassName, # lpClassName
"Session Watcher", # lpWindowName
0, # dwStyle
0, # x
0, # y
0, # nWidth
0, # nHeight
0, # hWndParent
0, # hMenu
hinst, # hInstance
None # lParam
wndclass.lpszClassName, # lpClassName
"Session Watcher", # lpWindowName
0, # dwStyle
0, # x
0, # y
0, # nWidth
0, # nHeight
0, # hWndParent
0, # hMenu
hinst, # hInstance
None # lParam
)
# Register for session notifications