A Swiss Army knife for developers.
C# blazorcross-platformcsharpdesktopdesktop-appdesktop-applicationdeveloper-toolsextensiblefluentfluent-designlinuxmacosmicasyntax-highlightingtypescriptwindowswindows-10windows-11winui
WASDK app now supports custom app title bar.
Etienne Baudoux2cff4697223c
diff --git a/src/app/dev/DevToys.UI.Framework/Controls/BackdropWindow/BackdropWindow.Windows.cs b/src/app/dev/DevToys.UI.Framework/Controls/BackdropWindow/BackdropWindow.Windows.csindex 42a1a2f68..d6d8ef56d 100644--- a/src/app/dev/DevToys.UI.Framework/Controls/BackdropWindow/BackdropWindow.Windows.cs+++ b/src/app/dev/DevToys.UI.Framework/Controls/BackdropWindow/BackdropWindow.Windows.cs@@ -6,11 +6,13 @@ using Microsoft.UI.Composition.SystemBackdrops;using Microsoft.UI.Dispatching;using Microsoft.UI.Windowing;using Microsoft.UI.Xaml;+using Windows.Foundation;using Windows.Graphics;-using WinRT;-using WinRT.Interop;using Windows.Win32.Graphics.Gdi;+using Windows.Win32.UI.HiDpi;using Windows.Win32.UI.Shell.Common;+using WinRT;+using WinRT.Interop;using static Windows.Win32.PInvoke;namespace DevToys.UI.Framework.Controls;@@ -53,8 +55,14 @@ public sealed partial class BackdropWindow : Window{DispatcherQueue.ThrowIfNotOnUIThread();+ if (Content is FrameworkElement frameworkElement)+ {+ frameworkElement.Loaded += Window_Loaded;+ }+SetStartupWindowLocation();SetBackdrop();+ ExtendTitleBar();AppWindow.Closing += Window_Closing;base.Activate();@@ -103,6 +111,37 @@ public sealed partial class BackdropWindow : Window}}+ private void ExtendTitleBar()+ {+ // Check to see if customization is supported.+ // Currently only supported on Windows 11.+ if (AppWindowTitleBar.IsCustomizationSupported())+ {+ AppWindowTitleBar titleBar = AppWindow.TitleBar;+ titleBar.ExtendsContentIntoTitleBar = true;++ // Set active window colors+ titleBar.ButtonBackgroundColor = Colors.Transparent;++ // Set inactive window colors+ titleBar.ButtonInactiveBackgroundColor = Colors.Transparent;+ }+ else+ {+ // Title bar customization using these APIs is currently+ // supported only on Windows 11. In other cases, hide+ // the custom title bar element.++ // Show alternative UI for any functionality in+ // the title bar, such as search.+ }+ }++ private void DraggableArea_SizeChanged(object sender, SizeChangedEventArgs e)+ {+ SetDragRegionForCustomTitleBar();+ }+private void SetBackdrop(){if (_micaController != null)@@ -168,6 +207,66 @@ public sealed partial class BackdropWindow : Window}}+ private void SetDragRegionForCustomTitleBar()+ {+ // Check to see if customization is supported.+ // Currently only supported on Windows 11.+ if (AppWindowTitleBar.IsCustomizationSupported()+ && AppWindow.TitleBar.ExtendsContentIntoTitleBar)+ {+ var defaultPoint = new Point(0, 0);+ double scaleAdjustment = GetScaleAdjustment();++ var dragRectsList = new List<RectInt32>();++ for (int i = 0; i < _draggableAreas.Count; i++)+ {+ FrameworkElement draggableArea = _draggableAreas[i];+ Point draggableAreaLocationInWindow+ = draggableArea+ .TransformToVisual(this.Content)+ .TransformPoint(defaultPoint);++ RectInt32 dragRectangle;+ dragRectangle.X = (int)(draggableAreaLocationInWindow.X * scaleAdjustment);+ dragRectangle.Y = (int)(draggableAreaLocationInWindow.Y * scaleAdjustment);+ dragRectangle.Height = (int)(draggableArea.ActualHeight * scaleAdjustment);+ dragRectangle.Width = (int)(draggableArea.ActualWidth * scaleAdjustment);+ dragRectsList.Add(dragRectangle);+ }++ AppWindow.TitleBar.SetDragRectangles(dragRectsList.ToArray());+ }+ }++ private double GetScaleAdjustment()+ {+ IntPtr hWnd = GetWindowHandle();+ WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);+ var displayArea = DisplayArea.GetFromWindowId(wndId, DisplayAreaFallback.Primary);+ IntPtr hMonitor = Win32Interop.GetMonitorFromDisplayId(displayArea.DisplayId);++ // Get DPI.+ int result = GetDpiForMonitor(new HMONITOR(hMonitor), MONITOR_DPI_TYPE.MDT_DEFAULT, out uint dpiX, out uint _);+ if (result != 0)+ {+ throw new Exception("Could not get DPI for monitor.");+ }++ uint scaleFactorPercent = (uint)(((long)dpiX * 100 + (96 >> 1)) / 96);+ return scaleFactorPercent / 100.0;+ }++ private void Window_Loaded(object sender, RoutedEventArgs e)+ {+ for (int i = 0; i < _draggableAreas.Count; i++)+ {+ _draggableAreas[i].SizeChanged += DraggableArea_SizeChanged;+ }++ SetDragRegionForCustomTitleBar();+ }+private void Window_Activated(object sender, WindowActivatedEventArgs args){if (_configurationSource is not null)@@ -200,6 +299,11 @@ public sealed partial class BackdropWindow : WindowActivated -= Window_Activated;_configurationSource = null;++ if (Content is FrameworkElement frameworkElement)+ {+ frameworkElement.Loaded -= Window_Loaded;+ }}private class WindowsSystemDispatcherQueueHelperdiff --git a/src/app/dev/DevToys.UI.Framework/Controls/BackdropWindow/BackdropWindow.cs b/src/app/dev/DevToys.UI.Framework/Controls/BackdropWindow/BackdropWindow.csindex 8ea2de63f..74e0dfac8 100644--- a/src/app/dev/DevToys.UI.Framework/Controls/BackdropWindow/BackdropWindow.cs+++ b/src/app/dev/DevToys.UI.Framework/Controls/BackdropWindow/BackdropWindow.cs@@ -1,9 +1,57 @@-using Windows.Foundation;+using DevToys.UI.Framework.Helpers;+using Microsoft.UI.Xaml;+using Windows.Foundation;namespace DevToys.UI.Framework.Controls;public sealed partial class BackdropWindow{+ private readonly List<FrameworkElement> _draggableAreas = new();++ #region IsWindowDraggableArea++ /// <summary>+ /// Attached property that can be used to set one or multiple controls as area that the user can user to move the window using the mouse.+ /// </summary>+ public static readonly DependencyProperty IsWindowDraggableAreaProperty+ = DependencyProperty.RegisterAttached(+ "IsWindowDraggableArea",+ typeof(bool),+ typeof(BackdropWindow),+ new PropertyMetadata(false, OnAttachingIsWindowDraggableArea));++ public static bool GetIsWindowDraggableArea(DependencyObject d)+ {+ return (bool)d.GetValue(IsWindowDraggableAreaProperty);+ }++ public static void SetIsWindowDraggableArea(DependencyObject d, bool value)+ {+ d.SetValue(IsWindowDraggableAreaProperty, value);+ }++ private static void OnAttachingIsWindowDraggableArea(DependencyObject obj, DependencyPropertyChangedEventArgs e)+ {+ if (obj is FrameworkElement frameworkElement)+ {+ if (frameworkElement.IsLoaded)+ {+ BackdropPage? parentPage = VisualTreeHelperExtend.FindParent<BackdropPage>(frameworkElement);+ parentPage?.Window._draggableAreas.Add(frameworkElement);+ }+ else+ {+ frameworkElement.Loaded += (_, _) =>+ {+ BackdropPage? parentPage = VisualTreeHelperExtend.FindParent<BackdropPage>(frameworkElement);+ parentPage?.Window._draggableAreas.Add(frameworkElement);+ };+ }+ }+ }++ #endregion+/// <summary>/// Gets or sets the position of the window when first shown./// </summary>diff --git a/src/app/dev/DevToys.UI.Framework/Helpers/VisualTreeHelperExtend.cs b/src/app/dev/DevToys.UI.Framework/Helpers/VisualTreeHelperExtend.csnew file mode 100644index 000000000..7e3655263--- /dev/null+++ b/src/app/dev/DevToys.UI.Framework/Helpers/VisualTreeHelperExtend.cs@@ -0,0 +1,29 @@+using Microsoft.UI.Xaml;+using Microsoft.UI.Xaml.Media;++namespace DevToys.UI.Framework.Helpers;++internal static class VisualTreeHelperExtend+{+ /// <summary>+ /// Get the first parent object of a given <paramref name="dependencyObject"/> that is of type <typeparamref name="T"/>.+ /// </summary>+ /// <returns>Returns null if no parent is of type <typeparamref name="T"/>.</returns>+ internal static T? FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject+ {+ Guard.IsNotNull(dependencyObject);+ DependencyObject parent = VisualTreeHelper.GetParent(dependencyObject);++ if (parent == null)+ {+ return default;+ }++ if (parent is T parentT)+ {+ return parentT;+ }++ return FindParent<T>(parent);+ }+}diff --git a/src/app/dev/DevToys.UI.Framework/NativeMethods.txt b/src/app/dev/DevToys.UI.Framework/NativeMethods.txtindex 49923e09c..6ea28d666 100644--- a/src/app/dev/DevToys.UI.Framework/NativeMethods.txt+++ b/src/app/dev/DevToys.UI.Framework/NativeMethods.txt@@ -1,2 +1,3 @@MonitorFromPoint-GetScaleFactorForMonitor\ No newline at end of file+GetScaleFactorForMonitor+GetDpiForMonitor\ No newline at end of filediff --git a/src/app/dev/DevToys.UI/Views/MainWindow.xaml b/src/app/dev/DevToys.UI/Views/MainWindow.xamlindex 2dcad1be3..0709bef4f 100644--- a/src/app/dev/DevToys.UI/Views/MainWindow.xaml+++ b/src/app/dev/DevToys.UI/Views/MainWindow.xaml@@ -5,49 +5,27 @@xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:controls="using:DevToys.UI.Framework.Controls"- xmlns:monaco="using:DevToys.MonacoEditor"xmlns:muxc="using:Microsoft.UI.Xaml.Controls"xmlns:wasm="http://uno.ui/wasm"+ xmlns:macos="http://uno.ui/macos"+ xmlns:ios="http://uno.ui/ios"xmlns:win="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:not_win="http://uno.ui/not_win"- mc:Ignorable="d wasm not_win">+ mc:Ignorable="d wasm not_win macos ios">- <Grid Background="Transparent">- <Grid.BackgroundTransition>- <BrushTransition />- </Grid.BackgroundTransition>-- <Grid.ColumnDefinitions>- <ColumnDefinition Width="*"/>- <ColumnDefinition Width="*"/>- </Grid.ColumnDefinitions>+ <Grid not_win:Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"><Grid.RowDefinitions>+ <RowDefinition Height="32"/><RowDefinition Height="*"/>- <RowDefinition Height="*"/>- <RowDefinition Height="*"/>- <RowDefinition Height="Auto"/>- <RowDefinition Height="Auto"/></Grid.RowDefinitions>- <monaco:CodeEditor x:Name="Editor"- Grid.Row="0"- Grid.Column="0"/>- <!--<monaco:CodeEditor x:Name="Editor2"- Grid.Row="0"- Grid.Column="1"- ReadOnly="True"- IsDiffViewMode="True"/>-->- <ToggleSwitch- Grid.Row="1"- Grid.Column="0"- Header="Test"/>- <ToggleSwitch- Grid.Row="1"- Grid.Column="1"- Header="Test"- Style="{StaticResource RightAlignedToggleSwitchStyle}"/>- <TextBox- Grid.Row="2"- Grid.Column="0"/>+ <Grid x:Name="AppTitleBar" Grid.Row="0">+ <Grid.ColumnDefinitions>+ <ColumnDefinition Width="Auto"/>+ <ColumnDefinition Width="*"/>+ </Grid.ColumnDefinitions>+ <Button Content="Hello world" Grid.Column="0"/>+ <Grid x:Name="DraggableArea" Grid.Column="1" controls:BackdropWindow.IsWindowDraggableArea="True"/>+ </Grid></Grid></controls:BackdropPage>