-
Notifications
You must be signed in to change notification settings - Fork 663
/
Copy pathTitleBarPage.xaml.cs
247 lines (211 loc) · 9.59 KB
/
TitleBarPage.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
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using WinUIGallery.Helpers;
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Shapes;
using System.Threading.Tasks;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.UI.Input;
using Windows.Foundation;
using System;
namespace WinUIGallery.ControlPages;
public sealed partial class TitleBarPage : Page
{
private Windows.UI.Color currentBgColor = Colors.Transparent;
private Windows.UI.Color currentFgColor = ThemeHelper.ActualTheme == ElementTheme.Dark ? Colors.White : Colors.Black;
private bool sizeChangedEventHandlerAdded = false;
public TitleBarPage()
{
this.InitializeComponent();
Loaded += (object sender, RoutedEventArgs e) =>
{
(sender as TitleBarPage).UpdateTitleBarColor();
UpdateButtonText();
};
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
ResetTitlebarSettings();
}
private void SetTitleBar(UIElement titlebar, bool forceCustomTitlebar = false)
{
var window = WindowHelper.GetWindowForElement(this as UIElement);
var titleBarElement = UIHelper.FindElementByName(this as UIElement, "AppTitleBar");
if (forceCustomTitlebar || !window.ExtendsContentIntoTitleBar)
{
titleBarElement.Visibility = Visibility.Visible;
window.ExtendsContentIntoTitleBar = true;
window.SetTitleBar(titlebar);
TitleBarHelper.SetCaptionButtonBackgroundColors(window, Colors.Transparent);
}
else
{
titleBarElement.Visibility = Visibility.Collapsed;
window.ExtendsContentIntoTitleBar = false;
window.SetTitleBar(null);
TitleBarHelper.SetCaptionButtonBackgroundColors(window, null);
}
UpdateButtonText();
UpdateTitleBarColor();
}
private void ResetTitlebarSettings()
{
var window = WindowHelper.GetWindowForElement(this as UIElement);
UIElement titleBarElement = UIHelper.FindElementByName(this as UIElement, "AppTitleBar");
SetTitleBar(titleBarElement, forceCustomTitlebar: true);
ClearClickThruRegions();
var txtBoxNonClientArea = UIHelper.FindElementByName(this as UIElement, "AppTitleBarTextBox") as FrameworkElement;
txtBoxNonClientArea.Visibility = Visibility.Collapsed;
addInteractiveElements.Content = "Add interactive control to titlebar";
}
private void SetClickThruRegions(Windows.Graphics.RectInt32[] rects)
{
var window = WindowHelper.GetWindowForElement(this as UIElement);
var nonClientInputSrc = InputNonClientPointerSource.GetForWindowId(window.AppWindow.Id);
nonClientInputSrc.SetRegionRects(NonClientRegionKind.Passthrough, rects);
}
private void ClearClickThruRegions()
{
var window = WindowHelper.GetWindowForElement(this as UIElement);
var noninputsrc = InputNonClientPointerSource.GetForWindowId(window.AppWindow.Id);
noninputsrc.ClearRegionRects(NonClientRegionKind.Passthrough);
}
public void UpdateButtonText()
{
var window = WindowHelper.GetWindowForElement(this as UIElement);
if (window.ExtendsContentIntoTitleBar)
{
customTitleBar.Content = "Reset to System TitleBar";
defaultTitleBar.Content = "Reset to System TitleBar";
}
else
{
customTitleBar.Content = "Set Custom TitleBar";
defaultTitleBar.Content = "Set Default Custom TitleBar";
}
}
private void BgGridView_ItemClick(object sender, ItemClickEventArgs e)
{
var rect = (Rectangle)e.ClickedItem;
var color = ((SolidColorBrush)rect.Fill).Color;
BackgroundColorElement.Background = new SolidColorBrush(color);
currentBgColor = color;
UpdateTitleBarColor();
// Delay required to circumvent GridView bug: https://github.com/microsoft/microsoft-ui-xaml/issues/6350
Task.Delay(10).ContinueWith(_ => myBgColorButton.Flyout.Hide(), TaskScheduler.FromCurrentSynchronizationContext());
}
private void FgGridView_ItemClick(object sender, ItemClickEventArgs e)
{
var rect = (Rectangle)e.ClickedItem;
var color = ((SolidColorBrush)rect.Fill).Color;
ForegroundColorElement.Background = new SolidColorBrush(color);
currentFgColor = color;
UpdateTitleBarColor();
// Delay required to circumvent GridView bug: https://github.com/microsoft/microsoft-ui-xaml/issues/6350
Task.Delay(10).ContinueWith(_ => myFgColorButton.Flyout.Hide(), TaskScheduler.FromCurrentSynchronizationContext());
}
public void UpdateTitleBarColor()
{
var window = WindowHelper.GetWindowForElement(this);
var titleBarElement = UIHelper.FindElementByName(this, "AppTitleBar");
var titleBarAppNameElement = UIHelper.FindElementByName(this, "AppTitle");
(titleBarElement as Border).Background = new SolidColorBrush(currentBgColor); // Changing titlebar uielement's color.
if (currentFgColor != Colors.Transparent)
{
(titleBarAppNameElement as TextBlock).Foreground = new SolidColorBrush(currentFgColor);
}
else
{
(titleBarAppNameElement as TextBlock).Foreground = Application.Current.Resources["TextFillColorPrimaryBrush"] as SolidColorBrush;
}
TitleBarHelper.SetCaptionButtonColors(window, currentFgColor);
if (currentBgColor == Colors.Transparent)
{
// If the current background is null, we want to revert to the default titlebar which is achieved using null as color.
TitleBarHelper.SetBackgroundColor(window, null);
}
else
{
TitleBarHelper.SetBackgroundColor(window, currentBgColor);
}
TitleBarHelper.SetForegroundColor(window, currentFgColor);
}
private void customTitleBar_Click(object sender, RoutedEventArgs e)
{
UIElement titleBarElement = UIHelper.FindElementByName(sender as UIElement, "AppTitleBar");
SetTitleBar(titleBarElement);
// announce visual change to automation
UIHelper.AnnounceActionForAccessibility(sender as UIElement, "TitleBar size and width changed", "TitleBarChangedNotificationActivityId");
}
private void defaultTitleBar_Click(object sender, RoutedEventArgs e)
{
SetTitleBar(null);
// announce visual change to automation
UIHelper.AnnounceActionForAccessibility(sender as UIElement, "TitleBar size and width changed", "TitleBarChangedNotificationActivityId");
}
private void setTxtBoxAsPasthrough(FrameworkElement txtBoxNonClientArea)
{
GeneralTransform transformTxtBox = txtBoxNonClientArea.TransformToVisual(null);
Rect bounds = transformTxtBox.TransformBounds(new Rect(0, 0, txtBoxNonClientArea.ActualWidth, txtBoxNonClientArea.ActualHeight));
var scale = WindowHelper.GetRasterizationScaleForElement(this);
var transparentRect = new Windows.Graphics.RectInt32(
_X: (int)Math.Round(bounds.X * scale),
_Y: (int)Math.Round(bounds.Y * scale),
_Width: (int)Math.Round(bounds.Width * scale),
_Height: (int)Math.Round(bounds.Height * scale)
);
var rectArr = new Windows.Graphics.RectInt32[] { transparentRect };
SetClickThruRegions(rectArr);
}
private void TitleBarHeightComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedHeight = titlebarHeight.SelectedItem.ToString();
var window = WindowHelper.GetWindowForElement(this);
if (selectedHeight != null && window != null && window.ExtendsContentIntoTitleBar)
{
window.AppWindow.TitleBar.PreferredHeightOption = EnumHelper.GetEnum<TitleBarHeightOption>(selectedHeight);
}
}
private void AddInteractiveElements_Click(object sender, RoutedEventArgs e)
{
var txtBoxNonClientArea = UIHelper.FindElementByName(sender as UIElement, "AppTitleBarTextBox") as FrameworkElement;
if (txtBoxNonClientArea.Visibility == Visibility.Visible)
{
ResetTitlebarSettings();
}
else
{
addInteractiveElements.Content = "Remove interactive control from titlebar";
txtBoxNonClientArea.Visibility = Visibility.Visible;
if (sizeChangedEventHandlerAdded)
{
setTxtBoxAsPasthrough(txtBoxNonClientArea);
}
else
{
sizeChangedEventHandlerAdded = true;
// run this code when textbox has been made visible and its actual width and height has been calculated
txtBoxNonClientArea.SizeChanged += (object sender, SizeChangedEventArgs e) =>
{
if (txtBoxNonClientArea.Visibility != Visibility.Collapsed)
{
setTxtBoxAsPasthrough(txtBoxNonClientArea);
}
};
}
// announce visual change to automation
UIHelper.AnnounceActionForAccessibility(sender as UIElement, "TitleBar size and width changed", "TitleBarChangedNotificationActivityId");
}
}
}