feat: add keyboard_nav module with unfocus support
This commit is contained in:
parent
352bf8e401
commit
f441a364a6
3 changed files with 68 additions and 26 deletions
|
|
@ -2,13 +2,10 @@
|
||||||
// SPDX-License-Identifier: MPL-2.0
|
// SPDX-License-Identifier: MPL-2.0
|
||||||
use cosmic::{
|
use cosmic::{
|
||||||
iced::widget::{self, button, column, container, horizontal_space, row, text},
|
iced::widget::{self, button, column, container, horizontal_space, row, text},
|
||||||
iced::{
|
iced::{self, Application, Command, Length, Subscription},
|
||||||
self,
|
|
||||||
event::{self, Event},
|
|
||||||
keyboard, Application, Command, Length, Subscription,
|
|
||||||
},
|
|
||||||
iced_native::{subscription, window},
|
iced_native::{subscription, window},
|
||||||
iced_winit::window::{close, drag, minimize, toggle_maximize},
|
iced_winit::window::{close, drag, minimize, toggle_maximize},
|
||||||
|
keyboard_nav,
|
||||||
theme::{self, Theme},
|
theme::{self, Theme},
|
||||||
widget::{
|
widget::{
|
||||||
header_bar, icon, list, nav_bar, nav_bar_toggle, scrollable, segmented_button, settings,
|
header_bar, icon, list, nav_bar, nav_bar_toggle, scrollable, segmented_button, settings,
|
||||||
|
|
@ -172,11 +169,11 @@ pub enum Message {
|
||||||
Desktop(desktop::Message),
|
Desktop(desktop::Message),
|
||||||
Drag,
|
Drag,
|
||||||
InputChanged,
|
InputChanged,
|
||||||
|
KeyboardNav(keyboard_nav::Message),
|
||||||
Maximize,
|
Maximize,
|
||||||
Minimize,
|
Minimize,
|
||||||
NavBar(segmented_button::Key),
|
NavBar(segmented_button::Key),
|
||||||
Page(Page),
|
Page(Page),
|
||||||
TabNav(bool),
|
|
||||||
ToggleNavBar,
|
ToggleNavBar,
|
||||||
ToggleNavBarCondensed,
|
ToggleNavBarCondensed,
|
||||||
}
|
}
|
||||||
|
|
@ -348,21 +345,9 @@ impl Application for Window {
|
||||||
_ => None,
|
_ => None,
|
||||||
});
|
});
|
||||||
|
|
||||||
let tab_navagation = subscription::events_with(|event, status| match (event, status) {
|
|
||||||
(
|
|
||||||
Event::Keyboard(keyboard::Event::KeyPressed {
|
|
||||||
key_code: keyboard::KeyCode::Tab,
|
|
||||||
modifiers,
|
|
||||||
..
|
|
||||||
}),
|
|
||||||
event::Status::Ignored,
|
|
||||||
) => Some(modifiers.shift()),
|
|
||||||
_ => None,
|
|
||||||
});
|
|
||||||
|
|
||||||
Subscription::batch(vec![
|
Subscription::batch(vec![
|
||||||
window_break.map(|_| Message::CondensedViewToggle),
|
window_break.map(|_| Message::CondensedViewToggle),
|
||||||
tab_navagation.map(Message::TabNav),
|
keyboard_nav::subscription().map(Message::KeyboardNav),
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -400,13 +385,11 @@ impl Application for Window {
|
||||||
Message::InputChanged => {}
|
Message::InputChanged => {}
|
||||||
|
|
||||||
Message::CondensedViewToggle => {}
|
Message::CondensedViewToggle => {}
|
||||||
Message::TabNav(shift) => {
|
Message::KeyboardNav(message) => match message {
|
||||||
if shift {
|
keyboard_nav::Message::Unfocus => ret = keyboard_nav::unfocus(),
|
||||||
ret = widget::focus_previous();
|
keyboard_nav::Message::FocusNext => ret = widget::focus_next(),
|
||||||
} else {
|
keyboard_nav::Message::FocusPrevious => ret = widget::focus_previous(),
|
||||||
ret = widget::focus_next();
|
},
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
|
||||||
58
src/keyboard_nav.rs
Normal file
58
src/keyboard_nav.rs
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
use iced::{event, keyboard, mouse, subscription, Command, Event, Subscription};
|
||||||
|
use iced_native::widget::{operation, Id, Operation};
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||||
|
pub enum Message {
|
||||||
|
FocusNext,
|
||||||
|
FocusPrevious,
|
||||||
|
Unfocus,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn subscription() -> Subscription<Message> {
|
||||||
|
subscription::events_with(|event, status| match (event, status) {
|
||||||
|
(
|
||||||
|
Event::Keyboard(keyboard::Event::KeyPressed {
|
||||||
|
key_code: keyboard::KeyCode::Tab,
|
||||||
|
modifiers,
|
||||||
|
..
|
||||||
|
}),
|
||||||
|
event::Status::Ignored,
|
||||||
|
) => Some(if modifiers.shift() {
|
||||||
|
Message::FocusPrevious
|
||||||
|
} else {
|
||||||
|
Message::FocusNext
|
||||||
|
}),
|
||||||
|
(Event::Mouse(mouse::Event::ButtonReleased { .. }), _) => Some(Message::Unfocus),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Unfocuses any actively-focused widget.
|
||||||
|
#[must_use]
|
||||||
|
pub fn unfocus<Message: 'static>() -> Command<Message> {
|
||||||
|
Command::<Message>::widget(unfocus_operation())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
fn unfocus_operation<T>() -> impl Operation<T> {
|
||||||
|
struct Unfocus {}
|
||||||
|
|
||||||
|
impl<T> Operation<T> for Unfocus {
|
||||||
|
fn focusable(&mut self, state: &mut dyn operation::Focusable, _id: Option<&Id>) {
|
||||||
|
if state.is_focused() {
|
||||||
|
state.unfocus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn container(
|
||||||
|
&mut self,
|
||||||
|
_id: Option<&Id>,
|
||||||
|
operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
|
||||||
|
) {
|
||||||
|
operate_on_children(self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Unfocus {}
|
||||||
|
}
|
||||||
|
|
@ -13,6 +13,7 @@ pub use iced_winit;
|
||||||
#[cfg(feature = "applet")]
|
#[cfg(feature = "applet")]
|
||||||
pub mod applet;
|
pub mod applet;
|
||||||
pub mod font;
|
pub mod font;
|
||||||
|
pub mod keyboard_nav;
|
||||||
pub mod theme;
|
pub mod theme;
|
||||||
pub mod widget;
|
pub mod widget;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue