Skip to content

Commit

Permalink
Merge pull request #30 from c2r0b/dev
Browse files Browse the repository at this point in the history
Fix macOS external display support
  • Loading branch information
c2r0b authored Aug 24, 2024
2 parents 992019b + 1021598 commit ae48f4e
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .changes/0.2.0.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"tauri-plugin-context-menu": "minor"
"tauri-plugin-context-menu": "major"
---

- Add separator support on MacOS
Expand Down
2 changes: 1 addition & 1 deletion .changes/0.3.0.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"tauri-plugin-context-menu": "minor"
"tauri-plugin-context-menu": "major"
---

- Add support for Windows
2 changes: 1 addition & 1 deletion .changes/0.4.0.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"tauri-plugin-context-menu": "minor"
"tauri-plugin-context-menu": "major"
---

- Add icon support on Windows
Expand Down
2 changes: 1 addition & 1 deletion .changes/0.6.0.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"tauri-plugin-context-menu": "minor"
"tauri-plugin-context-menu": "major"
---

- Add linux support
2 changes: 1 addition & 1 deletion .changes/0.7.0.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"tauri-plugin-context-menu": "minor"
"tauri-plugin-context-menu": "major"
---

- Add checked items support #10
Expand Down
2 changes: 1 addition & 1 deletion .changes/0.8.0.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"tauri-plugin-context-menu": "minor"
"tauri-plugin-context-menu": "major"
---

- Add support for `theme` option
Expand Down
4 changes: 4 additions & 0 deletions .changes/0.8.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
"tauri-plugin-context-menu": "minor"
---
- Fix support for external display on MacOS #25
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tauri-plugin-context-menu"
version = "0.8.0"
version = "0.8.1"
authors = [ "c2r0b" ]
description = "Handle native Context Menu in Tauri"
license = "MIT OR Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/multi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "multi-example",
"version": "0.8.0",
"version": "0.8.1",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
Expand Down
2 changes: 1 addition & 1 deletion examples/ts-utility/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-utility-example",
"version": "0.8.0",
"version": "0.8.1",
"main": "index.js",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion examples/vanilla/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vanilla-example",
"version": "0.8.0",
"version": "0.8.1",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "0.8.0"
"version": "0.8.1"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tauri-plugin-context-menu",
"version": "0.8.0",
"version": "0.8.1",
"author": "c2r0b",
"description": "",
"homepage": "https://github.com/c2r0b/tauri-plugin-context-menu",
Expand Down
2 changes: 1 addition & 1 deletion plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tauri-plugin-context-menu",
"version": "0.8.0",
"version": "0.8.1",
"author": "c2r0b",
"type": "module",
"description": "",
Expand Down
45 changes: 39 additions & 6 deletions src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ use crate::macos_window_holder::CURRENT_WINDOW;
use crate::theme::Theme;
use crate::{MenuItem, Position};

extern "C" {
fn NSPointInRect(aPoint: NSPoint, aRect: NSRect) -> bool;
}

extern "C" fn menu_item_action<R: Runtime>(_self: &Object, _cmd: Sel, _item: id) {
// Get the window from the CURRENT_WINDOW static
let window_arc: Arc<tauri::Window<R>> = match CURRENT_WINDOW.get_window() {
Expand Down Expand Up @@ -240,20 +244,49 @@ pub fn show_context_menu<R: Runtime>(
// Convert web page coordinates to screen coordinates
Some(pos) if pos.x != 0.0 || pos.y != 0.0 => unsafe {
let window_position = window.outer_position().unwrap();
let screen: id = msg_send![class!(NSScreen), mainScreen];
let frame: NSRect = msg_send![screen, frame];
let screen_height = frame.size.height;

// Get all screens and the mouse location
let screens: id = msg_send![class!(NSScreen), screens];
let screen_count: usize = msg_send![screens, count];
let mouse_location: NSPoint = msg_send![class!(NSEvent), mouseLocation];

// Find the screen under the mouse cursor
let mut target_screen: id = nil;
let mut target_screen_frame: NSRect =
NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(0.0, 0.0));

for i in 0..screen_count {
let screen: id = msg_send![screens, objectAtIndex:i];
let frame: NSRect = msg_send![screen, frame];
if NSPointInRect(mouse_location, frame) {
target_screen = screen;
target_screen_frame = frame;
break;
}
}

// Fallback to the main screen if no specific screen found
if target_screen == nil {
target_screen = msg_send![class!(NSScreen), mainScreen];
target_screen_frame = msg_send![target_screen, frame];
}

let screen_height = target_screen_frame.size.height;
let screen_origin_y = target_screen_frame.origin.y;
let scale_factor = match window.scale_factor() {
Ok(factor) => factor,
Err(_) => 1.0, // Use a default value if getting the scale factor fails
Err(_) => 1.0, // Default to 1.0 if scale factor can't be retrieved
};

if pos.is_absolute.unwrap_or(false) {
let x = pos.x;
let y = screen_height - pos.y;
let y = screen_origin_y + screen_height - pos.y;
NSPoint::new(x, y)
} else {
let x = pos.x + (window_position.x as f64 / scale_factor);
let y = screen_height - (window_position.y as f64 / scale_factor) - pos.y;
let y = screen_origin_y + screen_height
- (window_position.y as f64 / scale_factor)
- pos.y;
NSPoint::new(x, y)
}
},
Expand Down

0 comments on commit ae48f4e

Please sign in to comment.