Skip to content
29 changes: 29 additions & 0 deletions assets/locales/pending/component-menu-organize.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"en": {
"ui.organize": "Organize"
},
"en-GB": {
"ui.organize": "Organise"
},
"ar": {
"ui.organize": "تنظيم"
},
"de": {
"ui.organize": "Organisieren"
},
"he": {
"ui.organize": "ארגון"
},
"ja": {
"ui.organize": "整理"
},
"ko": {
"ui.organize": "정리"
},
"vi": {
"ui.organize": "Sắp xếp"
},
"zh": {
"ui.organize": "整理"
}
}
13 changes: 10 additions & 3 deletions crates/ui-egui/src/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,18 @@ pub fn folded_folder_items(app: &SolveApp, target: &Target) -> Vec<Item> {
},
_ => return Vec::new(),
};
if !bodies_folded(app, comp, &scope, entries(app, comp, "bodies").len()) {
let count = entries(app, comp, "bodies").len();
if !bodies_folded(app, comp, &scope, count) {
return Vec::new();
}
let items =
folder_items(app, comp, "bodies", &scope).into_iter().filter(|i| matches!(i.id.as_str(), "component.from_bodies" | "ui.groupSelected"));
// Components from a component's only body would just nest it one level deeper.
let items: Vec<_> = folder_items(app, comp, "bodies", &scope)
.into_iter()
.filter(|i| i.id == "ui.groupSelected" || (i.id == "component.from_bodies" && count > 1))
.collect();
if items.is_empty() {
return items;
}
std::iter::once(Item::sep()).chain(items).collect()
}

Expand Down
503 changes: 307 additions & 196 deletions crates/ui-egui/src/context_menu.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions crates/ui-egui/src/design_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ mod tests {
// Held until checked: under load the worker could otherwise finish before the menu returns.
let (finish, gate) = std::sync::mpsc::channel();
h.app.apply_gate = Some(std::sync::Arc::new(std::sync::Mutex::new(gate)));
let items = crate::context_menu::items(&h.app, &crate::context_menu::Target::Component { id: 0 });
let items = crate::context_menu::entries(&h.app, &crate::context_menu::Target::Component { id: 0 });
let item = items.iter().find(|i| i.label == "Properties").expect("Properties item");
crate::context_menu::run_item(&mut h.app, item, egui::pos2(0.0, 0.0));
assert!(h.app.menu.props_measuring.is_some(), "the menu returns at once, the card measuring");
Expand Down Expand Up @@ -732,7 +732,7 @@ mod tests {
#[test]
fn the_file_menu_browser_and_root_component_open_the_window() {
let mut h = Harness::new();
let items = crate::context_menu::items(&h.app, &crate::context_menu::Target::Component { id: 0 });
let items = crate::context_menu::entries(&h.app, &crate::context_menu::Target::Component { id: 0 });
let item = items.iter().find(|i| i.id == "ui.designProperties").expect("root component menu item");
assert_eq!(item.label, "Design Properties");
crate::context_menu::run_item(&mut h.app, item, egui::pos2(0.0, 0.0));
Expand Down
2 changes: 1 addition & 1 deletion crates/ui-egui/src/edit_in_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl EditInPlace {

/// A linked component the window edits in place when it is double-clicked: in a saved design
/// (its links are found from its file) with `double_click_components` on.
fn edits_on_double_click(app: &SolveApp, component: u64) -> bool {
pub(crate) fn edits_on_double_click(app: &SolveApp, component: u64) -> bool {
app.preferences.double_click_components && app.session.path.is_some() && app.session.doc.links.contains_key(&component)
}

Expand Down
11 changes: 11 additions & 0 deletions crates/ui-egui/src/edit_in_place_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,17 @@ fn double_clicking_a_body_of_this_design_or_with_the_preference_off_does_nothing
assert!(f.app.edit_in_place.active.is_none() && f.app.edit_in_place.pending.is_none());
}

/// A linked component's menu starts with what a double-click on its row does.
#[test]
fn a_linked_components_menu_starts_with_what_a_double_click_does() {
use crate::context_menu::{self, Target};
let mut f = assembly("menu-order");
let ids = |app: &SolveApp| context_menu::items(app, &Target::Component { id: f.plate }).into_iter().take(3).map(|i| i.id).collect::<Vec<_>>();
assert_eq!(ids(&f.app), ["ui.editInPlace", "ui.openLinkedDesign", "ui.activate"]);
f.app.preferences.double_click_components = false;
assert_eq!(ids(&f.app), ["ui.activate", "ui.editInPlace", "ui.openLinkedDesign"]);
}

#[test]
fn activate_only_activates_and_edit_in_place_opens_the_placement_right_clicked() {
use crate::context_menu::{self, Target};
Expand Down
14 changes: 8 additions & 6 deletions crates/ui-egui/src/linked_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ mod tests {
}

fn menu(app: &SolveApp, target: Target) -> Option<context_menu::Item> {
context_menu::items(app, &target).into_iter().find(|i| i.id == "ui.relinkSource")
context_menu::entries(app, &target).into_iter().find(|i| i.id == "ui.relinkSource")
}

fn answer(app: &mut SolveApp, path: Option<String>) {
Expand Down Expand Up @@ -393,16 +393,18 @@ mod tests {
let made = app.run("component.create", json!({"name": "Bracket", "grounded": false})).unwrap();
let (component, occurrence) = (made["component"].as_u64().unwrap(), made["occurrence"].as_u64().unwrap());
app.run("solid.box", json!({"length": 10, "width": 20, "height": 30})).unwrap();
let find = |app: &SolveApp, id: u64| context_menu::items(app, &Target::Component { id }).into_iter().find(|i| i.id == "ui.extractComponent");
let find =
|app: &SolveApp, id: u64| context_menu::entries(app, &Target::Component { id }).into_iter().find(|i| i.id == "ui.extractComponent");
assert!(find(&app, 0).is_none());
// Not offered while the component can't go: here, moved without Capture Position
app.run("occurrence.move", json!({"occurrence": occurrence, "translate": [5, 0, 0], "capture": false})).unwrap();
assert!(find(&app, component).is_none());
app.run("component.revert_position", json!({})).unwrap();
// In Relink Source's place, right after Properties
let ids: Vec<String> = context_menu::items(&app, &Target::Component { id: component }).into_iter().map(|i| i.id).collect();
let properties = ids.iter().position(|i| i == "ui.properties").unwrap();
assert_eq!(ids.get(properties + 1).map(String::as_str), Some("ui.extractComponent"));
// In Relink Source's place in the Component flyout, right below New Component
let rows = context_menu::items(&app, &Target::Component { id: component });
let ids: Vec<&str> = rows.iter().find(|i| i.label == "Component").unwrap().children.iter().map(|i| i.id.as_str()).collect();
let create = ids.iter().position(|i| *i == "component.create").unwrap();
assert_eq!(ids.get(create + 1), Some(&"ui.extractComponent"));
let item = find(&app, component).unwrap();
let chosen = folder.join("bracket").to_string_lossy().into_owned();
app.services.pick_save = Some(Box::new(move |name, extensions| {
Expand Down
164 changes: 159 additions & 5 deletions crates/ui-egui/src/menu_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ fn find(items: &[Item], label: &str) -> Item {
.unwrap_or_else(|| panic!("no `{label}` in {:?}", items.iter().map(|i| &i.label).collect::<Vec<_>>()))
}

/// Run the item labelled `label` of the target's menu.
/// Run the item labelled `label` of the target's menu or one of its flyouts.
fn act(app: &mut SolveApp, t: &Target, label: &str) {
let item = find(&context_menu::items(app, t), label);
let item = find(&context_menu::entries(app, t), label);
context_menu::run_item(app, &item, pos2(0.0, 0.0));
}

Expand Down Expand Up @@ -469,6 +469,17 @@ fn component_menu_activates_grounds_and_pastes() {
let t = Target::Component { id: c };
act(&mut app, &t, "Activate");
assert_eq!(app.session.active_component, c);
let rows = context_menu::items(&app, &t);
let ids: Vec<_> = rows.iter().map(|i| i.id.as_str()).collect();
let movement = ids.iter().position(|id| *id == "ui.moveOccurrence").expect("Move/Copy stays on the menu");
assert_eq!(ids.get(movement + 1), Some(&"ui.group.placement"), "Placement follows Move/Copy");
assert!(find(&rows, "Placement").children.iter().any(|i| i.id == "occurrence.ground"), "Ground sits in the Placement flyout");
let own: Vec<_> = find(&rows, "Component").children.iter().map(|i| i.id.clone()).collect();
for id in ["ui.rename", "ui.appearance", "ui.material", "component.cycle_color", "component.create", "ui.properties"] {
assert!(own.iter().any(|o| o == id) && !ids.contains(&id), "{id} sits in the Component flyout only");
}
assert!(find(&rows, "Organize").children.iter().any(|i| i.id == "ui.newGroup"), "New Group sits with the browser tree's entries");
assert!(ids.contains(&"ui.pasteNew"), "Paste New stays with the clipboard");
// The design's first part comes in grounded (#2804); the menu frees it and grounds it again.
assert!(app.session.doc.occurrence_of(c).unwrap().grounded);
act(&mut app, &t, "Unground");
Expand All @@ -484,7 +495,7 @@ fn component_menu_activates_grounds_and_pastes() {
act(&mut app, &placement, "Copy");
assert_eq!(app.copied_placements.len(), 1, "{:?}", app.status);
let design = Target::Component { id: 0 };
assert!(find(&context_menu::items(&app, &design), "Paste New").enabled);
assert!(find(&context_menu::entries(&app, &design), "Paste New").enabled);
act(&mut app, &design, "Paste New");
assert_eq!(app.session.doc.components.len(), 2);
}
Expand Down Expand Up @@ -613,7 +624,7 @@ fn expand_and_collapse_all_open_the_browser_tree_from_the_design_menu() {
let mut app = sample_app();
let n = app.session.undo.len();
let root = Target::Component { id: 0 };
assert!(context_menu::items(&app, &root).iter().any(|i| i.label == "Expand All" && i.id == "browser.expand_all"));
assert!(context_menu::entries(&app, &root).iter().any(|i| i.label == "Expand All" && i.id == "browser.expand_all"));
act(&mut app, &root, "Collapse All");
assert!(app.tree.collapsed.contains("c0/bodies"), "{:?}", app.tree.collapsed);
assert!(!app.tree.collapsed.contains("c0"), "the design row stays open: {:?}", app.tree.collapsed);
Expand Down Expand Up @@ -1082,7 +1093,7 @@ fn rename_shows_f2_and_f2_with_a_menu_open_renames_what_the_menu_is_for() {
let data = solidcraft_engine::doc::canvas::base64_encode(&png);
let canvas = h.app.run("canvas.insert", json!({"data": data, "plane": "XY", "width": 80})).unwrap()["canvas"].as_u64().unwrap();
let rename_key = |app: &SolveApp, target: &Target| {
context_menu::items(app, target)
context_menu::entries(app, target)
.into_iter()
.find(|i| i.id == "ui.rename")
.map(|i| i.shortcut)
Expand Down Expand Up @@ -1817,6 +1828,149 @@ fn only_one_row_is_lit_while_a_flyout_is_closing() {
assert_eq!(lit_rows(&h, &rows), vec![other.label.clone()], "the parent gives up its highlight as soon as the pointer leaves it");
}

/// A component's menu (its Component flyout holds Appearance, which has a flyout of its own),
/// open in a harness with the pointer on the menu.
fn component_menu() -> (crate::scenario::Harness, u64) {
let mut h = crate::scenario::Harness::new();
h.app = sample_app();
let b = body(&h.app);
h.app.run("component.from_bodies", json!({"bodies": [b]})).unwrap();
let c = h.app.session.doc.components[0].id;
h.frames(3);
context_menu::open_for(&mut h.app, pos2(300.0, 60.0), Target::Component { id: c });
h.frames(2);
h.step(&json!({"move":[10, 10]})).unwrap();
(h, c)
}

#[test]
fn a_flyout_inside_a_flyout_opens_on_hover_and_applies_its_choice() {
use crate::scenario::rect_of;
let (mut h, c) = component_menu();
h.step(&json!({"move":{"handle":"menu:ui.group.component"}})).unwrap();
h.frames(2);
h.step(&json!({"move":{"handle":"menu:ui.appearance:Appearance"}})).unwrap();
h.frames(2);
let swatch = "menu:appearance.assign:Aluminum - Satin";
assert!(rect_of(swatch).is_some(), "hovering Appearance in the Component flyout opens its quick choices");
assert!(rect_of("menu:ui.rename:Rename").is_some(), "the Component flyout stays open");
h.step(&json!({"click":{"handle":swatch}})).unwrap();
assert!(h.app.session.doc.appearances.components.contains_key(&c), "the choice applies to the component");
assert!(h.app.viewport.context_menu.is_none());
assert!(h.app.dialog.is_none(), "a quick choice opens no editor");
}

#[test]
fn keys_walk_into_nested_flyouts_and_left_steps_back_one_level() {
use crate::scenario::rect_of;
let (mut h, _) = component_menu();
let rows = context_menu::items(&h.app, &h.app.menu.target.clone());
h.app.menu.key_row = rows.iter().position(|i| i.id == "ui.group.component");
h.step(&json!({"key":"ArrowRight"})).unwrap();
assert!(rect_of("menu:ui.rename:Rename").is_some(), "Right opens the Component flyout");
let own = &find(&rows, "Component").children;
let downs = own.iter().filter(|i| i.enabled && i.id != "-" && !i.id.starts_with('#')).position(|i| i.id == "ui.appearance").unwrap();
for _ in 0..downs {
h.step(&json!({"key":"ArrowDown"})).unwrap();
}
h.step(&json!({"key":"ArrowRight"})).unwrap();
h.frames(2);
assert!(rect_of("menu:appearance.assign:Aluminum - Satin").is_some(), "Right on Appearance opens its quick choices");
assert!(h.app.dialog.is_none(), "opening a flyout runs nothing");
h.step(&json!({"key":"ArrowLeft"})).unwrap();
h.frames(2);
assert!(rect_of("menu:appearance.assign:Aluminum - Satin").is_none(), "Left closes the innermost flyout");
assert!(rect_of("menu:ui.rename:Rename").is_some(), "and keeps the one it came from");
h.step(&json!({"key":"ArrowLeft"})).unwrap();
h.frames(2);
assert!(rect_of("menu:ui.rename:Rename").is_none(), "Left again closes the Component flyout");
assert!(h.app.viewport.context_menu.is_some(), "the menu itself stays open");
}

/// Resting on another row of a flyout closes only the flyouts further in (#6657).
#[test]
fn resting_on_another_row_of_a_flyout_closes_only_the_flyout_beyond_it() {
use crate::scenario::rect_of;
let (mut h, _) = component_menu();
h.step(&json!({"move":{"handle":"menu:ui.group.component"}})).unwrap();
h.frames(2);
h.step(&json!({"move":{"handle":"menu:ui.appearance:Appearance"}})).unwrap();
h.frames(2);
h.step(&json!({"move":{"handle":"menu:ui.rename:Rename"}})).unwrap();
h.frames(30);
assert!(rect_of("menu:appearance.assign:Aluminum - Satin").is_none(), "resting on Rename closes the Appearance flyout");
assert!(rect_of("menu:ui.rename:Rename").is_some(), "the Component flyout stays open");
}

/// Left from a flyout the pointer opened chooses the row that opened it, so Enter never runs a
/// row the keys chose before.
#[test]
fn left_from_a_hovered_flyout_chooses_the_row_that_opened_it() {
let (mut h, _) = component_menu();
let rows = context_menu::items(&h.app, &h.app.menu.target.clone());
h.app.menu.key_row = rows.iter().position(|i| i.id == "ui.copy");
h.step(&json!({"move":{"handle":"menu:ui.group.component"}})).unwrap();
h.frames(2);
h.step(&json!({"key":"ArrowLeft"})).unwrap();
assert_eq!(h.app.menu.key_row, rows.iter().position(|i| i.id == "ui.group.component"));
assert!(crate::scenario::rect_of("menu:ui.rename:Rename").is_none(), "the flyout closed");
}

/// Near the right edge a flyout opens to the left, and a flyout inside it opens further left
/// instead of over the menu.
#[test]
fn a_flyout_inside_a_flyout_that_opened_to_the_left_opens_to_the_left() {
use crate::scenario::rect_of;
let (mut h, c) = component_menu();
let right = h.ctx.content_rect().right();
context_menu::open_for(&mut h.app, pos2(right - context_menu::LIST_W - 50.0, 60.0), Target::Component { id: c });
h.frames(2);
h.step(&json!({"move":{"handle":"menu:ui.group.component"}})).unwrap();
h.frames(2);
h.step(&json!({"move":{"handle":"menu:ui.appearance:Appearance"}})).unwrap();
h.frames(2);
let menu = rect_of("menu:ui.group.component").unwrap();
let own = rect_of("menu:ui.rename:Rename").unwrap();
let swatch = rect_of("menu:appearance.assign:Aluminum - Satin").unwrap();
assert!(own.right() <= menu.left(), "the Component flyout opens to the left: {own:?} {menu:?}");
assert!(swatch.right() <= own.left(), "and the Appearance flyout further left: {swatch:?} {own:?}");
}

/// `ui.menuPick` reaches the entries of every flyout and refuses a row that only opens one.
#[test]
fn menu_pick_runs_flyout_entries_and_refuses_a_row_that_only_opens_one() {
let (mut h, _) = component_menu();
assert!(context_menu::pick(&mut h.app, "Organize").is_err());
assert!(h.app.viewport.context_menu.is_some(), "a refused pick leaves the menu open");
assert_eq!(context_menu::pick(&mut h.app, "Collapse All").unwrap()["ran"], "browser.collapse_all");
}

/// Every flyout is as tall as its entries: one opened after a shorter one used to keep the shorter
/// one's height and scroll, so its last entries could not be clicked.
#[test]
fn a_flyout_opened_after_a_shorter_one_shows_all_its_entries() {
let (mut h, _) = component_menu();
h.step(&json!({"move":{"handle":"menu:ui.group.organize"}})).unwrap();
h.frames(2);
h.step(&json!({"move":{"handle":"menu:ui.group.component"}})).unwrap();
h.frames(3);
h.step(&json!({"click":{"handle":"menu:ui.properties:Properties"}})).unwrap();
assert!(h.app.menu.props.is_some(), "the last entry of the Component flyout is reachable");
}

/// A flyout taller than egui's default area size (the quick choices) is measured whole, so it
/// moves up to fit and its last entries can be clicked.
#[test]
fn a_tall_flyout_moves_up_to_fit_and_its_last_entries_can_be_clicked() {
let (mut h, c) = component_menu();
h.step(&json!({"move":{"handle":"menu:ui.group.component"}})).unwrap();
h.frames(2);
h.step(&json!({"move":{"handle":"menu:ui.appearance:Appearance"}})).unwrap();
h.frames(3);
h.step(&json!({"click":{"handle":"menu:appearance.assign:Glass - Smoked"}})).unwrap();
assert!(h.app.session.doc.appearances.components.contains_key(&c), "a late quick choice is reachable");
}

#[test]
fn narrow_overflow_submenu_keeps_opening_click_and_accepts_the_next_click() {
use crate::scenario::Harness;
Expand Down
Loading
Loading