From 0ca4fd7c26f9e7fdd7dccb31ee339a6e8e9c74f8 Mon Sep 17 00:00:00 2001 From: aiongg Date: Fri, 20 Nov 2026 04:41:35 +0000 Subject: [PATCH] craft-ui: a modifier read reruns the settled component that read it Cx::modifiers read the pass's modifiers without a record, so a settled region whose component painted by them (a menu bar underlining its mnemonics while Alt is held) replayed its last paint until something else ran it. The read is now noted under BindKey::Modifiers, and the pointer's begin hook notes an edit of that key when the pass's modifiers, latched ones included, differ from the last pass's: the regions that read them run in that frame, as for any edited value. Part of storytold/photocraft#15476. --- .../src/contract/overlay/dismissal.rs | 4 ++- crates/ui-contract/src/runtime/pointer.rs | 16 ++++++++- crates/ui-contract/src/runtime/reads.rs | 2 ++ crates/ui-widgets/tests/it/menu/bar.rs | 35 ++++++++++++++++++- 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/crates/ui-contract/src/contract/overlay/dismissal.rs b/crates/ui-contract/src/contract/overlay/dismissal.rs index 918738b59..0e7d80566 100644 --- a/crates/ui-contract/src/contract/overlay/dismissal.rs +++ b/crates/ui-contract/src/contract/overlay/dismissal.rs @@ -118,8 +118,10 @@ impl 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 } } diff --git a/crates/ui-contract/src/runtime/pointer.rs b/crates/ui-contract/src/runtime/pointer.rs index 68665ed38..cbd9d8597 100644 --- a/crates/ui-contract/src/runtime/pointer.rs +++ b/crates/ui-contract/src/runtime/pointer.rs @@ -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::>>::with_pass(ctx, viewport, pass_nr, |_, _| {}, |record| *record = Arc::new(pass_pointer)); + let mods_before = PerViewport::>>::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::::with(ctx, viewport, |shapes| { shapes.last = std::mem::take(&mut shapes.this); shapes.last_bodies = std::mem::take(&mut shapes.this_bodies); diff --git a/crates/ui-contract/src/runtime/reads.rs b/crates/ui-contract/src/runtime/reads.rs index ca91aecb4..31bd88acf 100644 --- a/crates/ui-contract/src/runtime/reads.rs +++ b/crates/ui-contract/src/runtime/reads.rs @@ -27,6 +27,8 @@ pub(crate) enum BindKey { Command(u32, Option), /// 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. diff --git a/crates/ui-widgets/tests/it/menu/bar.rs b/crates/ui-widgets/tests/it/menu/bar.rs index 67bfe501f..b5d5ced29 100644 --- a/crates/ui-widgets/tests/it/menu/bar.rs +++ b/crates/ui-widgets/tests/it/menu/bar.rs @@ -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"); +}