-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix tooltip positioning for ContentIsland and dismiss on scroll #15644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7718169
55c9416
896772f
63c7174
cdc06f2
e48b590
92b7423
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "prerelease", | ||
| "comment": "fix: Fix tooltip positioning for ContentIsland and dismiss on scroll", | ||
| "packageName": "react-native-windows", | ||
| "email": "nitchaudhary@microsoft.com", | ||
| "dependentChangeType": "patch" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,6 +161,45 @@ void RegisterTooltipWndClass() noexcept { | |
| 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( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should also handle negative or zero tooltip size , in case of integer overflow from bad text metrics |
||
| 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in case of very wide tooltip , tooltipWidth > (workArea.right - workArea.left), the first clamp sets x to a value less than workArea.left, and then the second clamp overrides it to workArea.left. I guess tooltip will overflow on the right suggestion |
||
| 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, | ||
|
|
@@ -227,6 +266,11 @@ void TooltipTracker::OnPointerExited( | |
| DestroyTooltip(); | ||
| } | ||
|
|
||
| void TooltipTracker::DismissActiveTooltip() noexcept { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should also dismiss the current tracking state, next hover should always new state |
||
| DestroyTimer(); | ||
| DestroyTooltip(); | ||
| } | ||
|
|
||
| void TooltipTracker::OnUnmounted( | ||
| const winrt::Windows::Foundation::IInspectable &, | ||
| const winrt::Microsoft::ReactNative::ComponentView &) noexcept { | ||
|
|
@@ -267,17 +311,24 @@ void TooltipTracker::ShowTooltip(const winrt::Microsoft::ReactNative::ComponentV | |
| static_cast<int>((tm.width + tooltipHorizontalPadding + tooltipHorizontalPadding) * scaleFactor); | ||
| tooltipData->height = static_cast<int>((tm.height + tooltipTopPadding + tooltipBottomPadding) * scaleFactor); | ||
|
|
||
| POINT pt = {static_cast<LONG>(m_pos.X * scaleFactor), static_cast<LONG>(m_pos.Y * scaleFactor)}; | ||
| ClientToScreen(parentHwnd, &pt); | ||
| // 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, | ||
| pt.x - tooltipData->width / 2, | ||
| static_cast<int>(pt.y - tooltipData->height - (toolTipPlacementMargin * scaleFactor)), | ||
| tooltipX, | ||
| tooltipY, | ||
| tooltipData->width, | ||
| tooltipData->height, | ||
| parentHwnd, | ||
|
|
@@ -326,6 +377,12 @@ void TooltipService::StopTracking(const winrt::Microsoft::ReactNative::Component | |
| } | ||
| } | ||
|
|
||
| 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{ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetCurrent can return nullptr if the TooltipService hasn't been initialized, right ?
This might lead to crashes