Merge branch 'design-port/fw-menu-outside-secondary' into design-port/pr-input-focus

# Conflicts:
#	docs/craft-framework.md
This commit is contained in:
skylartaylor committed 2026-11-25 03:08:13 +01:00
commit f8749c55e8
9 files changed
+69 -18

No files matched your search

+48
View File
@@ -5,6 +5,7 @@ use craft_behavior::geometry::{SnapKind, SnapLine, SnapOptions, SnapTarget, Snap
use craft_behavior::kurbo::{Point, Size};
use craft_behavior::{Axis, CancelReason, CanvasView, Cursor, CursorIcon, GesturePhase, PointerButton, PointerKind, PointerSample};
use craft_keymap::Mods;
use craft_ui::buttons::Button;
use craft_ui::canvas::{
Autoscroll, Canvas, CanvasScrollbars, CanvasTool, ContextRequest, CropOverlay, HandTool, Momentary, MomentaryCommands, MomentaryTool,
NavigationOptions, Snap, StandInKey, ToolAction, ToolClaims, ToolContext, ToolHost, ToolInput, ToolKey, ToolPath, ToolPointer, ToolRegion,
@@ -17,6 +18,7 @@ use craft_ui::contract::{
};
use craft_ui::dialog::Dialog;
use craft_ui::display::Text;
use craft_ui::menu::{Menu, MenuItem};
use craft_ui::primitives::Draw;
use craft_ui::runtime::access::Role;
use craft_ui::runtime::commands::{CommandGate, CommandSet as _};
@@ -1762,6 +1764,52 @@ fn menu_canvas(momentary: bool, bars: bool) -> MenuRig {
(fixture, contexts, view, rect)
}
/// A right-click on the canvas while another control's context menu is open closes that menu and reaches the canvas,
/// which asks for its own menu where the button came up, as a second right-click does in a native app: only a primary
/// press outside a menu goes no further.
#[trial]
fn a_right_click_on_the_canvas_closes_another_context_menu_and_asks_for_the_canvas_menu() {
const OPENER: Rect = Rect { min: Pos2::new(8.0, 8.0), max: Pos2::new(120.0, 32.0) };
let (contexts, rect) = (Rc::new(RefCell::new(Vec::new())), Rc::new(Cell::new(Rect::NOTHING)));
let (shown_contexts, shown_rect, shown_view) = (Rc::clone(&contexts), Rc::clone(&rect), Cell::new(None::<CanvasView>));
let mut hand = HandTool::new(NavigationOptions::default());
let mut fixture = Fixture::new(move |ui| {
let layers = Button::new("Layers").show_at(ui, OPENER);
Menu::context("Layers menu", &layers).show(ui, |ui, _| {
MenuItem::new("Duplicate layer").show(ui);
});
// The canvas fills the right half of the window, clear of the opener and of the menu it opens.
let room = ui.max_rect();
let room = Rect::from_min_max(Pos2::new(room.center().x, room.min.y), room.max);
let current = shown_view.get().unwrap_or_else(|| CanvasView::new(room, Point::new(600.0, 450.0), 4.0));
shown_rect.set(room);
let mut reply = None;
let shown = Canvas::new("Document").document(DOCUMENT).view(current).show_at(ui, room, |ui, body| {
if let Some(pass) = tool_pass(&body.env) {
reply = Some(ToolHost::new().run(ui, &pass, &mut hand));
}
});
let reply = reply.unwrap_or_default();
shown_contexts.borrow_mut().extend(reply.context);
shown_view.set(Some(reply.view.or(shown.output.view).unwrap_or(current)));
});
fixture.settle();
let right_click = |fixture: &mut Fixture, at: Pos2| {
fixture.frame(vec![pointer::moved(at)]).frame(vec![pointer::secondary(at, true)]).frame(vec![pointer::secondary(at, false)]);
fixture.settle();
};
let opener = fixture.by_label("Layers").expect("the opener has a node").center();
right_click(&mut fixture, opener);
let menu = fixture.by_role(Role::MENU).first().map(|menu| menu.rect).expect("the right-click on Layers opens its menu");
let at = rect.get().center();
assert!(!menu.contains(at), "the canvas point lies outside the menu: {menu:?} {at:?}");
assert!(contexts.borrow().is_empty(), "the canvas was not asked yet: {:?}", contexts.borrow());
right_click(&mut fixture, at);
assert!(fixture.by_label("Duplicate layer").is_none(), "the right-click on the canvas closed the Layers menu");
let asked = contexts.borrow().iter().map(|request| request.at).collect::<Vec<_>>();
assert_eq!(asked, [at], "the secondary press reached the canvas, which asks for its menu where it came up");
}
#[trial]
fn a_drag_past_the_canvas_edge_scrolls_the_view_and_a_turned_view_scrolls_on_the_clipped_axis() {
for (rotation, upright) in [(0.0, true), (90.0, false)] {
@@ -34,7 +34,8 @@ pub(super) fn dismissal(
if dismiss.anchor_gone && layer.overlay.opener.as_ref().is_some_and(|first| !first.active(ctx, None).0.enabled) {
return Some(CloseReason::AnchorGone);
}
// The input hook removed the press and recorded it; presses inside the family and its anchors never get here.
// The input hook recorded the press (and removed a primary one); presses inside the family and its anchors never
// get here.
if dismiss.outside == Outside::Absorb {
if overlay::absorbed_press(ctx).is_some() {
return Some(CloseReason::Outside);
@@ -22,7 +22,8 @@ pub enum Outside {
Close,
/// Nothing.
Keep,
/// Closes the layer and the press reaches nothing below.
/// Closes the layer. A primary press reaches nothing below; a secondary or middle press goes on to the widget
/// below, so a right-click elsewhere closes a menu and opens the menu there.
Absorb,
}
@@ -251,7 +252,7 @@ impl Overlay {
overlay
}
/// A menu: a popup the arrow keys move through. A press outside closes it and goes no further.
/// A menu: a popup the arrow keys move through. A press outside closes it, and a primary one goes no further.
pub fn menu() -> Overlay {
let mut overlay = Overlay::base(Rank::Popup, Overlay::below(), OverlayFit::Content, OwnerClass::Menu, FocusPolicy::MENU);
overlay.dismiss = Dismiss::new().outside(Outside::Absorb);
+1 -1
View File
@@ -776,7 +776,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) {
PerViewport::<ViewportRecord>::with(ctx, viewport, |record| record.held.hold_ahead(later));
}
Arrived::into_input(events, input);
// H3b: a press outside the top menu family goes no further than the menu's dismissal.
// H3b: a press outside the top menu family closes the menu; a primary one goes no further than its dismissal.
let absorbed = overlay::absorb_presses(ctx, input);
// H4: release after an OS resize.
let released = windows::input_hook(ctx, input);
+9 -8
View File
@@ -166,7 +166,7 @@ pub struct OpenEntry {
pub(crate) cover: Option<Layer>,
/// The layer has a focus scope.
pub(crate) scoped: bool,
/// A press outside its family closes it and goes no further.
/// A press outside its family closes it, and a primary one goes no further.
pub(crate) absorbs: bool,
/// A command run from outside it closes it (`Dismiss::commands`).
pub(crate) closes_on_commands: bool,
@@ -305,7 +305,7 @@ pub(crate) struct Persist {
/// The screen rects a press may land in without closing the top menu family: the family and its anchors. `None`
/// when the top layer does not absorb outside presses.
absorb_zone: Option<Vec<Rect>>,
/// An outside press the input hook removed for the running pass.
/// An outside press the input hook recorded for the running pass (and removed, for a primary one).
outside_press: Option<Pos2>,
/// The top live modal layer of the last finished pass in this viewport.
top_modal: Option<TopModal>,
@@ -706,18 +706,19 @@ pub(crate) fn closed_by_activation(ctx: &Context, id: Id) -> bool {
with_pass(ctx, |pass| pass.activated.iter().any(|(component, closes)| closes.contains(&id) && !pass.claimed.contains(component)))
}
/// The input hook: removes the primary presses that land outside the top menu family, its anchors and its parents,
/// so nothing below the menu sees them (`Outside::Absorb`), and keeps the first one for the layers' dismissal.
/// Returns whether it removed one.
/// The input hook: keeps the first press of any button that lands outside the top menu family, its anchors and its
/// parents for the layers' dismissal, and removes the primary ones, so nothing below the menu sees them
/// (`Outside::Absorb`). A secondary or middle press goes on to what it lands on: a right-click on another part closes
/// the menu and opens that part's menu. Returns whether it removed one.
pub(crate) fn absorb_presses(ctx: &Context, input: &mut crate::runtime::egui::input::RawInput) -> bool {
let zone = with_persist(ctx, input.viewport_id, |persist| persist.absorb_zone.clone());
let Some(zone) = zone else { return false };
let before = input.events.len();
let mut first = None;
input.events.retain(|event| match event {
egui::Event::PointerButton { pos, pressed: true, .. } if !zone.iter().any(|rect| rect.contains(*pos)) => {
egui::Event::PointerButton { pos, button, pressed: true, .. } if !zone.iter().any(|rect| rect.contains(*pos)) => {
first.get_or_insert(*pos);
false
*button != egui::PointerButton::Primary
}
_ => true,
});
@@ -727,7 +728,7 @@ pub(crate) fn absorb_presses(ctx: &Context, input: &mut crate::runtime::egui::in
input.events.len() != before
}
/// The outside press the input hook removed for the running pass. A tracked input (`Input::Press`).
/// The outside press the input hook recorded for the running pass. A tracked input (`Input::Press`).
pub(crate) fn absorbed_press(ctx: &Context) -> Option<Pos2> {
crate::runtime::replay::note_input(ctx, crate::runtime::inputs::Input::Press);
absorbed_press_raw(ctx)
+1 -1
View File
@@ -363,7 +363,7 @@ pub(crate) fn begin(ctx: &Context, hit: HitSizes, latched: craft_keymap::Mods) {
if events {
inputs::bump(ctx, Input::Pointer);
}
// A press or release, or an outside press the layer stack's input hook took before egui saw it.
// A press or release, or an outside press the layer stack's input hook recorded (and took, for a primary one).
if frame.press.is_some() || frame.release.is_some() || crate::runtime::overlay::absorbed_press_raw(ctx).is_some() {
inputs::bump(ctx, Input::Press);
}
+2 -2
View File
@@ -85,8 +85,8 @@ impl<T: Value> Outcome for MenuPick<T> {
}
}
/// The layer of a menu, or of a submenu: a press outside closes it and reaches nothing below, and running a row
/// closes the chain.
/// The layer of a menu, or of a submenu: a press outside closes it, a primary one reaches nothing below, and running a
/// row closes the chain.
#[doc(hidden)]
pub fn layer(submenu: bool) -> Overlay {
let base = if submenu { Overlay::submenu() } else { Overlay::menu() };
+2 -2
View File
@@ -2406,7 +2406,7 @@ The component traits, context types, `PaintUi`, `Looks`, the pipeline and its st
- `craft_ui::contract::Overlay`: `pub struct Overlay { pub place: Place, pub fit: OverlayFit, pub min_width: MinWidth, pub movable: bool, pub resize: Resize, pub rank: Rank, pub keys: OwnerClass, pub focus: FocusPolicy, pub dismiss: Dismiss, pub motion: MotionSpec, pub opener: Option<Opener>, pub popup: PopupKind, pub size: Option<Vec2>, pub size_limits: SizeLimits, pub placed: Option<Rect>, pub placed_at: Option<Pos2>, pub bounds: Option<Rect>, pub opacity: f32, pub body_scroll: bool, pub moves_window: bool, pub moved_by_app: bool, pub continue_move: Option<Vec2>, pub role: LayerRole }`. Data describing an egui layer. Presets fill focus and dismissal; builders change single fields.
- `pub fn popover() -> Overlay`. A popover: a popup anchored below, from its content, at least as wide as its anchor and capped by the window,
- `pub fn dropdown() -> Overlay`. A dropdown list: from its content, at least as wide as its anchor and capped, focus stays on the anchor. It
- `pub fn menu() -> Overlay`. A menu: a popup the arrow keys move through. A press outside closes it and goes no further.
- `pub fn menu() -> Overlay`. A menu: a popup the arrow keys move through. A press outside closes it, and a primary one goes no further.
- `pub fn submenu() -> Overlay`. A submenu: beside its parent entry, opening upward from a low entry when it fits there and not below.
- `pub fn tooltip() -> Overlay`. A tooltip: below the control, no focus, never takes input.
- `pub fn dialog() -> Overlay`. A dialog: modal with no backdrop (the window behind stays at true contrast), floating, movable and resizable.
@@ -2728,7 +2728,7 @@ Chapter 15: the `Overlay` data, its presets, popup handles, `OverlayUi` and the
- `craft_ui::contract::overlay::Overlay`: `pub struct Overlay { pub place: Place, pub fit: OverlayFit, pub min_width: MinWidth, pub movable: bool, pub resize: Resize, pub rank: Rank, pub keys: OwnerClass, pub focus: FocusPolicy, pub dismiss: Dismiss, pub motion: MotionSpec, pub opener: Option<Opener>, pub popup: PopupKind, pub size: Option<Vec2>, pub size_limits: SizeLimits, pub placed: Option<Rect>, pub placed_at: Option<Pos2>, pub bounds: Option<Rect>, pub opacity: f32, pub body_scroll: bool, pub moves_window: bool, pub moved_by_app: bool, pub continue_move: Option<Vec2>, pub role: LayerRole }`. Data describing an egui layer. Presets fill focus and dismissal; builders change single fields.
- `pub fn popover() -> Overlay`. A popover: a popup anchored below, from its content, at least as wide as its anchor and capped by the window,
- `pub fn dropdown() -> Overlay`. A dropdown list: from its content, at least as wide as its anchor and capped, focus stays on the anchor. It
- `pub fn menu() -> Overlay`. A menu: a popup the arrow keys move through. A press outside closes it and goes no further.
- `pub fn menu() -> Overlay`. A menu: a popup the arrow keys move through. A press outside closes it, and a primary one goes no further.
- `pub fn submenu() -> Overlay`. A submenu: beside its parent entry, opening upward from a low entry when it fits there and not below.
- `pub fn tooltip() -> Overlay`. A tooltip: below the control, no focus, never takes input.
- `pub fn dialog() -> Overlay`. A dialog: modal with no backdrop (the window behind stays at true contrast), floating, movable and resizable.
+1 -1
View File
@@ -167,7 +167,7 @@ An app with its own eframe `App` calls the hooks in `craft::ui::shell::embed`. `
| Close path | The runtime's window host owns the root window's close request. `embed::logic` asks the app through its closure, then hands the close to the platform quit where there is one (macOS). |
| Wheel | `RootEnv::wheel_speed` scales each wheel delta once; a Ctrl or Cmd wheel keeps the device's distance. The install's `WheelModifiers` set egui's zoom and sideways modifiers. On Windows and Linux, a Ctrl+wheel in fractions of a notch (a point-unit one on Linux) is a touchpad pinch and arrives as `Event::Zoom`, at egui's Ctrl-scroll zoom speed: Windows hands a precision touchpad's pinch to apps only that way. Whole notches stay wheel events, except within 0.25 s of a pinch step. An app whose Ctrl+wheel does something else, or zooms at its own rate, sets `RootEnv::ctrl_wheel_pinch(false)` and reads the wheel events itself. |
| App modals | A modal the app draws with its own egui code blocks nothing outside egui by itself. While one shows, the app names its window each frame with `FrameInput::app_modal(Some(viewport))`: the app's other windows, panels floated into windows of their own among them, are then blocked as under a framework app-modal layer (their presses, keys, wheel and text are dropped, a press there brings the modal's window forward, and they refuse to close). |
| Input pacing | One press per pass: extra presses in one frame wait for the next passes. A press outside an open framework menu closes the menu and goes no further. After an OS resize of the window, the held pointer is released. |
| Input pacing | One press per pass: extra presses in one frame wait for the next passes. A press outside an open framework menu closes the menu; a primary press goes no further, and a secondary or middle press reaches what it lands on. After an OS resize of the window, the held pointer is released. |
The window's keys are the ones the runtime routes to no component: an Escape or a plain Enter while no framework stop is focused, and the arrows' nudge. `with_window_intents` offers them to its closure while nothing has framework focus. The closure takes what it handles (`Intents::take(Intent::Cancel)`; `Intent` and the other types `Intents` reads are in `ui::runtime::keys`) and returns whether it took anything; the rest stays for later readers. A framework scope that ran earlier in the pass takes them first (an open dialog takes the Escape that closes it), so the app reads them last in its UI to see what no component took. App code that read `Key::Escape`, `Key::Tab`, F6 or F10 from egui while none of its own widgets had focus reads these intents and `FrameOutput::chrome_toggles` instead; its own focused widgets and popups keep reading Tab and Escape from egui.