vOOice/VoiceInk/PowerMode/ActiveWindowService.swift
Beingpax 4ea8d382a4 Add Power Mode keyboard shortcuts and improve session management
Power Mode Keyboard Shortcuts:
- Add hotkeyShortcut property to PowerModeConfig for storing custom shortcuts
- Implement keyboard shortcut UI in Power Mode configuration view
- Add hotkey registration system in HotkeyManager to manage Power Mode shortcuts
- Support cleanup of shortcuts when Power Mode configurations are removed
- Post notifications when Power Mode configurations change

Explicit Power Mode Activation:
- Add optional powerModeId parameter to toggleRecord and toggleMiniRecorder
- Refactor ActiveWindowService.applyConfigurationForCurrentApp to applyConfiguration
- Support direct Power Mode activation via powerModeId instead of auto-detection
- Pass powerModeId through recording flow for explicit mode selection

Session Management Improvements:
- Fix auto-restore to preserve baseline when switching Power Modes mid-recording
- Only capture baseline state on first session creation
- Prevent subsequent beginSession calls from overwriting original baseline
- Ensure auto-restore returns to settings from before recording started

UI Refinements:
- Remove redundant "Record" label from keyboard shortcut recorder
2026-01-03 08:43:11 +05:45

75 lines
2.5 KiB
Swift

import Foundation
import AppKit
import os
class ActiveWindowService: ObservableObject {
static let shared = ActiveWindowService()
@Published var currentApplication: NSRunningApplication?
private var enhancementService: AIEnhancementService?
private let browserURLService = BrowserURLService.shared
private var whisperState: WhisperState?
private let logger = Logger(
subsystem: "com.prakashjoshipax.voiceink",
category: "browser.detection"
)
private init() {}
func configure(with enhancementService: AIEnhancementService) {
self.enhancementService = enhancementService
}
func configureWhisperState(_ whisperState: WhisperState) {
self.whisperState = whisperState
}
func applyConfiguration(powerModeId: UUID? = nil) async {
if let powerModeId = powerModeId,
let config = PowerModeManager.shared.getConfiguration(with: powerModeId) {
await MainActor.run {
PowerModeManager.shared.setActiveConfiguration(config)
}
await PowerModeSessionManager.shared.beginSession(with: config)
return
}
guard let frontmostApp = NSWorkspace.shared.frontmostApplication,
let bundleIdentifier = frontmostApp.bundleIdentifier else {
return
}
await MainActor.run {
currentApplication = frontmostApp
}
var configToApply: PowerModeConfig?
if let browserType = BrowserType.allCases.first(where: { $0.bundleIdentifier == bundleIdentifier }) {
do {
let currentURL = try await browserURLService.getCurrentURL(from: browserType)
if let config = PowerModeManager.shared.getConfigurationForURL(currentURL) {
configToApply = config
}
} catch {
logger.error("❌ Failed to get URL from \(browserType.displayName): \(error.localizedDescription)")
}
}
if configToApply == nil {
configToApply = PowerModeManager.shared.getConfigurationForApp(bundleIdentifier)
}
if configToApply == nil {
configToApply = PowerModeManager.shared.getDefaultConfiguration()
}
if let config = configToApply {
await MainActor.run {
PowerModeManager.shared.setActiveConfiguration(config)
}
await PowerModeSessionManager.shared.beginSession(with: config)
}
}
}