forked from microsoft/PowerToys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
290 lines (257 loc) · 12.6 KB
/
MainWindow.xaml.cs
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using ManagedCommon;
using Microsoft.PowerLauncher.Telemetry;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Views;
using Microsoft.PowerToys.Telemetry;
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Windows.ApplicationModel.Resources;
using Windows.Data.Json;
using WinUIEx;
namespace Microsoft.PowerToys.Settings.UI
{
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : WindowEx
{
public MainWindow(bool isDark, bool createHidden = false)
{
var bootTime = new System.Diagnostics.Stopwatch();
bootTime.Start();
ShellPage.SetElevationStatus(App.IsElevated);
ShellPage.SetIsUserAnAdmin(App.IsUserAnAdmin);
// Set window icon
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
WindowId windowId = Win32Interop.GetWindowIdFromWindow(hWnd);
AppWindow appWindow = AppWindow.GetFromWindowId(windowId);
appWindow.SetIcon("icon.ico");
// Passed by parameter, as it needs to be evaluated ASAP, otherwise there is a white flash
if (isDark)
{
ThemeHelpers.SetImmersiveDarkMode(hWnd, isDark);
}
var placement = Utils.DeserializePlacementOrDefault(hWnd);
if (createHidden)
{
placement.ShowCmd = NativeMethods.SW_HIDE;
}
NativeMethods.SetWindowPlacement(hWnd, ref placement);
ResourceLoader loader = ResourceLoader.GetForViewIndependentUse();
Title = loader.GetString("SettingsWindow_Title");
// send IPC Message
ShellPage.SetDefaultSndMessageCallback(msg =>
{
// IPC Manager is null when launching runner directly
App.GetTwoWayIPCManager()?.Send(msg);
});
// send IPC Message
ShellPage.SetRestartAdminSndMessageCallback(msg =>
{
App.GetTwoWayIPCManager()?.Send(msg);
Environment.Exit(0); // close application
});
// send IPC Message
ShellPage.SetCheckForUpdatesMessageCallback(msg =>
{
App.GetTwoWayIPCManager()?.Send(msg);
});
// open main window
ShellPage.SetOpenMainWindowCallback(type =>
{
DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal, () =>
App.OpenSettingsWindow(type));
});
// open main window
ShellPage.SetUpdatingGeneralSettingsCallback((string module, bool isEnabled) =>
{
SettingsRepository<GeneralSettings> repository = SettingsRepository<GeneralSettings>.GetInstance(new SettingsUtils());
GeneralSettings generalSettingsConfig = repository.SettingsConfig;
bool needToUpdate = false;
switch (module)
{
case "AlwaysOnTop":
needToUpdate = generalSettingsConfig.Enabled.AlwaysOnTop != isEnabled;
generalSettingsConfig.Enabled.AlwaysOnTop = isEnabled; break;
case "Awake":
needToUpdate = generalSettingsConfig.Enabled.Awake != isEnabled;
generalSettingsConfig.Enabled.Awake = isEnabled; break;
case "ColorPicker":
needToUpdate = generalSettingsConfig.Enabled.ColorPicker != isEnabled;
generalSettingsConfig.Enabled.ColorPicker = isEnabled; break;
case "FancyZones":
needToUpdate = generalSettingsConfig.Enabled.FancyZones != isEnabled;
generalSettingsConfig.Enabled.FancyZones = isEnabled; break;
case "FileLocksmith":
needToUpdate = generalSettingsConfig.Enabled.FileLocksmith != isEnabled;
generalSettingsConfig.Enabled.FileLocksmith = isEnabled; break;
case "FindMyMouse":
needToUpdate = generalSettingsConfig.Enabled.FindMyMouse != isEnabled;
generalSettingsConfig.Enabled.FindMyMouse = isEnabled; break;
case "Hosts":
needToUpdate = generalSettingsConfig.Enabled.Hosts != isEnabled;
generalSettingsConfig.Enabled.Hosts = isEnabled; break;
case "ImageResizer":
needToUpdate = generalSettingsConfig.Enabled.ImageResizer != isEnabled;
generalSettingsConfig.Enabled.ImageResizer = isEnabled; break;
case "KeyboardManager":
needToUpdate = generalSettingsConfig.Enabled.KeyboardManager != isEnabled;
generalSettingsConfig.Enabled.KeyboardManager = isEnabled; break;
case "MouseHighlighter":
needToUpdate = generalSettingsConfig.Enabled.MouseHighlighter != isEnabled;
generalSettingsConfig.Enabled.MouseHighlighter = isEnabled; break;
case "MouseJump":
needToUpdate = generalSettingsConfig.Enabled.MouseJump != isEnabled;
generalSettingsConfig.Enabled.MouseJump = isEnabled; break;
case "MousePointerCrosshairs":
needToUpdate = generalSettingsConfig.Enabled.MousePointerCrosshairs != isEnabled;
generalSettingsConfig.Enabled.MousePointerCrosshairs = isEnabled; break;
case "MouseWithoutBorders":
needToUpdate = generalSettingsConfig.Enabled.MouseWithoutBorders != isEnabled;
generalSettingsConfig.Enabled.MouseWithoutBorders = isEnabled; break;
case "PastePlain":
needToUpdate = generalSettingsConfig.Enabled.PastePlain != isEnabled;
generalSettingsConfig.Enabled.PastePlain = isEnabled; break;
case "Peek":
needToUpdate = generalSettingsConfig.Enabled.Peek != isEnabled;
generalSettingsConfig.Enabled.Peek = isEnabled; break;
case "PowerRename":
needToUpdate = generalSettingsConfig.Enabled.PowerRename != isEnabled;
generalSettingsConfig.Enabled.PowerRename = isEnabled; break;
case "PowerLauncher":
needToUpdate = generalSettingsConfig.Enabled.PowerLauncher != isEnabled;
generalSettingsConfig.Enabled.PowerLauncher = isEnabled; break;
case "PowerAccent":
needToUpdate = generalSettingsConfig.Enabled.PowerAccent != isEnabled;
generalSettingsConfig.Enabled.PowerAccent = isEnabled; break;
case "RegistryPreview":
needToUpdate = generalSettingsConfig.Enabled.RegistryPreview != isEnabled;
generalSettingsConfig.Enabled.RegistryPreview = isEnabled; break;
case "MeasureTool":
needToUpdate = generalSettingsConfig.Enabled.MeasureTool != isEnabled;
generalSettingsConfig.Enabled.MeasureTool = isEnabled; break;
case "ShortcutGuide":
needToUpdate = generalSettingsConfig.Enabled.ShortcutGuide != isEnabled;
generalSettingsConfig.Enabled.ShortcutGuide = isEnabled; break;
case "PowerOCR":
needToUpdate = generalSettingsConfig.Enabled.PowerOCR != isEnabled;
generalSettingsConfig.Enabled.PowerOCR = isEnabled; break;
case "VideoConference":
needToUpdate = generalSettingsConfig.Enabled.VideoConference != isEnabled;
generalSettingsConfig.Enabled.VideoConference = isEnabled; break;
}
if (needToUpdate)
{
var outgoing = new OutGoingGeneralSettings(generalSettingsConfig);
this.DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal, () =>
{
ShellPage.SendDefaultIPCMessage(outgoing.ToString());
ShellPage.ShellHandler?.SignalGeneralDataUpdate();
});
}
return needToUpdate;
});
// open oobe
ShellPage.SetOpenOobeCallback(() =>
{
if (App.GetOobeWindow() == null)
{
App.SetOobeWindow(new OobeWindow(Microsoft.PowerToys.Settings.UI.OOBE.Enums.PowerToysModules.Overview, App.IsDarkTheme()));
}
App.GetOobeWindow().Activate();
});
// open flyout
ShellPage.SetOpenFlyoutCallback((POINT? p) =>
{
this.DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal, () =>
{
if (App.GetFlyoutWindow() == null)
{
App.SetFlyoutWindow(new FlyoutWindow(p));
}
FlyoutWindow flyout = App.GetFlyoutWindow();
flyout.FlyoutAppearPosition = p;
flyout.Activate();
// https://github.com/microsoft/microsoft-ui-xaml/issues/7595 - Activate doesn't bring window to the foreground
// Need to call SetForegroundWindow to actually gain focus.
Utils.BecomeForegroundWindow(flyout.GetWindowHandle());
});
});
// disable flyout hiding
ShellPage.SetDisableFlyoutHidingCallback(() =>
{
this.DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal, () =>
{
if (App.GetFlyoutWindow() == null)
{
App.SetFlyoutWindow(new FlyoutWindow(null));
}
App.GetFlyoutWindow().ViewModel.DisableHiding();
});
});
this.InitializeComponent();
SetTheme(isDark);
// receive IPC Message
App.IPCMessageReceivedCallback = (string msg) =>
{
if (ShellPage.ShellHandler.IPCResponseHandleList != null)
{
var success = JsonObject.TryParse(msg, out JsonObject json);
if (success)
{
foreach (Action<JsonObject> handle in ShellPage.ShellHandler.IPCResponseHandleList)
{
handle(json);
}
}
else
{
Logger.LogError("Failed to parse JSON from IPC message.");
}
}
};
bootTime.Stop();
PowerToysTelemetry.Log.WriteEvent(new SettingsBootEvent() { BootTimeMs = bootTime.ElapsedMilliseconds });
}
public void NavigateToSection(System.Type type)
{
ShellPage.Navigate(type);
}
public void CloseHiddenWindow()
{
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
if (!NativeMethods.IsWindowVisible(hWnd))
{
Close();
}
}
private void Window_Closed(object sender, WindowEventArgs args)
{
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
Utils.SerializePlacement(hWnd);
if (App.GetOobeWindow() == null)
{
App.ClearSettingsWindow();
}
else
{
args.Handled = true;
NativeMethods.ShowWindow(hWnd, NativeMethods.SW_HIDE);
}
}
internal void EnsurePageIsSelected()
{
ShellPage.EnsurePageIsSelected();
}
private void SetTheme(bool isDark)
{
shellPage.RequestedTheme = isDark ? ElementTheme.Dark : ElementTheme.Light;
}
}
}