From ee5979d62ed4d29b7841cb77dac3870725eb64e4 Mon Sep 17 00:00:00 2001 From: c2r0b Date: Sat, 24 Aug 2024 17:29:20 +0200 Subject: [PATCH 1/2] fix macOS external display support --- Cargo.toml | 2 +- examples/multi/package.json | 2 +- examples/ts-utility/package.json | 2 +- examples/vanilla/package.json | 2 +- lerna.json | 2 +- package.json | 2 +- plugin/package.json | 2 +- src/macos.rs | 45 +++++++++++++++++++++++++++----- 8 files changed, 46 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 447de1b..d6a6384 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/examples/multi/package.json b/examples/multi/package.json index b029761..ff821b0 100644 --- a/examples/multi/package.json +++ b/examples/multi/package.json @@ -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", diff --git a/examples/ts-utility/package.json b/examples/ts-utility/package.json index 8427fbc..3aa7f4f 100644 --- a/examples/ts-utility/package.json +++ b/examples/ts-utility/package.json @@ -1,6 +1,6 @@ { "name": "ts-utility-example", - "version": "0.8.0", + "version": "0.8.1", "main": "index.js", "type": "module", "scripts": { diff --git a/examples/vanilla/package.json b/examples/vanilla/package.json index e96adf4..5ba4f2a 100644 --- a/examples/vanilla/package.json +++ b/examples/vanilla/package.json @@ -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", diff --git a/lerna.json b/lerna.json index 2337e8d..3be9b8b 100644 --- a/lerna.json +++ b/lerna.json @@ -1,4 +1,4 @@ { "$schema": "node_modules/lerna/schemas/lerna-schema.json", - "version": "0.8.0" + "version": "0.8.1" } diff --git a/package.json b/package.json index 217c768..b42d3e0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/plugin/package.json b/plugin/package.json index 82ba741..a2ca254 100644 --- a/plugin/package.json +++ b/plugin/package.json @@ -1,6 +1,6 @@ { "name": "tauri-plugin-context-menu", - "version": "0.8.0", + "version": "0.8.1", "author": "c2r0b", "type": "module", "description": "", diff --git a/src/macos.rs b/src/macos.rs index 01725da..b393079 100644 --- a/src/macos.rs +++ b/src/macos.rs @@ -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(_self: &Object, _cmd: Sel, _item: id) { // Get the window from the CURRENT_WINDOW static let window_arc: Arc> = match CURRENT_WINDOW.get_window() { @@ -240,20 +244,49 @@ pub fn show_context_menu( // 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) } }, From 102159845332eb4cf9df44c2993b8272ef3f81d6 Mon Sep 17 00:00:00 2001 From: c2r0b Date: Sat, 24 Aug 2024 22:50:59 +0200 Subject: [PATCH 2/2] add and fix changelog --- .changes/0.2.0.md | 2 +- .changes/0.3.0.md | 2 +- .changes/0.4.0.md | 2 +- .changes/0.6.0.md | 2 +- .changes/0.7.0.md | 2 +- .changes/0.8.0.md | 2 +- .changes/0.8.1.md | 4 ++++ 7 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 .changes/0.8.1.md diff --git a/.changes/0.2.0.md b/.changes/0.2.0.md index 10b86ce..02a7f8b 100644 --- a/.changes/0.2.0.md +++ b/.changes/0.2.0.md @@ -1,5 +1,5 @@ --- -"tauri-plugin-context-menu": "minor" +"tauri-plugin-context-menu": "major" --- - Add separator support on MacOS diff --git a/.changes/0.3.0.md b/.changes/0.3.0.md index d4642e3..4566f35 100644 --- a/.changes/0.3.0.md +++ b/.changes/0.3.0.md @@ -1,5 +1,5 @@ --- -"tauri-plugin-context-menu": "minor" +"tauri-plugin-context-menu": "major" --- - Add support for Windows diff --git a/.changes/0.4.0.md b/.changes/0.4.0.md index bc1ca5a..81e1099 100644 --- a/.changes/0.4.0.md +++ b/.changes/0.4.0.md @@ -1,5 +1,5 @@ --- -"tauri-plugin-context-menu": "minor" +"tauri-plugin-context-menu": "major" --- - Add icon support on Windows diff --git a/.changes/0.6.0.md b/.changes/0.6.0.md index 64f8143..f991992 100644 --- a/.changes/0.6.0.md +++ b/.changes/0.6.0.md @@ -1,5 +1,5 @@ --- -"tauri-plugin-context-menu": "minor" +"tauri-plugin-context-menu": "major" --- - Add linux support \ No newline at end of file diff --git a/.changes/0.7.0.md b/.changes/0.7.0.md index 5fbe8e3..ed9389c 100644 --- a/.changes/0.7.0.md +++ b/.changes/0.7.0.md @@ -1,5 +1,5 @@ --- -"tauri-plugin-context-menu": "minor" +"tauri-plugin-context-menu": "major" --- - Add checked items support #10 diff --git a/.changes/0.8.0.md b/.changes/0.8.0.md index 5f1324a..b8af110 100644 --- a/.changes/0.8.0.md +++ b/.changes/0.8.0.md @@ -1,5 +1,5 @@ --- -"tauri-plugin-context-menu": "minor" +"tauri-plugin-context-menu": "major" --- - Add support for `theme` option diff --git a/.changes/0.8.1.md b/.changes/0.8.1.md new file mode 100644 index 0000000..eaa146e --- /dev/null +++ b/.changes/0.8.1.md @@ -0,0 +1,4 @@ +--- +"tauri-plugin-context-menu": "minor" +--- +- Fix support for external display on MacOS #25 \ No newline at end of file