Merge pull request #9361 from dotCooCoo/fix/context-menu-key-keeps-selection

Canvas: Shift+F10 and the Menu key open the menu for the selection as it is
This commit is contained in:
BFlat authored and GitHub committed 2026-11-26 16:47:29 +07:00
commit e94908bfc3
4 files changed
+202 -16

No files matched your search

+16 -4
View File
@@ -5,7 +5,8 @@
use craft::behavior::{CanvasView, PressPhase};
use craft::keymap::Mods;
use craft::ui::canvas::{
Canvas, CanvasScrollbars, CanvasSpace, RotateView, ToolHostResponse, ToolPass, ViewCommands, ViewRequest, ViewTarget, canvas_space, tool_pass,
Canvas, CanvasScrollbars, CanvasSpace, ContextSource, RotateView, ToolHostResponse, ToolPass, ViewCommands, ViewRequest, ViewTarget,
canvas_space, tool_pass,
};
use craft::ui::contract::{Bind, Props as _, Show as _, ShowContainer as _};
use craft::ui::runtime::env::Env;
@@ -133,6 +134,8 @@ pub(crate) fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
struct Body {
reply: Option<ToolHostResponse>,
context_at: Option<egui::Pos2>,
/// The pasteboard's `context_at` answers the Menu key or Shift+F10, not a secondary click.
context_key: bool,
/// A view a compass or scroll bar edit made this pass.
edited: Option<CanvasView>,
}
@@ -235,8 +238,12 @@ pub(crate) fn show_document(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kin
rulers::apply(app, ui, out);
}
let pressed = matches!(output.press.phase, PressPhase::Down { .. });
let Body { reply, context_at, edited } = body;
let request = reply.as_ref().and_then(|r| r.context.as_ref()).map(|c| c.at).or(context_at);
let Body { reply, context_at, context_key, edited } = body;
let request = reply
.as_ref()
.and_then(|r| r.context.as_ref())
.map(|c| context_menu::Request { at: c.at, key: c.source == ContextSource::Key })
.or(context_at.map(|at| context_menu::Request { at, key: context_key }));
let jumped = edited.is_some();
// Presentation Mode keeps its view through a drag of the middle button, which the canvas pans on its own.
let view_now =
@@ -270,7 +277,12 @@ pub(crate) fn show_document(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kin
fn show_body(app: &mut VectorcraftApp, ui: &mut Ui, space: &CanvasSpace, pass: Option<&ToolPass>, win: Win, kind: BodyKind) -> Body {
let live = kind == BodyKind::Live;
let presenting = app.ui.screen_mode == 3;
let mut out = Body { context_at: artboards::show(app, ui, space, win, kind).map(|p| space.view.to_screen(p)), ..Body::default() };
let background = space.background();
let mut out = Body {
context_at: artboards::show(app, ui, space, win, kind).map(|p| space.view.to_screen(p)),
context_key: background.context.is_none() && background.context_key,
..Body::default()
};
grids(app, ui, space, win);
if live {
find_shown_tool(app, ui.ctx(), space);
+55 -11
View File
@@ -4,25 +4,39 @@ use craft::ui::contract::{Bind, ShowContainer as _, Trigger};
use craft::ui::menu::Menu;
use egui::Pos2;
use serde_json::json;
use vectorcraft_geom::Point;
use super::marks;
use super::outline::{hit_with, selection_hit_at};
use crate::VectorcraftApp;
/// The canvas context menu (right-click, Menu key, Shift+F10). A `request` (screen points: the pointer, or
/// the view centre for a key) selects the object under it unless it already is, then the menu lists what
/// applies to the selection ([`crate::menus::context_items`]) at that point until it closes. The locked
/// objects under the pointer ([`vectorcraft_doc::hit::locked_objects_at`]) are kept in `ui.context_locked`
/// for its Unlock submenu. With the Intertwine tool, a right-click where three or more objects of the group
/// overlap keeps the selection and puts what it arranges in `ui.context_intertwine`
/// ([`vectorcraft_tools::intertwine::menu_target`]). With the Artboard tool, an artboard takes the place of
/// the object ([`crate::panels::artboards::right_click`]).
/// A request for the canvas menu at `at` (screen points): a secondary click at the pointer, or the Menu key or
/// Shift+F10 (`key`), which the canvas reports at the view centre.
#[derive(Clone, Copy, Debug)]
pub(crate) struct Request {
pub(crate) at: Pos2,
pub(crate) key: bool,
}
/// The canvas context menu (right-click, Menu key, Shift+F10). A right-click selects the object under the
/// pointer unless it already is, then the menu lists what applies to the selection
/// ([`crate::menus::context_items`]) at that point until it closes. The locked objects under the pointer
/// ([`vectorcraft_doc::hit::locked_objects_at`]) are kept in `ui.context_locked` for its Unlock submenu. With
/// the Intertwine tool, a right-click where three or more objects of the group overlap keeps the selection and
/// puts what it arranges in `ui.context_intertwine` ([`vectorcraft_tools::intertwine::menu_target`]). With the
/// Artboard tool, an artboard takes the place of the object ([`crate::panels::artboards::right_click`]). A key
/// request keeps the selection and opens the menu at it ([`prepare_key`]).
///
/// Called on every pass after the canvas shows, with its response as `opener`: the menu is shown from the
/// point stored in `ui.context_menu_at`, so a later pass without a request keeps it open.
pub(crate) fn show(app: &mut VectorcraftApp, ui: &mut egui::Ui, space: &CanvasSpace, opener: &egui::Response, request: Option<Pos2>) {
if let Some(at) = request {
prepare(app, space, at);
pub(crate) fn show(app: &mut VectorcraftApp, ui: &mut egui::Ui, space: &CanvasSpace, opener: &egui::Response, request: Option<Request>) {
if let Some(request) = request {
let at = if request.key {
prepare_key(app, space)
} else {
prepare(app, space, request.at);
request.at
};
app.ui.context_menu_at = Some(at);
}
let Some(at) = app.ui.context_menu_at else { return };
@@ -69,3 +83,33 @@ fn prepare(app: &mut VectorcraftApp, space: &CanvasSpace, at: Pos2) {
let _ = app.run("select.set", json!({ "ids": [top.0] }));
}
}
/// The one-shot work of a key request: the menu is for the selection as it is (the selected artboards with the
/// Artboard tool), at the centre of the part of it in view, or at the view centre when none of it is in view.
/// Nothing under a point is looked up: no locked objects, word or Intertwine target. Returns the menu's point.
fn prepare_key(app: &mut VectorcraftApp, space: &CanvasSpace) -> Pos2 {
app.ui.context_locked = None;
app.ui.context_intertwine = None;
app.ui.context_spelling = None;
let on_artboards = app.session.tool_id() == "artboard";
let bounds = if on_artboards {
app.session.active().and_then(|st| {
let picked = crate::panels::artboards::picked(app, st.doc.artboards.len());
picked.iter().filter_map(|&i| st.doc.artboards.get(i).map(|a| a.rect)).reduce(|a, b| a.union(b))
})
} else {
app.selection_bounds()
};
let view = &space.view;
let at = bounds
.map(|b| {
egui::Rect::from_points(&[b.origin(), Point::new(b.x1, b.y0), Point::new(b.x1, b.y1), Point::new(b.x0, b.y1)].map(|p| view.to_screen(p)))
})
.map(|r| r.intersect(view.rect))
.filter(|r| !r.is_negative())
.map_or(view.rect.center(), |r| r.center());
if on_artboards {
crate::panels::artboards::measure_menu(app, view.to_doc(at));
}
at
}
+9 -1
View File
@@ -324,10 +324,18 @@ pub(crate) fn fit_has_art(app: &VectorcraftApp, indices: &[usize]) -> bool {
/// isn't selected becomes the only selected one, and the menu's facts are measured.
pub(crate) fn right_click(app: &mut VectorcraftApp, p: vectorcraft_geom::Point) {
let Some(st) = app.session.active() else { return };
let (uid, n, hit) = (st.uid, st.doc.artboards.len(), st.doc.artboard_at(p));
let (n, hit) = (st.doc.artboards.len(), st.doc.artboard_at(p));
if let Some(i) = hit.filter(|i| !picked(app, n).contains(i)) {
select(app, i);
}
measure_menu(app, p);
}
/// The canvas menu's facts for the selected artboards, with the menu opened at `p` (a right-click, or the Menu key or
/// Shift+F10, which select nothing).
pub(crate) fn measure_menu(app: &mut VectorcraftApp, p: vectorcraft_geom::Point) {
let Some(st) = app.session.active() else { return };
let (uid, n) = (st.uid, st.doc.artboards.len());
let all = picked(app, n);
let any_empty = vectorcraft_engine::find_command("artboard.deleteEmpty").is_some_and(|c| (c.enabled)(&app.session).is_ok());
let any_art = fit_has_art(app, &all);
+122
View File
@@ -134,6 +134,128 @@ fn right_click_on_empty_canvas_offers_the_view_and_selects_nothing() {
assert_eq!(app.session.active().unwrap().doc.art_bounds(), None);
}
/// The document point at the centre of the canvas view, where a key request reports itself.
fn view_centre(app: &VectorcraftApp) -> Point {
let view = app.canvas_view().unwrap();
view.to_doc(view.rect.center())
}
/// `key` with `modifiers`, pressed on the focused canvas, then a frame to show the result.
fn menu_key(app: &mut VectorcraftApp, ctx: &egui::Context, key: egui::Key, modifiers: egui::Modifiers) {
frame(app, ctx, vec![Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers }]);
frame(app, ctx, vec![]);
}
/// Rectangle A, selected with a click (which also gives the canvas the keyboard focus), and an unselected
/// rectangle B under the view's centre with a locked rectangle over it.
fn selection_away_from_the_centre(app: &mut VectorcraftApp, ctx: &egui::Context) -> (NodeId, NodeId) {
let a = rect(app, 0.0, 0.0);
frame(app, ctx, vec![]);
let c = view_centre(app);
let b = rect(app, c.x - 50.0, c.y - 50.0);
let locked = rect(app, c.x - 25.0, c.y - 25.0);
app.run("layer.setProps", json!({"ids": [locked.0], "locked": true})).unwrap();
app.run("select.none", json!({})).unwrap();
let p = screen(app, 50.0, 50.0);
click(app, ctx, p, PointerButton::Primary);
assert_eq!(app.session.active().unwrap().selection.objects, [a]);
(a, b)
}
/// Asserts the menu opened for `a` as selected, at a point over it, without what lies under the view's centre.
fn opened_for_the_selection(app: &VectorcraftApp, a: NodeId) {
assert_eq!(app.session.active().unwrap().selection.objects, [a], "a key request keeps the selection");
let at = app.ui.context_menu_at.expect("the menu is open");
let over_a = Rect::from_two_pos(screen(app, 0.0, 0.0), screen(app, 100.0, 100.0));
assert!(over_a.contains(at), "the menu opens at the selection: {at:?} in {over_a:?}");
assert!(has(app, "Arrange") && has_command(app, "object.arrange.bringToFront"), "{:?}", labels(app));
assert!(app.ui.context_locked.is_none() && !has(app, "Unlock"), "nothing under the view's centre is measured");
}
#[test]
fn shift_f10_opens_the_menu_for_the_selection_without_changing_it() {
let mut app = app();
let ctx = crate::testing::ui_context();
let (a, _) = selection_away_from_the_centre(&mut app, &ctx);
menu_key(&mut app, &ctx, egui::Key::F10, egui::Modifiers::SHIFT);
opened_for_the_selection(&app, a);
}
#[cfg(not(target_os = "macos"))]
#[test]
fn menu_key_opens_the_menu_for_the_selection_without_changing_it() {
let mut app = app();
let ctx = crate::testing::ui_context();
let (a, _) = selection_away_from_the_centre(&mut app, &ctx);
menu_key(&mut app, &ctx, egui::Key::Menu, egui::Modifiers::NONE);
opened_for_the_selection(&app, a);
}
/// With nothing selected, Shift+F10 opens the menu at the view's centre and selects nothing, even over an object.
#[test]
fn shift_f10_with_nothing_selected_opens_the_menu_at_the_view_centre() {
let mut app = app();
let ctx = crate::testing::ui_context();
frame(&mut app, &ctx, vec![]);
let c = view_centre(&app);
rect(&mut app, c.x - 50.0, c.y - 50.0);
app.run("select.none", json!({})).unwrap();
// A click on the empty artboard gives the canvas the focus and keeps the selection empty.
let p = screen(&app, 380.0, 280.0);
click(&mut app, &ctx, p, PointerButton::Primary);
assert!(app.session.active().unwrap().selection.objects.is_empty());
menu_key(&mut app, &ctx, egui::Key::F10, egui::Modifiers::SHIFT);
assert!(app.session.active().unwrap().selection.objects.is_empty(), "the object under the view's centre stays unselected");
let at = app.ui.context_menu_at.expect("the menu is open");
let centre = app.canvas_view().unwrap().rect.center();
assert!(at.distance(centre) < 0.5, "{at:?} at {centre:?}");
assert!(has(&app, "Select All") && !has(&app, "Arrange"), "{:?}", labels(&app));
}
/// With the Artboard tool, Shift+F10 opens the menu for the selected artboard, at it, and selects no other artboard;
/// the menu's Paste would put artboards on it.
#[test]
fn shift_f10_with_the_artboard_tool_keeps_the_selected_artboard() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 100, "height": 100, "artboards": 3})).unwrap();
let ctx = crate::testing::ui_context();
frame(&mut app, &ctx, vec![]);
crate::menus::invoke(&mut app, "view.fitAll", json!({}));
for _ in 0..3 {
frame(&mut app, &ctx, vec![]);
}
let boards: Vec<vectorcraft_geom::Rect> = app.session.active().unwrap().doc.artboards.iter().map(|a| a.rect).collect();
let centre = view_centre(&app);
let away = boards.iter().position(|r| !r.contains(centre)).unwrap();
app.select_tool("artboard");
// A click on the artboard selects it and gives the canvas the focus.
let c = boards[away].center();
let p = screen(&app, c.x, c.y);
click(&mut app, &ctx, p, PointerButton::Primary);
assert_eq!(crate::panels::artboards::picked(&app, boards.len()), [away]);
menu_key(&mut app, &ctx, egui::Key::F10, egui::Modifiers::SHIFT);
assert_eq!(crate::panels::artboards::picked(&app, boards.len()), [away], "a key request keeps the selected artboard");
let at = app.ui.context_menu_at.expect("the menu is open");
let on_board = Rect::from_two_pos(screen(&app, boards[away].x0, boards[away].y0), screen(&app, boards[away].x1, boards[away].y1));
assert!(on_board.contains(at), "the menu opens at the selected artboard: {at:?} in {on_board:?}");
let pasted_at = app.ui.context_artboards.as_ref().map(|f| f.at).expect("the menu's artboard facts");
assert!(boards[away].contains(pasted_at), "{pasted_at:?} on {:?}", boards[away]);
}
/// A right-click on an object that is not selected still selects it, with another object selected before.
#[test]
fn right_click_still_selects_the_object_under_the_pointer() {
let mut app = app();
let ctx = crate::testing::ui_context();
let (_, b) = selection_away_from_the_centre(&mut app, &ctx);
// Inside B, outside the locked rectangle over its middle.
let c = view_centre(&app);
let p = screen(&app, c.x - 40.0, c.y - 40.0);
click(&mut app, &ctx, p, PointerButton::Secondary);
assert_eq!(app.session.active().unwrap().selection.objects, [b]);
assert!(app.ui.context_menu_at.is_some_and(|at| at.distance(p) < 0.5), "the menu opens at the pointer");
}
/// A right-click over a locked object adds an Unlock submenu that lists the object by its name; once
/// the menu is closed, the menu lists no Unlock items.
#[test]