-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWindowSaver.cs
More file actions
251 lines (209 loc) · 7.26 KB
/
WindowSaver.cs
File metadata and controls
251 lines (209 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
namespace Menees.Windows.Presentation
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
#endregion
/// <summary>
/// Used to save and load a window's position and state.
/// </summary>
public sealed class WindowSaver
{
#region Constructors
/// <summary>
/// Creates a new instance.
/// </summary>
public WindowSaver(Window window)
{
Conditions.RequireReference(window, nameof(window));
this.AutoLoad = true;
this.AutoSave = true;
this.SettingsNodeName = "Window Placement";
this.Window = window;
this.Window.SourceInitialized += this.Window_SourceInitialized;
this.Window.Closing += this.Window_Closing;
}
#endregion
#region Public Events
/// <summary>
/// Called when settings are being loaded.
/// </summary>
public event EventHandler<SettingsEventArgs>? LoadSettings;
/// <summary>
/// Called when settings are being saved.
/// </summary>
public event EventHandler<SettingsEventArgs>? SaveSettings;
#endregion
#region Public Properties
/// <summary>
/// Gets or sets whether the settings should automatically load when the window loads. This defaults to true.
/// </summary>
public bool AutoLoad { get; set; }
/// <summary>
/// Gets or sets whether the settings should automatically save when the window closes. This defaults to true.
/// </summary>
public bool AutoSave { get; set; }
/// <summary>
/// Gets or sets the node where settings should be saved. This can be empty to save to the root node.
/// This defaults to "Window Placement".
/// </summary>
public string SettingsNodeName { get; set; }
/// <summary>
/// Gets the window to save the settings for.
/// </summary>
public Window Window { get; }
/// <summary>
/// Gets or sets a window state to use during <see cref="Load"/> instead of any prior state loaded from settings.
/// This defaults to null, which means "don't use an override".
/// </summary>
public WindowState? LoadStateOverride { get; set; }
#endregion
#region Public Methods
/// <summary>
/// Loads the heights for the <see cref="RowDefinition"/>s before and after a <see cref="GridSplitter"/>.
/// </summary>
/// <param name="baseNode">The base settings node to look for <see cref="settingsNodeName"/> under.</param>
/// <param name="splitter">The grid splitter to load settings for.</param>
/// <param name="settingsNodeName">The node name where the <paramref name="splitter"/>'s settings were saved.</param>
public static void LoadSplits(ISettingsNode baseNode, GridSplitter splitter, string settingsNodeName = nameof(GridSplitter))
{
Conditions.RequireReference(baseNode, nameof(baseNode));
Conditions.RequireReference(splitter, nameof(splitter));
ISettingsNode? splitterNode = baseNode.TryGetSubNode(settingsNodeName);
if (splitterNode != null)
{
RowDefinition[] splitterTargetRows = GetTargetRows(splitter);
for (int i = 0; i < splitterTargetRows.Length; i++)
{
ISettingsNode? rowNode = splitterNode.TryGetSubNode($"Row{i}");
if (rowNode != null)
{
double value = rowNode.GetValue(nameof(GridLength.Value), 1.0);
GridUnitType unitType = rowNode.GetValue(nameof(GridLength.GridUnitType), GridUnitType.Star);
RowDefinition row = splitterTargetRows[i];
row.Height = new GridLength(value, unitType);
}
}
}
}
/// <summary>
/// Saves the heights for the <see cref="RowDefinition"/>s before and after a <see cref="GridSplitter"/>.
/// </summary>
/// <param name="baseNode">The base settings node to (re)create <paramref name="settingsNodeName"/> under.</param>
/// <param name="splitter">The grid splitter to save settings for.</param>
/// <param name="settingsNodeName">The node name where the <paramref name="splitter"/>'s settings should be saved.</param>
public static void SaveSplits(ISettingsNode baseNode, GridSplitter splitter, string settingsNodeName = nameof(GridSplitter))
{
Conditions.RequireReference(baseNode, nameof(baseNode));
Conditions.RequireReference(splitter, nameof(splitter));
baseNode.DeleteSubNode(settingsNodeName);
RowDefinition[] splitterTargetRows = GetTargetRows(splitter);
if (splitterTargetRows.Length > 0)
{
ISettingsNode splitterNode = baseNode.GetSubNode(settingsNodeName);
for (int i = 0; i < splitterTargetRows.Length; i++)
{
RowDefinition row = splitterTargetRows[i];
GridLength rowHeight = row.Height;
ISettingsNode rowNode = splitterNode.GetSubNode($"Row{i}");
rowNode.SetValue(nameof(rowHeight.Value), rowHeight.Value);
rowNode.SetValue(nameof(rowHeight.GridUnitType), rowHeight.GridUnitType);
}
}
}
/// <summary>
/// Loads the window settings manually, which is useful if <see cref="AutoLoad"/> is false.
/// </summary>
/// <returns>True if the previous settings were re-loaded; false if no previous settings existed.</returns>
public bool Load()
{
bool result = false;
if (!WindowsUtility.IsInDesignMode(this.Window))
{
using (ISettingsStore store = ApplicationInfo.CreateUserSettingsStore())
{
ISettingsNode? settingsNode = this.GetSettingsNode(store, false);
if (settingsNode != null)
{
NativeMethods.LoadWindowPlacement(this.Window, settingsNode, this.LoadStateOverride);
result = true;
}
else if (this.LoadStateOverride != null)
{
this.Window.WindowState = this.LoadStateOverride.Value;
}
this.LoadSettings?.Invoke(this, new SettingsEventArgs(store.RootNode));
}
}
return result;
}
/// <summary>
/// Saves the window settings manually, which is useful if <see cref="AutoSave"/> is false.
/// </summary>
public void Save()
{
if (!WindowsUtility.IsInDesignMode(this.Window))
{
using (ISettingsStore store = ApplicationInfo.CreateUserSettingsStore())
{
ISettingsNode? settingsNode = this.GetSettingsNode(store, true);
if (settingsNode != null)
{
NativeMethods.SaveWindowPlacement(this.Window, settingsNode);
}
this.SaveSettings?.Invoke(this, new SettingsEventArgs(store.RootNode));
// Make sure the settings get saved out.
store.Save();
}
}
}
#endregion
#region Private Methods
private static RowDefinition[] GetTargetRows(GridSplitter splitter)
{
RowDefinition[] result = [];
if (splitter.Parent is Grid grid)
{
int splitterRow = Grid.GetRow(splitter);
result =
[
grid.RowDefinitions[splitterRow - 1],
grid.RowDefinitions[splitterRow + 1],
];
}
return result;
}
private ISettingsNode? GetSettingsNode(ISettingsStore store, bool createIfNotFound)
{
ISettingsNode? result = store.RootNode;
if (!string.IsNullOrEmpty(this.SettingsNodeName))
{
result = createIfNotFound ? result.GetSubNode(this.SettingsNodeName) : result.TryGetSubNode(this.SettingsNodeName);
}
return result;
}
#endregion
#region Private Event Handlers
private void Window_Closing(object? sender, CancelEventArgs e)
{
if (this.AutoSave && !e.Cancel)
{
this.Save();
}
}
private void Window_SourceInitialized(object? sender, EventArgs e)
{
if (this.AutoLoad)
{
this.Load();
}
}
#endregion
}
}