Commit Graph

12 Commits

Author SHA1 Message Date
Yu Leng
4817709fda Unify monitor identification using stable Id property
Refactor monitor matching and persistence to use a single, stable Id (format: "{Source}_{EdidId}_{MonitorNumber}") across all components. Remove HardwareId and DeviceKey properties from Monitor, update ProfileMonitorSetting to use MonitorId, and simplify MonitorMatchingHelper logic. Update DDC/CI, WMI, ViewModels, Settings UI, and unit tests to rely on Id for all lookups, state management, and handle mapping. Improves reliability for multi-monitor setups and simplifies codebase by removing legacy fallback logic.
2025-12-10 13:34:36 +08:00
Yu Leng
db15380fcf Improve WMI monitor name extraction using length property
Refactor GetUserFriendlyName to use UserFriendlyNameLength for accurate string extraction from the WMI UserFriendlyName buffer. This ensures only valid characters are included, improving reliability when parsing monitor names.
2025-12-10 10:15:40 +08:00
Yu Leng
fc0ae601a6 Refactor monitor matching logic
Replaced legacy parsing and matching methods with a streamlined approach using `QueryDisplayConfig` and `MonitorDisplayInfo` to align monitor numbering with Windows Display Settings' "Identify" feature. Removed redundant methods and tests for parsing display numbers and device paths. Introduced a `MonitorNumber` property in `MonitorDisplayInfo` for consistent numbering. Updated `MonitorDiscoveryHelper` and `WmiController` to use the new logic. Enhanced logging for better debugging and maintainability.
2025-12-04 19:12:49 +08:00
Yu Leng
d9584de585 Fix some WMI issues 2025-12-03 06:24:20 +08:00
Yu Leng
0bbfc8015a Add GPO rule and profile management for PowerDisplay
Introduced a new GPO rule to manage PowerDisplay's enabled state via Group Policy, including updates to `GPOWrapper` and policy files (`PowerToys.admx` and `PowerToys.adml`).

Enhanced the PowerDisplay UI with profile management features, including quick apply, add, edit, and delete functionality. Updated `MainWindow.xaml` and `PowerDisplayPage.xaml` to support these changes, and added localized strings for improved accessibility.

Refactored `MainViewModel` to include a `Profiles` collection, `HasProfiles` property, and `ApplyProfileCommand`. Added methods to load profiles from disk and signal updates to PowerDisplay.

Improved error handling in `DdcCiController` and `WmiController` with input validation and WMI error classification. Optimized handle cleanup in `PhysicalMonitorHandleManager` with a more efficient algorithm.

Refactored `dllmain.cpp` to prevent duplicate PowerDisplay process launches. Updated initialization logic in `MainWindow.xaml.cs` to ensure proper ViewModel setup.

Localized strings for tooltips, warnings, and dialogs. Improved async behavior, logging, and UI accessibility.
2025-12-01 04:32:29 +08:00
Yu Leng
12916deca0 Refactor: Simplify codebase and remove unused methods
Removed unused methods across multiple files, including `GetContrastAsync`, `GetVolumeAsync`, and `SaveCurrentSettingsAsync` in `DdcCiController.cs` and `WmiController.cs`. Simplified the `IMonitorController` interface by removing redundant methods.

Replaced `ProcessThemeChangeAsync` with a synchronous `ProcessThemeChange` in `LightSwitchListener.cs`. Changed `ParseVcpCodesToIntegers` visibility to private in `MonitorFeatureHelper.cs` and removed related helper methods.

Eliminated retry logic by removing `ExecuteWithRetryAsync` in `RetryHelper.cs`. Simplified `SetBrightnessAsync` in `MonitorManager.cs` using a generic helper. Streamlined `DisplayName` in `MonitorViewModel.cs` and removed unnecessary property change notifications.

These changes reduce complexity, improve maintainability, and streamline the codebase by removing unused or redundant functionality.
2025-11-27 22:43:28 +08:00
Yu Leng
9b86aef4b3 Fix WMI parameter type mismatch in WmiSetBrightness
Updated the `WmiSetBrightness` method to pass `Timeout` and
`Brightness` parameters as strings instead of numeric types
to ensure compatibility with WMI driver implementations
that require string values. Updated comments to reflect
this change and clarify the reasoning behind it.
2025-11-27 18:14:14 +08:00
Yu Leng
59d0ac58aa Fix WMI Brightness type mismatch issue
Updated the `Brightness` parameter in the `WmiSetBrightness` method to use `int` instead of casting to `byte`, addressing potential `WBEM_E_TYPE_MISMATCH` errors (0x80041005) caused by certain WMI driver implementations expecting `VT_I4` instead of `VT_UI1`.

Added comments to clarify the rationale for this change. Applied the fix in two sections of `WmiController.cs`, including both dynamic and hardcoded `Brightness` values.
2025-11-27 18:08:49 +08:00
Yu Leng
be23f2d7fd Add input source management via VCP code 0x60
Introduced functionality to get and set monitor input sources using VESA MCCS VCP code 0x60. This includes backend logic, UI integration, and error handling.

Backend changes:
- Added `GetInputSourceAsync` and `SetInputSourceAsync` in `DdcCiController.cs` for input source management.
- Defined `VcpCodeInputSource` constant in `NativeConstants.cs`.
- Updated `Monitor.cs` to include `CurrentInputSource`, `InputSourceName`, and support detection for input sources.
- Enhanced `MonitorManager.cs` to initialize and manage input source capabilities.

UI changes:
- Added a "More Actions" button in `MainWindow.xaml` for input source switching.
- Implemented a flyout menu to display and select available input sources.
- Added `InputSourceItem_Click` handler in `MainWindow.xaml.cs`.

ViewModel changes:
- Introduced `InputSourceItem` class for UI representation of input sources.
- Updated `MonitorViewModel.cs` to expose input source properties and handle switching.

Other improvements:
- Added detailed logging for input source operations.
- Implemented fallback mechanisms for robust behavior.
- Enhanced user experience with dynamic UI updates for input source changes.
2025-11-27 14:51:31 +08:00
Yu Leng
3598c2c126 Refactor and enhance monitor matching logic
- Renamed project entries in `PowerToys.sln` for consistency.
- Added new projects: "runner," "NewShellExtensionContextMenu," and "BgcodePreviewHandlerCpp."
- Introduced `MonitorMatchingHelper` to centralize monitor identification logic.
- Added unit tests for `MonitorMatchingHelper` to validate parsing and matching.
- Updated `MonitorInfo` with `MonitorNumber`, `TotalMonitorCount`, and `DisplayName` for dynamic formatting.
- Enhanced WMI monitor matching with pre-fetched display devices.
- Updated UI components to use dynamic `DisplayName` for monitors.
- Added `PowerDisplay.Lib.UnitTests` project for testing.
- Improved serialization, logging, and null handling in `PowerDisplayViewModel`.
- Removed redundant parsing logic from `MonitorDiscoveryHelper`.
2025-11-26 19:08:01 +08:00
Yu Leng
65af62e77a Add monitor number and dynamic display name handling
Enhanced multi-monitor support by introducing a `MonitorNumber` property in the `Monitor` model to represent the Windows DISPLAY number. Updated the `MonitorViewModel` to include a `DisplayName` property that appends the monitor number to the name when multiple monitors are visible.

Modified `MainWindow.xaml` to bind to `DisplayName` instead of `Name`, ensuring the UI reflects the updated naming convention. Added logic to update `DisplayName` dynamically when the monitor count changes, improving clarity in multi-monitor setups.

Included comments in `MonitorViewModel` for better code readability and maintainability.
2025-11-26 14:50:57 +08:00
Yu Leng
15746e8f45 Refactor and enhance monitor management system
Refactored namespaces to improve modularity, including moving `PowerDisplay.Native` to `PowerDisplay.Common.Drivers`. Introduced the `IMonitorData` interface for better abstraction of monitor hardware data. Replaced `ColorTemperature` with `ColorTemperatureVcp` for precise VCP-based color temperature control, adding utilities for Kelvin conversion.

Enhanced monitor state management with a new `MonitorStateFile` for JSON persistence and updated `MonitorStateManager` for debounced saves. Added `MonitorMatchingHelper` for consistent monitor identification and `ProfileHelper` for profile management operations.

Refactored P/Invoke declarations into helper classes, updated UI bindings for `ColorTemperatureVcp`, and improved logging for better runtime visibility. Removed redundant code, added new utility classes (`MonitorValueConverter`, `MonitorMatchingHelper`), and ensured backward compatibility.

These changes improve code organization, maintainability, and extensibility while aligning with hardware-level control standards.
2025-11-24 21:58:34 +08:00