-
Notifications
You must be signed in to change notification settings - Fork 675
/
CppWinRTHelpers.h
364 lines (310 loc) · 10.1 KB
/
CppWinRTHelpers.h
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#pragma once
using winrt::com_ptr;
using winrt::weak_ref;
using winrt::hstring;
using std::wstring_view;
namespace winrt
{
template <typename T>
inline hstring hstring_name_of() noexcept
{
return hstring(winrt::name_of<T>());
}
}
inline winrt::IInspectable& to_winrt(::IInspectable*& instance)
{
return reinterpret_cast<winrt::IInspectable&>(instance);
}
inline winrt::DependencyProperty InitializeDependencyProperty(
wstring_view const& propertyNameString,
wstring_view const& propertyTypeNameString,
wstring_view const& ownerTypeNameString,
bool isAttached,
winrt::IInspectable const& defaultValue,
winrt::PropertyChangedCallback const& propertyChangedCallback = nullptr)
{
auto propertyType = winrt::Interop::TypeName();
propertyType.Name = propertyTypeNameString;
propertyType.Kind = winrt::Interop::TypeKind::Metadata;
auto ownerType = winrt::Interop::TypeName();
ownerType.Name = ownerTypeNameString;
ownerType.Kind = winrt::Interop::TypeKind::Metadata;
auto propertyMetadata = winrt::PropertyMetadata(defaultValue, propertyChangedCallback);
if (isAttached)
{
return winrt::DependencyProperty::RegisterAttached(propertyNameString, propertyType, ownerType, propertyMetadata);
}
else
{
return winrt::DependencyProperty::Register(propertyNameString, propertyType, ownerType, propertyMetadata);
}
}
// Helper to provide default values and boxing without differences at the call sites
template <typename T, typename Enable = void>
struct ValueHelper
{
static T GetDefaultValue()
{
#pragma warning(push)
#pragma warning(disable : 26444)
return T{};
#pragma warning(pop)
}
static winrt::IInspectable BoxValueIfNecessary(T const& value)
{
return winrt::box_value(value);
}
static T CastOrUnbox(winrt::IInspectable const& value)
{
return winrt::unbox_value<T>(value);
}
static winrt::IInspectable BoxedDefaultValue()
{
return BoxValueIfNecessary(GetDefaultValue());
}
};
template <typename T>
struct ValueHelper<T, std::enable_if_t<std::is_base_of_v<winrt::Windows::Foundation::IUnknown, T>>>
{
static T GetDefaultValue()
{
return T{ nullptr };
}
static winrt::IInspectable BoxValueIfNecessary(T const& value)
{
return value;
}
static T CastOrUnbox(winrt::IInspectable const& value)
{
return value.as<T>();
}
static winrt::IInspectable BoxedDefaultValue()
{
return GetDefaultValue();
}
};
template <>
struct ValueHelper<winrt::hstring, void>
{
static winrt::hstring GetDefaultValue()
{
return winrt::hstring{ L"" };
}
static winrt::IInspectable BoxValueIfNecessary(winrt::hstring const& value)
{
return winrt::box_value(value);
}
static winrt::hstring CastOrUnbox(winrt::IInspectable const& value)
{
return winrt::unbox_value<winrt::hstring>(value);
}
static winrt::IInspectable BoxedDefaultValue()
{
return winrt::box_value(L"");
}
};
void SetDefaultStyleKeyWorker(winrt::IControlProtected const& controlProtected, std::wstring_view const& className);
template <typename TThis>
void SetDefaultStyleKey(TThis *pThis)
{
SetDefaultStyleKeyWorker(*pThis, pThis->GetRuntimeClassName());
}
template<typename WinRTReturn>
WinRTReturn GetTemplateChildT(std::wstring_view const& childName, const winrt::IControlProtected& controlProtected)
{
winrt::DependencyObject childAsDO(controlProtected.GetTemplateChild(childName));
if (childAsDO)
{
return childAsDO.try_as<WinRTReturn>();
}
return nullptr;
}
struct PropertyChanged_revoker
{
PropertyChanged_revoker() noexcept = default;
PropertyChanged_revoker(PropertyChanged_revoker const&) = delete;
PropertyChanged_revoker& operator=(PropertyChanged_revoker const&) = delete;
PropertyChanged_revoker(PropertyChanged_revoker&& other) noexcept
{
move_from(other);
}
PropertyChanged_revoker& operator=(PropertyChanged_revoker&& other) noexcept
{
move_from(other);
return *this;
}
PropertyChanged_revoker(winrt::DependencyObject const& object, const winrt::DependencyProperty& dp, int64_t token) :
m_object(object),
m_property(dp),
m_token(token)
{}
~PropertyChanged_revoker() noexcept
{
revoke();
}
void revoke() noexcept
{
if (!m_object)
{
return;
}
if (auto object = m_object.get())
{
object.UnregisterPropertyChangedCallback(m_property, m_token);
}
m_object = nullptr;
}
explicit operator bool() const noexcept
{
return static_cast<bool>(m_object);
}
private:
void move_from(PropertyChanged_revoker& other)
{
if (this != &other)
{
revoke();
m_object = other.m_object;
m_token = other.m_token;
m_property = other.m_property;
other.m_object = nullptr;
other.m_token = 0;
other.m_property = nullptr;
}
}
weak_ref<winrt::DependencyObject> m_object;
winrt::DependencyProperty m_property{ nullptr };
int64_t m_token{};
};
inline PropertyChanged_revoker RegisterPropertyChanged(winrt::DependencyObject const& object, winrt::DependencyProperty const& dp, winrt::DependencyPropertyChangedCallback const& callback)
{
const auto value = object.RegisterPropertyChangedCallback(dp, callback);
return { object, dp, value };
}
inline bool SetFocus(winrt::DependencyObject const& object, winrt::FocusState focusState)
{
if (object)
{
// Use TryFocusAsync if it's available.
if (auto focusManager5 = winrt::get_activation_factory<winrt::FocusManager, winrt::IFocusManagerStatics>().try_as<winrt::IFocusManagerStatics5>())
{
auto result = focusManager5.TryFocusAsync(object, focusState);
if (result.Status() == winrt::AsyncStatus::Completed)
{
return result.GetResults().Succeeded();
}
// Operation was async, let's assume it worked.
return true;
}
if (auto control = object.try_as<winrt::Control>())
{
return control.Focus(focusState);
}
else if (auto hyperlink = object.try_as<winrt::Hyperlink>())
{
return hyperlink.Focus(focusState);
}
else if (auto contentlink = object.try_as<winrt::ContentLink>())
{
return contentlink.Focus(focusState);
}
else if (auto webview = object.try_as<winrt::WebView>())
{
return webview.Focus(focusState);
}
}
return false;
}
// This type exists for types that in metadata derive from FrameworkElement but internally want to derive from Panel
// to get "protected" Children.
// Using it is just like any other winrt::implementation::FooT type *except* that you must pass an additional parameter
// which is the winrt::Foo type. Most times you will be using ReferenceTracker too so that pattern would look like:
// class YourType : ReferenceTracker<YourType, DeriveFromPanelHelper_base, winrt::YourType> <--- note the extra winrt::YourType here that's normally not needed.
template <typename D, typename T, typename ... I>
struct __declspec(empty_bases) DeriveFromPanelHelper_base : winrt::Windows::UI::Xaml::Controls::PanelT<D, winrt::default_interface<T>, winrt::composable, I...>
{
using composable = T;
using class_type = typename T;
operator class_type() const noexcept
{
return static_cast<winrt::IInspectable>(*this).as<class_type>();
}
hstring GetRuntimeClassName() const
{
return hstring{ winrt::name_of<T>() };
}
};
template <typename D, typename T, typename ... I>
struct __declspec(empty_bases)DeriveFromBitmapIconHelper_base : winrt::Windows::UI::Xaml::Controls::BitmapIconT<D, winrt::default_interface<T>, winrt::composable, I...>
{
using composable = T;
using class_type = typename T;
operator class_type() const noexcept
{
return static_cast<winrt::IInspectable>(*this).as<class_type>();
}
hstring GetRuntimeClassName() const
{
return hstring{ winrt::name_of<T>() };
}
};
//
// An awaitable object. Completes when the CompleteAwaits() method is called.
// cf. .NET's TaskCompletionSource.
//
struct Awaitable
{
Awaitable()
{
m_signal.attach(::CreateEvent(nullptr, true, false, nullptr));
}
// Awaitable contract.
// Blocks until the awaitable is completed, or error.
bool await_ready() const noexcept
{
return ::WaitForSingleObject(m_signal.get(), 0) == 0;
}
// Registers a callback that will be called when the awaitable is completed.
void await_suspend(std::experimental::coroutine_handle<> resume)
{
m_wait.attach(
winrt::check_pointer(
::CreateThreadpoolWait(CoroutineCompletedCallback, resume.address(), nullptr)
)
);
::SetThreadpoolWait(m_wait.get(), m_signal.get(), nullptr);
}
// Called to get the value of the awaitable.
// This awaitable has no value, so nothing to do.
void await_resume() const noexcept { }
protected:
void CompleteAwaits() const noexcept
{
::SetEvent(m_signal.get());
}
private:
static void __stdcall CoroutineCompletedCallback(PTP_CALLBACK_INSTANCE, void* context, PTP_WAIT, TP_WAIT_RESULT) noexcept
{
// Resumes anyone waiting on the awaitable.
std::experimental::coroutine_handle<>::from_address(context)();
}
//
// Describes a PTP_WAIT.
//
struct PTP_WAIT_traits
{
using type = PTP_WAIT;
static void close(type value) noexcept
{
::CloseThreadpoolWait(value);
}
static constexpr type invalid() noexcept
{
return nullptr;
}
};
winrt::handle m_signal;
winrt::handle_type<PTP_WAIT_traits> m_wait;
};