diff options
author | 2024-01-16 13:28:00 +0100 | |
---|---|---|
committer | 2024-01-16 13:28:00 +0100 | |
commit | 64d1ce5532f55d152fa5819532a138da2dca1a39 (patch) | |
tree | 1a242bde122b5e715f2e980c5541353e2ae8b048 /examples | |
parent | 534c7dd7b0bc515c31b6de87b4aa6a35b44c46a0 (diff) | |
download | iced-64d1ce5532f55d152fa5819532a138da2dca1a39.tar.gz iced-64d1ce5532f55d152fa5819532a138da2dca1a39.tar.bz2 iced-64d1ce5532f55d152fa5819532a138da2dca1a39.zip |
Refactor `KeyCode` into `Key` and `Location`
Diffstat (limited to '')
-rw-r--r-- | examples/editor/src/main.rs | 4 | ||||
-rw-r--r-- | examples/integration/src/main.rs | 2 | ||||
-rw-r--r-- | examples/layout/src/main.rs | 10 | ||||
-rw-r--r-- | examples/modal/src/main.rs | 5 | ||||
-rw-r--r-- | examples/pane_grid/src/main.rs | 31 | ||||
-rw-r--r-- | examples/screenshot/src/main.rs | 33 | ||||
-rw-r--r-- | examples/stopwatch/src/main.rs | 12 | ||||
-rw-r--r-- | examples/toast/src/main.rs | 5 | ||||
-rw-r--r-- | examples/todos/src/main.rs | 16 |
9 files changed, 67 insertions, 51 deletions
diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index 03d1e283..bf2aaaa3 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -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, diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index fab81553..b0939d68 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -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, ) { diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs index 60dabe54..6cf0e570 100644 --- a/examples/layout/src/main.rs +++ b/examples/layout/src/main.rs @@ -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, }) } diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index d1cc7bb0..963c839e 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -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(); diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index 96bb8e4e..d5e5bcbe 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -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, } } diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 20d34be6..6955551e 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -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 diff --git a/examples/stopwatch/src/main.rs b/examples/stopwatch/src/main.rs index 0b0f0607..8a0674c1 100644 --- a/examples/stopwatch/src/main.rs +++ b/examples/stopwatch/src/main.rs @@ -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, } } diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index 609f9087..2e837fa3 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -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(), diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index aad47c20..3d79f087 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -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, |