-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathTooltipService.cpp
More file actions
400 lines (335 loc) · 14.6 KB
/
TooltipService.cpp
File metadata and controls
400 lines (335 loc) · 14.6 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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "TooltipService.h"
#include <react/renderer/textlayoutmanager/WindowsTextLayoutManager.h>
#include <CompositionSwitcher.Experimental.interop.h>
#include <Fabric/Composition/CompositionViewComponentView.h>
#include <react/renderer/attributedstring/AttributedStringBox.h>
#include <react/renderer/core/LayoutConstraints.h>
#include <winrt/Microsoft.ReactNative.Composition.h>
#include <winrt/Windows.UI.ViewManagement.h>
#include "TextDrawing.h"
#include "dwmapi.h"
namespace winrt::Microsoft::ReactNative {
constexpr PCWSTR c_tooltipWindowClassName = L"RN_TOOLTIP";
constexpr auto TooltipDataProperty = L"TooltipData";
constexpr float tooltipFontSize = 12;
constexpr float tooltipMaxHeight = 1000;
constexpr float tooltipMaxWidth = 320;
constexpr float tooltipHorizontalPadding = 8;
constexpr float tooltipTopPadding = 5;
constexpr float tooltipBottomPadding = 7;
constexpr float toolTipBorderThickness = 1;
constexpr int toolTipPlacementMargin = 12;
constexpr int toolTipAnimationTimeMs = 200;
constexpr int toolTipTimeToShowMs = 1000;
struct TooltipData {
TooltipData(const winrt::Microsoft::ReactNative::ComponentView &view) : view(view) {}
static TooltipData *GetFromWindow(HWND hwnd) {
auto data = reinterpret_cast<TooltipData *>(GetProp(hwnd, TooltipDataProperty));
return data;
}
int width;
int height;
::Microsoft::ReactNative::ReactTaggedView view;
winrt::com_ptr<::IDWriteTextLayout> textLayout;
facebook::react::AttributedStringBox attributedString;
winrt::Microsoft::ReactNative::Composition::Experimental::ICompositionContext compositionContext;
};
facebook::react::AttributedStringBox CreateTooltipAttributedString(const std::string &tooltip) noexcept {
auto attributedString = facebook::react::AttributedString{};
auto fragment = facebook::react::AttributedString::Fragment{};
fragment.string = tooltip;
fragment.textAttributes.fontSize = tooltipFontSize;
fragment.textAttributes.fontSizeMultiplier =
static_cast<float>(winrt::Windows::UI::ViewManagement::UISettings().TextScaleFactor());
attributedString.appendFragment(std::move(fragment));
return facebook::react::AttributedStringBox{attributedString};
}
LRESULT CALLBACK TooltipWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) noexcept {
switch (message) {
case WM_DESTROY: {
delete TooltipData::GetFromWindow(hwnd);
SetProp(hwnd, TooltipDataProperty, 0);
return 0;
}
case WM_PRINTCLIENT:
case WM_PAINT: {
HDC hdc;
PAINTSTRUCT ps;
if (message != WM_PRINTCLIENT)
hdc = BeginPaint(hwnd, &ps);
else
hdc = (HDC)wparam;
auto data = TooltipData::GetFromWindow(hwnd);
if (auto view = data->view.view()) {
auto scaleFactor = view.LayoutMetrics().PointScaleFactor;
auto ccInterop = data->compositionContext
.as<::Microsoft::ReactNative::Composition::Experimental::ICompositionContextInterop>();
winrt::com_ptr<ID2D1Factory1> factory;
ccInterop->D2DFactory(factory.put());
winrt::com_ptr<ID2D1DCRenderTarget> renderTarget;
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
D2D1_RENDER_TARGET_TYPE_DEFAULT,
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE),
0,
0,
D2D1_RENDER_TARGET_USAGE_NONE,
D2D1_FEATURE_LEVEL_DEFAULT);
winrt::check_hresult(factory->CreateDCRenderTarget(&props, renderTarget.put()));
RECT rc;
GetClientRect(hwnd, &rc);
winrt::check_hresult(renderTarget->BindDC(hdc, &rc));
auto theme = view.as<winrt::Microsoft::ReactNative::Composition::ComponentView>().Theme();
auto selfTheme = winrt::get_self<winrt::Microsoft::ReactNative::Composition::implementation::Theme>(theme);
renderTarget->BeginDraw();
renderTarget->Clear(selfTheme->D2DPlatformColor("ToolTipBackground"));
auto textAttributes = facebook::react::TextAttributes{};
facebook::react::Color fgColor;
fgColor.m_platformColor.push_back("ToolTipForeground");
textAttributes.foregroundColor = fgColor;
winrt::Microsoft::ReactNative::Composition::RenderText(
*renderTarget,
*data->textLayout,
data->attributedString.getValue(),
textAttributes,
{std::round(tooltipHorizontalPadding * scaleFactor), std::round(tooltipTopPadding * scaleFactor)},
scaleFactor,
*selfTheme);
auto hr = renderTarget->EndDraw();
}
if (message != WM_PRINTCLIENT)
EndPaint(hwnd, &ps);
return 0;
}
case WM_NCCREATE: {
auto cs = reinterpret_cast<CREATESTRUCT *>(lparam);
auto data = static_cast<TooltipData *>(cs->lpCreateParams);
WINRT_ASSERT(data);
SetProp(hwnd, TooltipDataProperty, reinterpret_cast<HANDLE>(data));
break;
}
}
return DefWindowProc(hwnd, message, wparam, lparam);
}
void RegisterTooltipWndClass() noexcept {
static bool registered = false;
if (registered) {
return;
}
HINSTANCE hInstance =
GetModuleHandle(NULL); // returns a handle to the file used to create the calling process (.exe file)
WNDCLASSEX wcex = {}; // contains window class information
wcex.cbSize = sizeof(wcex); // size of windows class (bytes)
wcex.style = CS_HREDRAW | CS_VREDRAW; // class style (redraw window on size adjustment)
wcex.lpfnWndProc = &TooltipWndProc; // pointer to windows procedure
wcex.cbClsExtra = DLGWINDOWEXTRA; // extra bytes to allocate
wcex.cbWndExtra = sizeof(TooltipData *); // extra bytes to allocate
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); // handle to class cursor
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); // background color
wcex.lpszClassName = c_tooltipWindowClassName; // specify resource name
ATOM classId = RegisterClassEx(&wcex); // register new windows class
WINRT_VERIFY(classId); // 0 = fail
winrt::check_win32(!classId);
registered = true;
}
// Clamp tooltip position so it stays within the nearest monitor's work area.
// Flips the tooltip below the cursor if it would go above the work area.
void ClampTooltipToMonitor(
POINT cursorScreenPt,
int tooltipWidth,
int tooltipHeight,
float scaleFactor,
int &x,
int &y) noexcept {
HMONITOR hMonitor = MonitorFromPoint(cursorScreenPt, MONITOR_DEFAULTTONEAREST);
if (!hMonitor)
return;
MONITORINFO mi = {};
mi.cbSize = sizeof(mi);
if (!GetMonitorInfo(hMonitor, &mi))
return;
const RECT &workArea = mi.rcWork;
// Clamp horizontally
if (x + tooltipWidth > workArea.right) {
x = workArea.right - tooltipWidth;
}
if (x < workArea.left) {
x = workArea.left;
}
// If tooltip goes above the work area, flip it below the cursor
if (y < workArea.top) {
y = static_cast<int>(cursorScreenPt.y + (toolTipPlacementMargin * scaleFactor));
}
// If tooltip goes below the work area (after flip), clamp to bottom
if (y + tooltipHeight > workArea.bottom) {
y = workArea.bottom - tooltipHeight;
}
}
TooltipTracker::TooltipTracker(
const winrt::Microsoft::ReactNative::ComponentView &view,
const winrt::Microsoft::ReactNative::ReactPropertyBag &properties,
TooltipService *outer)
: m_view(view), m_properties(properties), m_outer(outer) {
view.PointerEntered({this, &TooltipTracker::OnPointerEntered});
view.PointerExited({this, &TooltipTracker::OnPointerExited});
view.PointerMoved({this, &TooltipTracker::OnPointerMoved});
view.Unmounted({this, &TooltipTracker::OnUnmounted});
}
TooltipTracker::~TooltipTracker() {
DestroyTimer();
DestroyTooltip();
}
facebook::react::Tag TooltipTracker::Tag() const noexcept {
return m_view.Tag();
}
void TooltipTracker::OnPointerEntered(
const winrt::Windows::Foundation::IInspectable &sender,
const winrt::Microsoft::ReactNative::Composition::Input::PointerRoutedEventArgs &args) noexcept {
if (args.Pointer().PointerDeviceType() !=
winrt::Microsoft::ReactNative::Composition::Input::PointerDeviceType::Mouse &&
args.Pointer().PointerDeviceType() != winrt::Microsoft::ReactNative::Composition::Input::PointerDeviceType::Pen)
return;
auto pp = args.GetCurrentPoint(-1);
m_pos = pp.Position();
m_timer = winrt::Microsoft::ReactNative::Timer::Create(m_properties.Handle());
m_timer.Interval(std::chrono::milliseconds(toolTipTimeToShowMs));
m_timer.Tick({this, &TooltipTracker::OnTick});
m_timer.Start();
}
void TooltipTracker::OnPointerMoved(
const winrt::Windows::Foundation::IInspectable &sender,
const winrt::Microsoft::ReactNative::Composition::Input::PointerRoutedEventArgs &args) noexcept {
if (args.Pointer().PointerDeviceType() !=
winrt::Microsoft::ReactNative::Composition::Input::PointerDeviceType::Mouse &&
args.Pointer().PointerDeviceType() != winrt::Microsoft::ReactNative::Composition::Input::PointerDeviceType::Pen)
return;
auto pp = args.GetCurrentPoint(-1);
m_pos = pp.Position();
}
void TooltipTracker::OnTick(
const winrt::Windows::Foundation::IInspectable &,
const winrt::Windows::Foundation::IInspectable &) noexcept {
ShowTooltip(m_view.view());
}
void TooltipTracker::OnPointerExited(
const winrt::Windows::Foundation::IInspectable &sender,
const winrt::Microsoft::ReactNative::Composition::Input::PointerRoutedEventArgs &args) noexcept {
if (args.Pointer().PointerDeviceType() !=
winrt::Microsoft::ReactNative::Composition::Input::PointerDeviceType::Mouse &&
args.Pointer().PointerDeviceType() != winrt::Microsoft::ReactNative::Composition::Input::PointerDeviceType::Pen)
return;
DestroyTimer();
DestroyTooltip();
}
void TooltipTracker::DismissActiveTooltip() noexcept {
DestroyTimer();
DestroyTooltip();
}
void TooltipTracker::OnUnmounted(
const winrt::Windows::Foundation::IInspectable &,
const winrt::Microsoft::ReactNative::ComponentView &) noexcept {
DestroyTimer();
DestroyTooltip();
}
void TooltipTracker::ShowTooltip(const winrt::Microsoft::ReactNative::ComponentView &view) noexcept {
DestroyTimer();
if (!m_hwndTip) {
auto viewCompView = view.as<winrt::Microsoft::ReactNative::Composition::ViewComponentView>();
auto selfView =
winrt::get_self<winrt::Microsoft::ReactNative::Composition::implementation::ViewComponentView>(viewCompView);
auto parentHwnd = selfView->GetHwndForParenting();
auto tooltipData = std::make_unique<TooltipData>(view);
tooltipData->attributedString = CreateTooltipAttributedString(*selfView->viewProps()->tooltip);
tooltipData->compositionContext = selfView->CompositionContext();
tooltipData->view = view;
auto scaleFactor = view.LayoutMetrics().PointScaleFactor;
facebook::react::LayoutConstraints layoutConstraints;
layoutConstraints.layoutDirection = facebook::react::LayoutDirection::Undefined;
layoutConstraints.maximumSize.height = tooltipMaxHeight * scaleFactor;
layoutConstraints.maximumSize.width = tooltipMaxWidth * scaleFactor;
layoutConstraints.minimumSize.height = 0;
layoutConstraints.minimumSize.width = 0;
facebook::react::WindowsTextLayoutManager::GetTextLayout(
tooltipData->attributedString, {} /*paragraphAttributes*/, layoutConstraints, tooltipData->textLayout);
if (tooltipData->textLayout) {
DWRITE_TEXT_METRICS tm;
winrt::check_hresult(tooltipData->textLayout->GetMetrics(&tm));
tooltipData->width =
static_cast<int>((tm.width + tooltipHorizontalPadding + tooltipHorizontalPadding) * scaleFactor);
tooltipData->height = static_cast<int>((tm.height + tooltipTopPadding + tooltipBottomPadding) * scaleFactor);
// Convert island-local DIP coordinates to screen pixel coordinates.
// Use LocalToScreen which properly handles both ContentIsland and HWND hosting.
auto screenPt = selfView->LocalToScreen({m_pos.X, m_pos.Y});
POINT pt = {static_cast<LONG>(screenPt.X), static_cast<LONG>(screenPt.Y)};
// Calculate initial desired tooltip position and clamp to monitor work area
int tooltipX = pt.x - tooltipData->width / 2;
int tooltipY = static_cast<int>(pt.y - tooltipData->height - (toolTipPlacementMargin * scaleFactor));
ClampTooltipToMonitor(pt, tooltipData->width, tooltipData->height, scaleFactor, tooltipX, tooltipY);
RegisterTooltipWndClass();
HINSTANCE hInstance = GetModuleHandle(NULL);
m_hwndTip = CreateWindow(
c_tooltipWindowClassName,
L"Tooltip",
WS_POPUP,
tooltipX,
tooltipY,
tooltipData->width,
tooltipData->height,
parentHwnd,
NULL,
hInstance,
tooltipData.get());
DWM_WINDOW_CORNER_PREFERENCE preference = DWMWCP_ROUNDSMALL;
UINT borderThickness = 0;
DwmSetWindowAttribute(m_hwndTip, DWMWA_WINDOW_CORNER_PREFERENCE, &preference, sizeof(preference));
tooltipData.release();
AnimateWindow(m_hwndTip, toolTipAnimationTimeMs, AW_BLEND);
}
}
}
void TooltipTracker::DestroyTooltip() noexcept {
if (m_hwndTip) {
AnimateWindow(m_hwndTip, toolTipAnimationTimeMs, AW_BLEND | AW_HIDE);
DestroyWindow(m_hwndTip);
m_hwndTip = nullptr;
}
}
void TooltipTracker::DestroyTimer() noexcept {
if (m_timer) {
m_timer.Stop();
m_timer = nullptr;
}
}
TooltipService::TooltipService(const winrt::Microsoft::ReactNative::ReactPropertyBag &properties)
: m_properties(properties) {}
void TooltipService::StartTracking(const winrt::Microsoft::ReactNative::ComponentView &view) noexcept {
m_trackers.push_back(std::make_shared<TooltipTracker>(view, m_properties, this));
}
void TooltipService::StopTracking(const winrt::Microsoft::ReactNative::ComponentView &view) noexcept {
for (auto it = m_trackers.begin(); it != m_trackers.end();) {
if ((*it)->Tag() == view.Tag())
it = m_trackers.erase(it);
else
++it;
}
}
void TooltipService::DismissAllTooltips() noexcept {
for (auto &tracker : m_trackers) {
tracker->DismissActiveTooltip();
}
}
static const ReactPropertyId<winrt::Microsoft::ReactNative::ReactNonAbiValue<std::shared_ptr<TooltipService>>>
&TooltipServicePropertyId() noexcept {
static const ReactPropertyId<winrt::Microsoft::ReactNative::ReactNonAbiValue<std::shared_ptr<TooltipService>>> prop{
L"ReactNative", L"TooltipService"};
return prop;
}
std::shared_ptr<TooltipService> TooltipService::GetCurrent(
const winrt::Microsoft::ReactNative::ReactPropertyBag &properties) noexcept {
return *properties.GetOrCreate(TooltipServicePropertyId(), [properties]() -> std::shared_ptr<TooltipService> {
return std::make_shared<TooltipService>(properties);
});
}
} // namespace winrt::Microsoft::ReactNative