-
-
Notifications
You must be signed in to change notification settings - Fork 668
/
TitleBar.cs
572 lines (471 loc) · 20 KB
/
TitleBar.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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace WPFUI.Controls
{
/// <summary>
/// Custom navigation buttons for the window.
/// </summary>
public class TitleBar : UserControl
{
private Window _parent;
private Tray.NotifyIcon _notifyIcon;
private Common.SnapLayout _snapLayout;
/// <summary>
/// Property for <see cref="Title"/>.
/// </summary>
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(nameof(Title),
typeof(string), typeof(TitleBar), new PropertyMetadata(null));
/// <summary>
/// Property for <see cref="MinimizeToTray"/>.
/// </summary>
public static readonly DependencyProperty MinimizeToTrayProperty = DependencyProperty.Register(
nameof(MinimizeToTray),
typeof(bool), typeof(TitleBar), new PropertyMetadata(false));
/// <summary>
/// Property for <see cref="UseSnapLayout"/>.
/// </summary>
public static readonly DependencyProperty UseSnapLayoutProperty = DependencyProperty.Register(
nameof(UseSnapLayout),
typeof(bool), typeof(TitleBar), new PropertyMetadata(false));
/// <summary>
/// Property for <see cref="IsMaximized"/>.
/// </summary>
public static readonly DependencyProperty IsMaximizedProperty = DependencyProperty.Register(nameof(IsMaximized),
typeof(bool), typeof(TitleBar), new PropertyMetadata(false));
/// <summary>
/// Property for <see cref="ApplicationNavigation"/>.
/// </summary>
public static readonly DependencyProperty ApplicationNavigationProperty =
DependencyProperty.Register(nameof(ApplicationNavigation), typeof(bool), typeof(TitleBar),
new PropertyMetadata(false));
/// <summary>
/// Property for <see cref="ShowMaximize"/>.
/// </summary>
public static readonly DependencyProperty ShowMaximizeProperty = DependencyProperty.Register(
nameof(ShowMaximize),
typeof(bool), typeof(TitleBar), new PropertyMetadata(true));
/// <summary>
/// Property for <see cref="ShowMinimize"/>.
/// </summary>
public static readonly DependencyProperty ShowMinimizeProperty = DependencyProperty.Register(
nameof(ShowMinimize),
typeof(bool), typeof(TitleBar), new PropertyMetadata(true));
/// <summary>
/// Property for <see cref="Icon"/>.
/// </summary>
public static readonly DependencyProperty IconProperty = DependencyProperty.Register(
nameof(Icon),
typeof(ImageSource), typeof(TitleBar), new PropertyMetadata(null));
/// <summary>
/// Property for <see cref="NotifyIconTooltip"/>.
/// </summary>
public static readonly DependencyProperty NotifyIconTooltipProperty = DependencyProperty.Register(
nameof(NotifyIconTooltip),
typeof(string), typeof(TitleBar), new PropertyMetadata(String.Empty, NotifyIconTooltip_OnChanged));
/// <summary>
/// Property for <see cref="NotifyIconImage"/>.
/// </summary>
public static readonly DependencyProperty NotifyIconImageProperty = DependencyProperty.Register(
nameof(NotifyIconImage),
typeof(ImageSource), typeof(TitleBar), new PropertyMetadata(null));
/// <summary>
/// Property for <see cref="UseNotifyIcon"/>.
/// </summary>
public static readonly DependencyProperty UseNotifyIconProperty = DependencyProperty.Register(
nameof(UseNotifyIcon),
typeof(bool), typeof(TitleBar), new PropertyMetadata(false, UseNotifyIcon_OnChanged));
/// <summary>
/// Property for <see cref="NotifyIconMenu"/>.
/// </summary>
public static readonly DependencyProperty NotifyIconMenuProperty = DependencyProperty.Register(
nameof(NotifyIconMenu),
typeof(ContextMenu), typeof(TitleBar), new PropertyMetadata(null, NotifyIconMenu_OnChanged));
/// <summary>
/// Routed event for <see cref="NotifyIconClick"/>.
/// </summary>
public static readonly RoutedEvent NotifyIconClickEvent = EventManager.RegisterRoutedEvent(
nameof(NotifyIconClick), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TitleBar));
/// <summary>
/// Routed event for <see cref="NotifyIconDoubleClick"/>.
/// </summary>
public static readonly RoutedEvent NotifyIconDoubleClickEvent = EventManager.RegisterRoutedEvent(
nameof(NotifyIconDoubleClick), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TitleBar));
/// <summary>
/// Routed event for <see cref="CloseClicked"/>.
/// </summary>
public static readonly RoutedEvent CloseClickedEvent = EventManager.RegisterRoutedEvent(
nameof(CloseClicked), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TitleBar));
/// <summary>
/// Routed event for <see cref="MaximizeClicked"/>.
/// </summary>
public static readonly RoutedEvent MaximizeClickedEvent = EventManager.RegisterRoutedEvent(
nameof(MaximizeClicked), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TitleBar));
/// <summary>
/// Routed event for <see cref="MinimizeClicked"/>.
/// </summary>
public static readonly RoutedEvent MinimizeClickedEvent = EventManager.RegisterRoutedEvent(
nameof(MinimizeClicked), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TitleBar));
/// <summary>
/// Property for <see cref="ButtonCommand"/>.
/// </summary>
public static readonly DependencyProperty ButtonCommandProperty =
DependencyProperty.Register(nameof(ButtonCommand),
typeof(Common.IRelayCommand), typeof(TitleBar), new PropertyMetadata(null));
/// <summary>
/// Gets or sets title displayed on the left.
/// </summary>
public string Title
{
get => (string)GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
/// <summary>
/// Gets or sets information whether to minimize the application to tray.
/// </summary>
public bool MinimizeToTray
{
get => (bool)GetValue(MinimizeToTrayProperty);
set => SetValue(MinimizeToTrayProperty, value);
}
/// <summary>
/// Gets or sets information whether the use Windows 11 Snap Layout.
/// </summary>
public bool UseSnapLayout
{
get => (bool)GetValue(UseSnapLayoutProperty);
set => SetValue(UseSnapLayoutProperty, value);
}
/// <summary>
/// Gets or sets information whether the current window is maximized.
/// </summary>
public bool IsMaximized
{
get => (bool)GetValue(IsMaximizedProperty);
internal set => SetValue(IsMaximizedProperty, value);
}
/// <summary>
/// Gets or sets information whether the controls affect main application window.
/// </summary>
public bool ApplicationNavigation
{
get => (bool)GetValue(ApplicationNavigationProperty);
set => SetValue(ApplicationNavigationProperty, value);
}
/// <summary>
/// Gets or sets information whether to show maximize button.
/// </summary>
public bool ShowMaximize
{
get => (bool)GetValue(ShowMaximizeProperty);
set => SetValue(ShowMaximizeProperty, value);
}
/// <summary>
/// Gets or sets information whether to show minimize button.
/// </summary>
public bool ShowMinimize
{
get => (bool)GetValue(ShowMinimizeProperty);
set => SetValue(ShowMinimizeProperty, value);
}
/// <summary>
/// Titlebar icon.
/// </summary>
public ImageSource Icon
{
get => (ImageSource)GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
/// <summary>
/// Gets or sets text displayed when hover NotifyIcon in system tray.
/// </summary>
public string NotifyIconTooltip
{
get => (string)GetValue(NotifyIconTooltipProperty);
set => SetValue(NotifyIconTooltipProperty, value);
}
/// <summary>
/// BitmapSource of tray icon.
/// </summary>
public ImageSource NotifyIconImage
{
get => (ImageSource)GetValue(NotifyIconImageProperty);
set => SetValue(NotifyIconImageProperty, value);
}
/// <summary>
/// Gets or sets information whether to use shell icon with menu in system tray.
/// </summary>
public bool UseNotifyIcon
{
get => (bool)GetValue(UseNotifyIconProperty);
set => SetValue(UseNotifyIconProperty, value);
}
/// <summary>
/// Menu displayed when left click on NotifyIcon.
/// </summary>
public ContextMenu NotifyIconMenu
{
get => (ContextMenu)GetValue(NotifyIconMenuProperty);
set => SetValue(NotifyIconMenuProperty, value);
}
/// <summary>
/// Event triggered after clicking the left mouse button on the tray icon.
/// </summary>
public event RoutedEventHandler NotifyIconClick
{
add => AddHandler(NotifyIconClickEvent, value);
remove => RemoveHandler(NotifyIconClickEvent, value);
}
/// <summary>
/// Event triggered after double-clicking the left mouse button on the tray icon.
/// </summary>
public event RoutedEventHandler NotifyIconDoubleClick
{
add => AddHandler(NotifyIconDoubleClickEvent, value);
remove => RemoveHandler(NotifyIconDoubleClickEvent, value);
}
/// <summary>
/// Event triggered after clicking close button.
/// </summary>
public event RoutedEventHandler CloseClicked
{
add => AddHandler(CloseClickedEvent, value);
remove => RemoveHandler(CloseClickedEvent, value);
}
/// <summary>
/// Event triggered after clicking maximize or restore button.
/// </summary>
public event RoutedEventHandler MaximizeClicked
{
add => AddHandler(MaximizeClickedEvent, value);
remove => RemoveHandler(MaximizeClickedEvent, value);
}
/// <summary>
/// Event triggered after clicking minimize button.
/// </summary>
public event RoutedEventHandler MinimizeClicked
{
add => AddHandler(MinimizeClickedEvent, value);
remove => RemoveHandler(MinimizeClickedEvent, value);
}
/// <summary>
/// Command triggered after clicking the titlebar button.
/// </summary>
public Common.IRelayCommand ButtonCommand => (Common.IRelayCommand)GetValue(ButtonCommandProperty);
/// <summary>
/// Lets you override the behavior of the Close button with an <see cref="Action"/>.
/// </summary>
public Action<TitleBar, Window> CloseActionOverride { get; set; } = null;
/// <summary>
/// Lets you override the behavior of the Maximize/Restore button with an <see cref="Action"/>.
/// </summary>
public Action<TitleBar, Window> MaximizeActionOverride { get; set; } = null;
/// <summary>
/// Lets you override the behavior of the Minimize button with an <see cref="Action"/>.
/// </summary>
public Action<TitleBar, Window> MinimizeActionOverride { get; set; } = null;
private Window ParentWindow => _parent ??= Window.GetWindow(this);
/// <summary>
/// Creates a new instance of the class and sets the default <see cref="FrameworkElement.Loaded"/> event.
/// </summary>
public TitleBar()
{
SetValue(ButtonCommandProperty, new Common.RelayCommand(o => TemplateButton_OnClick(this, o)));
Loaded += TitleBar_Loaded;
}
/// <summary>
/// Resets icon.
/// </summary>
public void ResetIcon()
{
if (_notifyIcon != null)
_notifyIcon.Destroy();
InitializeNotifyIcon();
}
private void CloseWindow()
{
if (CloseActionOverride != null)
{
CloseActionOverride(this, _parent);
return;
}
if (ApplicationNavigation)
Application.Current.Shutdown();
else
ParentWindow.Close();
}
private void MinimizeWindow()
{
if (MinimizeToTray && UseNotifyIcon && MinimizeWindowToTray()) return;
if (MinimizeActionOverride != null)
{
MinimizeActionOverride(this, _parent);
return;
}
ParentWindow.WindowState = WindowState.Minimized;
}
private void MaximizeWindow()
{
if (MaximizeActionOverride != null)
{
MaximizeActionOverride(this, _parent);
return;
}
if (ParentWindow.WindowState == WindowState.Normal)
{
IsMaximized = true;
ParentWindow.WindowState = WindowState.Maximized;
}
else
{
IsMaximized = false;
ParentWindow.WindowState = WindowState.Normal;
}
}
private void InitializeNotifyIcon()
{
if (DesignerProperties.GetIsInDesignMode(new DependencyObject())) return;
NotifyIconClick += OnNotifyIconClick;
_notifyIcon = new()
{
Parent = this,
Tooltip = NotifyIconTooltip,
ContextMenu = NotifyIconMenu,
Icon = NotifyIconImage,
Click = icon => { RaiseEvent(new RoutedEventArgs(NotifyIconClickEvent, this)); },
DoubleClick = icon => { RaiseEvent(new RoutedEventArgs(NotifyIconDoubleClickEvent, this)); }
};
_notifyIcon.Show();
}
private bool MinimizeWindowToTray()
{
if (_notifyIcon == null)
return false;
ParentWindow.WindowState = WindowState.Minimized;
ParentWindow.Hide();
return true;
}
private void OnNotifyIconClick(object sender, RoutedEventArgs e)
{
if (!MinimizeToTray) return;
if (ParentWindow.WindowState != WindowState.Minimized) return;
ParentWindow.Show();
ParentWindow.WindowState = WindowState.Normal;
ParentWindow.Topmost = true;
ParentWindow.Topmost = false;
Focus();
}
private void InitializeSnapLayout(System.Windows.Controls.Button maximizeButton)
{
if (!Common.SnapLayout.IsSupported()) return;
_snapLayout = new Common.SnapLayout();
_snapLayout.Register(ParentWindow, maximizeButton);
}
private void TitleBar_Loaded(object sender, RoutedEventArgs e)
{
if (UseNotifyIcon)
InitializeNotifyIcon();
// It may look ugly, but at the moment it works surprisingly well
var maximizeButton = (System.Windows.Controls.Button)Template.FindName("ButtonMaximize", this);
if (maximizeButton != null && UseSnapLayout)
InitializeSnapLayout(maximizeButton);
var rootGrid = (System.Windows.Controls.Grid)Template.FindName("RootGrid", this);
if (rootGrid != null)
{
rootGrid.MouseDown += RootGrid_MouseDown;
rootGrid.MouseLeftButtonDown += RootGrid_MouseLeftButtonDown;
rootGrid.MouseMove += RootGrid_MouseMove;
}
if (ParentWindow != null)
ParentWindow.StateChanged += ParentWindow_StateChanged;
}
private void RootGrid_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton != MouseButtonState.Pressed || ParentWindow == null) return;
if (IsMaximized)
{
var screenPoint = PointToScreen(e.MouseDevice.GetPosition(this));
// TODO: refine the Left value to be more accurate
// - This calculation is good enough using the center
// of the titlebar, however this isn't quite accurate for
// how the OS operates.
// - It should be set as a % (e.g. screen X / maximized width),
// then offset from the left to line up more naturally.
ParentWindow.Left = screenPoint.X - (ParentWindow.RestoreBounds.Width * 0.5);
ParentWindow.Top = 0d;
// style has to be quickly swapped to avoid restore animation delay
var style = ParentWindow.WindowStyle;
ParentWindow.WindowStyle = WindowStyle.None;
ParentWindow.WindowState = WindowState.Normal;
ParentWindow.WindowStyle = style;
ParentWindow.DragMove();
}
}
private void ParentWindow_StateChanged(object sender, EventArgs e)
{
if (ParentWindow == null) return;
if (IsMaximized != (ParentWindow.WindowState == WindowState.Maximized))
IsMaximized = ParentWindow.WindowState == WindowState.Maximized;
}
private void RootGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton != MouseButton.Left)
return;
ParentWindow.DragMove();
}
private void RootGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
MaximizeWindow();
}
private void TemplateButton_OnClick(TitleBar sender, object parameter)
{
string command = parameter as string;
switch (command)
{
case "close":
RaiseEvent(new RoutedEventArgs(CloseClickedEvent, this));
CloseWindow();
break;
case "minimize":
RaiseEvent(new RoutedEventArgs(MinimizeClickedEvent, this));
MinimizeWindow();
break;
case "maximize":
RaiseEvent(new RoutedEventArgs(MaximizeClickedEvent, this));
MaximizeWindow();
break;
}
}
private static void NotifyIconTooltip_OnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not TitleBar titleBar) return;
if (!titleBar.UseNotifyIcon) return;
titleBar.ResetIcon();
}
private static void UseNotifyIcon_OnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not TitleBar titleBar) return;
if (titleBar.UseNotifyIcon)
titleBar.ResetIcon();
else
titleBar._notifyIcon.Destroy();
}
private static void NotifyIconMenu_OnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//if (d is not TitleBar titleBar) return;
//if (titleBar.UseNotifyIcon == false)
//{
// return;
//}
//titleBar.ResetIcon();
}
}
}