Skip to content

Fixes various issues in WPF Gallery in HC mode #699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sample Applications/WPFGallery/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
</Style.Triggers>
</Style>
</Window.Resources>
<Border x:Name="HighContrastBorder" BorderBrush="Transparent" BorderThickness="8 2 8 8">
<Border x:Name="HighContrastBorder" BorderBrush="Transparent" BorderThickness="8 1 8 8">
<Grid x:Name="MainGrid">
<Grid.RowDefinitions>
<RowDefinition Height="44" />
Expand Down
138 changes: 74 additions & 64 deletions Sample Applications/WPFGallery/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public MainWindow(MainWindowViewModel viewModel, IServiceProvider serviceProvide
InitializeComponent();

UpdateWindowBackground();
UpdateTitleBarButtonsVisibility();
UpdateMainWindowVisuals();

_navigationService = navigationService;
_navigationService.Navigating += OnNavigating;
Expand All @@ -37,22 +37,24 @@ public MainWindow(MainWindowViewModel viewModel, IServiceProvider serviceProvide
new WindowChrome
{
CaptionHeight = 50,
CornerRadius = default,
CornerRadius = new CornerRadius(12),
GlassFrameThickness = new Thickness(-1),
ResizeBorderThickness = ResizeMode == ResizeMode.NoResize ? default : new Thickness(4),
UseAeroCaptionButtons = true,
NonClientFrameEdges = NonClientFrameEdges.Right | NonClientFrameEdges.Bottom | NonClientFrameEdges.Left
NonClientFrameEdges = SystemParameters.HighContrast ? NonClientFrameEdges.None :
NonClientFrameEdges.Right | NonClientFrameEdges.Bottom | NonClientFrameEdges.Left
}
);

SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
this.StateChanged += MainWindow_StateChanged;
this.StateChanged += (s, e) => UpdateMainWindowVisuals();
this.Activated += (s, e) => UpdateMainWindowVisuals();
this.Deactivated += (s, e) => UpdateMainWindowVisuals();
}

private void UpdateWindowBackground()
{
if((!Utility.IsBackdropDisabled() &&
!Utility.IsBackdropSupported()))
if((!Utility.IsBackdropDisabled() && !Utility.IsBackdropSupported()))
{
this.SetResourceReference(BackgroundProperty, "WindowBackground");
}
Expand All @@ -62,40 +64,15 @@ private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceCha
{
Dispatcher.Invoke(() =>
{
UpdateTitleBarButtonsVisibility();
UpdateMainWindowVisuals();
});
}

private void MainWindow_StateChanged(object? sender, EventArgs e)
{
if (this.WindowState == WindowState.Maximized)
{
MainGrid.Margin = new Thickness(8);
}
else
{
MainGrid.Margin = default;
}
}

private readonly IServiceProvider _serviceProvider;
private readonly INavigationService _navigationService;

public MainWindowViewModel ViewModel { get; }

private void ControlsList_SelectedItemChanged()
{
if (ControlsList.SelectedItem is ControlInfoDataItem navItem)
{
_navigationService.Navigate(navItem.PageType);
var tvi = ControlsList.ItemContainerGenerator.ContainerFromItem(navItem) as TreeViewItem;
if(tvi != null)
{
tvi.BringIntoView();
}
}
}

private void UpdateTitleBarButtonsVisibility()
{
if (Utility.IsBackdropDisabled() || !Utility.IsBackdropSupported() ||
Expand All @@ -104,26 +81,47 @@ private void UpdateTitleBarButtonsVisibility()
MinimizeButton.Visibility = Visibility.Visible;
MaximizeButton.Visibility = Visibility.Visible;
CloseButton.Visibility = Visibility.Visible;

if(SystemParameters.HighContrast == true)
{
HighContrastBorder.SetResourceReference(BorderBrushProperty, SystemColors.ActiveCaptionBrushKey);
HighContrastBorder.BorderThickness = new Thickness(8, 2, 8, 8);
}
else
{
HighContrastBorder.BorderBrush = Brushes.Transparent;
HighContrastBorder.BorderThickness = new Thickness(0);
}
}
else
{
MinimizeButton.Visibility = Visibility.Collapsed;
MaximizeButton.Visibility = Visibility.Collapsed;
CloseButton.Visibility = Visibility.Collapsed;
}
}

HighContrastBorder.BorderThickness = new Thickness(0);
private void UpdateMainWindowVisuals()
{
MainGrid.Margin = default;
if(WindowState == WindowState.Maximized)
{
MainGrid.Margin = SystemParameters.HighContrast ? new Thickness(0,8,0,0) : new Thickness(8);
}

UpdateTitleBarButtonsVisibility();

if(SystemParameters.HighContrast == true)
{
HighContrastBorder.SetResourceReference(BorderBrushProperty, IsActive ? SystemColors.ActiveCaptionBrushKey :
SystemColors.InactiveCaptionBrushKey);
HighContrastBorder.BorderThickness = new Thickness(8, 1, 8, 8);

WindowChrome wc = WindowChrome.GetWindowChrome(this);
if(wc is not null)
{
wc.NonClientFrameEdges = NonClientFrameEdges.None;
}
}
else
{
HighContrastBorder.BorderBrush = Brushes.Transparent;
HighContrastBorder.BorderThickness = new Thickness(0);

var wc = WindowChrome.GetWindowChrome(this);
if(wc is not null)
{
wc.NonClientFrameEdges = NonClientFrameEdges.Right | NonClientFrameEdges.Bottom | NonClientFrameEdges.Left;
}
}
}

Expand Down Expand Up @@ -195,15 +193,6 @@ private void RootContentFrame_Navigated(object sender, NavigationEventArgs e)
ViewModel.UpdateCanNavigateBack();
}

private void SelectedItemChanged(TreeViewItem? tvi)
{
ControlsList_SelectedItemChanged();
if (tvi != null)
{
tvi.IsExpanded = !tvi.IsExpanded;
}
}

private void ControlsList_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
Expand All @@ -221,17 +210,6 @@ private void ControlsList_PreviewMouseLeftButtonUp(object sender, MouseButtonEve
SelectedItemChanged(ControlsList.ItemContainerGenerator.ContainerFromItem((sender as TreeView).SelectedItem) as TreeViewItem);
}

private void SettingsButton_Click(object sender, RoutedEventArgs e)
{
AutomationPeer peer = UIElementAutomationPeer.CreatePeerForElement((Button)sender);
peer.RaiseNotificationEvent(
AutomationNotificationKind.Other,
AutomationNotificationProcessing.ImportantMostRecent,
"Settings Page Opened",
"ButtonClickedActivity"
);
}

private void ControlsList_Loaded(object sender, RoutedEventArgs e)
{
if (ControlsList.Items.Count > 0)
Expand All @@ -244,4 +222,36 @@ private void ControlsList_Loaded(object sender, RoutedEventArgs e)
}
}

private void SelectedItemChanged(TreeViewItem? tvi)
{
ControlsList_SelectedItemChanged();
if (tvi != null)
{
tvi.IsExpanded = !tvi.IsExpanded;
}
}

private void ControlsList_SelectedItemChanged()
{
if (ControlsList.SelectedItem is ControlInfoDataItem navItem)
{
_navigationService.Navigate(navItem.PageType);
var tvi = ControlsList.ItemContainerGenerator.ContainerFromItem(navItem) as TreeViewItem;
if(tvi != null)
{
tvi.BringIntoView();
}
}
}

private void SettingsButton_Click(object sender, RoutedEventArgs e)
{
AutomationPeer peer = UIElementAutomationPeer.CreatePeerForElement((Button)sender);
peer.RaiseNotificationEvent(
AutomationNotificationKind.Other,
AutomationNotificationProcessing.ImportantMostRecent,
"Settings Page Opened",
"ButtonClickedActivity"
);
}
}
Loading