Skip to content

Commit 45b8e98

Browse files
authored
Merge pull request #4383 from MahApps/jk/feat-4359
Allow to reset the window settings
2 parents 6cf65e8 + f9edff6 commit 45b8e98

6 files changed

Lines changed: 41 additions & 35 deletions

File tree

src/Directory.packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</ItemGroup>
1616
<ItemGroup Label="Build">
1717
<PackageVersion Include="JetBrains.Annotations" Version="2022.3.1" />
18-
<PackageVersion Include="Microsoft.Windows.CsWin32" Version="0.2.164-beta" />
18+
<PackageVersion Include="Microsoft.Windows.CsWin32" Version="0.2.188-beta" />
1919
<PackageVersion Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
2020
<PackageVersion Include="MahApps.Metro.IconPacks.Entypo" Version="4.11.0" />
2121
<PackageVersion Include="MahApps.Metro.IconPacks.FontAwesome" Version="4.11.0" />

src/MahApps.Metro.Samples/MahApps.Metro.Demo/ExampleWindows/FlyoutDemo.xaml.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

@@ -29,8 +29,7 @@ public FlyoutDemo()
2929
}
3030
};
3131

32-
var mainWindow = (MetroWindow)this;
33-
var windowPlacementSettings = mainWindow.GetWindowPlacementSettings();
32+
var windowPlacementSettings = this.GetWindowPlacementSettings();
3433
if (windowPlacementSettings is not null && windowPlacementSettings.UpgradeSettings)
3534
{
3635
windowPlacementSettings.Upgrade();

src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@ protected override void OnAttached()
2323
{
2424
base.OnAttached();
2525

26-
this.AssociatedObject.SourceInitialized += this.AssociatedObject_SourceInitialized;
27-
}
28-
29-
/// <inheritdoc />
30-
protected override void OnDetaching()
31-
{
32-
this.CleanUp("from OnDetaching");
33-
base.OnDetaching();
34-
}
35-
36-
private void AssociatedObject_SourceInitialized(object? sender, EventArgs e)
37-
{
3826
this.LoadWindowState();
3927

4028
var window = this.AssociatedObject;
@@ -59,6 +47,13 @@ private void AssociatedObject_SourceInitialized(object? sender, EventArgs e)
5947
});
6048
}
6149

50+
/// <inheritdoc />
51+
protected override void OnDetaching()
52+
{
53+
this.CleanUp("from OnDetaching");
54+
base.OnDetaching();
55+
}
56+
6257
private void AssociatedObject_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
6358
{
6459
this.SaveWindowState();
@@ -99,7 +94,6 @@ private void CleanUp(string fromWhere)
9994
window.StateChanged -= this.AssociatedObject_StateChanged;
10095
window.Closing -= this.AssociatedObject_Closing;
10196
window.Closed -= this.AssociatedObject_Closed;
102-
window.SourceInitialized -= this.AssociatedObject_SourceInitialized;
10397

10498
// This operation must be thread safe
10599
Application.Current?.BeginInvoke(app =>
@@ -169,11 +163,11 @@ private void SaveWindowState()
169163
var windowHandle = new WindowInteropHelper(window).EnsureHandle();
170164
var wp = new WINDOWPLACEMENT
171165
{
172-
length = (uint)Marshal.SizeOf<WINDOWPLACEMENT>()
166+
length = (uint)Marshal.SizeOf<WINDOWPLACEMENT>()
173167
};
174168
unsafe
175169
{
176-
PInvoke.GetWindowPlacement((HWND)windowHandle, &wp);
170+
PInvoke.GetWindowPlacement(new HWND(windowHandle), &wp);
177171
}
178172

179173
// check for saveable values
@@ -195,7 +189,7 @@ private void SaveWindowState()
195189
{
196190
var monitorInfo = new MONITORINFO
197191
{
198-
cbSize = (uint)Marshal.SizeOf<MONITORINFO>()
192+
cbSize = (uint)Marshal.SizeOf<MONITORINFO>()
199193
};
200194
PInvoke.GetMonitorInfo(monitor, &monitorInfo);
201195
rect.Offset(monitorInfo.rcMonitor.left - monitorInfo.rcWork.left, monitorInfo.rcMonitor.top - monitorInfo.rcWork.top);

src/MahApps.Metro/Controls/MetroWindow.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,8 +1113,6 @@ public MetroWindow()
11131113
{
11141114
this.SetCurrentValue(MetroDialogOptionsProperty, new MetroDialogSettings());
11151115

1116-
this.InitializeSettingsBehavior();
1117-
11181116
this.DataContextChanged += this.MetroWindow_DataContextChanged;
11191117
this.Loaded += this.MetroWindow_Loaded;
11201118
}
@@ -1129,8 +1127,10 @@ private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
11291127
this.Unloaded += (_, _) => ThemeManager.Current.ThemeChanged -= this.HandleThemeManagerThemeChanged;
11301128
}
11311129

1132-
private void InitializeSettingsBehavior()
1130+
protected override void InitializeBehaviors()
11331131
{
1132+
base.InitializeBehaviors();
1133+
11341134
Interaction.GetBehaviors(this).Add(new WindowsSettingBehavior());
11351135
}
11361136

src/MahApps.Metro/Controls/WinApiHelper.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static unsafe Size GetMonitorWorkSize(this Visual? visual)
6767
{
6868
var monitorInfo = new MONITORINFO
6969
{
70-
cbSize = (uint)Marshal.SizeOf<MONITORINFO>()
70+
cbSize = (uint)Marshal.SizeOf<MONITORINFO>()
7171
};
7272
PInvoke.GetMonitorInfo(monitor, &monitorInfo);
7373
return new Size(monitorInfo.rcWork.right - monitorInfo.rcWork.left, monitorInfo.rcWork.bottom - monitorInfo.rcWork.top);
@@ -96,10 +96,17 @@ public static unsafe void SetWindowPlacement(Window? window, WINDOWPLACEMENT? wp
9696

9797
var placement = new WINDOWPLACEMENT
9898
{
99+
length = (uint)Marshal.SizeOf<WINDOWPLACEMENT>(),
99100
showCmd = (wp is null || wp.Value.showCmd == SHOW_WINDOW_CMD.SW_SHOWMINIMIZED ? SHOW_WINDOW_CMD.SW_SHOWNORMAL : wp.Value.showCmd),
100101
rcNormalPosition = new RECT { left = x, top = y, right = x + width, bottom = y + height }
101102
};
102103

104+
if (wp is not null)
105+
{
106+
placement.ptMinPosition = new System.Drawing.Point(wp.Value.ptMinPosition.X, wp.Value.ptMinPosition.Y);
107+
placement.ptMaxPosition = new System.Drawing.Point(wp.Value.ptMaxPosition.X, wp.Value.ptMaxPosition.Y);
108+
}
109+
103110
var hWnd = new WindowInteropHelper(window).EnsureHandle();
104111
if (PInvoke.SetWindowPlacement(new HWND(hWnd), &placement) == false)
105112
{

src/MahApps.Metro/Controls/WindowSettings.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ internal WINDOWPLACEMENT ToWINDOWPLACEMENT()
2222
{
2323
return new WINDOWPLACEMENT
2424
{
25-
length = (uint)Marshal.SizeOf<WINDOWPLACEMENT>(),
26-
showCmd = (SHOW_WINDOW_CMD)this.showCmd,
27-
ptMinPosition = new System.Drawing.Point { X = (int)this.minPosition.X, Y = (int)this.minPosition.Y },
28-
ptMaxPosition = new System.Drawing.Point { X = (int)this.maxPosition.X, Y = (int)this.maxPosition.Y },
29-
rcNormalPosition = new RECT { left = (int)this.normalPosition.X, top = (int)this.normalPosition.Y, right = (int)this.normalPosition.Right, bottom = (int)this.normalPosition.Bottom }
25+
length = (uint)Marshal.SizeOf<WINDOWPLACEMENT>(),
26+
showCmd = (SHOW_WINDOW_CMD)this.showCmd,
27+
ptMinPosition = new System.Drawing.Point { X = (int)this.minPosition.X, Y = (int)this.minPosition.Y },
28+
ptMaxPosition = new System.Drawing.Point { X = (int)this.maxPosition.X, Y = (int)this.maxPosition.Y },
29+
rcNormalPosition = new RECT { left = (int)this.normalPosition.X, top = (int)this.normalPosition.Y, right = (int)this.normalPosition.Right, bottom = (int)this.normalPosition.Bottom }
3030
};
3131
}
3232

3333
internal static WindowPlacementSetting FromWINDOWPLACEMENT(WINDOWPLACEMENT windowplacement)
3434
{
3535
return new WindowPlacementSetting
36-
{
37-
showCmd = (uint)windowplacement.showCmd,
38-
minPosition = new Point(windowplacement.ptMinPosition.X, windowplacement.ptMinPosition.Y),
39-
maxPosition = new Point(windowplacement.ptMaxPosition.X, windowplacement.ptMaxPosition.Y),
40-
normalPosition = new Rect(windowplacement.rcNormalPosition.left, windowplacement.rcNormalPosition.top, windowplacement.rcNormalPosition.GetWidth(), windowplacement.rcNormalPosition.GetHeight()),
41-
};
36+
{
37+
showCmd = (uint)windowplacement.showCmd,
38+
minPosition = new Point(windowplacement.ptMinPosition.X, windowplacement.ptMinPosition.Y),
39+
maxPosition = new Point(windowplacement.ptMaxPosition.X, windowplacement.ptMaxPosition.Y),
40+
normalPosition = new Rect(windowplacement.rcNormalPosition.left, windowplacement.rcNormalPosition.top, windowplacement.rcNormalPosition.GetWidth(), windowplacement.rcNormalPosition.GetHeight()),
41+
};
4242
}
4343
}
4444

@@ -65,6 +65,12 @@ public interface IWindowPlacementSettings
6565
/// Stores the current values of the settings properties.
6666
/// </summary>
6767
void Save();
68+
69+
/// <summary>
70+
/// Calls Reset on the providers.
71+
/// Providers must implement IApplicationSettingsProvider to support this.
72+
/// </summary>
73+
void Reset();
6874
}
6975

7076
/// <summary>

0 commit comments

Comments
 (0)