Module shortcut_text

Module shortcut_text 

Source
Expand description

Keyboard shortcut display widget.

The ShortcutText! is composite widget that generates localized and styled shortcut text, it can handle all shortcut variations, multiple shortcuts and partially invalid shortcuts. Extensive configuration is possible using contextual properties that can override.

The example below demonstrates a basic key binding editor that uses the ShortcutText! widget in multiple places.

pub fn shortcut_input(shortcut: Var<Shortcuts>) -> UiNode {
    Button! {
        // display the shortcut, or the `none_fn` content if there is no shortcut.
        child = ShortcutText! {
            shortcut = shortcut.clone();
            none_fn = wgt_fn!(|_| Text!("no shortcut"));
        };
        on_click = hn!(|_| {
            DIALOG.custom(shortcut_input_dialog(shortcut.clone()));
        });
    }
}
fn shortcut_input_dialog(output: Var<gesture::Shortcuts>) -> UiNode {
    let pressed = var(Shortcuts::new());
    let is_valid = var(true);
    Container! {
        child_top = Wrap!(ui_vec![
            Text!("Press the new shortcut and then press "),
            ShortcutText!(shortcut!(Enter)), // shortcut text supports inlining
        ]);
        child_spacing = 20;
        // default style is derived from the `font_size` and `font_color` values.
        child = ShortcutText! {
            shortcut = pressed.clone();
            font_size = 3.em();
            align = Align::TOP;
            when !#{is_valid.clone()} {
                font_color = colors::RED;
            }
        };

        on_pre_key_down = hn!(|args| {
            args.propagation().stop();
            match &args.key {
                Key::Enter => {
                    let shortcut = pressed.get();
                    if shortcut.is_empty() || shortcut[0].is_valid() {
                        is_valid.set(true);
                        output.set(shortcut);
                        DIALOG.respond(dialog::Response::ok());
                    } else {
                        is_valid.set(false);
                    }
                }
                Key::Escape => {
                    DIALOG.respond(dialog::Response::cancel());
                }
                _ => {
                    is_valid.set(true); // clear
                    pressed.set(args.editing_shortcut().unwrap());
                }
            }
        });
        align = Align::CENTER;
        min_height = 200;
        focusable = true;
        focus_on_init = true;
    }
}

§Full API

See zng_wgt_shortcut for the full widget API.

Structs§

ShortcutText
W Display keyboard shortcuts.

Functions§

chord_separator_fn
P Widget function that converts ChordSeparatorFnArgs to a widget.
first_n
P Maximum number of shortcuts to display.
key_fn
P Widget function that converts a KeyFnArgs to a widget.
key_gesture_fn
P Widget function that converts KeyGestureFnArgs to a widget.
key_gesture_separator_fn
P Widget function that converts KeyGestureSeparatorFnArgs to a widget.
key_txt
Gets the localized key name.
keycap
Widget used b the default_modifier_fn and default_key_fn to render a Text! styled to look like a keycap.
modifier_fn
P Widget function that converts a ModifierFnArgs to a widget.
modifier_txt
Gets the localized modifier name.
panel_fn
P Widget function that converts PanelFnArgs to a widget.
shortcut_fn
P Widget function that converts ShortcutFnArgs to a widget.
shortcuts_separator_fn
P Widget function that converts ShortcutsSeparatorFnArgs to a widget.