Refactor KeyCode into Key and Location

This commit is contained in:
Héctor Ramón Jiménez 2024-01-16 13:28:00 +01:00
parent 534c7dd7b0
commit 64d1ce5532
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
24 changed files with 1277 additions and 461 deletions

View file

@ -134,8 +134,8 @@ impl Application for Editor {
}
fn subscription(&self) -> Subscription<Message> {
keyboard::on_key_press(|key_code, modifiers| match key_code {
keyboard::KeyCode::S if modifiers.command() => {
keyboard::on_key_press(|key, modifiers| match key.as_ref() {
keyboard::Key::Character("s") if modifiers.command() => {
Some(Message::SaveFile)
}
_ => None,

View file

@ -278,7 +278,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
// Map window event to iced event
if let Some(event) = iced_winit::conversion::window_event(
window::Id::MAIN,
&event,
event,
window.scale_factor(),
modifiers,
) {

View file

@ -71,9 +71,13 @@ impl Application for Layout {
}
fn subscription(&self) -> Subscription<Message> {
keyboard::on_key_release(|key_code, _modifiers| match key_code {
keyboard::KeyCode::Left => Some(Message::Previous),
keyboard::KeyCode::Right => Some(Message::Next),
use keyboard::key;
keyboard::on_key_release(|key, _modifiers| match key {
keyboard::Key::Named(key::Named::ArrowLeft) => {
Some(Message::Previous)
}
keyboard::Key::Named(key::Named::ArrowRight) => Some(Message::Next),
_ => None,
})
}

View file

@ -1,6 +1,7 @@
use iced::event::{self, Event};
use iced::executor;
use iced::keyboard;
use iced::keyboard::key;
use iced::theme;
use iced::widget::{
self, button, column, container, horizontal_space, pick_list, row, text,
@ -85,7 +86,7 @@ impl Application for App {
}
Message::Event(event) => match event {
Event::Keyboard(keyboard::Event::KeyPressed {
key_code: keyboard::KeyCode::Tab,
key: keyboard::Key::Named(key::Named::Tab),
modifiers,
..
}) => {
@ -96,7 +97,7 @@ impl Application for App {
}
}
Event::Keyboard(keyboard::Event::KeyPressed {
key_code: keyboard::KeyCode::Escape,
key: keyboard::Key::Named(key::Named::Escape),
..
}) => {
self.hide_modal();

View file

@ -220,23 +220,26 @@ const PANE_ID_COLOR_FOCUSED: Color = Color::from_rgb(
0x47 as f32 / 255.0,
);
fn handle_hotkey(key_code: keyboard::KeyCode) -> Option<Message> {
use keyboard::KeyCode;
fn handle_hotkey(key: keyboard::Key) -> Option<Message> {
use keyboard::key::{self, Key};
use pane_grid::{Axis, Direction};
let direction = match key_code {
KeyCode::Up => Some(Direction::Up),
KeyCode::Down => Some(Direction::Down),
KeyCode::Left => Some(Direction::Left),
KeyCode::Right => Some(Direction::Right),
_ => None,
};
match key.as_ref() {
Key::Character("v") => Some(Message::SplitFocused(Axis::Vertical)),
Key::Character("h") => Some(Message::SplitFocused(Axis::Horizontal)),
Key::Character("w") => Some(Message::CloseFocused),
Key::Named(key) => {
let direction = match key {
key::Named::ArrowUp => Some(Direction::Up),
key::Named::ArrowDown => Some(Direction::Down),
key::Named::ArrowLeft => Some(Direction::Left),
key::Named::ArrowRight => Some(Direction::Right),
_ => None,
};
match key_code {
KeyCode::V => Some(Message::SplitFocused(Axis::Vertical)),
KeyCode::H => Some(Message::SplitFocused(Axis::Horizontal)),
KeyCode::W => Some(Message::CloseFocused),
_ => direction.map(Message::FocusAdjacent),
direction.map(Message::FocusAdjacent)
}
_ => None,
}
}

View file

@ -1,11 +1,13 @@
use iced::keyboard::KeyCode;
use iced::theme::{Button, Container};
use iced::alignment;
use iced::executor;
use iced::keyboard;
use iced::theme;
use iced::widget::{button, column, container, image, row, text, text_input};
use iced::window;
use iced::window::screenshot::{self, Screenshot};
use iced::{alignment, window};
use iced::{
event, executor, keyboard, Alignment, Application, Command, ContentFit,
Element, Event, Length, Rectangle, Renderer, Subscription, Theme,
Alignment, Application, Command, ContentFit, Element, Length, Rectangle,
Renderer, Subscription, Theme,
};
use ::image as img;
@ -147,7 +149,7 @@ impl Application for Example {
let image = container(image)
.padding(10)
.style(Container::Box)
.style(theme::Container::Box)
.width(Length::FillPortion(2))
.height(Length::Fill)
.center_x()
@ -202,9 +204,10 @@ impl Application for Example {
self.screenshot.is_some().then(|| Message::Png),
)
} else {
button(centered_text("Saving...")).style(Button::Secondary)
button(centered_text("Saving..."))
.style(theme::Button::Secondary)
}
.style(Button::Secondary)
.style(theme::Button::Secondary)
.padding([10, 20, 10, 20])
.width(Length::Fill)
]
@ -213,7 +216,7 @@ impl Application for Example {
crop_controls,
button(centered_text("Crop"))
.on_press(Message::Crop)
.style(Button::Destructive)
.style(theme::Button::Destructive)
.padding([10, 20, 10, 20])
.width(Length::Fill),
]
@ -256,16 +259,10 @@ impl Application for Example {
}
fn subscription(&self) -> Subscription<Self::Message> {
event::listen_with(|event, status| {
if let event::Status::Captured = status {
return None;
}
use keyboard::key;
if let Event::Keyboard(keyboard::Event::KeyPressed {
key_code: KeyCode::F5,
..
}) = event
{
keyboard::on_key_press(|key, _modifiers| {
if let keyboard::Key::Named(key::Named::F5) = key {
Some(Message::Screenshot)
} else {
None

View file

@ -86,12 +86,16 @@ impl Application for Stopwatch {
};
fn handle_hotkey(
key_code: keyboard::KeyCode,
key: keyboard::Key,
_modifiers: keyboard::Modifiers,
) -> Option<Message> {
match key_code {
keyboard::KeyCode::Space => Some(Message::Toggle),
keyboard::KeyCode::R => Some(Message::Reset),
use keyboard::key;
match key.as_ref() {
keyboard::Key::Named(key::Named::Space) => {
Some(Message::Toggle)
}
keyboard::Key::Character("r") => Some(Message::Reset),
_ => None,
}
}

View file

@ -1,6 +1,7 @@
use iced::event::{self, Event};
use iced::executor;
use iced::keyboard;
use iced::keyboard::key;
use iced::widget::{
self, button, column, container, pick_list, row, slider, text, text_input,
};
@ -93,12 +94,12 @@ impl Application for App {
Command::none()
}
Message::Event(Event::Keyboard(keyboard::Event::KeyPressed {
key_code: keyboard::KeyCode::Tab,
key: keyboard::Key::Named(key::Named::Tab),
modifiers,
..
})) if modifiers.shift() => widget::focus_previous(),
Message::Event(Event::Keyboard(keyboard::Event::KeyPressed {
key_code: keyboard::KeyCode::Tab,
key: keyboard::Key::Named(key::Named::Tab),
..
})) => widget::focus_next(),
Message::Event(_) => Command::none(),

View file

@ -260,15 +260,21 @@ impl Application for Todos {
}
fn subscription(&self) -> Subscription<Message> {
keyboard::on_key_press(|key_code, modifiers| {
match (key_code, modifiers) {
(keyboard::KeyCode::Tab, _) => Some(Message::TabPressed {
use keyboard::key;
keyboard::on_key_press(|key, modifiers| {
let keyboard::Key::Named(key) = key else {
return None;
};
match (key, modifiers) {
(key::Named::Tab, _) => Some(Message::TabPressed {
shift: modifiers.shift(),
}),
(keyboard::KeyCode::Up, keyboard::Modifiers::SHIFT) => {
(key::Named::ArrowUp, keyboard::Modifiers::SHIFT) => {
Some(Message::ToggleFullscreen(window::Mode::Fullscreen))
}
(keyboard::KeyCode::Down, keyboard::Modifiers::SHIFT) => {
(key::Named::ArrowDown, keyboard::Modifiers::SHIFT) => {
Some(Message::ToggleFullscreen(window::Mode::Windowed))
}
_ => None,