Skip to content

Commit 261c9f9

Browse files
refactor!(core & api): rename drag events for better consistency and clarity (#10170)
* refacotr!(core & api): rename drag events for better consistency and clarity * more renames * remove imports * fix drag over listen * update example --------- Co-authored-by: Lucas Nogueira <lucas@tauri.app>
1 parent 69dcfdf commit 261c9f9

13 files changed

Lines changed: 159 additions & 132 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
"@tauri-apps/api": "patch:breaking"
3+
---
4+
5+
Renamed drag and drop events in `TauriEvent` enum to better convey when they are triggered:
6+
7+
- `TauriEvent.DRAG` -> `TauriEvent.DRAG_ENTER`
8+
- `TauriEvent.DROP_OVER` -> `TauriEvent.DRAG_OVER`
9+
- `TauriEvent.DROP` -> `TauriEvent.DRAG_DROP`
10+
- `TauriEvent.DROP_CANCELLED` -> `TauriEvent::DRAG_LEAVE`
11+
12+
Also the `type` field values in `Window/Webview/WebviewWindow.onDropEvent` and `DragDropEvent` have changed:
13+
14+
- `dragged` -> `enter`
15+
- `dragOver` -> `over`
16+
- `dropped` -> `drop`
17+
- `cancelled` -> `leave`
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
"tauri": "patch:breaking"
3+
---
4+
5+
Renamed `DragDropEvent` enum variants to better convey when they are triggered:
6+
7+
- `DragDropEvent::Dragged` -> `DragDropEvent::Enter`
8+
- `DragDropEvent::DragOver` -> `DragDropEvent::Over`
9+
- `DragDropEvent::Dropped` -> `DragDropEvent::Drop`
10+
- `DragDropEvent::Cancelled` -> `DragDropEvent::Leave`
11+
12+
This also comes with a change in the events being emitted to JS and Rust event listeners:
13+
14+
- `tauri://drag` -> `tauri://drag-enter`
15+
- `tauri://drop-over` -> `tauri://drag-over`
16+
- `tauri://drop` -> `tauri://drag-drop`
17+
- `tauri://drag-cancelled` -> `tauri://drag-leave`

core/tauri-runtime-wry/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3865,21 +3865,21 @@ fn create_webview<T: UserEvent>(
38653865
WryDragDropEvent::Enter {
38663866
paths,
38673867
position: (x, y),
3868-
} => DragDropEvent::Dragged {
3868+
} => DragDropEvent::Enter {
38693869
paths,
38703870
position: PhysicalPosition::new(x as _, y as _),
38713871
},
3872-
WryDragDropEvent::Over { position: (x, y) } => DragDropEvent::DragOver {
3872+
WryDragDropEvent::Over { position: (x, y) } => DragDropEvent::Over {
38733873
position: PhysicalPosition::new(x as _, y as _),
38743874
},
38753875
WryDragDropEvent::Drop {
38763876
paths,
38773877
position: (x, y),
3878-
} => DragDropEvent::Dropped {
3878+
} => DragDropEvent::Drop {
38793879
paths,
38803880
position: PhysicalPosition::new(x as _, y as _),
38813881
},
3882-
WryDragDropEvent::Leave => DragDropEvent::Cancelled,
3882+
WryDragDropEvent::Leave => DragDropEvent::Leave,
38833883
_ => unimplemented!(),
38843884
};
38853885

core/tauri-runtime/src/window.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,27 @@ pub enum WebviewEvent {
7171
#[derive(Debug, Clone)]
7272
#[non_exhaustive]
7373
pub enum DragDropEvent {
74-
/// A drag operation started.
75-
Dragged {
76-
/// Paths of the files that are being dragged.
74+
/// A drag operation has entered the webview.
75+
Enter {
76+
/// List of paths that are being dragged onto the webview.
7777
paths: Vec<PathBuf>,
7878
/// The position of the mouse cursor.
7979
position: dpi::PhysicalPosition<f64>,
8080
},
81-
/// The files have been dragged onto the window, but have not been dropped yet.
82-
DragOver {
81+
/// A drag operation is moving over the webview.
82+
Over {
8383
/// The position of the mouse cursor.
8484
position: dpi::PhysicalPosition<f64>,
8585
},
86-
/// The user dropped the operation.
87-
Dropped {
88-
/// Path of the files that were dropped.
86+
/// The file(s) have been dropped onto the webview.
87+
Drop {
88+
/// List of paths that are being dropped onto the window.
8989
paths: Vec<PathBuf>,
9090
/// The position of the mouse cursor.
9191
position: dpi::PhysicalPosition<f64>,
9292
},
93-
/// The drag operation was cancelled.
94-
Cancelled,
93+
/// The drag operation has been cancelled or left the window.
94+
Leave,
9595
}
9696

9797
/// Describes the appearance of the mouse cursor.

core/tauri/scripts/bundle.global.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/src/manager/webview.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ use crate::{
2929
};
3030

3131
use super::{
32-
window::{
33-
DragDropPayload, DragOverPayload, DRAG_EVENT, DROP_CANCELLED_EVENT, DROP_EVENT,
34-
DROP_HOVER_EVENT,
35-
},
32+
window::{DragDropPayload, DRAG_DROP_EVENT, DRAG_ENTER_EVENT, DRAG_LEAVE_EVENT, DRAG_OVER_EVENT},
3633
AppManager,
3734
};
3835

@@ -689,15 +686,21 @@ impl<R: Runtime> Webview<R> {
689686
fn on_webview_event<R: Runtime>(webview: &Webview<R>, event: &WebviewEvent) -> crate::Result<()> {
690687
match event {
691688
WebviewEvent::DragDrop(event) => match event {
692-
DragDropEvent::Dragged { paths, position } => {
693-
let payload = DragDropPayload { paths, position };
694-
webview.emit_to_webview(DRAG_EVENT, payload)?
689+
DragDropEvent::Enter { paths, position } => {
690+
let payload = DragDropPayload {
691+
paths: Some(paths),
692+
position,
693+
};
694+
webview.emit_to_webview(DRAG_ENTER_EVENT, payload)?
695695
}
696-
DragDropEvent::DragOver { position } => {
697-
let payload = DragOverPayload { position };
698-
webview.emit_to_webview(DROP_HOVER_EVENT, payload)?
696+
DragDropEvent::Over { position } => {
697+
let payload = DragDropPayload {
698+
position,
699+
paths: None,
700+
};
701+
webview.emit_to_webview(DRAG_OVER_EVENT, payload)?
699702
}
700-
DragDropEvent::Dropped { paths, position } => {
703+
DragDropEvent::Drop { paths, position } => {
701704
let scopes = webview.state::<Scopes>();
702705
for path in paths {
703706
if path.is_file() {
@@ -706,10 +709,13 @@ fn on_webview_event<R: Runtime>(webview: &Webview<R>, event: &WebviewEvent) -> c
706709
let _ = scopes.allow_directory(path, false);
707710
}
708711
}
709-
let payload = DragDropPayload { paths, position };
710-
webview.emit_to_webview(DROP_EVENT, payload)?
712+
let payload = DragDropPayload {
713+
paths: Some(paths),
714+
position,
715+
};
716+
webview.emit_to_webview(DRAG_DROP_EVENT, payload)?
711717
}
712-
DragDropEvent::Cancelled => webview.emit_to_webview(DROP_CANCELLED_EVENT, ())?,
718+
DragDropEvent::Leave => webview.emit_to_webview(DRAG_LEAVE_EVENT, ())?,
713719
_ => unimplemented!(),
714720
},
715721
}

core/tauri/src/manager/window.rs

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ const WINDOW_FOCUS_EVENT: &str = "tauri://focus";
2929
const WINDOW_BLUR_EVENT: &str = "tauri://blur";
3030
const WINDOW_SCALE_FACTOR_CHANGED_EVENT: &str = "tauri://scale-change";
3131
const WINDOW_THEME_CHANGED: &str = "tauri://theme-changed";
32-
pub const DRAG_EVENT: &str = "tauri://drag";
33-
pub const DROP_EVENT: &str = "tauri://drop";
34-
pub const DROP_HOVER_EVENT: &str = "tauri://drop-over";
35-
pub const DROP_CANCELLED_EVENT: &str = "tauri://drag-cancelled";
32+
pub(crate) const DRAG_ENTER_EVENT: &str = "tauri://drag-enter";
33+
pub(crate) const DRAG_OVER_EVENT: &str = "tauri://drag-over";
34+
pub(crate) const DRAG_DROP_EVENT: &str = "tauri://drag-drop";
35+
pub(crate) const DRAG_LEAVE_EVENT: &str = "tauri://drag-leave";
3636

3737
pub struct WindowManager<R: Runtime> {
3838
pub windows: Mutex<HashMap<String, Window<R>>>,
@@ -144,13 +144,9 @@ impl<R: Runtime> Window<R> {
144144
}
145145

146146
#[derive(Serialize, Clone)]
147-
pub struct DragDropPayload<'a> {
148-
pub paths: &'a Vec<PathBuf>,
149-
pub position: &'a PhysicalPosition<f64>,
150-
}
151-
152-
#[derive(Serialize, Clone)]
153-
pub struct DragOverPayload<'a> {
147+
pub(crate) struct DragDropPayload<'a> {
148+
#[serde(skip_serializing_if = "Option::is_none")]
149+
pub paths: Option<&'a Vec<PathBuf>>,
154150
pub position: &'a PhysicalPosition<f64>,
155151
}
156152

@@ -194,28 +190,38 @@ fn on_window_event<R: Runtime>(window: &Window<R>, event: &WindowEvent) -> crate
194190
},
195191
)?,
196192
WindowEvent::DragDrop(event) => match event {
197-
DragDropEvent::Dragged { paths, position } => {
198-
let payload = DragDropPayload { paths, position };
193+
DragDropEvent::Enter { paths, position } => {
194+
let payload = DragDropPayload {
195+
paths: Some(paths),
196+
position,
197+
};
199198

200199
if window.is_webview_window() {
201-
window.emit_to(EventTarget::labeled(window.label()), DRAG_EVENT, payload)?
200+
window.emit_to(
201+
EventTarget::labeled(window.label()),
202+
DRAG_ENTER_EVENT,
203+
payload,
204+
)?
202205
} else {
203-
window.emit_to_window(DRAG_EVENT, payload)?
206+
window.emit_to_window(DRAG_ENTER_EVENT, payload)?
204207
}
205208
}
206-
DragDropEvent::DragOver { position } => {
207-
let payload = DragOverPayload { position };
209+
DragDropEvent::Over { position } => {
210+
let payload = DragDropPayload {
211+
position,
212+
paths: None,
213+
};
208214
if window.is_webview_window() {
209215
window.emit_to(
210216
EventTarget::labeled(window.label()),
211-
DROP_HOVER_EVENT,
217+
DRAG_OVER_EVENT,
212218
payload,
213219
)?
214220
} else {
215-
window.emit_to_window(DROP_HOVER_EVENT, payload)?
221+
window.emit_to_window(DRAG_OVER_EVENT, payload)?
216222
}
217223
}
218-
DragDropEvent::Dropped { paths, position } => {
224+
DragDropEvent::Drop { paths, position } => {
219225
let scopes = window.state::<Scopes>();
220226
for path in paths {
221227
if path.is_file() {
@@ -224,23 +230,26 @@ fn on_window_event<R: Runtime>(window: &Window<R>, event: &WindowEvent) -> crate
224230
let _ = scopes.allow_directory(path, true);
225231
}
226232
}
227-
let payload = DragDropPayload { paths, position };
233+
let payload = DragDropPayload {
234+
paths: Some(paths),
235+
position,
236+
};
228237

229238
if window.is_webview_window() {
230-
window.emit_to(EventTarget::labeled(window.label()), DROP_EVENT, payload)?
239+
window.emit_to(
240+
EventTarget::labeled(window.label()),
241+
DRAG_DROP_EVENT,
242+
payload,
243+
)?
231244
} else {
232-
window.emit_to_window(DROP_EVENT, payload)?
245+
window.emit_to_window(DRAG_DROP_EVENT, payload)?
233246
}
234247
}
235-
DragDropEvent::Cancelled => {
248+
DragDropEvent::Leave => {
236249
if window.is_webview_window() {
237-
window.emit_to(
238-
EventTarget::labeled(window.label()),
239-
DROP_CANCELLED_EVENT,
240-
(),
241-
)?
250+
window.emit_to(EventTarget::labeled(window.label()), DRAG_LEAVE_EVENT, ())?
242251
} else {
243-
window.emit_to_window(DROP_CANCELLED_EVENT, ())?
252+
window.emit_to_window(DRAG_LEAVE_EVENT, ())?
244253
}
245254
}
246255
_ => unimplemented!(),

examples/api/src-tauri/Cargo.lock

Lines changed: 0 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/api/src/App.svelte

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { onMount, tick } from 'svelte'
33
import { writable } from 'svelte/store'
44
import { invoke } from '@tauri-apps/api/core'
5+
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow'
56
67
import Welcome from './views/Welcome.svelte'
78
import Communication from './views/Communication.svelte'
@@ -17,6 +18,11 @@
1718
}
1819
})
1920
21+
const appWindow = getCurrentWebviewWindow()
22+
appWindow.onDragDropEvent((event) => {
23+
onMessage(event.payload)
24+
})
25+
2026
const userAgent = navigator.userAgent.toLowerCase()
2127
const isMobile = userAgent.includes('android') || userAgent.includes('iphone')
2228

tooling/api/src/event.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ enum TauriEvent {
5757
WINDOW_THEME_CHANGED = 'tauri://theme-changed',
5858
WINDOW_CREATED = 'tauri://window-created',
5959
WEBVIEW_CREATED = 'tauri://webview-created',
60-
DRAG = 'tauri://drag',
61-
DROP = 'tauri://drop',
62-
DROP_OVER = 'tauri://drop-over',
63-
DROP_CANCELLED = 'tauri://drag-cancelled'
60+
DRAG_ENTER = 'tauri://drag-enter',
61+
DRAG_OVER = 'tauri://drag-over',
62+
DRAG_DROP = 'tauri://drag-drop',
63+
DRAG_LEAVE = 'tauri://drag-leave'
6464
}
6565

6666
/**

0 commit comments

Comments
 (0)