From a209001e35a9063785a109880cf6bbb104f5ae14 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 20 Nov 2026 07:41:45 -0500 Subject: [PATCH] Framework fwb10-b: port follow-ups (see runs/fwb10-b.md) --- crates/ui-contract/src/primitives/sensing.rs | 24 ++++++++++---- crates/ui-contract/src/runtime/egui/input.rs | 7 ++++ crates/ui-contract/src/runtime/pointer.rs | 10 ++++-- crates/ui-contract/src/testing/fixture.rs | 14 ++++---- .../ui-contract/tests/it/runtime/pointer.rs | 32 +++++++++++++++++++ crates/ui-panels/src/sections/title_row.rs | 11 ++++--- .../src/sections/title_row/stories.rs | 5 +++ .../tests/it/sections/panel_header.rs | 14 ++++++++ 8 files changed, 98 insertions(+), 19 deletions(-) diff --git a/crates/ui-contract/src/primitives/sensing.rs b/crates/ui-contract/src/primitives/sensing.rs index 278179337..38c181406 100644 --- a/crates/ui-contract/src/primitives/sensing.rs +++ b/crates/ui-contract/src/primitives/sensing.rs @@ -517,12 +517,23 @@ impl PassRead { } /// The modifiers, sample and hover position the part and press results read. - fn inputs(&self) -> (Mods, Option, Option) { + fn inputs(&self, phase: Option) -> (Mods, Option, Option) { let frame = self.pointer.frame.as_ref(); - (frame.map_or(Mods::NONE, |frame| frame.mods), frame.and_then(|frame| frame.sample), self.pointer.hover) + (phase_mods(frame, phase), frame.and_then(|frame| frame.sample), self.pointer.hover) } } +/// A new press reads its event's modifiers; held and moving gestures read the current modifiers. +fn phase_mods(frame: Option<&pointer::PointerFrame>, phase: Option) -> Mods { + let Some(frame) = frame else { return Mods::NONE }; + if let Some(PressPhase::Down { start, .. }) = phase + && frame.press.is_some_and(|press| press.button == start.button) + { + return frame.press_mods.unwrap_or(frame.mods); + } + frame.mods +} + /// Steps the press machine of the root `id` before its measuring run, from what last pass registered: the root rect, /// widgets, layer and sense. A body reads the parts' presses of this pass in both of its runs through /// [`part_press`]; step 8 finds the memoised outcome. Does nothing on a root's first pass and when the root was not @@ -560,7 +571,7 @@ pub(crate) fn part_press(ctx: &Context, owner: Id, part: PartRef) -> PartPress { let (outcome, known) = with_arena_in(ctx, read.viewport, arena, read.pass_nr, |state| { (state.outcome.filter(|outcome| outcome.pass == read.pass_nr), known_in(state, owner)) }); - let (mods, sample, at) = read.inputs(); + let (mods, sample, at) = read.inputs(outcome.and_then(|outcome| outcome.phase)); match outcome { Some(outcome) => part_press_in(&outcome, &known, (mods, sample, at), owner, part), None => PartPress { press: PressState::new(PressPhase::Idle).mods(mods).sample(sample), hovered: false, hover_pos: None }, @@ -664,7 +675,7 @@ pub(crate) fn sense_root(ui: &Ui, spec: RootSpec<'_>) -> RootResult { Some(_) => (false, None), None => (false, None), }; - let inputs = read.inputs(); + let inputs = read.inputs(outcome.phase); RootResult { response, parts: part_results_in(&outcome, &known, inputs, spec.id, spec.own), @@ -767,8 +778,8 @@ pub(crate) fn sense_members(ui: &Ui, arena: ArenaId, specs: &[MemberSpec<'_>]) - let layer = ui.layer_id(); let pass_nr = pass::pass_nr(ctx, pass::current_viewport(ctx)); // One read of the pointer and one of the arena serve the joins, the outcome and the part results. - let (pointer_pos, target, mods, sample) = pointer::with_frame(ctx, |frame, hover| { - (hover, frame.map_or(0.0, |frame| frame.target), frame.map_or(Mods::NONE, |frame| frame.mods), frame.and_then(|frame| frame.sample)) + let (pointer_pos, target, sample) = pointer::with_frame(ctx, |frame, hover| { + (hover, frame.map_or(0.0, |frame| frame.target), frame.and_then(|frame| frame.sample)) }); let mut shapes = Vec::new(); let mut joined = Vec::with_capacity(specs.len()); @@ -797,6 +808,7 @@ pub(crate) fn sense_members(ui: &Ui, arena: ArenaId, specs: &[MemberSpec<'_>]) - if recording { journal_join(ctx, arena, &recorded); } + let mods = pointer::with_frame(ctx, |frame, _| phase_mods(frame, outcome.phase)); pointer::record_shapes(ctx, layer, arena.0, ui.clip_rect(), shapes); specs .iter() diff --git a/crates/ui-contract/src/runtime/egui/input.rs b/crates/ui-contract/src/runtime/egui/input.rs index c54de7899..d3148fa6d 100644 --- a/crates/ui-contract/src/runtime/egui/input.rs +++ b/crates/ui-contract/src/runtime/egui/input.rs @@ -275,6 +275,8 @@ pub(crate) struct PointerRead { pub primary_pressed: bool, /// The modifiers held. pub mods: Mods, + /// Modifiers carried by this pass's press, if any. + pub press_mods: Option, /// The pass's time in seconds. pub time: f64, pub kind: PointerKind, @@ -338,7 +340,11 @@ pub(crate) fn pointer_read(ctx: &Context) -> PointerRead { let mut rotation = 0.0; let mut barrel_press = tablet.as_ref().is_some_and(|tablet| tablet.barrel_press); let mut barrel_pressed = false; + let mut press_mods = None; for event in &input.raw.events { + if let Event::PointerButton { pressed: true, modifiers, .. } = event { + press_mods = Some(Mods::from_egui(modifiers, os)); + } match event { Event::PointerButton { pos, button, pressed, .. } if *button != egui::PointerButton::Primary => { let button = button_of(*button); @@ -429,6 +435,7 @@ pub(crate) fn pointer_read(ctx: &Context) -> PointerRead { other_press, primary_pressed: input.pointer.primary_pressed() && !barrel_pressed, mods: Mods::from_egui(&input.modifiers, os), + press_mods, time: input.time, kind, sample, diff --git a/crates/ui-contract/src/runtime/pointer.rs b/crates/ui-contract/src/runtime/pointer.rs index 3a66c1121..e7ce1b126 100644 --- a/crates/ui-contract/src/runtime/pointer.rs +++ b/crates/ui-contract/src/runtime/pointer.rs @@ -39,6 +39,8 @@ pub struct PointerFrame { pub buttons: Buttons, /// The primary press of the pass. pub press: Option, + /// Modifiers carried by `press`, including the app's latched modifiers. A pass has at most one press. + pub press_mods: Option, /// The button released in the pass. pub release: Option, /// The modifiers held, with the ones the app latches for the user (a Modifier Keys panel) added: the app holds @@ -163,6 +165,7 @@ pub(crate) fn begin(ctx: &Context, hit: HitSizes, latched: craft_keymap::Mods) { // Every pass reads its own input and the frame's latches, so a second pass of a frame builds the same // modifiers from its own (event-free) input and counts no press twice. let mods = with_latched(read.mods, latched); + let press_mods = read.press_mods.map(|mods| with_latched(mods, latched)); // A drag that started in another window reaches this one through the desktop pointer (8.4). if read.pos.is_none() { read.pos = drag::cross_window_pointer(ctx, viewport); @@ -175,8 +178,9 @@ pub(crate) fn begin(ctx: &Context, hit: HitSizes, latched: craft_keymap::Mods) { let mut events = read.events.clone(); for event in &mut events { if let PointerEvent::Pressed { button: PointerButton::Primary, pos, time, clicks, .. } = event { - *clicks = count_clicks(memory.last_press, *pos, *time, mods, hit.slop); - memory.last_press = Some((*pos, *time, *clicks, mods)); + let press_mods = press_mods.unwrap_or(mods); + *clicks = count_clicks(memory.last_press, *pos, *time, press_mods, hit.slop); + memory.last_press = Some((*pos, *time, *clicks, press_mods)); } } if read.primary_pressed || other_pressed { @@ -195,6 +199,7 @@ pub(crate) fn begin(ctx: &Context, hit: HitSizes, latched: craft_keymap::Mods) { delta: read.delta, buttons: read.buttons, press, + press_mods, release: read.release, mods, time: read.time, @@ -289,6 +294,7 @@ pub fn frame(ctx: &Context) -> PointerFrame { delta: Vec2::ZERO, buttons: Buttons::empty(), press: None, + press_mods: None, release: None, mods: Mods::NONE, time: 0.0, diff --git a/crates/ui-contract/src/testing/fixture.rs b/crates/ui-contract/src/testing/fixture.rs index c5c92f91a..1f6aa9e6f 100644 --- a/crates/ui-contract/src/testing/fixture.rs +++ b/crates/ui-contract/src/testing/fixture.rs @@ -572,7 +572,7 @@ impl Fixture { /// Hover, press and release at `pos`, in separate frames, then one idle frame. pub fn click_at(&mut self, pos: Pos2) -> &mut Self { - self.play(injected::click_frames(pos, egui::PointerButton::Primary, 1, Modifiers::NONE)); + self.play(injected::click_frames(pos, egui::PointerButton::Primary, 1, self.frames.held())); self.frame(Vec::new()) } @@ -595,12 +595,12 @@ impl Fixture { pub fn press(&mut self, target: &Target) -> &mut Self { let at = target.center(); self.frame(vec![super::pointer::moved(at)]); - self.frame(vec![super::pointer::press(at)]) + self.frame(vec![injected::button(at, egui::PointerButton::Primary, true, self.frames.held())]) } /// Release at the middle of `target`. pub fn release(&mut self, target: &Target) -> &mut Self { - self.frame(vec![super::pointer::release(target.center())]) + self.frame(vec![injected::button(target.center(), egui::PointerButton::Primary, false, self.frames.held())]) } /// Press on `target`, move by `by` in four steps, release. @@ -611,11 +611,11 @@ impl Fixture { /// A drag from the point `from` by `by`: the same frames as [`Fixture::drag`], for a point that is no target. pub fn drag_at(&mut self, from: Pos2, by: Vec2) -> &mut Self { self.frame(vec![super::pointer::moved(from)]); - self.frame(vec![super::pointer::press(from)]); + self.frame(vec![injected::button(from, egui::PointerButton::Primary, true, self.frames.held())]); for step in 1..=4 { self.frame(vec![super::pointer::moved(from + by * (step as f32 / 4.0))]); } - self.frame(vec![super::pointer::release(from + by)]); + self.frame(vec![injected::button(from + by, egui::PointerButton::Primary, false, self.frames.held())]); self.frame(Vec::new()) } @@ -624,7 +624,7 @@ impl Fixture { /// it with [`Fixture::release_at`]. pub fn drag_hold_at(&mut self, from: Pos2, by: Vec2) -> &mut Self { self.frame(vec![super::pointer::moved(from)]); - self.frame(vec![super::pointer::press(from)]); + self.frame(vec![injected::button(from, egui::PointerButton::Primary, true, self.frames.held())]); for step in 1..=4 { self.frame(vec![super::pointer::moved(from + by * (step as f32 / 4.0))]); } @@ -633,7 +633,7 @@ impl Fixture { /// Releases the button at `at`, then a frame with no input. pub fn release_at(&mut self, at: Pos2) -> &mut Self { - self.frame(vec![super::pointer::release(at)]); + self.frame(vec![injected::button(at, egui::PointerButton::Primary, false, self.frames.held())]); self.frame(Vec::new()) } diff --git a/crates/ui-contract/tests/it/runtime/pointer.rs b/crates/ui-contract/tests/it/runtime/pointer.rs index 3c2e3a252..a5bacd965 100644 --- a/crates/ui-contract/tests/it/runtime/pointer.rs +++ b/crates/ui-contract/tests/it/runtime/pointer.rs @@ -10,6 +10,38 @@ use std::rc::Rc; const BACKGROUND: PartKey = PartKey::new("background"); +#[trial] +fn quick_presses_count_clicks_with_their_own_modifiers() { + let mut frames = Frames::new(); + let at = Pos2::new(100.0, 100.0); + let mut counts = Vec::new(); + let mut show = |ui: &mut Ui| { + craft_ui::runtime::pointer::with_pass_pointer(ui.ctx(), |pass| { + for event in &pass.events { + if let craft_behavior::PointerEvent::Pressed { clicks, .. } = event { + counts.push(*clicks); + } + } + }); + }; + let button = |pressed, modifiers| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers }; + frames.frame(vec![pointer::moved(at), button(true, egui::Modifiers::SHIFT), button(false, egui::Modifiers::SHIFT), button(true, egui::Modifiers::NONE)], &mut show); + frames.idle(1, &mut show); + assert_eq!(counts, vec![1, 1], "a change in modifiers starts a new click sequence"); +} + +#[trial] +fn a_press_uses_its_event_modifiers_when_the_frame_has_released_them() { + let area = rect(100.0, 100.0, 80.0, 80.0); + let (mut show, log) = scene(vec![ProbeRoot::new("modified-press", area, Sense::CLICK)]); + let mut frames = Frames::new(); + frames.idle(2, &mut show); + let at = area.center(); + frames.frame(vec![pointer::moved(at)], &mut show); + frames.frame(vec![egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::SHIFT }], &mut show); + assert!(log.borrow().last().root.press.mods.shift, "the press reads Shift even after the frame has released it"); +} + /// A hover-revealed part must remain identifiable when a drag leaves its shape. #[trial] fn active_part_survives_a_drag_outside_its_shape() { diff --git a/crates/ui-panels/src/sections/title_row.rs b/crates/ui-panels/src/sections/title_row.rs index 9e8764d99..1422f2e04 100644 --- a/crates/ui-panels/src/sections/title_row.rs +++ b/crates/ui-panels/src/sections/title_row.rs @@ -160,7 +160,7 @@ pub struct TitleRowStyle { /// An optional icon, a title with an optional note run on after it ("· note", muted body text) and an optional /// subtitle at the band's end (faint caption text: the target's kind), a lead (a disclosure bound to the container's /// open state, or a check box bound to its gate) and action parts. A row too narrow for all its texts drops the -/// subtitle, then the note, before it elides the title; an elided title shows whole in a tooltip. A click on the title +/// subtitle, then cuts the note, before it elides the title; an elided title shows whole in a tooltip. A click on the title /// band toggles a disclosure lead, as a click on the lead does. #[derive(crate::contract::Component, crate::contract::Props)] pub struct TitleRow<'a> { @@ -283,7 +283,7 @@ impl<'a> TitleRow<'a> { } /// Divides the room left between the lead and the actions: the icon, the title, the note after it and the subtitle - /// at the end. The subtitle shows when every text fits whole, the note when the title and the note do. + /// at the end. The subtitle shows when every text fits whole; the note uses the room after the title. fn place(&self, mut band: Flow, cx: &PartsCx<'_, Self>) -> Placed { // A disclosure's glyph sits a gap before the title, as a title's own icon does; the gap is part of the title band, // so a click beside the glyph toggles too. @@ -317,7 +317,7 @@ impl<'a> TitleRow<'a> { band = rest.take_end(cx.gap()).1; } } - if self.note.is_some() && title + note <= band.rect().width() { + if self.note.is_some() && title + cx.gap() < band.rect().width() { let (title, rest) = band.take_start(title); let (_, rest) = rest.take_start(cx.gap()); return Placed { icon, handle, title, note: Some(rest.rect()), subtitle }; @@ -497,7 +497,7 @@ impl Component for TitleRow<'_> { } ui.draw().text_on_centre_line(placed.title, &title); if let (Some(rect), Some(note)) = (placed.note, &self.note) { - ui.draw().text_on_centre_line(rect, ¬e_run(note.as_str())); + ui.draw().text_on_centre_line(rect, ¬e_run(note.as_str()).elide()); } if let (Some(rect), Some(subtitle)) = (placed.subtitle, &self.subtitle) { let run = subtitle_run(subtitle.as_str(), cx.style.subtitle.role).color(cx.resolve(&cx.style.subtitle.color)); @@ -507,6 +507,9 @@ impl Component for TitleRow<'_> { fn access(&self, access: &mut Access, _: &Cx<'_, Self>) { access.level(self.level); + if let Some(note) = &self.note { + access.description(format!("\u{b7} {}", note.as_str())); + } } fn finish(self, cx: Cx<'_, Self>) -> TitleOutput { diff --git a/crates/ui-panels/src/sections/title_row/stories.rs b/crates/ui-panels/src/sections/title_row/stories.rs index e69656d48..fe2d9a720 100644 --- a/crates/ui-panels/src/sections/title_row/stories.rs +++ b/crates/ui-panels/src/sections/title_row/stories.rs @@ -52,6 +52,11 @@ fn title_row_with_icon_and_subtitle(ui: &mut Ui, _: &mut StoryData) -> StoryOutp TitleRow::new("Layer 3").icon(IconSpec::svg(IconId::new("more"))).subtitle("Pixel layer").show(ui).edit.into() } +#[story(TitleRow<'static>, covers = [], drive = Drive::Never)] +fn title_row_with_note(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + TitleRow::new("Properties").strength(HeaderStrength::Heading).note("Changes apply to the selection").show(ui).edit.into() +} + #[story(TitleRow<'static>, covers = [], drive = Drive::Never)] fn title_row_caption(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { TitleRow::new("Preset details").strength(HeaderStrength::Caption).show(ui).edit.into() diff --git a/crates/ui-panels/tests/it/sections/panel_header.rs b/crates/ui-panels/tests/it/sections/panel_header.rs index 321fba17a..090b7c9da 100644 --- a/crates/ui-panels/tests/it/sections/panel_header.rs +++ b/crates/ui-panels/tests/it/sections/panel_header.rs @@ -5,6 +5,20 @@ use craft_ui::contract::{Role, ShowContainer as _}; use craft_ui::sections::Panel; use craft_ui::testing::{Fixture, trial}; +#[trial] +fn title_note_remains_accessible_when_the_row_is_narrow() { + use craft_ui::contract::Show as _; + use craft_ui::sections::TitleRow; + let mut fixture = Fixture::new(|ui| { + ui.set_max_width(100.0); + TitleRow::new("Properties").note("A long description of the selection").show(ui); + }); + fixture.settle(); + let heading = fixture.by_label("Properties").expect("title node"); + assert!(fixture.tree().description_of(heading.node).is_some_and(|description| description.contains("A long description of the selection"))); + assert!(fixture.texts().iter().any(|run| run.text.contains('·')), "the note is painted even when it is cut"); +} + fn panel(titled: bool) -> Fixture { let mut fixture = Fixture::new(move |ui| { let panel = Panel::new("Brush settings");