feat: keybindings

unfortunately it looks like the tab operations can't focus buttons
This commit is contained in:
electria 2026-08-04 17:11:24 -07:00
commit d1056e9fbe
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -1,6 +1,9 @@
use std::{borrow::Cow, collections::HashMap, env, path::PathBuf, process}; use std::{borrow::Cow, collections::HashMap, env, path::PathBuf, process};
use iced::{Color, Element, Length, Renderer, Task, Theme, application, theme, widget}; use iced::{
Color, Element, Event, Length, Renderer, Subscription, Task, Theme, application, event,
keyboard, theme, widget, window,
};
use ignore::{WalkBuilder, types::TypesBuilder}; use ignore::{WalkBuilder, types::TypesBuilder};
use rayon::iter::{ParallelBridge, ParallelIterator}; use rayon::iter::{ParallelBridge, ParallelIterator};
@ -12,6 +15,8 @@ enum Message {
UnloadImage(PathBuf), UnloadImage(PathBuf),
OpenImage(PathBuf), OpenImage(PathBuf),
KeyPressed(keyboard::Key, keyboard::Modifiers),
} }
#[derive(Default)] #[derive(Default)]
@ -71,6 +76,22 @@ impl State {
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
.err() .err()
} }
Message::KeyPressed(key, modifiers) => match key.as_ref() {
// input field cycling
keyboard::Key::Named(keyboard::key::Named::Tab) => {
return if modifiers.shift() {
widget::operation::focus_previous()
} else {
widget::operation::focus_next()
};
}
keyboard::Key::Character("q") => {
return window::latest().and_then(window::close);
}
_ => {}
},
} }
Task::none() Task::none()
@ -107,10 +128,20 @@ impl State {
]) ])
.into() .into()
} }
fn subscription(&self) -> Subscription<Message> {
event::listen_with(|event, _status, _id| match event {
Event::Keyboard(keyboard::Event::KeyPressed { key, modifiers, .. }) => {
Some(Message::KeyPressed(key, modifiers))
}
_ => None,
})
}
} }
pub fn run() -> Result<(), impl std::error::Error> { pub fn run() -> Result<(), impl std::error::Error> {
application(State::new, State::update, State::view) application(State::new, State::update, State::view)
.subscription(State::subscription)
.theme(theme::Theme::custom( .theme(theme::Theme::custom(
"high-contrast-dark", "high-contrast-dark",
theme::Palette { theme::Palette {