mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 02:22:55 +00:00
top-w2: net change of 6 commits
- top-w2: written by an agent - top-w2: finished by an agent - top-w2: review fixes (review-1) - top-w2: the menu-letter step of the shortcut trial types its letter as text, which mnemonics read (top run round) - top-w2: typed letters in a menu go to the container of the focused row's level, with a trial for a title-click menu (top run round) - top-w2: a menu row's pick walks out from the menu it runs in, with a trial for Cut from the Help search field's edit menu (top run round)
This commit is contained in:
@@ -1,10 +1,29 @@
|
||||
//! `ModifierKeysPanel`: on-screen toggles for the modifier keys, for a touch screen without a keyboard.
|
||||
|
||||
use crate::contract::{Bind, Pattern};
|
||||
use craft_keymap::Mods;
|
||||
use crate::buttons::ToggleButton;
|
||||
use crate::contract::{Bind, Pattern, Props as _, Show as _, ShowContainer as _};
|
||||
use crate::layout_components::Row;
|
||||
use crate::runtime::env::{Env, Label};
|
||||
use craft_behavior::{EditEvent, Mixed};
|
||||
use craft_keymap::{Mods, Platform};
|
||||
use egui::{Response, Ui};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[cfg(feature = "testing")]
|
||||
mod stories;
|
||||
|
||||
/// The panel's buttons in their fixed order, each with the one modifier it latches: Shift, the command key (⌘ on
|
||||
/// macOS, Ctrl elsewhere) and Alt (⌥ on macOS).
|
||||
const KEYS: [(&str, Mods); 3] = [("shift", Mods::SHIFT), ("command", Mods::CMD), ("alt", Mods::ALT)];
|
||||
|
||||
/// A panel of latching modifier-key toggles: a press on one holds that modifier for the next click or key.
|
||||
///
|
||||
/// - Three toggle buttons, Shift, the command key and Alt, over one binding of the latched modifiers.
|
||||
/// - Each face is the modifier as chord text writes it on the OS (`⇧ ⌘ ⌥` on macOS, `Shift Ctrl Alt` elsewhere), and
|
||||
/// its accessible name the modifier's spoken name (`Shift`, `Cmd`, `Option` on macOS).
|
||||
/// - The app hands the binding's value to the runtime as the frame's latched modifiers (`FrameInput::latched`): every
|
||||
/// press and drag then reads them as held. Key chords read the keyboard's own modifiers.
|
||||
/// - The panel adds no words: its title is the dock panel's.
|
||||
#[must_use = "show the panel with `show`"]
|
||||
#[derive(Pattern)]
|
||||
pub struct ModifierKeysPanel<'a> {
|
||||
@@ -14,13 +33,55 @@ pub struct ModifierKeysPanel<'a> {
|
||||
impl<'a> ModifierKeysPanel<'a> {
|
||||
/// A panel over the latched modifiers in `latched`.
|
||||
pub fn new(latched: Bind<'a, Mods>) -> Self {
|
||||
let _ = latched;
|
||||
todo!("tp.T16")
|
||||
ModifierKeysPanel { latched }
|
||||
}
|
||||
|
||||
/// Shows the panel.
|
||||
pub fn show(self, ui: &mut Ui) -> Response {
|
||||
let _ = (ui, &self.latched);
|
||||
todo!("tp.T16")
|
||||
let ModifierKeysPanel { mut latched } = self;
|
||||
let os = Env::of(ui).view().shell().os;
|
||||
Row::new()
|
||||
.show(ui, |ui, row| {
|
||||
let held = latched.reborrow().read(row).and_then(|held| held.same().copied()).unwrap_or_default();
|
||||
// Off macOS a held Ctrl is the command key: the buttons show and edit the modifiers as the OS reads them.
|
||||
let before = held.effective(os);
|
||||
let mut now = before;
|
||||
for (salt, key) in KEYS {
|
||||
let mut on = holds(now, key);
|
||||
ToggleButton::new(face(key, os), Bind::bound(&mut on)).spoken(Label::from(spoken(key, os))).id_salt(salt).show(ui);
|
||||
now = set(now, key, on);
|
||||
}
|
||||
if now != before {
|
||||
let toggled = KEYS.iter().find(|(_, key)| holds(now, *key) != holds(before, *key)).map_or(Mods::NONE, |(_, key)| *key);
|
||||
let name: Arc<str> = Arc::from(spoken(toggled, os).as_str());
|
||||
let _ = latched.reborrow().write(row, &EditEvent::set(Mixed::Same(held), now, name, None));
|
||||
}
|
||||
})
|
||||
.output
|
||||
}
|
||||
}
|
||||
|
||||
/// The face of the button for the one-modifier `key`: its symbol on macOS, its name elsewhere.
|
||||
fn face(key: Mods, os: Platform) -> String {
|
||||
key.parts(os).concat()
|
||||
}
|
||||
|
||||
/// The name a screen reader says for the one-modifier `key`.
|
||||
fn spoken(key: Mods, os: Platform) -> String {
|
||||
key.spoken(os).join(" ")
|
||||
}
|
||||
|
||||
/// Whether `mods` holds the one modifier of `key`.
|
||||
fn holds(mods: Mods, key: Mods) -> bool {
|
||||
(key.shift && mods.shift) || (key.cmd && mods.cmd) || (key.alt && mods.alt)
|
||||
}
|
||||
|
||||
/// `mods` with the one modifier of `key` held or released.
|
||||
fn set(mods: Mods, key: Mods, on: bool) -> Mods {
|
||||
Mods {
|
||||
shift: if key.shift { on } else { mods.shift },
|
||||
cmd: if key.cmd { on } else { mods.cmd },
|
||||
alt: if key.alt { on } else { mods.alt },
|
||||
ctrl: mods.ctrl,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
//! Stories of `ModifierKeysPanel`.
|
||||
|
||||
use super::ModifierKeysPanel;
|
||||
use crate::contract::Bind;
|
||||
use crate::testing::{Drive, StoryData, StoryOutput, story};
|
||||
use craft_keymap::Mods;
|
||||
use egui::Ui;
|
||||
|
||||
#[story(ModifierKeysPanel<'static>, covers = [], drive = Drive::Never, title = "Shift latched")]
|
||||
fn modifier_keys_panel(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let latched = data.value("latched", Mods::SHIFT);
|
||||
ModifierKeysPanel::new(Bind::bound(&mut *latched)).show(ui);
|
||||
let readout = format!("{latched:?}");
|
||||
StoryOutput::default().readout(readout)
|
||||
}
|
||||
Reference in New Issue
Block a user