14
14
15
15
namespace Files . App . Data . Items
16
16
{
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
18
21
{
19
22
private readonly WNDPROC _oldWndProc ;
20
23
private readonly WNDPROC _newWndProc ;
21
24
22
25
private readonly ApplicationDataContainer _applicationDataContainer = ApplicationData . Current . LocalSettings ;
23
26
27
+ /// <summary>
28
+ /// Gets hWnd of this <see cref="Window"/>.
29
+ /// </summary>
24
30
public nint WindowHandle { get ; }
25
31
32
+ /// <summary>
33
+ /// Gets or sets min width of this <see cref="Window"/>.
34
+ /// </summary>
26
35
public int MinWidth { get ; set ; }
36
+
37
+ /// <summary>
38
+ /// Gets or sets min height of this <see cref="Window"/>.
39
+ /// </summary>
27
40
public int MinHeight { get ; set ; }
28
41
29
42
private bool _IsMaximizable = true ;
43
+ /// <summary>
44
+ /// Gets or sets a value that indicates whether this <see cref="Window"/> can be maximizable.
45
+ /// </summary>
30
46
public bool IsMaximizable
31
47
{
32
48
get => _IsMaximizable ;
@@ -54,6 +70,9 @@ public bool IsMaximizable
54
70
}
55
71
56
72
private bool _IsMinimizable = true ;
73
+ /// <summary>
74
+ /// Gets or sets a value that indicates whether this <see cref="Window"/> can be minimizable.
75
+ /// </summary>
57
76
public bool IsMinimizable
58
77
{
59
78
get => _IsMinimizable ;
@@ -66,6 +85,11 @@ public bool IsMinimizable
66
85
}
67
86
}
68
87
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>
69
93
public unsafe WindowEx ( int minWidth = 400 , int minHeight = 300 )
70
94
{
71
95
WindowHandle = WinRT . Interop . WindowNative . GetWindowHandle ( this ) ;
@@ -78,6 +102,8 @@ public unsafe WindowEx(int minWidth = 400, int minHeight = 300)
78
102
var pNewWndProc = Marshal . GetFunctionPointerForDelegate ( _newWndProc ) ;
79
103
var pOldWndProc = PInvoke . SetWindowLongPtr ( new ( WindowHandle ) , WINDOW_LONG_PTR_INDEX . GWL_WNDPROC , pNewWndProc ) ;
80
104
_oldWndProc = Marshal . GetDelegateForFunctionPointer < WNDPROC > ( pOldWndProc ) ;
105
+
106
+ Closed += ( s , e ) => { StoreWindowPlacementData ( ) ; } ;
81
107
}
82
108
83
109
private unsafe void StoreWindowPlacementData ( )
@@ -116,9 +142,12 @@ private unsafe void StoreWindowPlacementData()
116
142
sw . Write ( placementData ) ;
117
143
sw . Flush ( ) ;
118
144
119
- var values = GetDataStore ( out var oldDataExists ) ;
145
+ var values = GetDataStore ( out _ , true ) ;
120
146
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 ( ) ) ;
122
151
}
123
152
124
153
private void RestoreWindowPlacementData ( )
@@ -127,7 +156,7 @@ private void RestoreWindowPlacementData()
127
156
if ( ! GetType ( ) . Name . Equals ( nameof ( MainWindow ) , StringComparison . OrdinalIgnoreCase ) )
128
157
return ;
129
158
130
- var values = GetDataStore ( out var oldDataExists ) ;
159
+ var values = GetDataStore ( out var oldDataExists , false ) ;
131
160
132
161
byte [ ] ? data = null ;
133
162
if ( values . TryGetValue ( oldDataExists ? "WindowPersistance_FilesMainWindow" : "MainWindowPlacementData" , out object ? value ) )
@@ -179,13 +208,13 @@ private void RestoreWindowPlacementData()
179
208
return ;
180
209
}
181
210
182
- private IPropertySet GetDataStore ( out bool oldDataExists )
211
+ private IPropertySet GetDataStore ( out bool oldDataExists , bool useNewStore = true )
183
212
{
184
213
IPropertySet values ;
185
214
oldDataExists = false ;
186
215
187
216
// 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 ) )
189
218
{
190
219
values = oldDataContainer . Values ;
191
220
oldDataExists = true ;
@@ -251,12 +280,5 @@ private LRESULT NewWindowProc(HWND param0, uint param1, WPARAM param2, LPARAM pa
251
280
252
281
return PInvoke . CallWindowProc ( _oldWndProc , param0 , param1 , param2 , param3 ) ;
253
282
}
254
-
255
- // Disposer
256
-
257
- public void Dispose ( )
258
- {
259
- StoreWindowPlacementData ( ) ;
260
- }
261
283
}
262
284
}
0 commit comments