Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Clamp Tooltip Position To Monitor",
"packageName": "react-native-windows",
"email": "66076509+vineethkuttan@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,39 @@ LRESULT CALLBACK TooltipWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM l
return DefWindowProc(hwnd, message, wparam, lparam);
}

POINT ClampTooltipPositionToMonitor(const POINT &anchorPt, int tooltipWidth, int tooltipHeight, int margin) noexcept {
POINT result = {anchorPt.x - tooltipWidth / 2, anchorPt.y - tooltipHeight - margin};

HMONITOR hMonitor = MonitorFromPoint(anchorPt, MONITOR_DEFAULTTONEAREST);
MONITORINFO monitorInfo = {};
monitorInfo.cbSize = sizeof(monitorInfo);
if (!GetMonitorInfo(hMonitor, &monitorInfo)) {
return result;
}

const RECT &bounds = monitorInfo.rcWork;

if (result.y < bounds.top) {
result.y = anchorPt.y + margin;
}

if (result.y + tooltipHeight > bounds.bottom) {
result.y = bounds.bottom - tooltipHeight;
}
if (result.y < bounds.top) {
result.y = bounds.top;
Comment thread
vineethkuttan marked this conversation as resolved.
}

if (result.x + tooltipWidth > bounds.right) {
result.x = bounds.right - tooltipWidth;
}
if (result.x < bounds.left) {
result.x = bounds.left;
}

return result;
}

void RegisterTooltipWndClass() noexcept {
static bool registered = false;
if (registered) {
Expand Down Expand Up @@ -334,14 +367,17 @@ void TooltipTracker::ShowTooltip(const winrt::Microsoft::ReactNative::ComponentV
POINT pt = {static_cast<LONG>(m_pos.X * scaleFactor), static_cast<LONG>(m_pos.Y * scaleFactor)};
ClientToScreen(parentHwnd, &pt);

POINT tooltipPos = ClampTooltipPositionToMonitor(
pt, tooltipData->width, tooltipData->height, static_cast<int>(toolTipPlacementMargin * scaleFactor));

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)),
tooltipPos.x,
tooltipPos.y,
tooltipData->width,
tooltipData->height,
parentHwnd,
Expand Down
Loading