Skip to content

Commit ae718a3

Browse files
0x5bfayaira2
authored andcommitted
Update
1 parent 2bcc53b commit ae718a3

File tree

6 files changed

+81
-15
lines changed

6 files changed

+81
-15
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2024 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
using Microsoft.UI.Xaml;
5+
using System.Runtime.InteropServices;
6+
using Windows.Win32;
7+
using Windows.Win32.Foundation;
8+
using Windows.Win32.UI.WindowsAndMessaging;
9+
10+
namespace Files.App.Data.Items
11+
{
12+
public class WindowEx : Window
13+
{
14+
private WNDPROC _oldWndProc;
15+
16+
public int MinWidth = 400;
17+
public int MinHeight = 300;
18+
19+
public unsafe WindowEx()
20+
{
21+
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
22+
WNDPROC newWndProc = new(NewWindowProc);
23+
_oldWndProc = (WNDPROC)PInvoke.SetWindowLongPtr(new(hWnd), WINDOW_LONG_PTR_INDEX.GWL_WNDPROC, new(&newWndProc));
24+
}
25+
26+
private LRESULT NewWindowProc(HWND param0, uint param1, WPARAM param2, LPARAM param3)
27+
{
28+
switch (param1)
29+
{
30+
case 0x0024: /*WM_GETMINMAXINFO*/
31+
{
32+
var dpi = PInvoke.GetDpiForWindow(new(param0));
33+
float scalingFactor = (float)dpi / 96;
34+
35+
// ここで、最上の大きさをMINMAXINFOに入れて指定する
36+
MINMAXINFO minMaxInfo = Marshal.PtrToStructure<MINMAXINFO>(param3);
37+
minMaxInfo.minTrackSize.cx = (int)(MinWidth * scalingFactor);
38+
minMaxInfo.minTrackSize.cy = (int)(MinHeight * scalingFactor);
39+
Marshal.StructureToPtr(minMaxInfo, param3, true);
40+
break;
41+
}
42+
}
43+
44+
return PInvoke.CallWindowProc(_oldWndProc, param0, param1, param2, param3);
45+
}
46+
}
47+
}

src/Files.App/Helpers/Win32/Win32Helper.WindowManagement.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
using Microsoft.UI.Windowing;
66
using Microsoft.UI.Xaml;
77
using System.Reflection;
8+
using System.Runtime.InteropServices;
89
using Vanara.PInvoke;
10+
using Windows.Graphics;
911
using Windows.Win32;
1012
using Windows.Win32.UI.WindowsAndMessaging;
1113
using static Vanara.PInvoke.User32;
@@ -87,5 +89,15 @@ public static void UpdateOverlappedPresenter(Window window, Action<OverlappedPre
8789
else
8890
throw new NotSupportedException($"Not supported with a {appWindow.Presenter.Kind} presenter");
8991
}
92+
93+
public static void SetIsMaximizableWindow(Window window, bool maximizable)
94+
{
95+
UpdateOverlappedPresenter(window, (c) => c.IsMaximizable = maximizable);
96+
}
97+
98+
public static void SetIsMinimizableWindow(Window window, bool minimizable)
99+
{
100+
UpdateOverlappedPresenter(window, (c) => c.IsMinimizable = minimizable);
101+
}
90102
}
91103
}

src/Files.App/MainWindow.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<!-- Copyright (c) 2024 Files Community. Licensed under the MIT License. See the LICENSE. -->
2-
<Window
2+
<items:WindowEx
33
x:Class="Files.App.MainWindow"
44
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
55
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
66
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:items="using:Files.App.Data.Items"
78
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
89
mc:Ignorable="d" />

src/Files.App/MainWindow.xaml.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
// Copyright (c) 2024 Files Community
1+
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

44
using Microsoft.UI;
55
using Microsoft.UI.Windowing;
6-
using Microsoft.UI.Xaml;
76
using Microsoft.UI.Xaml.Controls;
87
using Microsoft.UI.Xaml.Media.Animation;
98
using Microsoft.UI.Xaml.Navigation;
9+
using System.Runtime.InteropServices;
1010
using Windows.ApplicationModel.Activation;
1111
using Windows.Storage;
12+
using Windows.Win32;
13+
using Windows.Win32.UI.WindowsAndMessaging;
14+
using static Vanara.PInvoke.User32;
1215
using IO = System.IO;
1316

1417
namespace Files.App
1518
{
16-
public sealed partial class MainWindow : Window
19+
public sealed partial class MainWindow : WindowEx
1720
{
1821
private static MainWindow? _Instance;
1922
public static MainWindow Instance => _Instance ??= new();
@@ -28,15 +31,15 @@ public MainWindow()
2831
ExtendsContentIntoTitleBar = true;
2932
Title = "Files";
3033

31-
//MinHeight = 416;
32-
//MinWidth = 516;
33-
3434
AppWindow.TitleBar.ButtonBackgroundColor = Colors.Transparent;
3535
AppWindow.TitleBar.ButtonInactiveBackgroundColor = Colors.Transparent;
3636
AppWindow.TitleBar.ButtonPressedBackgroundColor = Colors.Transparent;
3737
AppWindow.TitleBar.ButtonHoverBackgroundColor = Colors.Transparent;
3838
AppWindow.SetIcon(AppLifecycleHelper.AppIconPath);
3939

40+
MinHeight = 550;
41+
MinWidth = 460;
42+
4043
// Workaround for full screen window messing up the taskbar
4144
// https://github.com/microsoft/microsoft-ui-xaml/issues/8431
4245
// This property should only be set if the "Automatically hide the taskbar" in Windows 11,

src/Files.App/NativeMethods.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,6 @@ DesktopWallpaper
9494
SHCreateShellItemArrayFromIDLists
9595
ILCreateFromPath
9696
CLSIDFromString
97+
SetWindowLongPtr
98+
GetDpiForWindow
99+
CallWindowProc

src/Files.App/Utils/Storage/Helpers/FilePropertiesHelpers.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static nint GetWindowHandle(Window w)
3636
=> WinRT.Interop.WindowNative.GetWindowHandle(w);
3737

3838
private static TaskCompletionSource? PropertiesWindowsClosingTCS;
39-
private static readonly BlockingCollection<Window> WindowCache = [];
39+
private static readonly BlockingCollection<WindowEx> WindowCache = [];
4040

4141
/// <summary>
4242
/// Open properties window
@@ -101,17 +101,17 @@ public static void OpenPropertiesWindow(object item, IShellPage associatedInstan
101101
RequestedTheme = AppThemeModeService.AppThemeMode
102102
};
103103

104-
Window propertiesWindow;
105-
if (!WindowCache.TryTake(out propertiesWindow!))
104+
if (!WindowCache.TryTake(out var propertiesWindow!))
106105
{
107106
propertiesWindow = new();
108107
propertiesWindow.Closed += PropertiesWindow_Closed;
109108
}
110109

111-
//propertiesWindow.MinWidth = 460;
112-
//propertiesWindow.MinHeight = 550;
113-
Win32Helper.UpdateOverlappedPresenter(propertiesWindow, (c) => c.IsMaximizable = false);
114-
Win32Helper.UpdateOverlappedPresenter(propertiesWindow, (c) => c.IsMinimizable = false);
110+
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(propertiesWindow);
111+
Win32Helper.SetIsMaximizableWindow(propertiesWindow, false);
112+
Win32Helper.SetIsMinimizableWindow(propertiesWindow, false);
113+
propertiesWindow.MinHeight = 550;
114+
propertiesWindow.MinWidth = 460;
115115
propertiesWindow.AppWindow.Resize(new(800, 550));
116116
propertiesWindow.Content = frame;
117117
propertiesWindow.SystemBackdrop = new AppSystemBackdrop(true);
@@ -158,7 +158,7 @@ public static void OpenPropertiesWindow(object item, IShellPage associatedInstan
158158
// So instead of destroying the Window object, cache it and reuse it as a workaround.
159159
private static void PropertiesWindow_Closed(object sender, WindowEventArgs args)
160160
{
161-
if (!App.AppModel.IsMainWindowClosed && sender is Window window)
161+
if (!App.AppModel.IsMainWindowClosed && sender is WindowEx window)
162162
{
163163
args.Handled = true;
164164

0 commit comments

Comments
 (0)