Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions aw_watcher_window/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,20 @@ def get_current_window_windows() -> Optional[dict]:
from . import windows

window_handle = windows.get_active_window_handle()

# hwnd 0 means no foreground window (e.g. during UAC prompt, lock screen,
# or secure desktop). Return None so heartbeat_loop skips this poll cycle.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it skip poll cycle or lead to unknown?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question — I'd argue skip the poll cycle is the right behavior here, for the same reason we already do it for non-Windows (the get_current_window functions return None and heartbeat_loop treats it as 'no window, try again').

The distinction:

  • hwnd == 0: There is literally no foreground window (lock screen, UAC prompt, secure desktop). Nothing to record.
  • app = None (from failed OpenProcess / WMI): There is a window, we just can't identify it → falls back to "unknown".

Recording "unknown" during a UAC prompt or lock screen would create misleading activity — the user isn't actually in any app. Skipping the heartbeat leaves a gap (or the previous heartbeat's pulsetime covers it), which is semantically correct.

That said, if you'd prefer "unknown" for continuity, happy to change it — it's a one-liner swap.

if not window_handle:
return None

try:
app = windows.get_app_name(window_handle)
except Exception: # TODO: narrow down the exception
# try with wmi method
app = windows.get_app_name_wmi(window_handle)
except Exception:
# Fall back to WMI for elevated/admin processes where OpenProcess fails
try:
app = windows.get_app_name_wmi(window_handle)
except Exception:
app = None

title = windows.get_window_title(window_handle)

Expand Down
Loading