295 lines
10 KiB
Python
295 lines
10 KiB
Python
"""
|
|
EU-Utility - Activity Bar Overlay Modes
|
|
========================================
|
|
|
|
GW2/Blish-style overlay system with multiple visibility modes:
|
|
|
|
Modes:
|
|
- DESKTOP_APP: Activity bar shows in desktop app only
|
|
- OVERLAY_ALWAYS: Activity bar always visible as overlay
|
|
- OVERLAY_GAME_FOCUSED: Only visible when EU window is focused
|
|
- OVERLAY_HOTKEY_TOGGLE: Toggle with hotkey (Ctrl+Shift+B)
|
|
- OVERLAY_TEMPORARY: Show for 7-10 seconds on hotkey, then hide
|
|
"""
|
|
|
|
import threading
|
|
import time
|
|
from enum import Enum
|
|
from dataclasses import dataclass
|
|
from typing import Optional
|
|
from functools import lru_cache
|
|
from PyQt6.QtCore import QTimer, pyqtSignal, QObject
|
|
|
|
|
|
class OverlayMode(Enum):
|
|
"""Activity bar visibility modes."""
|
|
DESKTOP_APP = "desktop_app" # Only in desktop app
|
|
OVERLAY_ALWAYS = "overlay_always" # Always visible overlay
|
|
OVERLAY_GAME_FOCUSED = "overlay_game" # Only when EU game focused
|
|
OVERLAY_HOTKEY_TOGGLE = "overlay_toggle" # Toggle with hotkey
|
|
OVERLAY_TEMPORARY = "overlay_temp" # Show 7-10s on hotkey
|
|
|
|
|
|
@dataclass
|
|
class OverlayConfig:
|
|
"""Configuration for activity bar overlay behavior."""
|
|
# Default to game-focused mode (Blish HUD style)
|
|
mode: OverlayMode = OverlayMode.OVERLAY_GAME_FOCUSED
|
|
temporary_duration: int = 8000 # milliseconds (8 seconds default)
|
|
game_focus_poll_interval: int = 5000 # ms (5 seconds - reduces CPU load)
|
|
|
|
def to_dict(self):
|
|
return {
|
|
'mode': self.mode.value,
|
|
'temporary_duration': self.temporary_duration,
|
|
'game_focus_poll_interval': self.game_focus_poll_interval
|
|
}
|
|
|
|
@classmethod
|
|
def from_dict(cls, data):
|
|
mode_str = data.get('mode', 'overlay_toggle')
|
|
try:
|
|
mode = OverlayMode(mode_str)
|
|
except ValueError:
|
|
mode = OverlayMode.OVERLAY_HOTKEY_TOGGLE
|
|
|
|
return cls(
|
|
mode=mode,
|
|
temporary_duration=data.get('temporary_duration', 8000),
|
|
game_focus_poll_interval=data.get('game_focus_poll_interval', 5000)
|
|
)
|
|
|
|
|
|
class OverlayController(QObject):
|
|
"""
|
|
Controls activity bar visibility based on selected mode.
|
|
|
|
Usage:
|
|
controller = OverlayController(activity_bar, window_manager)
|
|
controller.set_mode(OverlayMode.OVERLAY_TEMPORARY)
|
|
controller.start()
|
|
"""
|
|
|
|
visibility_changed = pyqtSignal(bool) # is_visible
|
|
|
|
def __init__(self, activity_bar, window_manager=None, parent=None):
|
|
super().__init__(parent)
|
|
self.activity_bar = activity_bar
|
|
self.window_manager = window_manager
|
|
self.config = OverlayConfig()
|
|
|
|
# State
|
|
self._mode = OverlayMode.OVERLAY_HOTKEY_TOGGLE
|
|
self._is_visible = False
|
|
self._game_focused = False
|
|
|
|
# Caching for performance
|
|
self._cached_window_state = None
|
|
self._last_check_time = 0
|
|
self._cache_ttl = 2.0 # Cache window state for 2 seconds
|
|
|
|
# Threading for non-blocking focus checks
|
|
self._focus_check_thread = None
|
|
self._focus_check_lock = threading.Lock()
|
|
self._focus_check_running = False
|
|
self._pending_focus_result = None
|
|
|
|
# Timers
|
|
self._game_focus_timer = QTimer()
|
|
self._game_focus_timer.timeout.connect(self._check_game_focus_async)
|
|
|
|
self._temporary_timer = QTimer()
|
|
self._temporary_timer.setSingleShot(True)
|
|
self._temporary_timer.timeout.connect(self._hide_temporary)
|
|
|
|
def set_mode(self, mode: OverlayMode):
|
|
"""Change overlay mode."""
|
|
self._mode = mode
|
|
self._apply_mode()
|
|
|
|
def start(self):
|
|
"""Start the overlay controller based on current mode."""
|
|
self._apply_mode()
|
|
|
|
def stop(self):
|
|
"""Stop all timers and hide overlay."""
|
|
self._game_focus_timer.stop()
|
|
self._temporary_timer.stop()
|
|
# Stop any running focus check thread
|
|
self._focus_check_running = False
|
|
if self.activity_bar:
|
|
self.activity_bar.hide()
|
|
|
|
def _apply_mode(self):
|
|
"""Apply current mode settings."""
|
|
self._game_focus_timer.stop()
|
|
self._temporary_timer.stop()
|
|
|
|
if self._mode == OverlayMode.DESKTOP_APP:
|
|
# Only show in desktop app - hide overlay
|
|
self._hide()
|
|
|
|
elif self._mode == OverlayMode.OVERLAY_ALWAYS:
|
|
# Always show
|
|
self._show()
|
|
|
|
elif self._mode == OverlayMode.OVERLAY_GAME_FOCUSED:
|
|
# Show only when game focused - use 5 second poll interval
|
|
self._game_focus_timer.start(self.config.game_focus_poll_interval)
|
|
self._check_game_focus_async() # Check immediately (non-blocking)
|
|
|
|
elif self._mode == OverlayMode.OVERLAY_HOTKEY_TOGGLE:
|
|
# Hotkey toggle - start hidden
|
|
self._hide()
|
|
|
|
elif self._mode == OverlayMode.OVERLAY_TEMPORARY:
|
|
# Temporary mode - start hidden
|
|
self._hide()
|
|
|
|
def toggle(self):
|
|
"""Toggle overlay visibility (for hotkey)."""
|
|
if self._mode == OverlayMode.OVERLAY_TEMPORARY:
|
|
self.show_temporary()
|
|
else:
|
|
if self._is_visible:
|
|
self._hide()
|
|
else:
|
|
self._show()
|
|
|
|
def show_temporary(self):
|
|
"""Show overlay temporarily (for temporary mode)."""
|
|
self._show()
|
|
self._temporary_timer.start(self.config.temporary_duration)
|
|
|
|
def _hide_temporary(self):
|
|
"""Hide after temporary duration."""
|
|
self._hide()
|
|
|
|
def _show(self):
|
|
"""Show the activity bar."""
|
|
if self.activity_bar and not self._is_visible:
|
|
self.activity_bar.show()
|
|
self._is_visible = True
|
|
self.visibility_changed.emit(True)
|
|
|
|
def _hide(self):
|
|
"""Hide the activity bar."""
|
|
if self.activity_bar and self._is_visible:
|
|
self.activity_bar.hide()
|
|
self._is_visible = False
|
|
self.visibility_changed.emit(False)
|
|
|
|
def _check_game_focus_async(self):
|
|
"""Start focus check in background thread to avoid blocking UI."""
|
|
# Use cached result if available and recent
|
|
current_time = time.time()
|
|
if self._cached_window_state is not None and (current_time - self._last_check_time) < self._cache_ttl:
|
|
self._apply_focus_state(self._cached_window_state)
|
|
return
|
|
|
|
# Don't start a new thread if one is already running
|
|
with self._focus_check_lock:
|
|
if self._focus_check_running:
|
|
return
|
|
self._focus_check_running = True
|
|
|
|
# Start background thread for focus check
|
|
self._focus_check_thread = threading.Thread(
|
|
target=self._do_focus_check_threaded,
|
|
daemon=True,
|
|
name="FocusCheckThread"
|
|
)
|
|
self._focus_check_thread.start()
|
|
|
|
def _do_focus_check_threaded(self):
|
|
"""Background thread: Check focus without blocking main thread."""
|
|
start_time = time.perf_counter()
|
|
|
|
try:
|
|
# Check if window manager is available
|
|
if not self.window_manager:
|
|
return
|
|
|
|
# Use fast cached window find
|
|
is_focused = self._fast_focus_check()
|
|
|
|
# Store result for main thread to apply
|
|
self._pending_focus_result = is_focused
|
|
|
|
# Update cache
|
|
self._cached_window_state = is_focused
|
|
self._last_check_time = time.time()
|
|
|
|
# Schedule UI update on main thread
|
|
from PyQt6.QtCore import QMetaObject, Qt, Q_ARG
|
|
QMetaObject.invokeMethod(
|
|
self,
|
|
"_apply_pending_focus_result",
|
|
Qt.ConnectionType.QueuedConnection
|
|
)
|
|
|
|
# Log performance (only if slow, for debugging)
|
|
elapsed = (time.perf_counter() - start_time) * 1000
|
|
if elapsed > 50: # Log if >50ms (way above our 10ms target)
|
|
print(f"[OverlayController] Warning: Focus check took {elapsed:.1f}ms")
|
|
|
|
except Exception as e:
|
|
# Silently handle errors in background thread
|
|
pass
|
|
finally:
|
|
self._focus_check_running = False
|
|
|
|
def _fast_focus_check(self) -> bool:
|
|
"""Fast focus check using cached window handle and psutil."""
|
|
try:
|
|
# Use psutil for fast process-based detection (no window enumeration)
|
|
if hasattr(self.window_manager, 'is_eu_focused_fast'):
|
|
return self.window_manager.is_eu_focused_fast()
|
|
|
|
# Fallback to window manager's optimized method
|
|
if hasattr(self.window_manager, 'find_eu_window_cached'):
|
|
window = self.window_manager.find_eu_window_cached()
|
|
if window:
|
|
return window.is_focused
|
|
|
|
# Last resort - standard find (slow)
|
|
window = self.window_manager.find_eu_window()
|
|
if window:
|
|
return window.is_focused
|
|
|
|
return False
|
|
except Exception:
|
|
return False
|
|
|
|
def _apply_pending_focus_result(self):
|
|
"""Apply focus result from background thread (called on main thread)."""
|
|
if self._pending_focus_result is not None:
|
|
self._apply_focus_state(self._pending_focus_result)
|
|
self._pending_focus_result = None
|
|
|
|
def _apply_focus_state(self, is_focused: bool):
|
|
"""Apply focus state change (early exit if no change)."""
|
|
# Early exit - no change in state
|
|
if is_focused == self._game_focused:
|
|
return
|
|
|
|
# Update state and visibility
|
|
self._game_focused = is_focused
|
|
if is_focused:
|
|
self._show()
|
|
else:
|
|
self._hide()
|
|
|
|
|
|
# Singleton instance
|
|
_overlay_controller = None
|
|
|
|
def get_overlay_controller(activity_bar=None, window_manager=None, parent=None):
|
|
"""Get or create the overlay controller singleton."""
|
|
global _overlay_controller
|
|
if _overlay_controller is None:
|
|
if activity_bar is None:
|
|
raise ValueError("activity_bar required for first initialization")
|
|
_overlay_controller = OverlayController(activity_bar, window_manager, parent)
|
|
return _overlay_controller
|