mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
5.3 KiB
5.3 KiB
Flow.Launcher.Infrastructure/
The Infrastructure project provides the foundational services, utilities, and shared models used across the Flow.Launcher ecosystem. It serves as a bridge between high-level application logic and low-level system interactions.
Responsibility
- Persistence & Storage: Manages serialization and deserialization of application data and settings using robust JSON/Binary storage mechanisms.
- Fuzzy Matching: Implements the core search logic, including fuzzy string matching, acronym detection, and multi-language support (e.g., Pinyin).
- Global Hotkey Management: Intercepts and processes system-wide keyboard events using low-level Windows hooks.
- Configuration: Provides a centralized, reactive
Settingsmodel that drives the application's behavior and appearance. - Logging: Offers a unified logging interface wrapping NLog for error reporting and debugging.
- System Integration: Wraps complex Win32 API calls and provides helpers for file system operations, image processing, and environment management.
Design Patterns
- Persistence Framework (Generic Storage):
JsonStorage<T>uses a generic approach to provide consistent serialization logic, including atomic writes and backup recovery. - Reactive Model: The
Settingsclass uses the MVVM pattern (viaCommunityToolkit.Mvvm) to notify the UI of configuration changes. - Static Service/Singleton: Core utilities like
Log,DataLocation, andWin32Helperare implemented as static services for global accessibility. - Dependency Injection: Services like
StringMatcherare registered and resolved viaCommunityToolkit.Mvvm.DependencyInjectionto decouple components. - Template Method:
JsonStorage<T>defines the save/load algorithm, while derived classes likeFlowLauncherJsonStorage<T>specialize the storage location.
Key Classes
Configuration & Data
UserSettings.Settings: The primary configuration model. It implementsIHotkeySettingsand holds hundreds of user-configurable properties. It is reactive, triggering UI updates immediately upon property change.UserSettings.DataLocation: Encapsulates the logic for determining where application data is stored (Roaming vs. Portable mode).Constant: Centralized repository for application-wide constants, paths, and URLs.
Search Engine
StringMatcher: The heart of Flow's search capability. It combines acronym matching and fuzzy substring matching. It supports search precision tuning and leveragesIAlphabetfor localized character translation.MatchResult: A model representing the outcome of a search match, containing the score and the indices of matched characters for UI highlighting.
Storage Systems
Storage.JsonStorage<T>: A robust JSON storage implementation featuring:- Atomic Writes: Uses
.tmpfiles andFile.Replaceto prevent data corruption during crashes. - Backup System: Automatically maintains
.bakfiles and timestamped backups if loading fails.
- Atomic Writes: Uses
Storage.FlowLauncherJsonStorage<T>: A specialization ofJsonStoragethat automatically routes files to the appropriateUserDatadirectory.
Logging & Diagnostics
Logger.Log: A static wrapper for NLog. It supports asynchronous file logging and debug string output. It includes aDemystifystep for exception stacks to make them more readable.Stopwatch: High-resolution timing utility for performance profiling.
Input & System
Hotkey.GlobalHotkey: Manages low-levelWH_KEYBOARD_LLhooks to intercept hotkeys before they reach other applications.Win32Helper: Provides a clean C# interface for complex P/Invoke operations, including window management, shell execution, and system font retrieval.
Data & Control Flow
Search & Result Flow
- Input: A query string enters from the UI.
- Preprocessing:
StringMatchertrims the query and optionally translates it viaPinyinAlphabetor otherIAlphabetimplementations. - Matching:
- Acronym Match: Checks if query characters match the starts of words or uppercase letters.
- Fuzzy Match: Performs substring matching and character distance calculation.
- Scoring: A score is calculated based on match length, position, and closeness of characters.
- Output: A
MatchResultis returned to thePluginManagerto be ranked against other results.
Settings Lifecycle
- Loading: On startup,
FlowLauncherJsonStorage<Settings>readsSettings.json. If corrupt, it attempts to restore fromSettings.json.bak. - Injection: The
Settingsinstance is registered in theIoccontainer. - Modification: UI components bind to
Settingsproperties. Changes triggerOnPropertyChanged. - Persistence: The
Save()method is called (often triggered by application exit or manual save), executing an atomic write to disk.
Integration Points
- NLog: Backend for the
Logservice. - CommunityToolkit.Mvvm: Used for the
Ioccontainer andObservableObjectbase classes. - CsWin32 / PInvoke: Deep integration with Windows APIs for hotkeys, file explorer interaction, and UI rendering hints.
- Flow.Launcher.Plugin: Infrastructure provides the concrete implementations for many interfaces defined in the Plugin SDK.
- System.Text.Json: The primary serialization engine for all storage components.