forked from lepoco/wpfui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTitleBarButton.cs
227 lines (196 loc) · 7.22 KB
/
TitleBarButton.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
// 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.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using Wpf.Ui.Extensions;
using Wpf.Ui.Interop;
// ReSharper disable once CheckNamespace
namespace Wpf.Ui.Controls;
public class TitleBarButton : Wpf.Ui.Controls.Button
{
/// <summary>
/// Property for <see cref="ButtonType"/>.
/// </summary>
public static readonly DependencyProperty ButtonTypeProperty = DependencyProperty.Register(
nameof(ButtonType),
typeof(TitleBarButtonType),
typeof(TitleBarButton),
new PropertyMetadata(TitleBarButtonType.Unknown, ButtonTypePropertyCallback)
);
/// <summary>
/// Property for <see cref="ButtonsForeground"/>.
/// </summary>
public static readonly DependencyProperty ButtonsForegroundProperty = DependencyProperty.Register(
nameof(ButtonsForeground),
typeof(Brush),
typeof(TitleBarButton),
new FrameworkPropertyMetadata(
SystemColors.ControlTextBrush,
FrameworkPropertyMetadataOptions.Inherits
)
);
/// <summary>
/// Property for <see cref="MouseOverButtonsForeground"/>.
/// </summary>
public static readonly DependencyProperty MouseOverButtonsForegroundProperty =
DependencyProperty.Register(
nameof(MouseOverButtonsForeground),
typeof(Brush),
typeof(TitleBarButton),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits)
);
/// <summary>
/// Property for <see cref="RenderButtonsForeground"/>.
/// </summary>
public static readonly DependencyProperty RenderButtonsForegroundProperty = DependencyProperty.Register(
nameof(RenderButtonsForeground),
typeof(Brush),
typeof(TitleBarButton),
new FrameworkPropertyMetadata(
SystemColors.ControlTextBrush,
FrameworkPropertyMetadataOptions.Inherits
)
);
/// <summary>
/// Sets or gets the
/// </summary>
public TitleBarButtonType ButtonType
{
get => (TitleBarButtonType)GetValue(ButtonTypeProperty);
set => SetValue(ButtonTypeProperty, value);
}
/// <summary>
/// Foreground of the navigation buttons.
/// </summary>
public Brush ButtonsForeground
{
get => (Brush)GetValue(ButtonsForegroundProperty);
set => SetValue(ButtonsForegroundProperty, value);
}
/// <summary>
/// Foreground of the navigation buttons while mouse over.
/// </summary>
public Brush? MouseOverButtonsForeground
{
get => (Brush)GetValue(MouseOverButtonsForegroundProperty);
set => SetValue(MouseOverButtonsForegroundProperty, value);
}
public Brush RenderButtonsForeground
{
get => (Brush)GetValue(RenderButtonsForegroundProperty);
set => SetValue(RenderButtonsForegroundProperty, value);
}
public bool IsHovered { get; private set; }
private User32.WM_NCHITTEST _returnValue;
private Brush _defaultBackgroundBrush = Brushes.Transparent; //Should it be transparent?
private bool _isClickedDown;
public TitleBarButton()
{
Loaded += TitleBarButton_Loaded;
Unloaded += TitleBarButton_Unloaded;
}
private void TitleBarButton_Unloaded(object sender, RoutedEventArgs e)
{
DependencyPropertyDescriptor
.FromProperty(ButtonsForegroundProperty, typeof(Brush))
.RemoveValueChanged(this, OnButtonsForegroundChanged);
}
private void TitleBarButton_Loaded(object sender, RoutedEventArgs e)
{
RenderButtonsForeground = ButtonsForeground;
DependencyPropertyDescriptor
.FromProperty(ButtonsForegroundProperty, typeof(Brush))
.AddValueChanged(this, OnButtonsForegroundChanged);
}
private void OnButtonsForegroundChanged(object sender, EventArgs e)
{
SetCurrentValue(
RenderButtonsForegroundProperty,
IsHovered ? MouseOverButtonsForeground : ButtonsForeground
);
}
/// <summary>
/// Forces button background to change.
/// </summary>
public void Hover()
{
if (IsHovered)
return;
Background = MouseOverBackground;
if (MouseOverButtonsForeground != null)
{
RenderButtonsForeground = MouseOverButtonsForeground;
}
IsHovered = true;
}
/// <summary>
/// Forces button background to change.
/// </summary>
public void RemoveHover()
{
if (!IsHovered)
return;
Background = _defaultBackgroundBrush;
RenderButtonsForeground = ButtonsForeground;
IsHovered = false;
_isClickedDown = false;
}
/// <summary>
/// Invokes click on the button.
/// </summary>
public void InvokeClick()
{
if (
new ButtonAutomationPeer(this).GetPattern(PatternInterface.Invoke)
is IInvokeProvider invokeProvider
)
invokeProvider.Invoke();
_isClickedDown = false;
}
internal bool ReactToHwndHook(User32.WM msg, IntPtr lParam, out IntPtr returnIntPtr)
{
returnIntPtr = IntPtr.Zero;
switch (msg)
{
case User32.WM.NCHITTEST:
if (this.IsMouseOverElement(lParam))
{
//Debug.WriteLine($"Hitting {ButtonType} | return code {_returnValue}");
Hover();
returnIntPtr = (IntPtr)_returnValue;
return true;
}
RemoveHover();
return false;
case User32.WM.NCMOUSELEAVE: // Mouse leaves the window
RemoveHover();
return false;
case User32.WM.NCLBUTTONDOWN when this.IsMouseOverElement(lParam): // Left button clicked down
_isClickedDown = true;
return true;
case User32.WM.NCLBUTTONUP when _isClickedDown && this.IsMouseOverElement(lParam): // Left button clicked up
InvokeClick();
return true;
default:
return false;
}
}
private void UpdateReturnValue(TitleBarButtonType buttonType) =>
_returnValue = buttonType switch
{
TitleBarButtonType.Unknown => User32.WM_NCHITTEST.HTNOWHERE,
TitleBarButtonType.Help => User32.WM_NCHITTEST.HTHELP,
TitleBarButtonType.Minimize => User32.WM_NCHITTEST.HTMINBUTTON,
TitleBarButtonType.Close => User32.WM_NCHITTEST.HTCLOSE,
TitleBarButtonType.Restore => User32.WM_NCHITTEST.HTMAXBUTTON,
TitleBarButtonType.Maximize => User32.WM_NCHITTEST.HTMAXBUTTON,
_ => throw new ArgumentOutOfRangeException(nameof(buttonType), buttonType, null)
};
private static void ButtonTypePropertyCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var titleBarButton = (TitleBarButton)d;
titleBarButton.UpdateReturnValue((TitleBarButtonType)e.NewValue);
}
}