-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTeachingTipHelper.cpp
More file actions
188 lines (143 loc) · 6.36 KB
/
TeachingTipHelper.cpp
File metadata and controls
188 lines (143 loc) · 6.36 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
#include "pch.h"
#include "TeachingTipHelper.h"
#if __has_include("TeachingTipHelper.g.cpp")
#include "TeachingTipHelper.g.cpp"
#endif
#include "VisualTreeHelper.hpp"
#include "AcrylicVisual.h"
#include "AcrylicVisualWithBoundedCornerRadius.h"
namespace winrt::WinUI3Package::implementation
{
winrt::Microsoft::UI::Xaml::DependencyProperty TeachingTipHelper::s_acrylicWorkaroundProperty =
winrt::Microsoft::UI::Xaml::DependencyProperty::RegisterAttached(
L"AcrylicWorkaround",
winrt::xaml_typename<bool>(),
winrt::xaml_typename<class_type>(),
winrt::Microsoft::UI::Xaml::PropertyMetadata{
nullptr,
&TeachingTipHelper::acrylicWorkaroundChanged
}
);
winrt::Microsoft::UI::Xaml::DependencyProperty TeachingTipHelper::AcrylicWorkaroundProperty()
{
return s_acrylicWorkaroundProperty;
}
bool TeachingTipHelper::GetAcrylicWorkaround(winrt::Microsoft::UI::Xaml::Controls::TeachingTip const& teachingTip)
{
return winrt::unbox_value<bool>(teachingTip.GetValue(AcrylicWorkaroundProperty()));
}
void TeachingTipHelper::SetAcrylicWorkaround(
winrt::Microsoft::UI::Xaml::Controls::TeachingTip const& teachingTip,
bool value
)
{
teachingTip.SetValue(AcrylicWorkaroundProperty(), winrt::box_value(value));
}
void ApplyAcrylicToGrid(winrt::Microsoft::UI::Xaml::Controls::Grid const& grid)
{
grid.Background(winrt::Microsoft::UI::Xaml::Media::SolidColorBrush(
winrt::Windows::UI::Colors::Transparent()));
AcrylicVisualWithBoundedCornerRadius acrylicLayer{ grid };
winrt::get_self<winrt::WinUI3Package::implementation::AcrylicVisual>(
static_cast<winrt::WinUI3Package::AcrylicVisual const&>(acrylicLayer)
)->ClipOffset = { 0, 0, 0, 1 };
winrt::Microsoft::UI::Xaml::Controls::Grid::SetRow(acrylicLayer, 0);
winrt::Microsoft::UI::Xaml::Controls::Grid::SetRowSpan(acrylicLayer, 3);
grid.Children().InsertAt(0, acrylicLayer);
}
void PatchTailBorder(
winrt::Microsoft::UI::Xaml::Controls::Grid const& contentRootGrid,
winrt::Microsoft::UI::Xaml::Shapes::Polygon const& tailPolygon)
{
auto borderBrush = contentRootGrid.BorderBrush();
auto bottomBorder = winrt::Microsoft::UI::Xaml::Controls::Grid();
winrt::Microsoft::UI::Xaml::Controls::ColumnDefinition col0;
col0.Width({ 1, winrt::Microsoft::UI::Xaml::GridUnitType::Star });
winrt::Microsoft::UI::Xaml::Controls::ColumnDefinition col1;
col1.Width({ 20, winrt::Microsoft::UI::Xaml::GridUnitType::Pixel });
winrt::Microsoft::UI::Xaml::Controls::ColumnDefinition col2;
col2.Width({ 1, winrt::Microsoft::UI::Xaml::GridUnitType::Star });
bottomBorder.ColumnDefinitions().Append(col0);
bottomBorder.ColumnDefinitions().Append(col1);
bottomBorder.ColumnDefinitions().Append(col2);
auto leftSeg = winrt::Microsoft::UI::Xaml::Controls::Grid();
leftSeg.Background(borderBrush);
winrt::Microsoft::UI::Xaml::Controls::Grid::SetColumn(leftSeg, 0);
bottomBorder.Children().Append(leftSeg);
auto rightSeg = winrt::Microsoft::UI::Xaml::Controls::Grid();
rightSeg.Background(borderBrush);
winrt::Microsoft::UI::Xaml::Controls::Grid::SetColumn(rightSeg, 2);
bottomBorder.Children().Append(rightSeg);
double borderHeight = 1;
bottomBorder.Height(borderHeight);
bottomBorder.VerticalAlignment(winrt::Microsoft::UI::Xaml::VerticalAlignment::Bottom);
winrt::Microsoft::UI::Xaml::Controls::Grid::SetRowSpan(bottomBorder, 3);
contentRootGrid.Children().Append(bottomBorder);
winrt::Microsoft::UI::Xaml::Media::TranslateTransform transform;
transform.Y(1);
tailPolygon.RenderTransform(transform);
}
void PatchLightDismissSetters(winrt::Microsoft::UI::Xaml::Controls::Border const& contentRootGrid)
{
auto transparentBrush = winrt::Microsoft::UI::Xaml::Media::SolidColorBrush(
winrt::Windows::UI::Colors::Transparent());
auto groups = winrt::Microsoft::UI::Xaml::VisualStateManager::GetVisualStateGroups(contentRootGrid);
if (!groups) return;
for (auto const& group : groups)
{
if (group.Name() != L"LightDismissStates") continue;
for (auto const& state : group.States())
{
if (state.Name() != L"LightDismiss") continue;
for (auto const& setterBase : state.Setters())
{
auto setter = setterBase.try_as<winrt::Microsoft::UI::Xaml::Setter>();
if (!setter) continue;
auto target = setter.Target();
if (!target) continue;
auto PathName = target.Path().Path();
if (PathName == L"Background" || PathName == L"Fill")
{
setter.Value(transparentBrush);
}
}
}
}
}
void TeachingTipHelper::acrylicWorkaroundChanged(
winrt::Microsoft::UI::Xaml::DependencyObject const& object,
winrt::Microsoft::UI::Xaml::DependencyPropertyChangedEventArgs const& arg)
{
auto const acrylicWorkaround = winrt::unbox_value<bool>(arg.NewValue());
if (!acrylicWorkaround) return;
auto teachingTip = object.as<winrt::Microsoft::UI::Xaml::Controls::TeachingTip>();
teachingTip.Background(winrt::Microsoft::UI::Xaml::Media::SolidColorBrush{
winrt::Windows::UI::Colors::Transparent()
});
auto teachingTipLoadedRevoker = std::make_shared<winrt::Microsoft::UI::Xaml::Controls::TeachingTip::Loaded_revoker>();
*teachingTipLoadedRevoker = teachingTip.Loaded(winrt::auto_revoke, [teachingTipLoadedRevoker](winrt::Windows::Foundation::IInspectable const& teachingTipRef, auto&&)
{
teachingTipLoadedRevoker->revoke();
auto teachingTip = teachingTipRef.as<winrt::Microsoft::UI::Xaml::Controls::TeachingTip>();
teachingTip.ShouldConstrainToRootBounds(false);
auto border = VisualTreeHelper::FindVisualChildByName<winrt::Microsoft::UI::Xaml::Controls::Border>(
teachingTip,
L"Container"
);
if (!border) return;
PatchLightDismissSetters(border);
auto borderLoadedRevoker = std::make_shared<winrt::Microsoft::UI::Xaml::Controls::Border::Loaded_revoker>();
*borderLoadedRevoker = border.Loaded(winrt::auto_revoke, [borderLoadedRevoker](winrt::Windows::Foundation::IInspectable const& borderRef, auto&&)
{
borderLoadedRevoker->revoke();
auto border = borderRef.as<winrt::Microsoft::UI::Xaml::Controls::Border>();
auto contentRootGrid = border.FindName(L"ContentRootGrid").try_as<winrt::Microsoft::UI::Xaml::Controls::Grid>();
auto tailPolygon = border.FindName(L"TailPolygon").try_as<winrt::Microsoft::UI::Xaml::Shapes::Polygon>();
if (contentRootGrid)
ApplyAcrylicToGrid(contentRootGrid);
if (contentRootGrid && tailPolygon)
PatchTailBorder(contentRootGrid, tailPolygon);
});
});
}
}