From 0c43859784a9e2e4543c5eba55c377680796552c Mon Sep 17 00:00:00 2001 From: Yu Leng Date: Thu, 27 Nov 2025 16:24:33 +0800 Subject: [PATCH] Enhance input source selection in UI Added dynamic visibility binding to the input source button. Updated the `ListView` to bind to `AvailableInputSources` and replaced hardcoded items with a `DataTemplate` for better flexibility. Introduced `InputSourceListView_SelectionChanged` to handle selection changes, update the monitor's input source, and close the flyout after selection. Added logging for improved debugging and error handling. --- .../PowerDisplayXAML/MainWindow.xaml | 30 ++++++++++--- .../PowerDisplayXAML/MainWindow.xaml.cs | 43 +++++++++++++++++++ 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/src/modules/powerdisplay/PowerDisplay/PowerDisplayXAML/MainWindow.xaml b/src/modules/powerdisplay/PowerDisplay/PowerDisplayXAML/MainWindow.xaml index bd81debf9e..c35bdad329 100644 --- a/src/modules/powerdisplay/PowerDisplay/PowerDisplayXAML/MainWindow.xaml +++ b/src/modules/powerdisplay/PowerDisplay/PowerDisplayXAML/MainWindow.xaml @@ -121,11 +121,15 @@ HorizontalAlignment="Right" Content="{ui:FontIcon Glyph=, FontSize=16}" - Style="{StaticResource SubtleButtonStyle}"> + Style="{StaticResource SubtleButtonStyle}" + Visibility="{x:Bind ConvertBoolToVisibility(SupportsInputSource), Mode=OneWay}"> - + - - - + + + + + + + + + + + + diff --git a/src/modules/powerdisplay/PowerDisplay/PowerDisplayXAML/MainWindow.xaml.cs b/src/modules/powerdisplay/PowerDisplay/PowerDisplayXAML/MainWindow.xaml.cs index a601387461..bd866d76f7 100644 --- a/src/modules/powerdisplay/PowerDisplay/PowerDisplayXAML/MainWindow.xaml.cs +++ b/src/modules/powerdisplay/PowerDisplay/PowerDisplayXAML/MainWindow.xaml.cs @@ -738,6 +738,49 @@ namespace PowerDisplay Logger.LogInfo("[UI] InputSourceItem_Click: SetInputSourceAsync completed"); } + /// + /// Input source ListView selection changed handler - switches the monitor input source + /// + private async void InputSourceListView_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + if (sender is not ListView listView) + { + return; + } + + // Get the selected input source item + var selectedItem = listView.SelectedItem as InputSourceItem; + if (selectedItem == null) + { + return; + } + + Logger.LogInfo($"[UI] InputSourceListView_SelectionChanged: Selected {selectedItem.Name} (0x{selectedItem.Value:X2}) for monitor {selectedItem.MonitorId}"); + + // Find the monitor by ID + MonitorViewModel? monitorVm = null; + if (!string.IsNullOrEmpty(selectedItem.MonitorId) && _viewModel != null) + { + monitorVm = _viewModel.Monitors.FirstOrDefault(m => m.Id == selectedItem.MonitorId); + } + + if (monitorVm == null) + { + Logger.LogWarning("[UI] InputSourceListView_SelectionChanged: Could not find MonitorViewModel"); + return; + } + + // Set the input source + await monitorVm.SetInputSourceAsync(selectedItem.Value); + + // Close the flyout after selection + if (listView.Parent is StackPanel stackPanel && + stackPanel.Parent is Flyout flyout) + { + flyout.Hide(); + } + } + public void Dispose() { _viewModel?.Dispose();