Skip to content

Commit 6ca5e61

Browse files
authored
Merge pull request #162 from hecrj/feature/window-file-events
Window file events
2 parents 11495b4 + b310673 commit 6ca5e61

6 files changed

Lines changed: 50 additions & 18 deletions

File tree

native/src/event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
/// additional events, feel free to [open an issue] and share your use case!_
1010
///
1111
/// [open an issue]: https://github.com/hecrj/iced/issues
12-
#[derive(PartialEq, Clone, Copy, Debug)]
12+
#[derive(PartialEq, Clone, Debug)]
1313
pub enum Event {
1414
/// A keyboard event
1515
Keyboard(keyboard::Event),

native/src/widget/column.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ where
159159
self.children.iter_mut().zip(layout.children()).for_each(
160160
|(child, layout)| {
161161
child.widget.on_event(
162-
event,
162+
event.clone(),
163163
layout,
164164
cursor_position,
165165
messages,

native/src/widget/row.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ where
160160
self.children.iter_mut().zip(layout.children()).for_each(
161161
|(child, layout)| {
162162
child.widget.on_event(
163-
event,
163+
event.clone(),
164164
layout,
165165
cursor_position,
166166
messages,

native/src/window/event.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use std::path::PathBuf;
2+
13
/// A window-related event.
2-
#[derive(PartialEq, Clone, Copy, Debug)]
4+
#[derive(PartialEq, Clone, Debug)]
35
pub enum Event {
46
/// A window was resized
57
Resized {
@@ -9,4 +11,22 @@ pub enum Event {
911
/// The new height of the window (in units)
1012
height: u32,
1113
},
14+
15+
/// A file is being hovered over the window.
16+
///
17+
/// When the user hovers multiple files at once, this event will be emitted
18+
/// for each file separately.
19+
FileHovered(PathBuf),
20+
21+
/// A file has beend dropped into the window.
22+
///
23+
/// When the user drops multiple files at once, this event will be emitted
24+
/// for each file separately.
25+
FileDropped(PathBuf),
26+
27+
/// A file was hovered, but has exited the window.
28+
///
29+
/// There will be a single `FilesHoveredLeft` event triggered even if
30+
/// multiple files were hovered.
31+
FilesHoveredLeft,
1232
}

winit/src/application.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,10 @@ pub trait Application: Sized {
209209
);
210210

211211
debug.event_processing_started();
212-
events.iter().for_each(|event| {
213-
subscription_pool.broadcast_event(*event)
214-
});
212+
events
213+
.iter()
214+
.cloned()
215+
.for_each(|event| subscription_pool.broadcast_event(event));
215216

216217
let mut messages = user_interface.update(
217218
&renderer,
@@ -330,6 +331,18 @@ pub trait Application: Sized {
330331
event: window_event,
331332
..
332333
} => match window_event {
334+
WindowEvent::Resized(new_size) => {
335+
events.push(Event::Window(window::Event::Resized {
336+
width: new_size.width.round() as u32,
337+
height: new_size.height.round() as u32,
338+
}));
339+
340+
size = new_size;
341+
resized = true;
342+
}
343+
WindowEvent::CloseRequested => {
344+
*control_flow = ControlFlow::Exit;
345+
}
333346
WindowEvent::CursorMoved { position, .. } => {
334347
events.push(Event::Mouse(mouse::Event::CursorMoved {
335348
x: position.x as f32,
@@ -398,17 +411,16 @@ pub trait Application: Sized {
398411
modifiers: conversion::modifiers_state(modifiers),
399412
}));
400413
}
401-
WindowEvent::CloseRequested => {
402-
*control_flow = ControlFlow::Exit;
414+
WindowEvent::HoveredFile(path) => {
415+
events
416+
.push(Event::Window(window::Event::FileHovered(path)));
403417
}
404-
WindowEvent::Resized(new_size) => {
405-
events.push(Event::Window(window::Event::Resized {
406-
width: new_size.width.round() as u32,
407-
height: new_size.height.round() as u32,
408-
}));
409-
410-
size = new_size;
411-
resized = true;
418+
WindowEvent::DroppedFile(path) => {
419+
events
420+
.push(Event::Window(window::Event::FileDropped(path)));
421+
}
422+
WindowEvent::HoveredFileCancelled => {
423+
events.push(Event::Window(window::Event::FilesHoveredLeft));
412424
}
413425
_ => {}
414426
},

winit/src/subscription.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl Pool {
8686
.values_mut()
8787
.filter_map(|connection| connection.listener.as_mut())
8888
.for_each(|listener| {
89-
if let Err(error) = listener.try_send(event) {
89+
if let Err(error) = listener.try_send(event.clone()) {
9090
log::error!(
9191
"Error sending event to subscription: {:?}",
9292
error

0 commit comments

Comments
 (0)