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§
- Shortcut
Text WDisplay keyboard shortcuts.
Functions§
- chord_
separator_ fn PWidget function that convertsChordSeparatorFnArgsto a widget.- first_n
PMaximum number of shortcuts to display.- key_fn
PWidget function that converts aKeyFnArgsto a widget.- key_
gesture_ fn PWidget function that convertsKeyGestureFnArgsto a widget.- key_
gesture_ separator_ fn PWidget function that convertsKeyGestureSeparatorFnArgsto a widget.- key_txt
- Gets the localized key name.
- keycap
- Widget used b the
default_modifier_fnanddefault_key_fnto render aText!styled to look like a keycap. - modifier_
fn PWidget function that converts aModifierFnArgsto a widget.- modifier_
txt - Gets the localized modifier name.
- panel_
fn PWidget function that convertsPanelFnArgsto a widget.- shortcut_
fn PWidget function that convertsShortcutFnArgsto a widget.- shortcuts_
separator_ fn PWidget function that convertsShortcutsSeparatorFnArgsto a widget.