Skip to content

Commit ff33e6a

Browse files
authored
Merge branch 'main' into 1.8
2 parents c4ff5b5 + 013ee8e commit ff33e6a

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

WinUIGallery/Helpers/WindowHelper.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using Microsoft.UI.Windowing;
45
using Microsoft.UI.Xaml;
56
using System;
67
using System.Collections.Generic;
@@ -62,6 +63,33 @@ static public double GetRasterizationScaleForElement(UIElement element)
6263
return 0.0;
6364
}
6465

66+
static public void SetWindowMinSize(Window window, double width, double height)
67+
{
68+
if (window.Content is not FrameworkElement windowContent)
69+
{
70+
System.Diagnostics.Debug.WriteLine("Window content is not a FrameworkElement.");
71+
return;
72+
}
73+
74+
if (windowContent.XamlRoot is null)
75+
{
76+
System.Diagnostics.Debug.WriteLine("Window content's XamlRoot is null.");
77+
return;
78+
}
79+
80+
if (window.AppWindow.Presenter is not OverlappedPresenter presenter)
81+
{
82+
System.Diagnostics.Debug.WriteLine("Window's AppWindow.Presenter is not an OverlappedPresenter.");
83+
return;
84+
}
85+
86+
var scale = windowContent.XamlRoot.RasterizationScale;
87+
var minWidth = width * scale;
88+
var minHeight = height * scale;
89+
presenter.PreferredMinimumWidth = (int)minWidth;
90+
presenter.PreferredMinimumHeight = (int)minHeight;
91+
}
92+
6593
static public List<Window> ActiveWindows { get { return _activeWindows; } }
6694

6795
static private List<Window> _activeWindows = new List<Window>();

WinUIGallery/MainWindow.xaml.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,29 @@ public MainWindow()
3636
{
3737
this.InitializeComponent();
3838
SetWindowProperties();
39-
RootGrid.ActualThemeChanged += (_,_) => TitleBarHelper.ApplySystemThemeToCaptionButtons(this, RootGrid.ActualTheme);
39+
RootGrid.ActualThemeChanged += (_, _) => TitleBarHelper.ApplySystemThemeToCaptionButtons(this, RootGrid.ActualTheme);
4040
dispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread();
4141
}
4242

4343
private void RootGrid_Loaded(object sender, RoutedEventArgs e)
4444
{
45+
// We need to set the minimum size here because the XamlRoot is not available in the constructor.
46+
WindowHelper.SetWindowMinSize(this, 640, 500);
47+
48+
if (sender is FrameworkElement rootGrid && rootGrid.XamlRoot is not null)
49+
{
50+
rootGrid.XamlRoot.Changed += RootGridXamlRoot_Changed;
51+
}
52+
4553
NavigationOrientationHelper.UpdateNavigationViewForElement(NavigationOrientationHelper.IsLeftMode());
4654
TitleBarHelper.ApplySystemThemeToCaptionButtons(this, RootGrid.ActualTheme);
4755
}
4856

57+
private void RootGridXamlRoot_Changed(XamlRoot sender, XamlRootChangedEventArgs args)
58+
{
59+
WindowHelper.SetWindowMinSize(this, 640, 500);
60+
}
61+
4962
private void SetWindowProperties()
5063
{
5164
#if DEBUG
@@ -58,9 +71,6 @@ private void SetWindowProperties()
5871
this.SetTitleBar(titleBar);
5972
this.AppWindow.SetIcon("Assets/Tiles/GalleryIcon.ico");
6073
this.AppWindow.TitleBar.PreferredHeightOption = TitleBarHeightOption.Tall;
61-
62-
Win32WindowHelper win32WindowHelper = new Win32WindowHelper(this);
63-
win32WindowHelper.SetWindowMinMaxSize(new Win32WindowHelper.POINT() { x = 640, y = 500 });
6474
}
6575

6676
private void OnPaneDisplayModeChanged(NavigationView sender, NavigationViewDisplayModeChangedEventArgs args)

0 commit comments

Comments
 (0)