Skip to main content

Module keys

Module keys 

Source
Expand description

Keyboard shortcuts that apps can rebind.

iced has no focus model for most widgets, so shortcuts are app-wide: the app subscribes with subscription, stores each Event it receives, and resolves it through a component’s Keymap into that component’s own action. Every interactive component has an Action enum and a default_keymap(), and an app can bind, unbind or clear the defaults.

use iced::Subscription;
use iced::keyboard::key::Named;
use iced_cube::keys::{self, Chord, Keymap};
use iced_cube::navigation::tabs::{self, Action};

#[derive(Debug, Clone)]
enum Message {
    Key(keys::Event),
    Tabs(tabs::Event<u8>),
}

struct App {
    tabs: tabs::State<u8>,
    keymap: Keymap<Action>,
}

impl App {
    fn new() -> Self {
        let keymap = tabs::default_keymap()
            .unbind(&Chord::named(Named::ArrowLeft))
            .unbind(&Chord::named(Named::ArrowRight));
        Self { tabs: tabs::State::new([tabs::tab(1, "One"), tabs::tab(2, "Two")]), keymap }
    }

    fn update(&mut self, message: Message) {
        let event = match message {
            Message::Key(key) => self
                .keymap
                .resolve_event(&key)
                .and_then(|action| action.event(&self.tabs)),
            Message::Tabs(event) => Some(event),
        };
        if let Some(event) = event {
            let _ = self.tabs.update(event);
        }
    }

    fn subscription(&self) -> Subscription<Message> {
        keys::subscription().map(Message::Key)
    }
}

Structs§

Chord
A key plus the modifiers held with it, such as Ctrl+Shift+Tab.
Event
A key press delivered by subscription.
Keymap
Chords bound to actions. Each chord triggers at most one action; an action may have several chords.
Tracker
Turns raw iced events into the Events that subscription delivers. Apps that listen to window events themselves can feed it every event, captured or not, to get the same behaviour.

Enums§

ParseChordError
Why a string is not a valid Chord.

Traits§

Action
A component action that can be bound to keys.

Functions§

subscription
Key presses that no widget captured, such as a focused text input, plus lone modifier keys as described on Event.