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.
This commit is contained in:
Yu Leng
2025-11-27 16:24:33 +08:00
parent 8bdd2ffdfd
commit 0c43859784
2 changed files with 68 additions and 5 deletions

View File

@@ -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}">
<Button.Flyout>
<Flyout>
<StackPanel Orientation="Vertical">
<ListView SelectedIndex="2">
<ListView
ItemsSource="{x:Bind AvailableInputSources, Mode=OneWay}"
SelectionChanged="InputSourceListView_SelectionChanged"
SelectionMode="Single">
<ListView.Header>
<TextBlock
Margin="16,0,8,0"
@@ -133,9 +137,25 @@
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="Input source" />
</ListView.Header>
<ListViewItem Content="Source 1" />
<ListViewItem Content="Source 2" />
<ListViewItem Content="Source 3" />
<ListView.ItemTemplate>
<DataTemplate x:DataType="vm:InputSourceItem">
<Grid Padding="0,4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock
VerticalAlignment="Center"
Text="{x:Bind Name}" />
<FontIcon
Grid.Column="1"
Margin="8,0,0,0"
FontSize="12"
Glyph="&#xE73E;"
Visibility="{x:Bind SelectionVisibility}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</Flyout>

View File

@@ -738,6 +738,49 @@ namespace PowerDisplay
Logger.LogInfo("[UI] InputSourceItem_Click: SetInputSourceAsync completed");
}
/// <summary>
/// Input source ListView selection changed handler - switches the monitor input source
/// </summary>
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();