Power Mode
Relevant source files
The following files were used as context for generating this wiki page:
- VoiceInk/PowerMode/PowerModeConfig.swift
- VoiceInk/PowerMode/PowerModeConfigView.swift
- VoiceInk/PowerMode/PowerModePopover.swift
- VoiceInk/PowerMode/PowerModeSessionManager.swift
- VoiceInk/PowerMode/PowerModeValidator.swift
- VoiceInk/PowerMode/PowerModeView.swift
- VoiceInk/PowerMode/PowerModeViewComponents.swift
- VoiceInk/Views/Settings/PowerModeSettingsSection.swift
- VoiceInk/VoiceInk.swift
Power Mode is a feature that allows users to create and apply custom configurations for transcription and AI enhancement based on the active application or website. It is designed to automate workflows by intelligently switching settings to match the user's current context, such as using a specific transcription model for a coding app or a formal email prompt for a mail client.
The feature can be enabled or disabled globally from the application's settings. When enabled, it monitors the active window and URL to find a matching configuration. A Power Mode session is initiated, which saves the user's current settings, applies the new configuration, and restores the original settings upon completion.
Sources: README.md:19, VoiceInk/PowerMode/PowerModeView.swift:51-57, VoiceInk/Views/Settings/PowerModeSettingsSection.swift:14-16
Core Components
The Power Mode system is composed of several key classes that manage configuration, session state, and context detection.
| Class/Service | Description | Source File |
|---|---|---|
PowerModeManager | A singleton that manages the lifecycle of PowerModeConfig objects. It handles creating, reading, updating, deleting (CRUD), and persisting configurations to UserDefaults. | VoiceInk/PowerMode/PowerModeConfig.swift:120-264 |
PowerModeSessionManager | A singleton responsible for managing a temporary Power Mode session. It saves the application's state, applies a selected configuration, and restores the state afterward. | VoiceInk/PowerMode/PowerModeSessionManager.swift:18-178 |
ActiveWindowService | A service that monitors the active application on the user's system to trigger the corresponding Power Mode. | VoiceInk/VoiceInk.swift:70-73 |
PowerModeValidator | A struct that enforces validation rules when creating or saving a PowerModeConfig, preventing issues like duplicate names or triggers. | VoiceInk/PowerMode/PowerModeValidator.swift:29-81 |
PowerModeView | The main SwiftUI view for displaying, reordering, and initiating the creation/editing of Power Modes. | VoiceInk/PowerMode/PowerModeView.swift:35-212 |
ConfigurationView | The SwiftUI view for creating and editing the details of a single PowerModeConfig. | VoiceInk/PowerMode/PowerModeConfigView.swift:5-763 |
Architectural Overview
The following diagram illustrates the relationships between the main components of the Power Mode feature.
Data Model
The core of the Power Mode feature is the PowerModeConfig data structure, which holds all the settings for a specific mode.
PowerModeConfig Structure
The diagram below shows the entity relationship for Power Mode configurations.
PowerModeConfig Properties
| Property | Type | Description |
|---|---|---|
id | UUID | A unique identifier for the configuration. |
name | String | The user-defined name for the Power Mode. |
emoji | String | An emoji icon to visually represent the mode. |
appConfigs | [AppConfig]? | An optional array of application bundle identifiers that trigger this mode. |
urlConfigs | [URLConfig]? | An optional array of website URLs that trigger this mode. |
isAIEnhancementEnabled | Bool | Toggles AI enhancement for this mode. |
selectedPrompt | String? | The UUID of the CustomPrompt to use for AI enhancement. |
selectedTranscriptionModelName | String? | The name of the transcription model to use. If nil, the global default is used. |
selectedLanguage | String? | The language for transcription. If nil, the global default is used. |
useScreenCapture | Bool | Enables "Context Awareness" by using screen capture content. |
selectedAIProvider | String? | The AI provider (e.g., "OpenAI") for enhancement. |
selectedAIModel | String? | The specific AI model for enhancement. |
isAutoSendEnabled | Bool | If true, automatically presses the Return/Enter key after pasting text. |
isEnabled | Bool | A flag to enable or disable this configuration from being triggered. |
isDefault | Bool | If true, this mode is used when no other app or website match is found. |
Sources: VoiceInk/PowerMode/PowerModeConfig.swift:4-19, VoiceInk/PowerMode/PowerModeConfigView.swift:200-207, 608-617
Configuration Management
Configuration management is handled by the PowerModeManager singleton, which is responsible for all CRUD operations and persistence.
Persistence
Configurations are stored in UserDefaults under the key powerModeConfigurationsV2. The PowerModeManager loads these configurations at launch and saves them whenever a change is made.
Sources: VoiceInk/PowerMode/PowerModeConfig.swift:124, 134-138
Trigger Matching
The manager provides methods to find a matching configuration based on the current context:
getConfigurationForApp(_ bundleId: String): Iterates through enabled configurations to find one matching the provided application bundle ID.getConfigurationForURL(_ url: String): Cleans the input URL and checks against theurlConfigsof enabled configurations to find a match.
The cleanURL method standardizes URLs by converting them to lowercase and removing prefixes like http://, https://, and www..
Sources: VoiceInk/PowerMode/PowerModeConfig.swift:175-202, 257-263
Session Management
When a Power Mode is activated, PowerModeSessionManager orchestrates the process of applying the new settings and ensuring the original settings are restored later.
Session Flow
The following sequence diagram illustrates the session lifecycle.
This flow ensures that Power Mode settings are temporary and do not permanently alter the user's global preferences.
Sources: VoiceInk/PowerMode/PowerModeSessionManager.swift:35-61, 63-73
State Snapshot (ApplicationState)
Before applying a new configuration, PowerModeSessionManager creates an ApplicationState struct to store the current settings. This snapshot includes:
isEnhancementEnableduseScreenCaptureContextselectedPromptIdselectedAIProviderselectedAIModelselectedLanguagetranscriptionModelName
When endSession() is called, these properties are restored to their original values. The manager also includes a recoverSession() method to restore state from an abandoned session on app launch.
Sources: VoiceInk/PowerMode/PowerModeSessionManager.swift:4-15, 114-142, 161-167
Validation
To maintain data integrity and prevent conflicts, PowerModeValidator checks configurations before they are saved.
Validation Rules
The validateForSave method checks for the following conditions:
| Error Case | Description |
|---|---|
emptyName | The Power Mode name cannot be empty. |
duplicateName | Another Power Mode with the same name already exists. |
duplicateAppTrigger | An application is already assigned as a trigger in another Power Mode. |
duplicateWebsiteTrigger | A website URL is already assigned as a trigger in another Power Mode. |
If any of these validation rules fail, an alert is shown to the user, and the configuration is not saved.
Sources: VoiceInk/PowerMode/PowerModeValidator.swift:5-26, 34-80
User Interface
The user interacts with Power Mode through several SwiftUI views.
- PowerModeView: The main screen for this feature. It lists all created Power Modes, showing their name, emoji, and trigger counts. From here, users can enable/disable modes, initiate editing, add new modes, or enter a "Reorder" mode to change the priority of configurations.
- ConfigurationView: A detailed view for adding or editing a
PowerModeConfig. It is divided into sections:- Triggers: Add applications and websites.
- Transcription: Select a specific transcription model and language.
- AI Enhancement: Enable/disable enhancement, select an AI provider/model, choose a prompt, and toggle "Context Awareness" (
useScreenCapture). - Advanced: Toggle "Auto Send".
- PowerModePopover: A small popover accessible from the UI that allows for manual selection and activation of an enabled Power Mode, overriding the automatic detection.
- PowerModeSettingsSection: A component within the main app settings that provides a global toggle to enable or disable the entire Power Mode feature.
Sources: VoiceInk/PowerMode/PowerModeView.swift:35-212, VoiceInk/PowerMode/PowerModeConfigView.swift:5-763, VoiceInk/PowerMode/PowerModePopover.swift:5-86, VoiceInk/Views/Settings/PowerModeSettingsSection.swift:7-58