From 51906e68a466ce8b2d20b2df13c1d54b8ca0f006 Mon Sep 17 00:00:00 2001 From: Davide Giacometti Date: Fri, 15 Sep 2023 08:23:20 +0200 Subject: [PATCH] [Hosts]Fix content dialogs overlapping title bar (#28547) * fix content dialogs overlapping title bar * fix spellcheck --- .github/actions/spell-check/expect.txt | 1 + .../Hosts/Hosts/Helpers/VisualTreeUtils.cs | 67 +++++++++++++++++++ .../Hosts/Hosts/HostsXAML/Views/MainPage.xaml | 2 + .../Hosts/HostsXAML/Views/MainPage.xaml.cs | 16 +++++ 4 files changed, 86 insertions(+) create mode 100644 src/modules/Hosts/Hosts/Helpers/VisualTreeUtils.cs diff --git a/.github/actions/spell-check/expect.txt b/.github/actions/spell-check/expect.txt index c7c72631cc..7ecf3fb779 100644 --- a/.github/actions/spell-check/expect.txt +++ b/.github/actions/spell-check/expect.txt @@ -1919,6 +1919,7 @@ textblock TEXTEXTRACTOR TEXTINCLUDE tgz +themeresources THH THICKFRAME THISCOMPONENT diff --git a/src/modules/Hosts/Hosts/Helpers/VisualTreeUtils.cs b/src/modules/Hosts/Hosts/Helpers/VisualTreeUtils.cs new file mode 100644 index 0000000000..4be55866d9 --- /dev/null +++ b/src/modules/Hosts/Hosts/Helpers/VisualTreeUtils.cs @@ -0,0 +1,67 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Media; + +namespace Hosts.Helpers +{ + // Taken from https://github.com/microsoft/microsoft-ui-xaml/blob/main/test/MUXControlsTestApp/Utilities/VisualTreeUtils.cs + // Original copyright header: + // Copyright (c) Microsoft Corporation. All rights reserved. + // Licensed under the MIT License. See LICENSE in the project root for license information. + public static class VisualTreeUtils + { + public static T FindVisualChildByType(this DependencyObject element) + where T : DependencyObject + { + if (element == null) + { + return null; + } + + if (element is T elementAsT) + { + return elementAsT; + } + + int childrenCount = VisualTreeHelper.GetChildrenCount(element); + for (int i = 0; i < childrenCount; i++) + { + var result = VisualTreeHelper.GetChild(element, i).FindVisualChildByType(); + if (result != null) + { + return result; + } + } + + return null; + } + + public static FrameworkElement FindVisualChildByName(this DependencyObject element, string name) + { + if (element == null || string.IsNullOrWhiteSpace(name)) + { + return null; + } + + if (element is FrameworkElement elementAsFE && elementAsFE.Name == name) + { + return elementAsFE; + } + + int childrenCount = VisualTreeHelper.GetChildrenCount(element); + for (int i = 0; i < childrenCount; i++) + { + var result = VisualTreeHelper.GetChild(element, i).FindVisualChildByName(name); + if (result != null) + { + return result; + } + } + + return null; + } + } +} diff --git a/src/modules/Hosts/Hosts/HostsXAML/Views/MainPage.xaml b/src/modules/Hosts/Hosts/HostsXAML/Views/MainPage.xaml index 2474c56412..e889c0381e 100644 --- a/src/modules/Hosts/Hosts/HostsXAML/Views/MainPage.xaml +++ b/src/modules/Hosts/Hosts/HostsXAML/Views/MainPage.xaml @@ -397,6 +397,7 @@ @@ -449,6 +450,7 @@ diff --git a/src/modules/Hosts/Hosts/HostsXAML/Views/MainPage.xaml.cs b/src/modules/Hosts/Hosts/HostsXAML/Views/MainPage.xaml.cs index b5bc005a97..c24560ab02 100644 --- a/src/modules/Hosts/Hosts/HostsXAML/Views/MainPage.xaml.cs +++ b/src/modules/Hosts/Hosts/HostsXAML/Views/MainPage.xaml.cs @@ -6,9 +6,11 @@ using System; using System.Threading.Tasks; using System.Windows.Input; using CommunityToolkit.Mvvm.Input; +using Hosts.Helpers; using Hosts.Models; using Hosts.Settings; using Hosts.ViewModels; +using ManagedCommon; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Input; @@ -186,5 +188,19 @@ namespace Hosts.Views var entry = (e.OriginalSource as FrameworkElement).DataContext as Entry; ViewModel.Selected = entry; } + + private void ContentDialog_Loaded_ApplyMargin(object sender, RoutedEventArgs e) + { + try + { + // Based on the template from dev/CommonStyles/ContentDialog_themeresources.xaml in https://github.com/microsoft/microsoft-ui-xaml + var border = VisualTreeUtils.FindVisualChildByName(sender as ContentDialog, "BackgroundElement") as Border; + border.Margin = new Thickness(0, 32, 0, 0); // Should be the size reserved for the title bar as in MainWindow.xaml + } + catch (Exception ex) + { + Logger.LogError("Couldn't set the margin for a content dialog. It will appear on top of the title bar.", ex); + } + } } }