Skip to content

Commit 622ebcd

Browse files
committed
Update
1 parent 90644d4 commit 622ebcd

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

src/Files.App/Data/Items/WindowEx.cs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,35 @@
1414

1515
namespace Files.App.Data.Items
1616
{
17-
public unsafe class WindowEx : Window, IDisposable
17+
/// <summary>
18+
/// Represents base <see cref="Window"/> class to extend its features.
19+
/// </summary>
20+
public unsafe class WindowEx : Window
1821
{
1922
private readonly WNDPROC _oldWndProc;
2023
private readonly WNDPROC _newWndProc;
2124

2225
private readonly ApplicationDataContainer _applicationDataContainer = ApplicationData.Current.LocalSettings;
2326

27+
/// <summary>
28+
/// Gets hWnd of this <see cref="Window"/>.
29+
/// </summary>
2430
public nint WindowHandle { get; }
2531

32+
/// <summary>
33+
/// Gets or sets min width of this <see cref="Window"/>.
34+
/// </summary>
2635
public int MinWidth { get; set; }
36+
37+
/// <summary>
38+
/// Gets or sets min height of this <see cref="Window"/>.
39+
/// </summary>
2740
public int MinHeight { get; set; }
2841

2942
private bool _IsMaximizable = true;
43+
/// <summary>
44+
/// Gets or sets a value that indicates whether this <see cref="Window"/> can be maximizable.
45+
/// </summary>
3046
public bool IsMaximizable
3147
{
3248
get => _IsMaximizable;
@@ -54,6 +70,9 @@ public bool IsMaximizable
5470
}
5571

5672
private bool _IsMinimizable = true;
73+
/// <summary>
74+
/// Gets or sets a value that indicates whether this <see cref="Window"/> can be minimizable.
75+
/// </summary>
5776
public bool IsMinimizable
5877
{
5978
get => _IsMinimizable;
@@ -66,6 +85,11 @@ public bool IsMinimizable
6685
}
6786
}
6887

88+
/// <summary>
89+
/// Initializes <see cref="WindowEx"/> class.
90+
/// </summary>
91+
/// <param name="minWidth">Min width to set when initialized.</param>
92+
/// <param name="minHeight">Min height to set when initialized.</param>
6993
public unsafe WindowEx(int minWidth = 400, int minHeight = 300)
7094
{
7195
WindowHandle = WinRT.Interop.WindowNative.GetWindowHandle(this);
@@ -78,6 +102,8 @@ public unsafe WindowEx(int minWidth = 400, int minHeight = 300)
78102
var pNewWndProc = Marshal.GetFunctionPointerForDelegate(_newWndProc);
79103
var pOldWndProc = PInvoke.SetWindowLongPtr(new(WindowHandle), WINDOW_LONG_PTR_INDEX.GWL_WNDPROC, pNewWndProc);
80104
_oldWndProc = Marshal.GetDelegateForFunctionPointer<WNDPROC>(pOldWndProc);
105+
106+
Closed += (s, e) => { StoreWindowPlacementData(); };
81107
}
82108

83109
private unsafe void StoreWindowPlacementData()
@@ -116,9 +142,12 @@ private unsafe void StoreWindowPlacementData()
116142
sw.Write(placementData);
117143
sw.Flush();
118144

119-
var values = GetDataStore(out var oldDataExists);
145+
var values = GetDataStore(out _, true);
120146

121-
values[oldDataExists ? "WindowPersistance_FilesMainWindow" : "MainWindowPlacementData"] = Convert.ToBase64String(data.ToArray());
147+
if (_applicationDataContainer.Containers.ContainsKey("WinUIEx"))
148+
_applicationDataContainer.Values.Remove("WinUIEx");
149+
150+
values["MainWindowPlacementData"] = Convert.ToBase64String(data.ToArray());
122151
}
123152

124153
private void RestoreWindowPlacementData()
@@ -127,7 +156,7 @@ private void RestoreWindowPlacementData()
127156
if (!GetType().Name.Equals(nameof(MainWindow), StringComparison.OrdinalIgnoreCase))
128157
return;
129158

130-
var values = GetDataStore(out var oldDataExists);
159+
var values = GetDataStore(out var oldDataExists, false);
131160

132161
byte[]? data = null;
133162
if (values.TryGetValue(oldDataExists ? "WindowPersistance_FilesMainWindow" : "MainWindowPlacementData", out object? value))
@@ -179,13 +208,13 @@ private void RestoreWindowPlacementData()
179208
return;
180209
}
181210

182-
private IPropertySet GetDataStore(out bool oldDataExists)
211+
private IPropertySet GetDataStore(out bool oldDataExists, bool useNewStore = true)
183212
{
184213
IPropertySet values;
185214
oldDataExists = false;
186215

187216
// TODO: Remove this after a couple of months past
188-
if (_applicationDataContainer.Containers.TryGetValue("WinUIEx", out var oldDataContainer))
217+
if (!useNewStore && _applicationDataContainer.Containers.TryGetValue("WinUIEx", out var oldDataContainer))
189218
{
190219
values = oldDataContainer.Values;
191220
oldDataExists = true;
@@ -251,12 +280,5 @@ private LRESULT NewWindowProc(HWND param0, uint param1, WPARAM param2, LPARAM pa
251280

252281
return PInvoke.CallWindowProc(_oldWndProc, param0, param1, param2, param3);
253282
}
254-
255-
// Disposer
256-
257-
public void Dispose()
258-
{
259-
StoreWindowPlacementData();
260-
}
261283
}
262284
}

0 commit comments

Comments
 (0)