refactor: filter out events in State::subscription

this may improve performance, saving update and view calls
This commit is contained in:
electria 2026-08-15 09:47:03 -07:00
commit 869e38f3a5
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -23,7 +23,8 @@ enum Message {
ImageDisplayReady(Result<widget::image::Allocation, widget::image::Error>),
SavePathPicked(Result<PathBuf, String>),
Event(iced::Event),
KeyPressed(keyboard::Key, keyboard::Modifiers),
FileDropped(PathBuf),
}
#[derive(Default)]
@ -115,11 +116,7 @@ impl State {
.err();
}
Message::Event(iced::Event::Keyboard(keyboard::Event::KeyPressed {
key,
modifiers,
..
})) => match key.as_ref() {
Message::KeyPressed(key, modifiers) => match key.as_ref() {
// input field cycling
Key::Named(key::Named::Tab) => {
if modifiers.shift() {
@ -215,18 +212,13 @@ impl State {
_ => {}
},
Message::Event(iced::Event::Window(window::Event::FileDropped(file))) => {
match self.load_image(file) {
Message::FileDropped(path) => match self.load_image(path) {
Ok(task) => {
self.error = None;
return task;
}
Err(e) => self.error = Some(e),
}
}
// ignore unused events
Message::Event(_) => {}
},
}
Task::none()
@ -284,7 +276,15 @@ impl State {
}
fn subscription(&self) -> Subscription<Message> {
event::listen().map(Message::Event)
event::listen().filter_map(|event| match event {
iced::Event::Keyboard(keyboard::Event::KeyPressed { key, modifiers, .. }) => {
Some(Message::KeyPressed(key, modifiers))
}
iced::Event::Window(window::Event::FileDropped(path)) => {
Some(Message::FileDropped(path))
}
_ => None,
})
}
fn title(&self) -> String {
self.info.as_ref().map_or_else(