Skip to content

Commit 53071e2

Browse files
committed
Fix tooltips in high dpi (microsoft#14397)
* Fix tooltips in high dpi * Change files * format * tooltips dont respect textscalefactor
1 parent 622c095 commit 53071e2

File tree

3 files changed

+49
-38
lines changed

3 files changed

+49
-38
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Fix tooltips in high dpi",
4+
"packageName": "react-native-windows",
5+
"email": "30809111+acoates-ms@users.noreply.github.com",
6+
"dependentChangeType": "patch"
7+
}

vnext/Microsoft.ReactNative/Fabric/Composition/CompositionViewComponentView.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void ComponentView::updateProps(
183183
updateShadowProps(oldViewProps, newViewProps);
184184
}
185185
if (oldViewProps.tooltip != newViewProps.tooltip) {
186-
if (!m_tooltipTracked && newViewProps.tooltip) {
186+
if (!m_tooltipTracked && newViewProps.tooltip && !newViewProps.tooltip->empty()) {
187187
TooltipService::GetCurrent(m_reactContext.Properties())->StartTracking(*this);
188188
m_tooltipTracked = true;
189189
} else if (m_tooltipTracked && !newViewProps.tooltip) {

vnext/Microsoft.ReactNative/Fabric/Composition/TooltipService.cpp

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <react/renderer/core/LayoutConstraints.h>
1111
#include <react/renderer/textlayoutmanager/TextLayoutManager.h>
1212
#include <winrt/Microsoft.ReactNative.Composition.h>
13+
#include <winrt/Windows.UI.ViewManagement.h>
1314
#include "TextDrawing.h"
1415
#include "dwmapi.h"
1516

@@ -49,7 +50,9 @@ facebook::react::AttributedStringBox CreateTooltipAttributedString(const std::st
4950
auto fragment = facebook::react::AttributedString::Fragment{};
5051
fragment.string = tooltip;
5152
fragment.textAttributes.fontSize = tooltipFontSize;
52-
attributedString.appendFragment(fragment);
53+
fragment.textAttributes.fontSizeMultiplier =
54+
static_cast<float>(winrt::Windows::UI::ViewManagement::UISettings().TextScaleFactor());
55+
attributedString.appendFragment(std::move(fragment));
5356
return facebook::react::AttributedStringBox{attributedString};
5457
}
5558

@@ -231,14 +234,13 @@ void TooltipTracker::OnUnmounted(
231234
}
232235

233236
void TooltipTracker::ShowTooltip(const winrt::Microsoft::ReactNative::ComponentView &view) noexcept {
234-
auto viewCompView = view.as<winrt::Microsoft::ReactNative::Composition::ViewComponentView>();
235-
236-
auto selfView =
237-
winrt::get_self<winrt::Microsoft::ReactNative::Composition::implementation::ViewComponentView>(viewCompView);
238-
auto parentHwnd = selfView->GetHwndForParenting();
239237
DestroyTimer();
240238

241239
if (!m_hwndTip) {
240+
auto viewCompView = view.as<winrt::Microsoft::ReactNative::Composition::ViewComponentView>();
241+
auto selfView =
242+
winrt::get_self<winrt::Microsoft::ReactNative::Composition::implementation::ViewComponentView>(viewCompView);
243+
auto parentHwnd = selfView->GetHwndForParenting();
242244
auto tooltipData = std::make_unique<TooltipData>(view);
243245
tooltipData->attributedString = CreateTooltipAttributedString(*selfView->viewProps()->tooltip);
244246

@@ -256,37 +258,39 @@ void TooltipTracker::ShowTooltip(const winrt::Microsoft::ReactNative::ComponentV
256258
facebook::react::TextLayoutManager::GetTextLayout(
257259
tooltipData->attributedString, {} /*paragraphAttributes*/, layoutConstraints, tooltipData->textLayout);
258260

259-
DWRITE_TEXT_METRICS tm;
260-
winrt::check_hresult(tooltipData->textLayout->GetMetrics(&tm));
261-
262-
tooltipData->width =
263-
static_cast<int>(tm.width + ((tooltipHorizontalPadding + tooltipHorizontalPadding) * scaleFactor));
264-
tooltipData->height = static_cast<int>(tm.height + ((tooltipTopPadding + tooltipBottomPadding) * scaleFactor));
265-
266-
POINT pt = {static_cast<LONG>(m_pos.X), static_cast<LONG>(m_pos.Y)};
267-
ClientToScreen(parentHwnd, &pt);
268-
269-
RegisterTooltipWndClass();
270-
HINSTANCE hInstance = GetModuleHandle(NULL);
271-
m_hwndTip = CreateWindow(
272-
c_tooltipWindowClassName,
273-
L"Tooltip",
274-
WS_POPUP,
275-
pt.x - tooltipData->width / 2,
276-
static_cast<int>(pt.y - tooltipData->height - (toolTipPlacementMargin * scaleFactor)),
277-
tooltipData->width,
278-
tooltipData->height,
279-
parentHwnd,
280-
NULL,
281-
hInstance,
282-
tooltipData.get());
283-
284-
DWM_WINDOW_CORNER_PREFERENCE preference = DWMWCP_ROUNDSMALL;
285-
UINT borderThickness = 0;
286-
DwmSetWindowAttribute(m_hwndTip, DWMWA_WINDOW_CORNER_PREFERENCE, &preference, sizeof(preference));
287-
288-
tooltipData.release();
289-
AnimateWindow(m_hwndTip, toolTipAnimationTimeMs, AW_BLEND);
261+
if (tooltipData->textLayout) {
262+
DWRITE_TEXT_METRICS tm;
263+
winrt::check_hresult(tooltipData->textLayout->GetMetrics(&tm));
264+
265+
tooltipData->width =
266+
static_cast<int>((tm.width + tooltipHorizontalPadding + tooltipHorizontalPadding) * scaleFactor);
267+
tooltipData->height = static_cast<int>((tm.height + tooltipTopPadding + tooltipBottomPadding) * scaleFactor);
268+
269+
POINT pt = {static_cast<LONG>(m_pos.X), static_cast<LONG>(m_pos.Y)};
270+
ClientToScreen(parentHwnd, &pt);
271+
272+
RegisterTooltipWndClass();
273+
HINSTANCE hInstance = GetModuleHandle(NULL);
274+
m_hwndTip = CreateWindow(
275+
c_tooltipWindowClassName,
276+
L"Tooltip",
277+
WS_POPUP,
278+
pt.x - tooltipData->width / 2,
279+
static_cast<int>(pt.y - tooltipData->height - (toolTipPlacementMargin * scaleFactor)),
280+
tooltipData->width,
281+
tooltipData->height,
282+
parentHwnd,
283+
NULL,
284+
hInstance,
285+
tooltipData.get());
286+
287+
DWM_WINDOW_CORNER_PREFERENCE preference = DWMWCP_ROUNDSMALL;
288+
UINT borderThickness = 0;
289+
DwmSetWindowAttribute(m_hwndTip, DWMWA_WINDOW_CORNER_PREFERENCE, &preference, sizeof(preference));
290+
291+
tooltipData.release();
292+
AnimateWindow(m_hwndTip, toolTipAnimationTimeMs, AW_BLEND);
293+
}
290294
}
291295
}
292296

0 commit comments

Comments
 (0)