-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathAppBarWindow.cs
More file actions
454 lines (395 loc) · 15.5 KB
/
AppBarWindow.cs
File metadata and controls
454 lines (395 loc) · 15.5 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
using ManagedShell.Common.Helpers;
using ManagedShell.Common.Logging;
using ManagedShell.Interop;
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Threading;
using Application = System.Windows.Application;
namespace ManagedShell.AppBar
{
public class AppBarWindow : Window, INotifyPropertyChanged
{
protected readonly AppBarManager _appBarManager;
protected readonly ExplorerHelper _explorerHelper;
protected readonly FullScreenHelper _fullScreenHelper;
public AppBarScreen Screen;
public double DpiScale = 1.0;
protected bool ProcessScreenChanges = true;
// Window properties
private WindowInteropHelper helper;
private bool IsRaising;
public IntPtr Handle;
public bool AllowClose;
public bool IsClosing;
protected double DesiredHeight;
protected double DesiredWidth;
private bool EnableBlur;
// AppBar properties
private int AppBarMessageId = -1;
private AppBarEdge _appBarEdge;
public AppBarEdge AppBarEdge
{
get
{
return _appBarEdge;
}
set
{
_appBarEdge = value;
OnPropertyChanged();
OnPropertyChanged("Orientation");
}
}
protected internal bool EnableAppBar = true;
protected internal bool RequiresScreenEdge;
public Orientation Orientation
{
get => (AppBarEdge == AppBarEdge.Left || AppBarEdge == AppBarEdge.Right) ? Orientation.Vertical : Orientation.Horizontal;
}
public AppBarWindow(AppBarManager appBarManager, ExplorerHelper explorerHelper, FullScreenHelper fullScreenHelper, AppBarScreen screen, AppBarEdge edge, double size)
{
_explorerHelper = explorerHelper;
_fullScreenHelper = fullScreenHelper;
_appBarManager = appBarManager;
Closing += OnClosing;
SourceInitialized += OnSourceInitialized;
ResizeMode = ResizeMode.NoResize;
ShowInTaskbar = false;
Title = "";
Topmost = true;
UseLayoutRounding = true;
WindowStyle = WindowStyle.None;
Screen = screen;
AppBarEdge = edge;
if (Orientation == Orientation.Vertical)
{
DesiredWidth = size;
}
else
{
DesiredHeight = size;
}
}
#region Events
protected virtual void OnSourceInitialized(object sender, EventArgs e)
{
// set up helper and get handle
helper = new WindowInteropHelper(this);
Handle = helper.Handle;
WindowHelper.SetWindowNoActivate(Handle);
// set up window procedure
HwndSource source = HwndSource.FromHwnd(Handle);
source.AddHook(WndProc);
// set initial DPI. We do it here so that we get the correct value when DPI has changed since initial user logon to the system.
if (Screen.Primary)
{
DpiHelper.DpiScale = PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformToDevice.M11;
}
DpiScale = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice.M11;
SetPosition();
if (EnvironmentHelper.IsAppRunningAsShell)
{
// set position again, on a delay, in case one display has a different DPI. for some reason the system overrides us if we don't wait
DelaySetPosition();
}
RegisterAppBar();
// hide from alt-tab etc
WindowHelper.HideWindowFromTasks(Handle);
// register for full-screen notifications
_fullScreenHelper.FullScreenApps.CollectionChanged += FullScreenApps_CollectionChanged;
}
private void OnClosing(object sender, CancelEventArgs e)
{
IsClosing = true;
CustomClosing();
if (AllowClose)
{
UnregisterAppBar();
// unregister full-screen notifications
_fullScreenHelper.FullScreenApps.CollectionChanged -= FullScreenApps_CollectionChanged;
}
else
{
IsClosing = false;
e.Cancel = true;
}
}
private void FullScreenApps_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
bool found = false;
foreach (FullScreenApp app in _fullScreenHelper.FullScreenApps)
{
if (app.screen.DeviceName == Screen.DeviceName || app.screen.IsVirtualScreen)
{
// we need to not be on top now
found = true;
break;
}
}
if (found && Topmost)
{
setFullScreenMode(true);
}
else if (!found && !Topmost)
{
setFullScreenMode(false);
}
}
protected virtual IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == AppBarMessageId && AppBarMessageId != -1)
{
switch ((NativeMethods.AppBarNotifications)wParam.ToInt32())
{
case NativeMethods.AppBarNotifications.PosChanged:
if (Orientation == Orientation.Vertical)
{
_appBarManager.ABSetPos(this, DesiredWidth * DpiScale, ActualHeight * DpiScale, AppBarEdge);
}
else
{
_appBarManager.ABSetPos(this, ActualWidth * DpiScale, DesiredHeight * DpiScale, AppBarEdge);
}
break;
case NativeMethods.AppBarNotifications.WindowArrange:
if ((int)lParam != 0) // before
{
Visibility = Visibility.Collapsed;
}
else // after
{
Visibility = Visibility.Visible;
}
break;
}
handled = true;
}
else if (msg == (int)NativeMethods.WM.ACTIVATE && EnableAppBar && !EnvironmentHelper.IsAppRunningAsShell && !AllowClose)
{
_appBarManager.AppBarActivate(hwnd);
}
else if (msg == (int)NativeMethods.WM.WINDOWPOSCHANGING)
{
// Extract the WINDOWPOS structure corresponding to this message
NativeMethods.WINDOWPOS wndPos = NativeMethods.WINDOWPOS.FromMessage(lParam);
// Determine if the z-order is changing (absence of SWP_NOZORDER flag)
// If we are intentionally trying to become topmost, make it so
if (IsRaising && (wndPos.flags & NativeMethods.SetWindowPosFlags.SWP_NOZORDER) == 0)
{
// Sometimes Windows thinks we shouldn't go topmost, so poke here to make it happen.
wndPos.hwndInsertAfter = (IntPtr)NativeMethods.WindowZOrder.HWND_TOPMOST;
wndPos.UpdateMessage(lParam);
}
}
else if (msg == (int)NativeMethods.WM.WINDOWPOSCHANGED && EnableAppBar && !EnvironmentHelper.IsAppRunningAsShell && !AllowClose)
{
_appBarManager.AppBarWindowPosChanged(hwnd);
}
else if (msg == (int)NativeMethods.WM.DPICHANGED)
{
if (Screen.Primary)
{
DpiHelper.DpiScale = (wParam.ToInt32() & 0xFFFF) / 96d;
}
DpiScale = (wParam.ToInt32() & 0xFFFF) / 96d;
ProcessScreenChange(ScreenSetupReason.DpiChange);
}
else if (msg == (int)NativeMethods.WM.DISPLAYCHANGE)
{
ProcessScreenChange(ScreenSetupReason.DisplayChange);
handled = true;
}
else if (msg == (int)NativeMethods.WM.DEVICECHANGE && (int)wParam == 0x0007)
{
ProcessScreenChange(ScreenSetupReason.DeviceChange);
handled = true;
}
else if (msg == (int)NativeMethods.WM.DWMCOMPOSITIONCHANGED)
{
ProcessScreenChange(ScreenSetupReason.DwmChange);
handled = true;
}
return IntPtr.Zero;
}
#endregion
#region Helpers
private void DelaySetPosition()
{
// delay changing things when we are shell. it seems that explorer AppBars do this too.
// if we don't, the system moves things to bad places
var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(0.1) };
timer.Start();
timer.Tick += (sender1, args) =>
{
SetPosition();
timer.Stop();
};
}
public void SetScreenPosition()
{
// set our position if running as shell, otherwise let AppBar do the work
if (EnvironmentHelper.IsAppRunningAsShell || !EnableAppBar)
{
DelaySetPosition();
}
else if (EnableAppBar)
{
if (Orientation == Orientation.Vertical)
{
_appBarManager.ABSetPos(this, DesiredWidth * DpiScale, ActualHeight * DpiScale, AppBarEdge);
}
else
{
_appBarManager.ABSetPos(this, ActualWidth * DpiScale, DesiredHeight * DpiScale, AppBarEdge);
}
}
}
internal void SetAppBarPosition(NativeMethods.Rect rect)
{
Top = rect.Top / DpiScale;
Left = rect.Left / DpiScale;
if (rect.Width >= 0) Width = rect.Width / DpiScale;
if (rect.Height >= 0) Height = rect.Height / DpiScale;
}
private void ProcessScreenChange(ScreenSetupReason reason)
{
// process screen changes if we are on the primary display and the designated window
// (or any display in the case of a DPI change, since only the changed display receives that message and not all windows receive it reliably)
// suppress this if we are shutting down (which can trigger this method on multi-dpi setups due to window movements)
if (((Screen.Primary && ProcessScreenChanges) || reason == ScreenSetupReason.DpiChange) && !AllowClose)
{
SetScreenProperties(reason);
}
}
private void setFullScreenMode(bool entering)
{
if (entering)
{
ShellLogger.Debug($"AppBarWindow: {Name} on {Screen.DeviceName} conceding to full-screen app");
Topmost = false;
WindowHelper.ShowWindowBottomMost(Handle);
}
else
{
ShellLogger.Debug($"AppBarWindow: {Name} on {Screen.DeviceName} returning to normal state");
IsRaising = true;
Topmost = true;
WindowHelper.ShowWindowTopMost(Handle);
IsRaising = false;
}
}
protected void SetBlur(bool enable)
{
if (EnableBlur != enable && Handle != IntPtr.Zero && AllowsTransparency)
{
EnableBlur = enable;
WindowHelper.SetWindowBlur(Handle, enable);
}
}
protected void RegisterAppBar()
{
if (!EnableAppBar || _appBarManager.AppBars.Contains(this))
{
return;
}
if (Orientation == Orientation.Vertical)
{
AppBarMessageId = _appBarManager.RegisterBar(this, DesiredWidth * DpiScale, ActualHeight * DpiScale, AppBarEdge);
}
else
{
AppBarMessageId = _appBarManager.RegisterBar(this, ActualWidth * DpiScale, DesiredHeight * DpiScale, AppBarEdge);
}
}
protected void UnregisterAppBar()
{
if (!_appBarManager.AppBars.Contains(this))
{
return;
}
if (Orientation == Orientation.Vertical)
{
_appBarManager.RegisterBar(this, DesiredWidth * DpiScale, ActualHeight * DpiScale);
}
else
{
_appBarManager.RegisterBar(this, ActualWidth * DpiScale, DesiredHeight * DpiScale);
}
}
#endregion
#region Virtual methods
public virtual void AfterAppBarPos(bool isSameCoords, NativeMethods.Rect rect)
{
if (!isSameCoords)
{
var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(0.1) };
timer.Tick += (sender1, args) =>
{
// set position again, since WPF may have overridden the original change from AppBarHelper
SetAppBarPosition(rect);
timer.Stop();
};
timer.Start();
}
}
protected virtual void CustomClosing() { }
protected virtual void SetScreenProperties(ScreenSetupReason reason)
{
_fullScreenHelper.NotifyScreensChanged();
Screen = AppBarScreen.FromPrimaryScreen();
SetScreenPosition();
}
public virtual void SetPosition()
{
double edgeOffset = 0;
if (!RequiresScreenEdge)
{
edgeOffset = _appBarManager.GetAppBarEdgeWindowsHeight(AppBarEdge, Screen);
}
if (Orientation == Orientation.Vertical)
{
Top = Screen.Bounds.Top / DpiScale;
Height = Screen.Bounds.Height / DpiScale;
Width = DesiredWidth;
if (AppBarEdge == AppBarEdge.Left)
{
Left = Screen.Bounds.Left / DpiScale + edgeOffset;
}
else
{
Left = Screen.Bounds.Right / DpiScale - Width - edgeOffset;
}
}
else
{
Left = Screen.Bounds.Left / DpiScale;
Width = Screen.Bounds.Width / DpiScale;
Height = DesiredHeight;
if (AppBarEdge == AppBarEdge.Top)
{
Top = (Screen.Bounds.Top / DpiScale) + edgeOffset;
}
else
{
Top = Screen.Bounds.Bottom / DpiScale - Height - edgeOffset;
}
}
if (EnvironmentHelper.IsAppRunningAsShell)
{
_appBarManager.SetWorkArea(Screen);
}
}
#endregion
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}