Merge pull request #8779 from yoriswind/codex/8687-status-bar-snapping

Status bar: add quick snapping controls
This commit is contained in:
FredApps
2026-11-27 23:28:36 +01:00
committed by GitHub
7 changed files with 227 additions and 18 deletions
+21 -14
View File
@@ -165,6 +165,21 @@ fn command_rows(ui: &mut Ui, rows: &[CommandRow]) -> Option<(&'static str, Value
/// The Control bar's snapping quick access; every change uses its existing command, and Pixel
/// Snapping Options… opens the dialog of `view.snapToPixel.options`.
fn snapping_menu(app: &mut VectorcraftApp, ui: &mut Ui) {
let label = tl!("Snapping");
let mut ran = None;
ranked(ui, rank::FIELDS, |ui| {
let button = Button::new(label).trailing_icon(IconId::new("chevron-down")).show(ui);
Menu::new(label).opened_by(&button, Trigger::Click).show(ui, |ui, _| {
ran = snapping_menu_contents(app, ui);
});
});
if let Some((id, params)) = ran {
crate::menus::invoke(app, id, params);
}
}
/// The same live command menu in the Control and Status bars.
pub(super) fn snapping_menu_contents(app: &VectorcraftApp, ui: &mut Ui) -> Option<(&'static str, Value)> {
let toggle = |label, id, on| CommandRow { label, check: Some(on), id, params: json!({}) };
let preference =
|label, key: &'static str, on: bool| CommandRow { label, check: Some(on), id: "prefs.set", params: json!({"key": key, "value": !on}) };
@@ -185,22 +200,14 @@ fn snapping_menu(app: &mut VectorcraftApp, ui: &mut Ui) {
preference(tl!("Snap to Perpendicular"), "snapPerpendicular", prefs.snap_perpendicular),
],
];
let label = tl!("Snapping");
let mut ran = None;
ranked(ui, rank::FIELDS, |ui| {
let button = Button::new(label).trailing_icon(IconId::new("chevron-down")).show(ui);
Menu::new(label).opened_by(&button, Trigger::Click).show(ui, |ui, _| {
for (i, rows) in sections.iter().enumerate() {
if i > 0 {
Divider::horizontal().show(ui);
}
ran = command_rows(ui, rows).or(ran.take());
}
});
});
if let Some((id, params)) = ran {
crate::menus::invoke(app, id, params);
for (i, rows) in sections.iter().enumerate() {
if i > 0 {
Divider::horizontal().show(ui);
}
ran = command_rows(ui, rows).or(ran.take());
}
ran
}
/// The Control bar's isolation action: icon, name and command. In isolation mode it is Exit Isolation Mode,
+26 -4
View File
@@ -49,25 +49,44 @@ pub fn status_bar(app: &mut VectorcraftApp, ui: &mut Ui) {
let (mut zoom_to, mut rotate_to, mut go, mut show_pick, mut stop) = (None, None, None, None, false);
let mut invalid_zoom = None;
// The readouts end the bar, the pointer's position and then the timings; a narrow bar leaves out the timings
// first, then the position, before the message.
let mut snapping = None;
// Convenience controls leave first: direct toggles, then the menu. The existing timings and pointer position
// retain their priority, and all trailing groups leave before the navigation, info field or message narrows.
let readouts = |ui: &mut Ui, _: &mut BodyCx<'_>| {
// With no hover point the axes stay empty and keep their width.
let origin = app.ruler_origin();
Row::new().gap(Gap::Item).fit_rank(2).show(ui, |ui, _| {
Row::new().gap(Gap::Item).fit_rank(4).show(ui, |ui, _| {
for (axis, value) in [("X", app.hover_doc.map(|p| p.x - origin.x)), ("Y", app.hover_doc.map(|p| p.y - origin.y))] {
let text = value.map(|v| format!("{axis}: {}", unit.readout(v))).unwrap_or_default();
Text::new(text).role(TypeRole::Mono).shade(Shade::Muted).sample(format!("{axis}: -8888.88 {}", unit.suffix())).show(ui);
}
});
let readout = |text: String, sample: String| Text::new(text).shade(Shade::Faint).sample(sample);
Row::new().gap(Gap::Item).fit_rank(1).show(ui, |ui, _| {
Row::new().gap(Gap::Item).fit_rank(3).show(ui, |ui, _| {
readout(format!("{render} {:.1} ms", app.perf.render_ms), format!("{render} 8888.8 ms")).show(ui);
readout(format!("{ui_name} {:.1} ms", app.perf.frame_ms), format!("{ui_name} 8888.8 ms")).show(ui);
// No rate until the window has drawn continuously for a moment (`frame_rate`).
let fps = if app.perf.fps > 0.0 { format!("{:.0} fps", app.perf.fps) } else { "\u{2013} fps".into() };
readout(fps, "8888 fps".into()).show(ui);
});
Row::new().gap(Gap::Item).fit_rank(1).show(ui, |ui, _| {
for (icon, label, command, on) in [
("sparkles", tl!("Smart Guides"), "view.smartGuides", app.ui.view.smart_guides),
("dc-reference-point", tl!("Snap to Point"), "view.snapToPoint", app.ui.view.snap_to_point),
("grid-3x3", tl!("Snap to Grid"), "view.snapToGrid", app.ui.view.snap_to_grid),
] {
if IconButton::new(IconId::new(icon)).label(label).selected(on).show(ui).clicked() {
snapping = Some((command, json!({})));
}
}
});
Row::new().fit_rank(2).show(ui, |ui, _| {
let label = tl!("Snapping");
let button = Button::new(label).trailing_icon(IconId::new("chevron-down")).show(ui);
Menu::new(label).opened_by(&button, Trigger::Click).show(ui, |ui, _| {
snapping = super::control_bar::snapping_menu_contents(app, ui).or(snapping.take());
});
});
};
// PLAN: assumed VectorcraftApp::canvas_view() -> Option<CanvasView> (cv.S4) and the label msgid "Status Bar".
Bar::new(tl!("Status Bar"), BarKind::Status).trailing(readouts).show(ui, |ui, _| {
@@ -144,6 +163,9 @@ pub fn status_bar(app: &mut VectorcraftApp, ui: &mut Ui) {
if stop {
app.session.script_interrupt().set();
}
if let Some((id, params)) = snapping {
crate::menus::invoke(app, id, params);
}
// It fails only without a document, when there is no view to turn or zoom.
if let Some(zoom) = zoom_to {
app.run("view.setZoom", json!({"zoom": zoom})).ok();
+176
View File
@@ -184,6 +184,182 @@ impl Shown {
}
}
/// #8687: the framework buttons and the shared menu run the existing commands, retain their live accessible
/// state, and leave the document, selection, history, tool, view and unconfirmed pixel options untouched.
#[test]
fn review_8687_status_snapping_uses_existing_commands_and_accessible_state() {
let mut bar = status_readout_app();
bar.room(egui::vec2(2200.0, 300.0));
let (document, selection, history, tool, camera) = {
let mut app = bar.app();
app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 20, "height": 30})).unwrap();
app.ui.view.smart_guides = false;
app.ui.view.snap_to_point = false;
app.ui.view.snap_to_grid = false;
app.ui.view.snap_to_pixel = false;
app.ui.view.snap_to_glyph = false;
app.run("prefs.set", json!({"key": "alignmentGuides", "value": true})).unwrap();
let st = app.session.active().unwrap();
(
json!(st.doc.as_ref()),
st.selection.clone(),
(st.history.undo.len(), st.history.redo.len()),
app.session.tool_id().to_owned(),
app.view().map(|v| (v.zoom, v.rotation)),
)
};
for (name, command) in [("Smart Guides", "view.smartGuides"), ("Snap to Point", "view.snapToPoint"), ("Snap to Grid", "view.snapToGrid")] {
let button = bar.settle().named(name);
assert_eq!(bar.tree().role_of(button.node), Some(Role::BUTTON));
// Native IconButton exposes its active state as pressed/toggled through its BUTTON role.
assert_eq!(bar.tree().toggled_of(button.node), Some(Mixed::Same(false)));
let before = bar.app().run_count;
bar.click_on(&button).settle();
assert_eq!(bar.app().run_count, before + 1, "direct {name} uses the existing command entry");
assert_eq!(crate::menus::checked(&bar.app(), command, &json!({})), Some(true));
let button = bar.named(name);
assert_eq!(bar.tree().toggled_of(button.node), Some(Mixed::Same(true)));
crate::menus::invoke(&mut bar.app(), command, json!({}));
let button = bar.settle().named(name);
assert_eq!(bar.tree().toggled_of(button.node), Some(Mixed::Same(false)), "reverse commands refresh the live button");
}
for (name, command) in [
("Smart Guides", "view.smartGuides"),
("Snap to Point", "view.snapToPoint"),
("Snap to Grid", "view.snapToGrid"),
("Snap to Pixel", "view.snapToPixel"),
("Snap to Glyph", "view.snapToGlyph"),
] {
bar.click("Snapping").settle();
// The menu follows the direct buttons in the tree; select the checked menu row by role.
let row = review_8687_menu_row(&bar, name);
assert!(!bar.checked(&row));
let before = bar.app().run_count;
bar.click_on(&row).settle();
assert_eq!(bar.app().run_count, before + 1, "menu {name} uses the existing command entry");
assert_eq!(crate::menus::checked(&bar.app(), command, &json!({})), Some(true));
bar.key(egui::Key::Escape);
crate::menus::invoke(&mut bar.app(), command, json!({}));
bar.settle().click("Snapping").settle();
assert!(!bar.checked(&review_8687_menu_row(&bar, name)), "menu rereads external command state");
bar.key(egui::Key::Escape);
}
bar.click("Snapping").settle();
let preferences = {
let app = bar.app();
[
("Alignment Guides", app.session.prefs.alignment_guides),
("Snap to Tangent", app.session.prefs.snap_tangent),
("Snap to Perpendicular", app.session.prefs.snap_perpendicular),
]
};
for (name, on) in preferences {
assert_eq!(bar.checked(&review_8687_menu_row(&bar, name)), on);
}
let row = review_8687_menu_row(&bar, "Alignment Guides");
let before = bar.app().run_count;
bar.click_on(&row).settle();
assert_eq!(bar.app().run_count, before + 1);
assert!(!bar.app().session.prefs.alignment_guides);
bar.key(egui::Key::Escape).click("Snapping").settle();
let pixel = bar.app().session.prefs.pixel_snap;
bar.click(tl!("Pixel Snapping Options…")).settle();
{
let app = bar.app();
let dialogs = app.dialogs().expect("the native dialog host");
let id = craft::ui::dialog::dialog_id(crate::dialogs::pixel_snap_options::KIND);
dialogs.with(|host| {
let dialog = host.open_dialogs().into_iter().find(|dialog| dialog.id == id).expect("the existing Pixel Snapping Options dialog");
assert_eq!(dialog.key.as_deref(), Some(crate::dialogs::pixel_snap_options::KIND));
assert_eq!(dialog.fields, Some(json!({"drawing": pixel.drawing, "moving": pixel.moving, "scaling": pixel.scaling})));
assert!(host.cancel_by_id(bar.fixture.ctx(), id));
assert!(!host.open_dialogs().iter().any(|dialog| dialog.id == id));
});
assert_eq!(app.session.prefs.pixel_snap, pixel, "closing without OK changes no option");
let st = app.session.active().unwrap();
assert_eq!(json!(st.doc.as_ref()), document);
assert_eq!(st.selection, selection);
assert_eq!((st.history.undo.len(), st.history.redo.len()), history);
assert_eq!(app.session.tool_id(), tool);
assert_eq!(app.view().map(|v| (v.zoom, v.rotation)), camera);
}
}
fn review_8687_menu_row(bar: &Shown, name: &str) -> Target {
let tree = bar.tree();
let rows: Vec<_> = tree
.keys()
.into_iter()
.filter(|key| tree.role_of(*key) == Some(Role::MENU_ITEM_CHECK_BOX))
.filter(|key| tree.name_of(*key) == Some(name) && tree.toggled_of(*key).is_some())
.collect();
assert_eq!(rows.len(), 1, "one live checked menu row named {name}");
bar.fixture.target(rows[0]).unwrap()
}
/// #8687: fit ranks replace raw pixel reservations. The direct group leaves whole, then the menu, while current
/// main's navigation, info, message and higher-priority readouts keep their places and controls stay in the bar.
#[test]
fn review_8687_status_snapping_fits_full_menu_and_omitted_real_frames() {
let mut bar = status_readout_app();
bar.app().hover_doc = Some(vectorcraft_geom::Point::new(7.0, 5.0));
let mut stages = Vec::new();
let (mut message_origin, mut info_origin) = (None, None);
for width in (400..=2200).rev().step_by(10).map(|w| w as f32) {
bar.room(egui::vec2(width, 300.0)).settle();
let direct: Vec<_> = ["Smart Guides", "Snap to Point", "Snap to Grid"].into_iter().filter_map(|name| bar.find(name)).collect();
let menu = bar.find("Snapping");
assert!(direct.is_empty() || direct.len() == 3, "the direct group remains whole at {width}");
let stage = if !direct.is_empty() {
2
} else if menu.is_some() {
1
} else {
0
};
assert!(stage != 2 || menu.is_some(), "full includes Snapping");
if let Some(previous) = stages.last() {
assert!(stage <= *previous, "controls cannot return as the bar narrows");
}
if stages.last() != Some(&stage) {
stages.push(stage);
}
let texts = bar.texts();
let message = status_text(&texts, "Saved recovery data");
let info = status_text(&texts, "Selection");
let controls_band = bar.find("Zoom").unwrap().rect.union(bar.find("Rotation").unwrap().rect);
assert!(message.bounds.left() >= info.bounds.right());
for (origin, at) in [(&mut message_origin, message.at), (&mut info_origin, info.at)] {
if let Some(first) = *origin {
assert_eq!(at, first, "snapping cannot displace core status content");
} else {
*origin = Some(at);
}
}
for name in ["Zoom", "Rotation", "First Artboard", "Previous Artboard", "Next Artboard", "Last Artboard"] {
assert!(bar.find(name).is_some(), "{name} remains at {width}");
}
let mut rects = Vec::new();
for target in direct.into_iter().chain(menu) {
let rect = target.rect;
assert!(rect.left() >= info.bounds.right() && rect.right() <= width);
assert!(rect.top() >= controls_band.top() && rect.bottom() <= controls_band.bottom(), "closed controls stay in the status bar's row");
assert!(!rect.intersects(message.bounds), "controls leave the message its own rectangle");
assert!(rects.iter().all(|old: &egui::Rect| !old.intersects(rect)), "controls do not overlap");
rects.push(rect);
}
for field in texts.iter().filter(|t| t.text.starts_with("X:") || t.text.starts_with("Y:") || t.text.starts_with("render")) {
assert!(field.bounds.left() >= info.bounds.right());
assert!(!field.bounds.intersects(message.bounds));
assert!(rects.iter().all(|r| !r.intersects(field.bounds)));
}
if stage == 0 {
break;
}
}
assert_eq!(stages, [2, 1, 0], "real frames reach full, dropdown-only, then omitted");
}
/// A tab dropped on the strip lands at an index among all the open documents: the documents without a tab (the
/// floating ones) keep their places in the documents' order, and a document with two views moves as one (#5663).
#[test]
+1
View File
File diff suppressed because one or more lines are too long
+1
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long