Commit Graph

21 Commits

Author SHA1 Message Date
Yu Leng
570cff4590 Remove redundant Logger initialization in App.xaml.cs
Logger initialization is now handled in Program.cs before the App constructor, so the duplicate initialization in App.xaml.cs has been removed. A clarifying comment was added to document this change and improve startup sequence clarity.
2025-12-11 11:04:41 +08:00
Yu Leng
87eb7cc07f Refactor LightSwitch integration: decouple event handling
Removed LightSwitchListener and ThemeChangedEventArgs. Added LightSwitchService to centralize theme/profile logic. Updated event registration and MainViewModel to handle LightSwitch theme changes via service, decoupling event listening from profile application. Event listening now handled externally.
2025-12-11 10:08:51 +08:00
Yu Leng
a0f45c444f Refactor error handling, DPI scaling, and UI events
- Replace custom error UI with Logger.LogError for exceptions
- Remove acrylic backdrop; use semi-transparent black background
- Scale IdentifyWindow size for DPI using GetDpiForWindow
- Remove ValueChanged handlers from sliders; use PointerCaptureLost
- Delete unused event handlers and clean up using directives
- Simplify input source switching logic in MainWindow
2025-12-09 15:28:18 +08:00
Yu Leng
bd9b66afa4 Refactor to use nint for window handles and P/Invoke
Replaces IntPtr with nint for window handles and native API calls in WindowHelper, removing 32/64-bit conditional logic. Cleans up unused minimize/restore methods. Updates TrayIconService constructor usage in App.xaml.cs.
2025-12-09 10:52:53 +08:00
Yu Leng
0655497762 Refactor SettingsUtils and update project ID
Refactored `SettingsUtils` initialization across multiple files
(`App.xaml.cs`, `MainWindow.xaml.cs`, `MainViewModel.cs`) to use
`SettingsUtils.Default` instead of creating new instances. This
improves consistency, reduces redundancy, and promotes better
resource management.

Updated the project ID for `PowerDisplayModuleInterface.vcxproj`
in `PowerToys.slnx` to reflect a configuration or structural
change in the project setup.
2025-12-08 13:37:06 +08:00
Yu Leng
9413b7cc37 Replace regex parser with recursive descent parser
Introduce a new `MccsCapabilitiesParser` to replace the
regex-based `VcpCapabilitiesParser` for parsing MCCS capabilities
strings. The new parser uses a grammar-based recursive descent
approach, improving performance, extensibility, and error handling.

Key changes:
- Implement `MccsCapabilitiesParser` with `ref struct` for zero
  heap allocation and efficient parsing using `ReadOnlySpan<char>`.
- Add sub-parsers for `vcp()` and `vcpname()` segments.
- Accumulate errors in `ParseError` list and return partial results.
- Replace all references to `VcpCapabilitiesParser` with the new
  parser in `DdcCiNative.cs` and `MonitorManager.cs`.
- Enhance `VcpCapabilities` model with `MccsVersion` property.
- Add comprehensive unit tests in `MccsCapabilitiesParserTests.cs`
  to validate real-world examples and edge cases.
- Remove legacy `VcpCapabilitiesParser` and associated code.

Additional improvements:
- Optimize parsing for both space-separated and concatenated hex
  formats.
- Improve logging for parsing progress and error diagnostics.
- Adjust application initialization to prevent race conditions.
2025-12-03 01:32:06 +08:00
moooyo
391f61d4ed Fix some issues 2025-12-01 06:09:26 +08:00
Yu Leng
589aaf6f3e Add PowerDisplay support to PowerToys Settings UI
Updated `OpenSettings` and `OnSettingsClick` methods in the
`PowerDisplay` namespace to handle the deployment structure
of PowerDisplay as a WinUI 3 app in a subfolder. The
`mainExecutableIsOnTheParentFolder` parameter is set to `true`
to reflect this structure.

Added a new case for `PowerDisplay` in the `Microsoft.PowerToys.Settings.UI`
namespace to enable navigation to the `PowerDisplayPage` in the
PowerToys Settings UI.
2025-11-28 00:27:49 +08:00
Yu Leng
7c69874689 Add system tray icon support for PowerDisplay
Introduced a `TrayIconService` to manage the system tray icon, enabling quick access to settings and exit options. Added a new `ShowSystemTrayIcon` setting to control tray icon visibility, with UI integration in the settings page.

Implemented `SettingsDeepLink` to open PowerDisplay settings directly in the PowerToys Settings UI. Updated `App.xaml.cs` to integrate tray icon lifecycle management and refresh behavior.

Replaced `ManagedCsWin32` with `CsWin32` for Windows API interop. Added localized strings for tray menu options and updated default settings to enable the tray icon by default. Improved resilience by handling `WM_TASKBAR_RESTART` for tray icon recreation.
2025-11-27 20:57:54 +08:00
Yu Leng
de44da04de Refactor and enhance monitor control logic
- Consolidated brightness and capabilities retrieval logic with `GetBrightnessInfoCore` and `RetryHelper` for improved modularity and resiliency.
- Introduced `LockedDictionary` for thread-safe dictionary operations, replacing manual locking in `PhysicalMonitorHandleManager` and `MonitorStateManager`.
- Refactored monitor discovery in `MonitorManager` to separate discovery, initialization, and validation steps for better maintainability.
- Simplified event registration in `App.xaml.cs` with helper methods to reduce repetitive code.
- Enhanced VCP code handling with new methods for formatted and sorted VCP code retrieval.
- Added `SuspendNotifications` in `MonitorInfo` to optimize batch property updates and improve UI performance.
- Simplified parameter update methods in `MonitorViewModel` by removing redundant `fromProfile` logic.
- Improved state management with synchronous save support and reusable JSON building logic in `MonitorStateManager`.
- Updated UI bindings in `ProfileEditorDialog.xaml` and improved VCP code display in `MainViewModel`.
- Cleaned up redundant code, improved logging, and standardized method naming for better readability and maintainability.
2025-11-26 05:02:49 +08:00
Yu Leng
580651b47a Refactor PowerDisplay module and add shared library
Introduced `PowerDisplay.Lib` to centralize shared logic and models, improving modularity and reusability. Refactored namespaces, moving classes to `PowerDisplay.Common`. Added utilities like `ColorTemperatureHelper` and `MonitorFeatureHelper` for consistent logic.

Replaced `ProfileManager` with `ProfileService` for centralized profile management. Enhanced event handling, monitor state management, and settings synchronization. Improved color temperature handling and feature detection.

Removed redundant code and converters. Updated `Settings.UI.Library` and XAML bindings to use shared models. Enhanced logging, serialization, and disposal logic. Updated project files and added documentation for better maintainability.
2025-11-24 18:08:11 +08:00
Yu Leng
b8abff02ac Add profile management system to PowerDisplay
Introduced a comprehensive profile management system for PowerDisplay, enabling users to create, edit, delete, and apply predefined monitor settings. Key changes include:

- Added `ProfileManager` for handling profile storage and retrieval.
- Introduced `PowerDisplayProfile`, `PowerDisplayProfiles`, and related data models for profile representation.
- Enhanced `MainViewModel` and `MonitorViewModel` to support profile application and parameter change detection.
- Created `ProfileEditorDialog` for editing and creating profiles via the UI.
- Updated `PowerDisplayViewModel` to manage profiles, including commands for adding, deleting, renaming, and saving profiles.
- Added new events (`ApplyProfileEvent`) and constants for profile application.
- Updated `PowerDisplayPage` UI to include a "Profiles" section for managing profiles.
- Added serialization support for profile-related classes.
- Updated `dllmain.cpp` and `App.xaml.cs` to handle profile-related events.

These changes improve user experience by allowing quick switching between tailored monitor configurations.
2025-11-19 17:18:01 +08:00
Yu Leng
a48e999963 Improve error handling, debouncing, and code cleanup
Enhanced exception handling in RelayCommand to improve robustness.
Standardized slider debounce delays using a new constant
`SliderDebounceDelayMs`. Improved resource management in
SimpleDebouncer with proper disposal of CancellationTokenSource
and added support for synchronous actions. Refactored event
handling in App.xaml.cs for clarity and consistency. Removed
redundant logging in MonitorStateManager and MainViewModel to
reduce verbosity. Updated namespaces and dependencies for better
organization. General code cleanup to improve readability and
maintainability.
2025-11-19 15:26:35 +08:00
Yu Leng
f10c9f49e9 Add color temperature support to PowerDisplay
Enhanced PowerDisplay with support for applying color temperature settings.

- Added `APPLY_COLOR_TEMPERATURE_POWER_DISPLAY_EVENT` and event handling logic.
- Introduced `ApplyColorTemperatureFromSettings` in `MainViewModel` for explicit hardware updates.
- Refactored `MonitorInfo` to dynamically compute and cache color temperature presets.
- Updated `ReloadMonitorsFromSettings` to preserve object references and improve UI responsiveness.
- Simplified UI bindings and removed redundant properties like `MonitorType`.
- Improved event handling in `dllmain.cpp` for the new color temperature action.
- Enhanced logging for better debugging and traceability.
- Updated JSON serialization context to include new types for color temperature.
- Removed unused code and improved documentation for maintainability.
2025-11-18 20:03:36 +08:00
Yu Leng
55cd6c95b8 Fix settings issue 2025-11-17 18:21:46 +08:00
Yu Leng
94bc13e703 Refactor PowerDisplay for dynamic monitor capabilities
Removed reliance on static `MonitorType` enumeration, replacing it with dynamic `CommunicationMethod` for better flexibility. Updated `IMonitorController` and `MonitorManager` to dynamically determine monitor control capabilities.

Refactored `Monitor` model to streamline properties and improve color temperature handling. Enhanced `MonitorViewModel` with unified methods for brightness, contrast, volume, and color temperature updates, improving UI responsiveness and hardware synchronization.

Improved settings handling by adding support for hidden monitors, preserving user preferences, and separating UI configuration from hardware parameter updates. Updated the PowerDisplay Settings UI with warnings, confirmation dialogs, and better VCP capabilities formatting.

Removed legacy IPC code in favor of event-driven settings updates. Conducted general code cleanup, improving logging, error handling, and documentation for maintainability.
2025-11-14 16:45:22 +08:00
Yu Leng
0b7109dee4 Add toggle event for PowerDisplay window visibility
Introduced `TOGGLE_POWER_DISPLAY_EVENT` to enable toggling the
PowerDisplay window's visibility. Updated `App.xaml.cs` to handle
the new event and added the `ToggleWindow` method in
`MainWindow.xaml.cs` to manage window visibility.

Enhanced `MainWindow` with auto-hide functionality when the
window loses focus. Updated `dllmain.cpp` to integrate the
toggle event, including creating, signaling, and cleaning up
the event handle. Replaced `SHOW_POWER_DISPLAY_EVENT` with
`TOGGLE_POWER_DISPLAY_EVENT` for improved functionality.

Improved logging across the codebase for better traceability
and debugging. Performed general refactoring and ensured proper
resource management for event handles.
2025-11-13 12:56:12 +08:00
Yu Leng
ac9fd27095 Refactor PowerDisplay to use Windows Named Events
Replaced IPC-based communication with Windows Named Events for
simpler and more reliable process interaction. Introduced the
`NativeEventWaiter` helper class to handle event signaling and
callbacks. Removed the `PowerDisplayProcessManager` class and
refactored process lifecycle management to use direct process
launching and event signaling.

Simplified `App.xaml.cs` by removing IPC logic and adding event-
based handling for window visibility, monitor refresh, settings
updates, and termination. Enhanced `MainWindow` initialization
and show logic with detailed logging and error handling.

Updated `dllmain.cpp` to manage persistent event handles and
refactored the `enable` and `disable` methods to use event-based
communication. Improved process termination logic with additional
checks and logging.

Performed general cleanup, including removing unused code,
improving readability, and enhancing error handling throughout
the codebase.
2025-11-13 12:40:56 +08:00
Yu Leng
5d63ca7a9c Refactor IPC and enhance PowerDisplay functionality
Refactored IPC communication by introducing a `NamedPipeProcessor` utility for unidirectional named pipe handling, replacing the old bidirectional implementation. Simplified application lifecycle management by removing mutex usage and relying on `AppInstance` for single-instance enforcement.

Replaced IPC-based monitor updates with file-based updates, saving monitor data to `settings.json` for the Settings UI. Added an `ActivationShortcut` property to PowerDisplay settings and updated the settings page UI to support shortcut configuration.

Simplified named pipe creation in `PowerDisplayProcessManager` by removing bidirectional pipe logic and focusing on unidirectional communication. Improved `MainWindow` initialization with an `EnsureInitializedAsync` method.

Updated localization resources and integrated PowerDisplay into the launcher menu. Removed redundant code, improved logging, and streamlined resource cleanup for better maintainability.
2025-11-12 16:36:43 +08:00
Yu Leng
e90c4273f7 Refactor PowerDisplay IPC and add hotkey support
Refactored IPC initialization to handle window visibility based on
launch mode (standalone or IPC). Added `IsWindowVisible` P/Invoke
method and implemented IPC commands for window control, monitor
refresh, and settings updates.

Fixed bidirectional pipe creation and adjusted process startup
order in `PowerDisplayProcessManager`. Made `ShowWindow` and
`HideWindow` methods public and added `IsWindowVisible` to
`MainWindow.xaml.cs`.

Introduced activation hotkey parsing and configuration with a
default of `Win+Alt+M`. Exposed hotkey to PowerToys runner and
integrated it into the dashboard with localization and a launch
button. Renamed module DLL for consistency.
2025-11-12 13:18:36 +08:00
Yu Leng
e2774eff2d Introduce PowerDisplay 2025-10-20 16:22:47 +08:00