From f81a06bc4a4eeb8137b1ebea029f54fd6e5e4504 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Wed, 25 Jan 2023 04:47:27 +0100 Subject: [PATCH] feat(keyboard-nav): add Escape and Search messages --- examples/cosmic/src/window.rs | 1 + src/keyboard_nav.rs | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/examples/cosmic/src/window.rs b/examples/cosmic/src/window.rs index c95d7a83..589d9e82 100644 --- a/examples/cosmic/src/window.rs +++ b/examples/cosmic/src/window.rs @@ -406,6 +406,7 @@ impl Application for Window { keyboard_nav::Message::Unfocus => ret = keyboard_nav::unfocus(), keyboard_nav::Message::FocusNext => ret = widget::focus_next(), keyboard_nav::Message::FocusPrevious => ret = widget::focus_previous(), + _ => (), }, Message::ToggleWarning => self.toggle_warning(), } diff --git a/src/keyboard_nav.rs b/src/keyboard_nav.rs index 98bdafd9..814bcc48 100644 --- a/src/keyboard_nav.rs +++ b/src/keyboard_nav.rs @@ -1,19 +1,26 @@ -use iced::{event, keyboard, mouse, subscription, Command, Event, Subscription}; +use iced::{ + event, + keyboard::{self, KeyCode}, + mouse, subscription, Command, Event, Subscription, +}; use iced_native::widget::{operation, Id, Operation}; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum Message { + Escape, FocusNext, FocusPrevious, Unfocus, + Search, } #[must_use] pub fn subscription() -> Subscription { subscription::events_with(|event, status| match (event, status) { + // Focus ( Event::Keyboard(keyboard::Event::KeyPressed { - key_code: keyboard::KeyCode::Tab, + key_code: KeyCode::Tab, modifiers, .. }), @@ -23,6 +30,29 @@ pub fn subscription() -> Subscription { } else { Message::FocusNext }), + // Escape + ( + Event::Keyboard(keyboard::Event::KeyPressed { + key_code: KeyCode::Escape, + .. + }), + _, + ) => Some(Message::Escape), + // Search + ( + Event::Keyboard(keyboard::Event::KeyPressed { + key_code: KeyCode::F, + modifiers, + }), + event::Status::Ignored, + ) => { + if modifiers.control() { + Some(Message::Search) + } else { + None + } + } + // Unfocus (Event::Mouse(mouse::Event::ButtonPressed { .. }), event::Status::Ignored) => { Some(Message::Unfocus) }