mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 06:24:39 +00:00
76 lines
2.8 KiB
Rust
76 lines
2.8 KiB
Rust
//! Shortcuts and checked states from the app's craft-keymap [`Keymap`], and the per-frame
|
|
//! enabled and checked update.
|
|
|
|
use craft_keymap::{CommandKind, CommandRef, Keymap};
|
|
|
|
use crate::model::{Builtin, ItemKind, MenuBar};
|
|
|
|
/// Give each item the shortcut its command has in `keymap`, the user's overrides included, and
|
|
/// make the items of toggle commands (kind [`CommandKind::Toggle`]) check items.
|
|
///
|
|
/// The shortcut is [`Keymap::shown_chord`]: the first single-press binding that runs the command,
|
|
/// one in its home scope first, so a chord another command wins is not shown. A command with no
|
|
/// such binding loses its shortcut, and an item whose command the keymap doesn't know keeps its
|
|
/// own. A toggle's check item keeps its checked state; radio items stay radio items. Call it on
|
|
/// the app's bar, before the layout.
|
|
pub fn apply_keymap(bar: &mut MenuBar, keymap: &Keymap) {
|
|
bar.for_each_item_mut(|it| {
|
|
let Some(bindable) = keymap.bindable(&it.command) else { return };
|
|
it.shortcut = keymap.shown_chord(&it.command);
|
|
if bindable.kind == CommandKind::Toggle && it.kind == ItemKind::Plain {
|
|
it.kind = ItemKind::Check(false);
|
|
}
|
|
});
|
|
}
|
|
|
|
/// An item's state for the frame.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
#[non_exhaustive]
|
|
pub struct ItemState {
|
|
/// Whether the item can be chosen.
|
|
pub enabled: bool,
|
|
/// Checked or selected; `None` leaves the item's [`ItemKind`] as it is.
|
|
pub checked: Option<bool>,
|
|
}
|
|
|
|
impl ItemState {
|
|
/// Set whether the item can be chosen.
|
|
pub fn enabled(mut self, enabled: bool) -> ItemState {
|
|
self.enabled = enabled;
|
|
self
|
|
}
|
|
|
|
/// Set the checked state.
|
|
pub fn checked(mut self, checked: Option<bool>) -> ItemState {
|
|
self.checked = checked;
|
|
self
|
|
}
|
|
}
|
|
|
|
impl Default for ItemState {
|
|
/// Enabled, the kind left as it is.
|
|
fn default() -> ItemState {
|
|
ItemState { enabled: true, checked: None }
|
|
}
|
|
}
|
|
|
|
/// Set every item's enabled and checked state from `state`, called once per item of the app's
|
|
/// commands. `checked: Some(..)` makes a plain item a check item and sets a radio item's
|
|
/// selection. The built-in items and the open documents are left alone.
|
|
pub fn apply_state(bar: &mut MenuBar, mut state: impl FnMut(&CommandRef) -> ItemState) {
|
|
let documents: Vec<CommandRef> = bar.documents.iter().map(|d| d.command.clone()).collect();
|
|
bar.for_each_item_mut(|it| {
|
|
if Builtin::of(&it.command).is_some() || documents.contains(&it.command) {
|
|
return;
|
|
}
|
|
let s = state(&it.command);
|
|
it.enabled = s.enabled;
|
|
if let Some(on) = s.checked {
|
|
it.kind = match it.kind {
|
|
ItemKind::Radio(_) => ItemKind::Radio(on),
|
|
ItemKind::Plain | ItemKind::Check(_) => ItemKind::Check(on),
|
|
};
|
|
}
|
|
});
|
|
}
|