Skip to content

fix(panel): improve tray icon positioning logic for macOS - #154

Merged
robinebers merged 7 commits into
robinebers:mainfrom
MuhammadAli511:ui-on-second-monitor
Feb 19, 2026
Merged

robinebers merged 7 commits into
robinebers:mainfrom
MuhammadAli511:ui-on-second-monitor

Conversation

@MuhammadAli511

@MuhammadAli511 MuhammadAli511 commented Feb 10, 2026 •

Copy link
Copy Markdown

Description

Fix panel overlay appearing on primary monitor instead of the monitor where the tray icon was clicked.

Related Issue

Fixes #150

Type of Change

  • Bug fix
  • New feature
  • New provider plugin
  • Documentation
  • Performance improvement
  • Other (describe below)

Testing

  • I ran bun run build and it succeeded
  • I ran bun run test and all tests pass
  • I tested the change locally with bun tauri dev

Screenshots

Before:
Screenshot 2026-02-11 at 2 47 52 AM

After:
Screenshot 2026-02-11 at 2 34 59 AM

Checklist

  • I read CONTRIBUTING.md
  • My PR targets the main branch
  • I did not introduce new dependencies without justification

Summary by cubic

Fixes the macOS panel opening on the wrong display by anchoring it to the monitor under the cursor, with consistent behavior on mixed‑DPI setups. Also prevents edge-case misplacement on monitor seams.

  • Bug Fixes

    • Uses NSEvent::mouseLocation (via objc2-app-kit) in unified point space to pick the correct monitor; flips to top‑left origin, falls back to primary if needed, and uses half‑open bounds to avoid seam double matches.
    • Converts the tray icon’s physical position and size to that monitor’s points, centers the panel under the icon with a 6pt nudge, reads the panel width from the window (or tauri.conf.json), and sets the position using LogicalPosition to avoid mixed‑DPI drift.
  • Refactors

    • Removed leftover debug logs.

Written for commit df7bd51. Summary will update on new commits.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

@robinebers

Copy link
Copy Markdown
Owner

Thank you so much! I can’t easily test it for the next two days. But if others jump in and review this, I’d be happy to merge it.

@robinebers robinebers left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! 👋 This is Rob's AI reviewer. Thanks for the contribution — really appreciate you tackling this multi-monitor bug!

The approach is solid: the coordinate model (logical_pos × scale = physical origin) correctly matches the upstream Tauri bug (tauri-apps/tauri#7890), the fallback to primary monitor is much safer than the old expect() panic, switching to LogicalPosition is the right call, and the inline comments explaining why are excellent. Two things to address before merge:

  1. Icon-width heuristic breaks for dynamic tray icons — The icon_phys_w > 100.0 threshold was likely tested against the static fallback PNG (~132px on Retina). But OpenUsage dynamically replaces the tray icon with variable-width rendered content (bars, circles, text like "75%"). On a 2x display, most of these produce icons well under 100px — for example, bars-only is ~36px, bars + "75%" is ~99px, circle is ~36px. That means the scale detection silently fails for the majority of real-world states, and the overlapping-region disambiguation (the whole point of the fix) won't work. A more robust approach: instead of inferring scale from icon size, compare icon_phys_w against the monitor's own scale factor when disambiguating. For example, check if (icon_phys_w / expected_logical_w) is closer to each candidate monitor's scale, or simply use the monitor whose scale factor best explains the icon's reported physical dimensions.

  2. Hardcoded panel width — let panel_width = 400.0_f64 duplicates a value from tauri.conf.json. The old code read this from window.outer_size(), which stayed in sync automatically. If the configured width ever changes, this silently breaks positioning. Consider using window.outer_size() again, converting to logical points.

Everything else — the monitor detection logic, the coordinate math, graceful fallback, and the quality of the comments — looks great. Nice work!

@robinebers robinebers left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs addressing of icon width

@MuhammadAli511

Copy link
Copy Markdown
Author

Looking into it.

Copilot AI review requested due to automatic review settings February 12, 2026 17:23
@MuhammadAli511

Copy link
Copy Markdown
Author

@robinebers Fixed the PR comments.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 3 files (changes from recent commits).

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="src-tauri/src/panel.rs">

<violation number="1" location="src-tauri/src/panel.rs:162">
P2: Monitor hit-testing mixes logical mouse coordinates with Tauri’s physical monitor positions, so HiDPI displays can fail to detect the cursor’s monitor and fall back to primary.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread src-tauri/src/panel.rs Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request fixes a bug where the panel overlay appeared on the primary monitor instead of the monitor where the tray icon was clicked in multi-monitor macOS setups. The fix uses NSEvent::mouseLocation() to unambiguously determine which monitor contains the cursor, then converts the tray icon's coordinates to logical coordinates within that monitor for proper panel positioning.

Changes:

  • Replaced direct tray coordinate-based monitor detection with cursor position-based detection using macOS's NSEvent API
  • Added coordinate space conversion logic to handle mixed-DPI setups
  • Implemented fallback to primary monitor when cursor position cannot be determined
  • Added objc2-app-kit dependency for NSEvent access

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 12 comments.

File Description
src-tauri/src/panel.rs Rewrote position_panel_at_tray_icon function to use NSEvent::mouseLocation() for monitor detection and added coordinate conversion logic for mixed-DPI setups
src-tauri/Cargo.toml Added objc2-app-kit dependency with NSEvent, NSScreen, and NSGraphics features
src-tauri/Cargo.lock Updated lock file to include objc2-app-kit dependency

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src-tauri/src/panel.rs Resolved
Comment thread src-tauri/src/panel.rs Resolved
Comment thread src-tauri/src/panel.rs Resolved
Comment thread src-tauri/src/panel.rs Resolved
Comment thread src-tauri/src/panel.rs Resolved
Comment thread src-tauri/src/panel.rs Resolved
Comment thread src-tauri/Cargo.toml Resolved
Comment thread src-tauri/src/panel.rs Resolved
Comment thread src-tauri/src/panel.rs Resolved
Comment thread src-tauri/src/panel.rs Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file (changes from recent commits).

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="src-tauri/src/panel.rs">

<violation number="1" location="src-tauri/src/panel.rs:162">
P2: Monitor position is physical pixels while mouse coordinates are logical points; comparing them directly mixes units and can mis-detect the monitor on HiDPI setups.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread src-tauri/src/panel.rs Resolved
robinebers and others added 2 commits February 19, 2026 08:57
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
Prevent edge-case double matches when the cursor is exactly on monitor seams by using half-open monitor bounds.

Co-authored-by: Cursor <[email protected]>
@robinebers
robinebers merged commit d3fb059 into robinebers:main Feb 19, 2026
3 checks passed
@MuhammadAli511
MuhammadAli511 deleted the ui-on-second-monitor branch February 21, 2026 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong position on secondary screen

3 participants