mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-21 10:01:42 +01:00
Merge pull request #2504 from aiongg/modifier-reads
craft-ui: a modifier read reruns the settled component that read it
This commit is contained in:
4 files changed
+54
-3
No files matched your search
@@ -118,8 +118,10 @@ impl<C: Component> Cx<'_, C> {
|
||||
self.core.injected = Some(source);
|
||||
}
|
||||
|
||||
/// The modifiers held this frame, Alt alone included.
|
||||
/// The modifiers held this frame, Alt alone included. The read is recorded like a value's: a settled component
|
||||
/// that read them runs again in the frame they change (a menu title underlines its mnemonic while Alt is held).
|
||||
pub fn modifiers(&self) -> Mods {
|
||||
crate::runtime::replay::note_read(&self.core.ctx, crate::runtime::reads::BindKey::Modifiers);
|
||||
pointer::frame(&self.core.ctx).mods
|
||||
}
|
||||
}
|
||||
@@ -218,7 +218,21 @@ pub(crate) fn begin(ctx: &Context, hit: HitSizes, latched: craft_keymap::Mods) {
|
||||
stirred: read.stirred,
|
||||
};
|
||||
let hover_at = pass_pointer.hover;
|
||||
PerViewport::<Stamped<Arc<PassPointer>>>::with_pass(ctx, viewport, pass_nr, |_, _| {}, |record| *record = Arc::new(pass_pointer));
|
||||
let mods_before = PerViewport::<Stamped<Arc<PassPointer>>>::with_pass(
|
||||
ctx,
|
||||
viewport,
|
||||
pass_nr,
|
||||
|_, _| {},
|
||||
|record| {
|
||||
let before = record.frame.as_ref().map(|frame| frame.mods);
|
||||
*record = Arc::new(pass_pointer);
|
||||
before
|
||||
},
|
||||
);
|
||||
// The modifiers are a value components read (`Cx::modifiers`): a change runs the settled ones that read them.
|
||||
if mods_before.is_some_and(|before| before != mods) {
|
||||
crate::runtime::lazy::note_edit(ctx, crate::runtime::reads::BindKey::Modifiers);
|
||||
}
|
||||
PerViewport::<Shapes>::with(ctx, viewport, |shapes| {
|
||||
shapes.last = std::mem::take(&mut shapes.this);
|
||||
shapes.last_bodies = std::mem::take(&mut shapes.this_bodies);
|
||||
|
||||
@@ -27,6 +27,8 @@ pub(crate) enum BindKey {
|
||||
Command(u32, Option<Id>),
|
||||
/// A command the focused stop answers for (an edit it takes): the command's number.
|
||||
FocusedEdit(u32),
|
||||
/// The modifiers held, with the latched ones (`Cx::modifiers`).
|
||||
Modifiers,
|
||||
}
|
||||
|
||||
/// One measure a reader made from the value it read: what it was offered and the size it took.
|
||||
|
||||
@@ -2,13 +2,15 @@
|
||||
|
||||
use craft_host::target::Os;
|
||||
use craft_i18n::Direction;
|
||||
use craft_ui::contract::Show as _;
|
||||
use craft_ui::contract::{Lazy, Show as _};
|
||||
use craft_ui::menu::MenuItem;
|
||||
use craft_ui::menu::bar::{MenuBar, MenuHelp, MenuTitle};
|
||||
use craft_ui::menu::search::HelpEntry;
|
||||
use craft_ui::runtime::commands::EditCommands;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Key, Modifiers};
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A menu bar with File, Edit and Help.
|
||||
struct Rig {
|
||||
@@ -264,3 +266,34 @@ fn a_settled_menu_bar_takes_its_mnemonics_and_f10() {
|
||||
fixture.settle();
|
||||
assert!(fixture.by_label("New").is_some(), "Alt+F opens File");
|
||||
}
|
||||
|
||||
/// A menu bar in a settled region with a content version underlines its mnemonics while Alt is held: the modifiers
|
||||
/// it paints by are a recorded read, so the frame Alt goes down runs the region, and so does the frame it comes up,
|
||||
/// though the version stays the same. In between the region settles again.
|
||||
#[trial]
|
||||
fn holding_alt_runs_the_settled_region_of_a_menu_bar() {
|
||||
let runs = Rc::new(Cell::new(0));
|
||||
let counted = Rc::clone(&runs);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
Lazy::new("title", 0.0).settles(true).content(1).show(ui, |ui| {
|
||||
counted.set(counted.get() + 1);
|
||||
let titles = vec![MenuTitle::new("&File", |ui| {
|
||||
MenuItem::new("&New").show(ui);
|
||||
})];
|
||||
MenuBar::new("Main menu", titles).show(ui);
|
||||
});
|
||||
});
|
||||
fixture.os(Os::Linux);
|
||||
fixture.settle();
|
||||
let frame = |fixture: &mut Fixture| {
|
||||
runs.set(0);
|
||||
fixture.frame(Vec::new());
|
||||
runs.get()
|
||||
};
|
||||
assert_eq!(frame(&mut fixture), 0, "the region settles");
|
||||
fixture.hold(Modifiers::ALT);
|
||||
assert_eq!(frame(&mut fixture), 1, "Alt going down runs the region, which underlines the mnemonics");
|
||||
assert_eq!(frame(&mut fixture), 0, "with Alt held the region settles again");
|
||||
fixture.hold(Modifiers::NONE);
|
||||
assert_eq!(frame(&mut fixture), 1, "Alt coming up runs the region, which drops the underlines");
|
||||
}
|
||||
Reference in new issue
Block a user