Skip to content

Commit 6d7a19f

Browse files
authored
Merge pull request #138 from ahkohd/share-picker
refactor: use position
2 parents 923b56f + f24b72c commit 6d7a19f

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

libs/share-picker/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,26 @@ share-picker = { git = "https://github.com/ahkohd/tauri-toolkit", branch = "v2"
2323
## Usage
2424
```rust
2525
use share_picker::{SharePicker, PreferredEdge};
26+
use tauri::PhysicalPosition;
2627

2728
fn main() {
2829
let window = app_handle.get_webview_window("window_name");
2930

3031
let item = Path::from("/foo/bar.pdf");
3132

32-
window.share(vec![item.to_path_buf()], None, PreferredEdge::BottomLeft);
33+
window.share(vec![item.to_path_buf()], PhysicalPosition {
34+
x: 0.0,
35+
y: 0.0
36+
}, PreferredEdge::BottomLeft);
3337
}
3438
```
3539

3640
## Functions
3741

38-
- `share(window: &tauri::WebviewWindow, items: Vec<PathBuf>, offset: Option<(f64, f64)>, preferred_edge: PreferredEdge)`:
42+
- `share(window: &tauri::WebviewWindow, items: Vec<PathBuf>, position: PhysicalPosition<f64>, preferred_edge: PreferredEdge)`:
3943
Displays the Share picker at the cursor position within a WebviewWindow.
4044
- `items: Vec<PathBuf>`: A list of paths to items to share.
41-
- `offset: Option<(f64, f64)>`: Adjust the position of the share picker relative to the cursor.
45+
- `position: PhysicalPosition<f64>`: Set the position to display the share picker. The origin is the top left corner of the window.
4246
- `preferred_edge: PreferredEdge`: The preferred edge for displaying the share picker at the cursor position's rectangle.
4347

4448

libs/share-picker/src/macos/share.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::path::PathBuf;
22

33
use objc2::{msg_send_id, rc::Retained, ClassType};
4-
use objc2_app_kit::{NSEvent, NSSharingServicePicker, NSView, NSWindow};
4+
use objc2_app_kit::{NSSharingServicePicker, NSView, NSWindow};
55
use objc2_foundation::{
66
MainThreadMarker, NSArray, NSPoint, NSRect, NSRectEdge, NSSize, NSString, NSURL,
77
};
8-
use tauri::{Runtime, WebviewWindow};
8+
use tauri::{PhysicalPosition, Runtime, WebviewWindow};
99

1010
pub enum PreferredEdge {
1111
TopLeft,
@@ -26,14 +26,19 @@ impl PreferredEdge {
2626
}
2727

2828
pub trait SharePicker<R: Runtime> {
29-
fn share(&self, items: Vec<PathBuf>, offset: Option<(f64, f64)>, preferred_edge: PreferredEdge);
29+
fn share(
30+
&self,
31+
items: Vec<PathBuf>,
32+
position: PhysicalPosition<f64>,
33+
preferred_edge: PreferredEdge,
34+
);
3035
}
3136

3237
impl<R: Runtime> SharePicker<R> for WebviewWindow<R> {
3338
fn share(
3439
&self,
3540
items: Vec<PathBuf>,
36-
offset: Option<(f64, f64)>,
41+
position: PhysicalPosition<f64>,
3742
preferred_edge: PreferredEdge,
3843
) {
3944
let items = items
@@ -58,29 +63,23 @@ impl<R: Runtime> SharePicker<R> for WebviewWindow<R> {
5863

5964
let content_view = window.contentView().unwrap();
6065

61-
let mouse_location_in_screen = unsafe { NSEvent::mouseLocation() };
66+
let scale_factor = self.scale_factor().unwrap();
67+
68+
let position = PhysicalPosition {
69+
x: position.x,
70+
y: window.frame().size.height - position.y,
71+
};
6272

63-
let mouse_location_in_window = window.convertPointFromScreen(mouse_location_in_screen);
73+
let point_in_window = NSPoint::new(position.x, position.y);
6474

65-
let mouse_location_in_content_view =
66-
content_view.convertPoint_fromView(mouse_location_in_window, None);
75+
let point_in_content_view = content_view.convertPoint_fromView(point_in_window, None);
6776

6877
let mtm = MainThreadMarker::new().unwrap();
6978

7079
let view = unsafe { NSView::new(mtm) };
7180

7281
unsafe {
73-
view.setFrame(NSRect::new(
74-
if let Some((x, y)) = offset {
75-
NSPoint::new(
76-
mouse_location_in_content_view.x + x,
77-
mouse_location_in_content_view.y + y,
78-
)
79-
} else {
80-
mouse_location_in_content_view
81-
},
82-
NSSize::new(1.0, 1.0),
83-
));
82+
view.setFrame(NSRect::new(point_in_content_view, NSSize::new(1.0, 1.0)));
8483

8584
content_view.addSubview(&view);
8685
}

0 commit comments

Comments
 (0)