diff --git a/crates/ui-egui/src/panel_docking/native_tests.rs b/crates/ui-egui/src/panel_docking/native_tests.rs index e9e8c4a3df..8a2e3e271f 100644 --- a/crates/ui-egui/src/panel_docking/native_tests.rs +++ b/crates/ui-egui/src/panel_docking/native_tests.rs @@ -25,26 +25,13 @@ struct Window { frames: usize, /// The texts its last frame drew, with their rects in the window. texts: Vec<(String, Rect)>, - accessibility: Vec, } struct Host { windows: HashMap, - child_overlay: Option<(ViewportId, Rect)>, - child_transparent_overlay: Option<(ViewportId, Rect)>, - child_transition_overlay: Option<(ViewportId, Rect, bool)>, - child_empty_overlay: Option<(ViewportId, Rect)>, - child_pass_through_overlay: Option<(ViewportId, Rect)>, - child_raise_overlay: Option<(ViewportId, Rect, bool)>, - child_raise_layer: Option<(ViewportId, egui::LayerId)>, - child_layer_order: HashMap>, /// The window with the keyboard. focus: ViewportId, time: f64, - /// Every command sent to a window, in order. - commands: Vec<(ViewportId, ViewportCommand)>, - /// The monitor size each window reports. - monitor_size: Option, } impl Host { @@ -54,7 +41,6 @@ impl Host { outer_rect: Some(rect), focused: Some(self.focus == id), native_pixels_per_point: Some(1.0), - monitor_size: self.monitor_size, ..Default::default() }; std::iter::once((ViewportId::ROOT, info(ViewportId::ROOT, SCREEN.translate(ORIGIN.to_vec2())))) @@ -65,7 +51,6 @@ impl Host { fn apply(&mut self, full: &egui::FullOutput) { for (id, output) in &full.viewport_output { for command in &output.commands { - self.commands.push((*id, command.clone())); let Some(window) = self.windows.get_mut(id) else { continue }; match command { ViewportCommand::OuterPosition(pos) => window.rect = Rect::from_min_size(*pos, window.rect.size()), @@ -83,14 +68,7 @@ impl Host { let mut host = host.borrow_mut(); let builder = &viewport.builder; let rect = Rect::from_min_size(builder.position.unwrap_or_default(), builder.inner_size.unwrap_or(vec2(300.0, 200.0))); - host.windows.entry(id).or_insert_with(|| Window { - rect, - events: Vec::new(), - dropped: Vec::new(), - frames: 0, - texts: Vec::new(), - accessibility: Vec::new(), - }); + host.windows.entry(id).or_insert_with(|| Window { rect, events: Vec::new(), dropped: Vec::new(), frames: 0, texts: Vec::new() }); let viewports = host.infos(); let (time, focused) = (host.time, host.focus == id); let Some(window) = host.windows.get_mut(&id) else { return }; @@ -106,69 +84,7 @@ impl Host { ..Default::default() } }; - let child_overlay = host.borrow().child_overlay.filter(|(viewport, _)| *viewport == id).map(|(_, rect)| rect); - let child_transparent_overlay = host.borrow().child_transparent_overlay.filter(|(viewport, _)| *viewport == id).map(|(_, rect)| rect); - let child_transition_overlay = - host.borrow().child_transition_overlay.filter(|(viewport, _, _)| *viewport == id).map(|(_, rect, painted)| (rect, painted)); - let child_empty_overlay = host.borrow().child_empty_overlay.filter(|(viewport, _)| *viewport == id).map(|(_, rect)| rect); - let child_pass_through_overlay = host.borrow().child_pass_through_overlay.filter(|(viewport, _)| *viewport == id).map(|(_, rect)| rect); - let child_raise_overlay = host.borrow().child_raise_overlay.filter(|(viewport, _, _)| *viewport == id).map(|(_, rect, raise)| (rect, raise)); - let child_raise_layer = host.borrow().child_raise_layer.filter(|(viewport, _)| *viewport == id).map(|(_, layer)| layer); - let mut layer_order = Vec::new(); - let mut full = ctx.run_ui(input, |ui| { - layer_order = ui.ctx().memory(|memory| memory.layer_ids().collect()); - if let Some((rect, _)) = child_raise_overlay { - egui::Area::new(egui::Id::unique("Child raised overlay")).order(egui::Order::Background).fixed_pos(rect.min).show(ui.ctx(), |ui| { - ui.allocate_space(rect.size()); - }); - } - (viewport.viewport_ui_cb)(ui); - if let Some(layer) = child_raise_layer { - ui.ctx().move_to_top(layer); - } - if child_raise_overlay.is_some_and(|(_, raise)| raise) { - ui.ctx().move_to_top(egui::LayerId::new(egui::Order::Background, egui::Id::unique("Child raised overlay"))); - } - if let Some(rect) = child_overlay { - egui::Window::new("Child overlay").fixed_pos(rect.min).fixed_size(rect.size()).show(ui.ctx(), |ui| { - ui.label("Child overlay"); - }); - } - if let Some(rect) = child_transparent_overlay { - egui::Area::new(egui::Id::unique("Child transparent overlay")).order(egui::Order::Foreground).fixed_pos(rect.min).show( - ui.ctx(), - |ui| { - ui.allocate_space(rect.size()); - }, - ); - } - if let Some((rect, painted)) = child_transition_overlay { - egui::Area::new(egui::Id::unique("Child transition overlay")).order(egui::Order::Foreground).fixed_pos(rect.min).show( - ui.ctx(), - |ui| { - ui.allocate_space(rect.size()); - if painted { - ui.label("Child transition overlay"); - } - }, - ); - } - if let Some(rect) = child_empty_overlay { - egui::Area::new(egui::Id::unique("Child empty overlay")).order(egui::Order::Foreground).fixed_pos(rect.min).show(ui.ctx(), |ui| { - ui.set_min_size(rect.size()); - }); - } - if let Some(rect) = child_pass_through_overlay { - egui::Area::new(egui::Id::unique("Child pass-through overlay")) - .order(egui::Order::Foreground) - .fixed_pos(rect.min) - .interactable(false) - .show(ui.ctx(), |ui| { - let (paint, _) = ui.allocate_exact_size(rect.size(), egui::Sense::hover()); - ui.painter().rect_filled(paint, 0.0, egui::Color32::TRANSPARENT); - }); - } - }); + let mut full = ctx.run_ui(input, |ui| (viewport.viewport_ui_cb)(ui)); full.textures_delta.clear(); fn texts(s: &egui::Shape, out: &mut Vec<(String, Rect)>) { match s { @@ -179,13 +95,9 @@ impl Host { } let mut shown = Vec::new(); full.shapes.iter().for_each(|c| texts(&c.shape, &mut shown)); - let accessibility = - full.platform_output.accesskit_update.take().map(|update| update.nodes.into_iter().map(|(_, node)| node).collect()).unwrap_or_default(); let mut host = host.borrow_mut(); - host.child_layer_order.insert(id, layer_order); if let Some(window) = host.windows.get_mut(&id) { window.texts = shown; - window.accessibility = accessibility; } host.apply(&full); } @@ -195,33 +107,18 @@ struct Harness { app: VectorcraftApp, ctx: egui::Context, host: Rc>, - root_overlay: Option, } impl Harness { fn new() -> Self { - let host = Rc::new(RefCell::new(Host { - windows: HashMap::new(), - child_overlay: None, - child_transparent_overlay: None, - child_transition_overlay: None, - child_empty_overlay: None, - child_pass_through_overlay: None, - child_raise_overlay: None, - child_raise_layer: None, - child_layer_order: HashMap::new(), - focus: ViewportId::ROOT, - time: 0.0, - commands: Vec::new(), - monitor_size: None, - })); + let host = Rc::new(RefCell::new(Host { windows: HashMap::new(), focus: ViewportId::ROOT, time: 0.0 })); let shared = host.clone(); egui::Context::set_immediate_viewport_renderer(move |ctx, viewport| Host::render(&shared, ctx, viewport)); let ctx = egui::Context::default(); ctx.set_embed_viewports(false); let mut app = VectorcraftApp::new(Session::new(), Default::default()); app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); - let mut h = Self { app, ctx, host, root_overlay: None }; + let mut h = Self { app, ctx, host }; h.frames(3); h } @@ -233,17 +130,12 @@ impl Harness { (host.infos(), host.focus == ViewportId::ROOT, host.time) }; let mut input = egui::RawInput { time: Some(time), events, screen_rect: Some(SCREEN), viewports, focused, ..Default::default() }; - self.app.raw_input_hook(&self.ctx, &mut input); + // PLAN: assumed `embed::before_input(ctx, &mut input, &mut app.frame_input)` (hub-w1 M7; shell A1). + craft_ui::runtime::embed::before_input(&self.ctx, &mut input, &mut self.app.frame_input); let app = &mut self.app; - let root_overlay = self.root_overlay; let mut full = self.ctx.run_ui(input, |ui| { app.logic(ui.ctx()); app.ui(ui); - if let Some(rect) = root_overlay { - egui::Window::new("Root overlay").fixed_pos(rect.min).fixed_size(rect.size()).show(ui.ctx(), |ui| { - ui.label("Root overlay"); - }); - } }); full.textures_delta.clear(); self.host.borrow_mut().apply(&full); @@ -258,7 +150,7 @@ impl Harness { /// The viewport of the floating window holding `panel`. fn viewport(&self, panel: &str) -> ViewportId { let layout = layout_of(&self.app.ui); - let window = floating_window(&layout, panel).expect("the panel floats"); + let window = floating_window(layout, panel).expect("the panel floats"); craft_ui::docking::DockArea::::window_viewport(area_id(), window.id) } @@ -306,6 +198,8 @@ impl egui::DroppedFile for MockDrop { } } +/// `window.panel.float` with a rect moves an OS window the panel already has, in each of the modes +/// (a panel, its group, its column); a missing `x`/`y` or size keeps the window's own. #[test] fn an_explicit_float_command_updates_an_existing_native_windows_geometry() { for mode in [json!({}), json!({"group": true}), json!({"column": true})] { @@ -337,288 +231,6 @@ fn an_explicit_float_command_updates_an_existing_native_windows_geometry() { } } -#[test] -fn saved_native_geometry_updates_before_viewport_metadata_is_available() { - let mut h = Harness::new(); - let viewport = h.float("swatches"); - h.app.ui_ctx = None; - h.app.run("window.panel.float", json!({"panel": "swatches", "x": 280, "y": 190, "height": 390})).unwrap(); - h.app.run("window.panel.float", json!({"panel": "swatches", "width": 350})).unwrap(); - h.frames(3); - assert_eq!(h.viewport("swatches"), viewport); - assert_eq!(h.host.borrow().windows[&viewport].rect, Rect::from_min_size(ORIGIN + vec2(280.0, 190.0), vec2(350.0, 390.0))); - h.app.run("window.panel.float", json!({"panel": "color", "onto": "swatches"})).unwrap(); - h.frames(3); - h.app.ui_ctx = None; - h.app.run("window.panel.float", json!({"panel": "swatches", "column": true, "x": 480, "y": 260})).unwrap(); - h.app.run("window.panel.dock", json!({"panel": "swatches", "anchor": "layers", "zone": "tab"})).unwrap(); - h.frames(3); - assert!(!floats(&h.app.ui, "swatches")); - assert_eq!(h.viewport("color"), viewport); - assert_eq!(h.host.borrow().windows[&viewport].rect.min, ORIGIN + vec2(480.0, 260.0)); -} - -#[test] -fn a_floated_panel_opens_its_own_os_window_where_the_backend_has_viewports() { - let mut h = Harness::new(); - let viewport = h.float("swatches"); - let rect = h.host.borrow().windows.get(&viewport).map(|w| w.rect).unwrap(); - assert!(rect.min.x >= ORIGIN.x && rect.min.y >= ORIGIN.y, "opens over its place in the app window: {rect:?}"); - // The window keeps its id, and with it its OS window. - let before = layout_of(&h.app.ui).floating[0].id; - h.frames(3); - assert_eq!(layout_of(&h.app.ui).floating[0].id, before); - assert_eq!(h.host.borrow().windows.len(), 1); - - // Embedded viewports (the web) draw it in the app window. - let ctx = egui::Context::default(); - let mut app = VectorcraftApp::new(Session::new(), Default::default()); - app.run("window.panel.float", json!({"panel": "swatches", "x": 200, "y": 200})).unwrap(); - for _ in 0..3 { - let input = egui::RawInput { screen_rect: Some(SCREEN), ..Default::default() }; - let mut full = ctx.run_ui(input, |ui| { - app.logic(ui.ctx()); - app.ui(ui); - }); - full.textures_delta.clear(); - } - let tab = craft_ui::docking::DockArea::::hit_rects(&ctx, area_id()) - .into_iter() - .any(|(hit, _)| hit == craft_ui::docking::Hit::Tab("swatches".into())); - assert!(tab, "its tab is drawn in the app window"); -} - -/// Sets the height of the OS window `viewport`, as a user's resize does. -fn resize_window(h: &mut Harness, viewport: ViewportId, height: f32) { - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().rect.set_height(height); - h.frames(3); -} - -/// A click on the `side` pt square button with the highest `rank` in the OS window `viewport`, and -/// the frames its menu takes to settle. Returns where the button is. -fn open_button_menu(h: &mut Harness, viewport: ViewportId, side: f32, rank: impl Fn(Pos2) -> f32) -> Pos2 { - let ctx = h.ctx.clone(); - let menu = ctx - .viewport_for(viewport, |vp| { - vp.prev_pass.widgets.layers().flat_map(|(_, w)| w.iter()).map(|w| (w.rect, w.sense.senses_click())).collect::>() - }) - .into_iter() - .filter(|(rect, clicks)| *clicks && rect.size() == vec2(side, side)) - .map(|(rect, _)| rect.center()) - .max_by(|a, b| rank(*a).total_cmp(&rank(*b))) - .expect("the menu's button"); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([Event::PointerMoved(menu), button(menu, true), button(menu, false)]); - h.frames(8); - menu -} - -/// Click a named button, independent of other controls sharing its size or position. -fn open_named_button_menu(h: &mut Harness, viewport: ViewportId, name: &str) -> Pos2 { - let buttons: Vec = h.host.borrow().windows[&viewport] - .accessibility - .iter() - .filter(|node| node.role() == egui::accesskit::Role::Button && node.label() == Some(name)) - .filter_map(|node| node.bounds()) - .map(|bounds| Rect::from_min_max(pos2(bounds.x0 as f32, bounds.y0 as f32), pos2(bounds.x1 as f32, bounds.y1 as f32))) - .collect(); - assert_eq!(buttons.len(), 1, "one visible button named {name}"); - let at = buttons[0].center(); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([Event::PointerMoved(at), button(at, true), button(at, false)]); - h.frames(8); - at -} - -/// A click on the ≡ button in the OS window `viewport`, the 16 pt square at the right end of the -/// panel's tab strip. Returns where the button is. -fn open_panel_menu(h: &mut Harness, viewport: ViewportId) -> Pos2 { - open_button_menu(h, viewport, 16.0, |at| at.x) -} - -/// The rect of the text `wanted` in the last frame of the OS window `viewport`: the last one drawn, -/// so a menu's item comes before a panel's text of the same name. -fn text_rect(h: &Harness, viewport: ViewportId, wanted: &str) -> Option { - h.host.borrow().windows.get(&viewport).unwrap().texts.iter().rfind(|(text, _)| text.trim() == wanted).map(|(_, rect)| *rect) -} - -/// Turns the wheel down by `by` points with the pointer at `over` in the OS window `viewport`, and -/// lets the scrolling finish. -fn wheel_down(h: &mut Harness, viewport: ViewportId, over: Pos2, by: f32) { - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(over), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Point, - delta: vec2(0.0, -by), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - ]); - h.frames(40); -} - -/// A floating panel's window of each height keeps its ≡ menu inside it: the list scrolls, and the -/// wheel brings the last item into the window. The list opens level with the button where the -/// window has room for it, and does not slide up to fit. 100 pt is the shortest a window can be -/// resized to, and there the list takes the whole window. -#[test] -fn a_long_panel_menu_scrolls_inside_a_short_native_window() { - for (height, level_with_the_button) in [(240.0, true), (100.0, false)] { - let mut h = Harness::new(); - let viewport = h.float("layers"); - resize_window(&mut h, viewport, height); - let menu = open_panel_menu(&mut h, viewport); - if level_with_the_button { - let first = text_rect(&h, viewport, "New Layer…").expect("the menu's first item"); - assert!(first.top() >= menu.y - 16.0, "the first item at {first:?} is level with the button at {menu:?}"); - } - let last_item = |h: &Harness| text_rect(h, viewport, "Dock Panel"); - assert!(last_item(&h).is_none_or(|r| r.bottom() > height), "the {height} pt window starts at the top of the list"); - wheel_down(&mut h, viewport, pos2(menu.x - 60.0, height - 30.0), 5000.0); - let item = last_item(&h).unwrap_or_else(|| panic!("the wheel brings the menu's last item into the {height} pt window")); - assert!(item.bottom() <= height, "the last item at {item:?} is inside the {height} pt window"); - } -} - -/// The Layers menu's Save Selection submenu lists every saved selection: in a short window the -/// wheel brings the last one into view. -#[test] -fn a_long_panel_submenu_scrolls_inside_a_short_native_window() { - let mut h = Harness::new(); - h.app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 50, "height": 40})).unwrap(); - h.app.run("select.all", json!({})).unwrap(); - // A document keeps at most 25 saved selections. - for n in 0..25 { - h.app.run("select.save", json!({"name": format!("Saved {n}")})).unwrap(); - } - assert_submenu_reaches_its_end(&mut h, "layers", 240.0, "Save Selection", "Saved 0", "Saved 24"); -} - -/// The Swatches menu's Open Swatch Library lists the libraries between its head and tail items. -#[test] -fn the_library_submenu_scrolls_inside_a_short_native_window() { - let mut h = Harness::new(); - assert_submenu_reaches_its_end(&mut h, "swatches", 240.0, "Open Swatch Library", "Default Swatches", "Other Library…"); -} - -/// The Layers panel's named Filter button opens its popup: the wheel brings its last row -/// into a short window. -#[test] -fn the_layers_filter_popup_scrolls_inside_a_short_native_window() { - let mut h = Harness::new(); - h.ctx.enable_accesskit(); - let viewport = h.float("layers"); - resize_window(&mut h, viewport, 160.0); - // The header's collapse control is also 24 pt square and sits above Filter. - assert!(text_rect(&h, viewport, "»").is_some(), "keep the competing header control in this regression"); - open_named_button_menu(&mut h, viewport, "Filter"); - assert!(floating_window(&layout_of(&h.app.ui), "layers").is_some(), "Filter must not collapse the panel"); - let over = text_rect(&h, viewport, "By Object").expect("the popup's By Object heading"); - assert!(text_rect(&h, viewport, "Hidden").is_none_or(|end| end.bottom() > 160.0), "the popup opens at the top of its list"); - wheel_down(&mut h, viewport, over.center(), 5000.0); - let end = text_rect(&h, viewport, "Hidden").expect("the wheel brings the popup's last row into the window"); - assert!(end.bottom() <= 160.0, "the last row at {end:?} is inside the window"); -} - -/// The Color Guide's Limit to Library popup, on the leftmost button of the bottom bar: with the -/// window made short while the popup is open, the wheel moves the list up past its Document -/// Swatches row. -#[test] -fn the_color_guide_library_popup_scrolls_inside_a_short_native_window() { - let mut h = Harness::new(); - let viewport = h.float("colorGuide"); - resize_window(&mut h, viewport, 320.0); - open_button_menu(&mut h, viewport, 24.0, |at| at.y * 1000.0 - at.x); - resize_window(&mut h, viewport, 100.0); - let first = text_rect(&h, viewport, "Document Swatches").expect("the popup's Document Swatches row"); - wheel_down(&mut h, viewport, first.center(), 5000.0); - let moved = text_rect(&h, viewport, "Document Swatches").is_none_or(|row| row.top() < first.top()); - assert!(moved, "the wheel scrolls the list that starts at {first:?}"); -} - -/// In an OS window of `panel`, `height` tall, scrolls the ≡ menu until its row `row` shows, clicks -/// the row to open its submenu, turns the wheel over the submenu's item `first`, and checks that its -/// item `last` comes into the window. -fn assert_submenu_reaches_its_end(h: &mut Harness, panel: &str, height: f32, row: &str, first: &str, last: &str) { - let viewport = h.float(panel); - resize_window(h, viewport, height); - let menu = open_panel_menu(h, viewport); - let inside = |h: &Harness| text_rect(h, viewport, row).filter(|rect| rect.bottom() < height - 12.0); - for _ in 0..40 { - if inside(h).is_some() { - break; - } - wheel_down(h, viewport, pos2(menu.x - 60.0, height - 30.0), 40.0); - } - let at = inside(h).expect("the row that opens the submenu").center(); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([Event::PointerMoved(at), button(at, true), button(at, false)]); - h.frames(8); - let over = text_rect(h, viewport, first).expect("the submenu's first item"); - assert!(text_rect(h, viewport, last).is_none_or(|end| end.bottom() > height), "the submenu opens at the top of its list"); - for _ in 0..3 { - if text_rect(h, viewport, last).is_some() { - break; - } - wheel_down(h, viewport, pos2(over.center().x, height - 30.0), 5000.0); - } - let end = text_rect(h, viewport, last).expect("the submenu's last item"); - assert!(end.bottom() <= height, "{last} at {end:?} is inside the {height} pt window"); -} - -/// A new launch on a desktop of `monitors`, with `saved` as the panel layout: where the window of -/// the floating Swatches panel opens. -fn reopened(saved: &craft_ui::docking::Layout, monitors: &[crate::DockMonitor]) -> Rect { - let mut h = Harness::new(); - h.host.borrow_mut().monitor_size = Some(vec2(1920.0, 1080.0)); - h.app.services.dock_monitors = monitors.to_vec(); - h.app.ui.docking = Some(saved.clone()); - h.frames(3); - let viewport = h.viewport("swatches"); - h.host.borrow().windows[&viewport].rect -} - -/// Two monitors side by side, with the main window on the left one (#5582). -#[test] -fn a_floating_panel_reopens_on_the_monitor_where_it_was_left() { - let left = crate::DockMonitor { name: Some("left".into()), rect: [0.0, 0.0, 1920.0, 1080.0], scale: 1.0, work_area: None }; - let right = crate::DockMonitor { name: Some("right".into()), rect: [1920.0, 0.0, 1920.0, 1080.0], scale: 1.0, work_area: None }; - let both = [left.clone(), right.clone()]; - - let mut h = Harness::new(); - h.host.borrow_mut().monitor_size = Some(vec2(1920.0, 1080.0)); - h.app.services.dock_monitors = both.to_vec(); - let viewport = h.float("swatches"); - let size = h.host.borrow().windows[&viewport].rect.size(); - let placed_at = pos2(2400.0, 300.0); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().rect = Rect::from_min_size(placed_at, size); - // The layout follows the window after craft-ui's wait for a move to settle, half a second. - h.frames(30); - - let layout = layout_of(&h.app.ui); - let window = &layout.floating[0]; - assert_eq!(window.native.map(|[x, y, ..]| pos2(x, y)), Some(placed_at)); - assert_eq!(window.monitor.as_ref(), Some(&right), "the window stores the monitor it is on"); - // The preferences keep the layout as JSON. - let saved: craft_ui::docking::Layout = serde_json::from_str(&serde_json::to_string(&layout).unwrap()).unwrap(); - - assert_eq!(reopened(&saved, &both).min, placed_at, "the monitor is connected: the saved place"); - - // The monitor got a new name and the same place. - let renamed = [left.clone(), crate::DockMonitor { name: Some("DP-2".into()), ..right.clone() }]; - assert_eq!(reopened(&saved, &renamed).min, placed_at); - - // A layout saved before the monitors were listed stores none. - let mut unstored = saved.clone(); - unstored.floating[0].monitor = None; - assert_eq!(reopened(&unstored, &both).min, placed_at, "a place on a connected monitor is kept"); - - // The monitor is gone: the main window's monitor, at the same offset from its origin. - assert_eq!(reopened(&saved, std::slice::from_ref(&left)).min, pos2(placed_at.x - 1920.0, placed_at.y)); - - // No monitor list (a host that can't list them): onto the main window's monitor by egui's size. - let no_list = reopened(&saved, &[]); - assert_eq!(no_list.min, pos2(1920.0 - size.x, placed_at.y)); -} - #[test] fn keys_typed_in_a_native_window_run_the_main_windows_shortcuts() { let mut h = Harness::new(); @@ -667,366 +279,16 @@ fn enter_and_escape_in_a_native_windows_field_stay_in_the_field() { } h.frames(2); h.key_in(viewport, key); - assert!(h.app.ui.dialog.is_none(), "{key:?} in the field opens no dialog"); + assert!(!h.app.dialogs().any_dialog_open(), "{key:?} in the field opens no dialog"); assert_eq!(h.app.session.active().unwrap().selection.objects.len(), 1, "{key:?} in the field keeps the selection"); } // Keys typed with no field editing still reach the main window. h.key_in(viewport, Key::Enter); - assert!(h.app.ui.dialog.is_some(), "Enter outside a field opens the Move dialog"); -} - -#[test] -fn a_modal_blocks_native_windows_and_takes_the_keyboard_from_them() { - let mut h = Harness::new(); - let viewport = h.float("swatches"); - h.app.ui.about = true; - h.frames(6); - let focus_commands = - |h: &Harness| h.host.borrow().commands.iter().filter(|(id, c)| *id == ViewportId::ROOT && *c == ViewportCommand::Focus).count(); - assert_eq!(focus_commands(&h), 1, "the main window gets the keyboard"); - h.key_in(viewport, Key::P); - assert_eq!(h.app.session.tool_id(), "selection", "a key typed in the blocked window does nothing"); - assert_eq!(focus_commands(&h), 1, "once"); - - h.app.ui.about = false; - h.frames(3); - h.key_in(viewport, Key::P); - assert_eq!(h.app.session.tool_id(), "pen", "the window takes keys again once the modal closes"); -} - -/// The update prompt and the result window of a check block a floating panel's OS window as any -/// modal does. -#[test] -#[cfg(not(target_arch = "wasm32"))] -fn the_update_dialogs_block_native_windows_and_take_the_keyboard_from_them() { - type Open = fn(&mut Harness); - let opens: [(&str, Open); 2] = [ - ("the prompt", |h| crate::updates_ui::open_prompt_for_test(&mut h.app)), - ("the result window", |h| crate::updates_ui::open_result_window_for_test(&mut h.app, &h.ctx)), - ]; - for (name, open) in opens { - let mut h = Harness::new(); - let viewport = h.float("swatches"); - open(&mut h); - h.frames(6); - let focus_commands = h.host.borrow().commands.iter().filter(|(id, c)| *id == ViewportId::ROOT && *c == ViewportCommand::Focus).count(); - assert_eq!(focus_commands, 1, "{name}: the main window gets the keyboard"); - h.key_in(viewport, Key::P); - assert_eq!(h.app.session.tool_id(), "selection", "{name}: a key typed in the blocked window does nothing"); - - crate::updates_ui::close_dialogs_for_test(&mut h.app); - h.frames(3); - h.key_in(viewport, Key::P); - assert_eq!(h.app.session.tool_id(), "pen", "{name}: the window takes keys again once the dialog closes"); - } -} - -/// The level and visibility a palette window takes under a dialog or a menu, and otherwise: a -/// palette stays out of the way of anything drawn in the main window. -#[test] -fn a_palette_window_keeps_out_of_the_way_of_the_main_windows_dialog_and_menus() { - use egui::WindowLevel; - assert_eq!(palette_window_state(true, true, true), (WindowLevel::Normal, false), "hidden under a dialog, even in a menu"); - assert_eq!(palette_window_state(true, false, true), (WindowLevel::Normal, false), "hidden under a dialog"); - assert_eq!(palette_window_state(false, true, true), (WindowLevel::Normal, false), "hidden under a menu: it is drawn in the main window"); - assert_eq!(palette_window_state(false, false, true), (WindowLevel::AlwaysOnTop, true), "its palette level while the app is active"); - let inactive = palette_window_state(false, false, false); - assert!(inactive.1, "shown while the app is inactive"); - assert_ne!(inactive.0, WindowLevel::AlwaysOnTop, "and not above other applications' windows: {:?}", inactive.0); -} - -/// Focus moving between the app's own windows passes through a frame where none has it: the -/// palettes keep their level then, or the window system hands the keyboard to another palette in -/// the middle of a press on a palette's bar. They leave the top level once the app has had no -/// focus for a while. -#[test] -fn focus_moving_between_the_apps_windows_leaves_the_palettes_on_top() { - let mut h = Harness::new(); - let viewport = h.float("navigator"); - h.host.borrow_mut().focus = ViewportId::ROOT; - h.frames(2); - take_palette_commands(&h, viewport); - let nowhere = ViewportId::from_hash_of("no window of the app"); - h.host.borrow_mut().focus = nowhere; - h.frames(1); - h.host.borrow_mut().focus = viewport; - h.frames(2); - assert_eq!(take_palette_commands(&h, viewport), (vec![], vec![]), "a frame without focus leaves the palette's level"); - // The wait starts again each time focus leaves: a second frame without focus, longer after - // the first than the wait, leaves the level too. - h.frames(6); - h.host.borrow_mut().focus = nowhere; - h.frames(1); - h.host.borrow_mut().focus = ViewportId::ROOT; - h.frames(2); - assert_eq!(take_palette_commands(&h, viewport), (vec![], vec![]), "the wait is counted from when focus last left"); - h.host.borrow_mut().focus = nowhere; - h.frames(8); - let (_, levels) = take_palette_commands(&h, viewport); - assert_eq!(levels.last(), Some(&palette_window_state(false, false, false).0), "the app left: the palette leaves the top level"); - h.host.borrow_mut().focus = ViewportId::ROOT; - h.frames(2); - assert_eq!(take_palette_commands(&h, viewport).1, [egui::WindowLevel::AlwaysOnTop], "and takes it again when the app is back"); -} - -/// With a menu of the in-window menu bar open, the palettes take the normal level (still shown): -/// the menu is drawn in the main window, or it would open under them. -#[test] -fn a_menu_of_the_menu_bar_lowers_the_palette_windows() { - let mut h = Harness::new(); - let viewport = h.float("navigator"); - h.host.borrow_mut().commands.clear(); - let layout = layout_of(&h.app.ui); - h.ctx.data_mut(|d| d.insert_temp(egui::Id::unique("menu-bar-open"), true)); - assert!(crate::menus::menu_open(&h.ctx), "the menu bar's state says a menu is open"); - palette_window_levels(&h.ctx, &layout, false, crate::menus::menu_open(&h.ctx)); - h.frames(1); - let commands: Vec<(egui::ViewportId, ViewportCommand)> = h.host.borrow().commands.clone(); - let levels: Vec = commands - .iter() - .filter_map(|(id, command)| match command { - ViewportCommand::WindowLevel(level) if *id == viewport => Some(*level), - _ => None, - }) - .collect(); - assert!(levels.contains(&egui::WindowLevel::Normal), "the palette drops its topmost level under the menu: {levels:?}"); - assert!( - commands.iter().any(|(id, command)| *id == viewport && matches!(command, ViewportCommand::Visible(false))), - "and is hidden until the menu closes" - ); -} - -/// The visibility and level commands the palette window `viewport` was sent, cleared after reading. -fn take_palette_commands(h: &Harness, viewport: ViewportId) -> (Vec, Vec) { - let commands = std::mem::take(&mut h.host.borrow_mut().commands); - let shown = commands - .iter() - .filter_map(|(id, command)| match command { - ViewportCommand::Visible(visible) if *id == viewport => Some(*visible), - _ => None, - }) - .collect(); - let levels = commands - .iter() - .filter_map(|(id, command)| match command { - ViewportCommand::WindowLevel(level) if *id == viewport => Some(*level), - _ => None, - }) - .collect(); - (shown, levels) -} - -/// Opens the flyout of the group `tools` on the Tools panel. -fn open_tool_flyout(h: &mut Harness, tools: &[&str]) { - h.app.ui.flyout = Some(0); - let tools: Vec = tools.iter().map(|tool| tool.to_string()).collect(); - h.ctx.data_mut(|d| { - d.insert_temp(egui::Id::unique("flyout-tools"), tools); - d.insert_temp(crate::toolbar::flyout_toolbar_id(), None::); - }); -} - -/// A tool group's flyout is drawn in the main window, so a palette window over it hides until it -/// closes (#5813), and takes its palette level again with the keyboard left where it was. -#[test] -fn a_tool_flyout_hides_the_palette_windows_until_it_closes() { - let mut h = Harness::new(); - let viewport = h.float("navigator"); - h.host.borrow_mut().focus = ViewportId::ROOT; - h.frames(2); - take_palette_commands(&h, viewport); - open_tool_flyout(&mut h, &["pen", "curvature"]); - h.frames(2); - let (shown, levels) = take_palette_commands(&h, viewport); - assert_eq!(shown, [false], "hidden while the flyout is open"); - assert_eq!(levels, [egui::WindowLevel::Normal], "and out of the top level"); - assert_eq!(h.app.ui.flyout, Some(0), "the flyout stays open"); - h.frames(2); - assert_eq!(take_palette_commands(&h, viewport), (vec![], vec![]), "the commands go once"); - h.app.ui.flyout = None; - h.frames(2); - let focus_commands = h.host.borrow().commands.iter().filter(|(_, command)| matches!(command, ViewportCommand::Focus)).count(); - assert_eq!(focus_commands, 0, "nothing takes the keyboard"); - let (shown, levels) = take_palette_commands(&h, viewport); - assert_eq!(shown, [true], "shown again when the flyout closes"); - assert_eq!(levels, [egui::WindowLevel::AlwaysOnTop], "at its palette level"); -} - -/// The All Tools drawer is drawn in the main window too, so it hides a palette window the same way. -#[test] -fn the_all_tools_drawer_hides_the_palette_windows_until_it_closes() { - let mut h = Harness::new(); - let viewport = h.float("navigator"); - h.host.borrow_mut().focus = ViewportId::ROOT; - h.frames(2); - take_palette_commands(&h, viewport); - h.app.run("window.editToolbar", json!({"open": true})).unwrap(); - h.frames(2); - assert_eq!(take_palette_commands(&h, viewport).0, [false], "hidden while the drawer is open"); - h.app.run("window.editToolbar", json!({"open": false})).unwrap(); - h.frames(2); - assert_eq!(take_palette_commands(&h, viewport).0, [true], "shown again when the drawer closes"); -} - -/// A flyout or a drawer left set while its toolbar is hidden does not draw, so the palette windows -/// stay on screen. -#[test] -fn a_flyout_or_drawer_of_a_hidden_toolbar_leaves_the_palette_windows_shown() { - let mut h = Harness::new(); - let viewport = h.float("navigator"); - h.host.borrow_mut().focus = ViewportId::ROOT; - h.frames(2); - take_palette_commands(&h, viewport); - h.app.run("window.editToolbar", json!({"open": true})).unwrap(); - h.frames(2); - assert_eq!(take_palette_commands(&h, viewport).0, [false], "hidden under the drawer"); - h.app.run("window.toolbar", json!({})).unwrap(); - h.frames(1); - assert!(!h.app.ui.toolbar && h.app.ui.tools_drawer, "the drawer is left set with the Tools panel hidden"); - assert_eq!(take_palette_commands(&h, viewport).0, [true], "shown again: the drawer does not draw"); - h.app.ui.tools_drawer = false; - h.app.run("window.toolbar", json!({})).unwrap(); - open_tool_flyout(&mut h, &["pen", "curvature"]); - h.frames(2); - assert_eq!(take_palette_commands(&h, viewport).0, [false], "hidden under the flyout"); - // Tab hides the toolbar and the panels, and Shift+Tab shows the panels alone. - h.app.run("window.dock", json!({})).unwrap(); - h.app.run("window.panels", json!({})).unwrap(); - h.frames(1); - assert!(!h.app.ui.toolbar && h.app.ui.flyout.is_some(), "the flyout is left set with the Tools panel hidden"); - assert_eq!(take_palette_commands(&h, viewport).0, [true], "shown again: the flyout does not draw"); -} - -/// A strip torn off a flyout and a toolbar made with New Toolbar stay on screen like panels, so the -/// palette windows stay shown beside them. -#[test] -fn a_torn_off_strip_or_a_new_toolbar_leaves_the_palette_windows_shown() { - let mut h = Harness::new(); - let viewport = h.float("navigator"); - h.host.borrow_mut().focus = ViewportId::ROOT; - h.frames(2); - take_palette_commands(&h, viewport); - // The commands go only on a change, so the drawer opening and closing shows the palette windows - // were on screen before the strip and the toolbar. - h.app.run("window.editToolbar", json!({"open": true})).unwrap(); - h.frames(2); - assert_eq!(take_palette_commands(&h, viewport).0, [false], "hidden under the drawer"); - h.app.run("window.editToolbar", json!({"open": false})).unwrap(); - h.frames(2); - assert_eq!(take_palette_commands(&h, viewport).0, [true], "shown when the drawer closes"); - h.app.run("window.floatTools", json!({"tool": "pen"})).unwrap(); - h.app.run("window.toolbar.new", json!({"name": "Mine"})).unwrap(); - h.frames(2); - let strips: Vec<&str> = h.app.ui.floating_flyouts.iter().filter_map(|strip| strip.tools.first().map(String::as_str)).collect(); - assert_eq!(strips, ["pen"], "the Pen strip floats"); - let toolbars: Vec<(&str, bool)> = h.app.ui.custom_toolbars.iter().map(|toolbar| (toolbar.name.as_str(), toolbar.shown)).collect(); - assert_eq!(toolbars, [("Mine", true)], "the new toolbar shows"); - assert_eq!(h.app.ui.flyout, None, "no flyout is open"); - assert_eq!(take_palette_commands(&h, viewport), (vec![], vec![]), "the palette windows are left as they were"); - h.app.run("window.editToolbar", json!({"open": true})).unwrap(); - h.frames(2); - assert_eq!(take_palette_commands(&h, viewport).0, [false], "still shown: the drawer hides them again"); -} - -/// A panel floated while the palette windows are hidden opens hidden too, and shows with the others -/// once the drawer or the dialog over them closes. -#[test] -fn a_panel_floated_while_the_palette_windows_are_hidden_opens_hidden() { - let mut h = Harness::new(); - let navigator = h.float("navigator"); - h.host.borrow_mut().focus = ViewportId::ROOT; - h.frames(2); - take_palette_commands(&h, navigator); - h.app.run("window.editToolbar", json!({"open": true})).unwrap(); - h.frames(2); - assert_eq!(take_palette_commands(&h, navigator).0, [false], "hidden under the drawer"); - let layers = h.float("layers"); - h.host.borrow_mut().focus = ViewportId::ROOT; - assert!(h.app.ui.tools_drawer, "the drawer stays open"); - let (shown, levels) = take_palette_commands(&h, layers); - assert_eq!(shown, [false], "the new palette window is hidden under the drawer"); - assert_eq!(levels, [egui::WindowLevel::Normal], "and out of the top level"); - h.app.run("window.editToolbar", json!({"open": false})).unwrap(); - h.frames(2); - assert_eq!(take_palette_commands(&h, layers).0, [true], "shown when the drawer closes"); - - h.app.ui.palette_open = true; - h.frames(2); - assert_eq!(take_palette_commands(&h, layers).0, [false], "hidden under the command palette"); - let swatches = h.float("swatches"); - h.host.borrow_mut().focus = ViewportId::ROOT; - assert_eq!(take_palette_commands(&h, swatches).0, [false], "a panel floated under it is hidden too"); -} - -/// The frame in which a click elsewhere closes a flyout or the drawer shows the palette windows, as -/// no later frame need come while the app is idle. -#[test] -fn a_click_elsewhere_shows_the_palette_windows_in_the_frame_it_closes_a_flyout() { - let mut h = Harness::new(); - let viewport = h.float("navigator"); - h.host.borrow_mut().focus = ViewportId::ROOT; - h.frames(2); - take_palette_commands(&h, viewport); - // On the dock: a press on the canvas closes a flyout later in its frame, and the release's frame - // shows the palette windows. - let at = pos2(1500.0, 700.0); - let button = |pressed| Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }; - let click = |h: &mut Harness| { - h.frame(vec![Event::PointerMoved(at), button(true)]); - h.frame(vec![button(false)]); - }; - open_tool_flyout(&mut h, &["pen", "curvature"]); - h.frames(2); - assert_eq!(take_palette_commands(&h, viewport).0, [false], "hidden under the flyout"); - click(&mut h); - assert_eq!(h.app.ui.flyout, None, "the click closes the flyout"); - assert_eq!(take_palette_commands(&h, viewport).0, [true], "shown in that frame"); - h.app.run("window.editToolbar", json!({"open": true})).unwrap(); - h.frames(2); - assert_eq!(take_palette_commands(&h, viewport).0, [false], "hidden under the drawer"); - click(&mut h); - assert!(!h.app.ui.tools_drawer, "the click closes the drawer"); - assert_eq!(take_palette_commands(&h, viewport).0, [true], "shown in that frame"); -} - -/// A modal dialog is drawn in the main window, so the palettes' OS windows leave their topmost -/// level while it is open and take it back after, or the dialog would be covered by them. -#[test] -fn a_modal_lowers_the_native_palette_windows() { - let mut h = Harness::new(); - let viewport = h.float("swatches"); - let levels = |h: &Harness| -> Vec { - h.host - .borrow() - .commands - .iter() - .filter_map(|(id, command)| match command { - ViewportCommand::WindowLevel(level) if *id == viewport => Some(*level), - _ => None, - }) - .collect() - }; - let hidden = |h: &Harness| -> Vec { - h.host - .borrow() - .commands - .iter() - .filter_map(|(id, command)| match command { - ViewportCommand::Visible(visible) if *id == viewport => Some(*visible), - _ => None, - }) - .collect() - }; - h.app.ui.about = true; - h.frames(4); - assert!(levels(&h).contains(&egui::WindowLevel::Normal), "the palette leaves AlwaysOnTop under the modal: {:?}", levels(&h)); - assert_eq!(hidden(&h), [false], "and is hidden, so a click cannot bring it up over the dialog: {:?}", hidden(&h)); - h.app.ui.about = false; - h.frames(4); - assert!(levels(&h).contains(&egui::WindowLevel::AlwaysOnTop), "and takes its palette level back: {:?}", levels(&h)); - assert_eq!(hidden(&h), [false, true], "shown again with the dialog gone: {:?}", hidden(&h)); + assert!(h.app.dialogs().any_dialog_open(), "Enter outside a field opens the Move dialog"); } +/// A file dropped on a native panel window opens through the same route as one dropped on the app +/// window: the drop's place is unknown, so it is not placed on the canvas. #[test] fn a_file_dropped_on_a_native_window_opens() { let mut h = Harness::new(); @@ -1057,11 +319,12 @@ fn art_dragged_from_the_canvas_onto_a_native_panel_window_drops_there() { let styles = h.app.session.active().unwrap().doc.graphic_styles.len(); let art = |h: &Harness| format!("{:?}", h.app.session.active().unwrap().doc.node(id).map(|n| &n.kind)); let before = art(&h); - let xf = crate::canvas::Xf::new(h.app.canvas_rect.unwrap(), h.app.view().unwrap()); - let from = xf.to_screen(vectorcraft_geom::Point::new(35.0, 30.0)); + // PLAN: assumed `app.canvas_view()` is the `View` with `canvas: Rect` and `to_screen(Point)` (hub-w1 M12, cv.S4). + let view = h.app.canvas_view(); + let from = view.to_screen(vectorcraft_geom::Point::new(35.0, 30.0)); let window = h.host.borrow().windows.get(&viewport).map(|w| w.rect).unwrap(); let to = window.min + vec2(60.0, 120.0) - ORIGIN.to_vec2(); - assert!(h.app.canvas_rect.unwrap().contains(to), "the window lies over the canvas"); + assert!(view.canvas.contains(to), "the window lies over the canvas"); h.frame(vec![Event::PointerMoved(from), button(from, true)]); for k in 1..=8 { h.frame(vec![Event::PointerMoved(from + (to - from) * (k as f32 / 8.0))]); @@ -1086,9 +349,7 @@ fn delete_after_a_row_click_in_a_native_appearance_window_removes_the_row() { }; assert_eq!(items(&h), Some(2), "a fill and a stroke"); let viewport = h.float("appearance"); - for _ in 0..20 { - h.frame(vec![]); - } + h.frames(20); let fill = h.host.borrow().windows.get(&viewport).and_then(|w| w.texts.iter().find(|(t, _)| t == "Fill:").map(|(_, r)| *r)).expect("the Fill row"); // The row's empty space right of its swatch (its name is a link that opens the Color panel). @@ -1109,712 +370,3 @@ fn delete_after_a_row_click_in_a_native_appearance_window_removes_the_row() { assert_eq!(h.objects(), before, "the object stays"); assert_eq!(items(&h), Some(1), "the fill row goes"); } - -#[test] -fn ui_docking_leaves_out_the_controls_of_os_windows() { - let mut h = Harness::new(); - h.float("swatches"); - let report = inspect(&h.app, &h.ctx); - let hits = report["hitRects"].as_array().unwrap(); - assert!(hits.iter().any(|hit| hit["hit"] == json!({"Tab": "layers"})), "the main window's tabs are there"); - let swatches = |hit: &Value| hit["hit"].as_object().is_some_and(|o| o.values().any(|v| v == "swatches")); - assert!(!hits.iter().any(swatches), "none of the OS window's: {hits:?}"); - assert!(report["layout"]["floating"][0]["native"].is_array(), "its place is the window's native rect"); -} - -#[test] -fn wheel_over_a_floating_character_font_field_changes_the_font() { - let mut h = Harness::new(); - // The later turns step to whichever families the process has loaded, test fonts without Latin - // glyphs included. Missing glyph protection would keep the text in its previous font. - h.app.run("prefs.set", json!({"key": "missingGlyphProtection", "value": false})).unwrap(); - h.app.run("text.create", json!({"x": 30, "y": 50, "text": "Hi", "font": "Source Sans 3"})).unwrap(); - let viewport = h.float("character"); - let field = h - .host - .borrow() - .windows - .get(&viewport) - .and_then(|w| w.texts.iter().find(|(text, _)| text == "Source Sans 3").map(|(_, rect)| *rect)) - .expect("the Character panel's font family field"); - let pointer = pos2(field.min.x + 4.0, field.center().y); - h.root_overlay = Some(field.expand(10.0)); - h.frames(2); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(pointer), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - ]); - h.frame(vec![]); - h.frames(2); - let fonts = h.app.run("text.fonts", json!({})).unwrap(); - assert_ne!(fonts[0]["family"], "Source Sans 3", "{fonts}"); - - h.host.borrow_mut().child_overlay = Some((viewport, field.expand(10.0))); - h.frames(2); - let covered = h.app.run("text.fonts", json!({})).unwrap(); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(pointer), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - ]); - h.frame(vec![]); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap(), covered); - - h.host.borrow_mut().child_overlay = None; - h.frame(vec![]); - h.host.borrow_mut().time += crate::widgets::WHEEL_STREAM_GAP; - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(pointer + vec2(1.0, 0.0)), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - ]); - h.frame(vec![]); - h.frame(vec![]); - assert_ne!(h.app.run("text.fonts", json!({})).unwrap(), covered); - - h.host.borrow_mut().child_transparent_overlay = Some((viewport, field.expand(10.0))); - h.frames(2); - let covered = h.app.run("text.fonts", json!({})).unwrap(); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(pointer), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - ]); - h.frame(vec![]); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap(), covered); - - h.host.borrow_mut().child_transparent_overlay = None; - h.host.borrow_mut().child_pass_through_overlay = Some((viewport, field.expand(10.0))); - h.frames(2); - h.host.borrow_mut().time += crate::widgets::WHEEL_STREAM_GAP; - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(pointer + vec2(2.0, 0.0)), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - ]); - h.frame(vec![]); - h.frame(vec![]); - assert_ne!(h.app.run("text.fonts", json!({})).unwrap(), covered); -} - -#[test] -fn a_modal_blocks_font_wheel_input_in_a_floating_panel() { - let mut h = Harness::new(); - h.app.run("text.create", json!({"x": 30, "y": 50, "text": "Hi", "font": "Source Sans 3"})).unwrap(); - let viewport = h.float("character"); - let field = h - .host - .borrow() - .windows - .get(&viewport) - .and_then(|w| w.texts.iter().find(|(text, _)| text == "Source Sans 3").map(|(_, rect)| *rect)) - .expect("the Character panel's font family field"); - let pointer = pos2(field.min.x + 4.0, field.center().y); - let before = h.app.run("text.fonts", json!({})).unwrap(); - h.app.ui.about = true; - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(pointer), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - ]); - h.frame(vec![]); - h.frames(2); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap(), before); - h.app.ui.about = false; - h.frames(2); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap(), before); -} - -#[test] -fn wheel_stays_under_an_overlay_that_stops_painting() { - let mut h = Harness::new(); - h.app.run("text.create", json!({"x": 30, "y": 50, "text": "Hi", "font": "Source Sans 3"})).unwrap(); - let viewport = h.float("character"); - let field = h - .host - .borrow() - .windows - .get(&viewport) - .and_then(|w| w.texts.iter().find(|(text, _)| text == "Source Sans 3").map(|(_, rect)| *rect)) - .expect("the Character panel's font family field"); - let pointer = pos2(field.min.x + 4.0, field.center().y); - h.host.borrow_mut().child_transition_overlay = Some((viewport, field.expand(10.0), true)); - h.frames(2); - let covered = h.app.run("text.fonts", json!({})).unwrap(); - h.host.borrow_mut().child_transition_overlay = Some((viewport, field.expand(10.0), false)); - h.frame(vec![]); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(pointer), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - ]); - h.frame(vec![]); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap(), covered); -} - -#[test] -fn wheel_stays_under_an_empty_interactive_overlay() { - let mut h = Harness::new(); - h.app.run("text.create", json!({"x": 30, "y": 50, "text": "Hi", "font": "Source Sans 3"})).unwrap(); - let viewport = h.float("character"); - let field = h - .host - .borrow() - .windows - .get(&viewport) - .and_then(|w| w.texts.iter().find(|(text, _)| text == "Source Sans 3").map(|(_, rect)| *rect)) - .expect("the Character panel's font family field"); - let pointer = pos2(field.min.x + 4.0, field.center().y); - h.host.borrow_mut().child_empty_overlay = Some((viewport, field.expand(10.0))); - h.frames(2); - let covered = h.app.run("text.fonts", json!({})).unwrap(); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(pointer), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - ]); - h.frame(vec![]); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap(), covered); -} - -#[test] -fn a_raised_overlay_blocks_the_next_font_wheel_event() { - let mut h = Harness::new(); - h.app.run("text.create", json!({"x": 30, "y": 50, "text": "Hi", "font": "Source Sans 3"})).unwrap(); - let viewport = h.float("character"); - let field = h - .host - .borrow() - .windows - .get(&viewport) - .and_then(|w| w.texts.iter().find(|(text, _)| text == "Source Sans 3").map(|(_, rect)| *rect)) - .expect("the Character panel's font family field"); - let pointer = pos2(field.min.x + 4.0, field.center().y); - h.host.borrow_mut().child_raise_overlay = Some((viewport, field.expand(10.0), false)); - h.frames(2); - let field_layer = crate::font_menu::wheel_field_layer_for_test(&h.ctx, viewport, pointer).expect("the font field layer"); - h.host.borrow_mut().child_raise_layer = Some((viewport, field_layer)); - h.frame(vec![]); - h.host.borrow_mut().child_raise_layer = None; - let overlay_layer = egui::LayerId::new(egui::Order::Background, egui::Id::unique("Child raised overlay")); - let before = h.app.run("text.fonts", json!({})).unwrap(); - h.host.borrow_mut().child_raise_overlay = Some((viewport, field.expand(10.0), true)); - h.frame(vec![]); - let order = h.host.borrow().child_layer_order.get(&viewport).cloned().expect("child layer order"); - let field_index = order.iter().position(|layer| *layer == field_layer).expect("font field in layer order"); - let overlay_index = order.iter().position(|layer| *layer == overlay_layer).unwrap_or_else(|| panic!("overlay absent from {order:?}")); - assert!(field_index > overlay_index, "the font field must start above the overlay: {order:?}"); - h.host.borrow_mut().child_raise_overlay = Some((viewport, field.expand(10.0), false)); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(pointer), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - Event::PointerGone, - ]); - h.frame(vec![]); - let order = h.host.borrow().child_layer_order.get(&viewport).cloned().expect("child layer order"); - let field_index = order.iter().position(|layer| *layer == field_layer).expect("font field in layer order"); - let overlay_index = order.iter().position(|layer| *layer == overlay_layer).unwrap_or_else(|| panic!("overlay absent from {order:?}")); - assert!(overlay_index > field_index, "the overlay must finish above the font field: {order:?}"); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap(), before); - h.frame(vec![]); - h.frame(vec![]); - assert!( - h.ctx.input_for(viewport, |input| input.raw.events.iter().any(|event| matches!(event, Event::MouseWheel { .. }))), - "the raised overlay must receive the wheel event" - ); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap(), before); - assert_eq!(h.ctx.input_for(viewport, |input| input.pointer.latest_pos()), None); -} - -#[test] -fn an_overlay_raised_with_the_wheel_event_does_not_change_the_font() { - let mut h = Harness::new(); - h.app.run("text.create", json!({"x": 30, "y": 50, "text": "Hi", "font": "Source Sans 3"})).unwrap(); - let viewport = h.float("character"); - let field = h - .host - .borrow() - .windows - .get(&viewport) - .and_then(|w| w.texts.iter().find(|(text, _)| text == "Source Sans 3").map(|(_, rect)| *rect)) - .expect("the Character panel's font family field"); - let pointer = pos2(field.min.x + 4.0, field.center().y); - h.host.borrow_mut().child_raise_overlay = Some((viewport, field.expand(10.0), false)); - h.frames(2); - let field_layer = crate::font_menu::wheel_field_layer_for_test(&h.ctx, viewport, pointer).expect("the font field layer"); - h.host.borrow_mut().child_raise_layer = Some((viewport, field_layer)); - h.frame(vec![]); - h.host.borrow_mut().child_raise_layer = None; - h.frame(vec![]); - let overlay_layer = egui::LayerId::new(egui::Order::Background, egui::Id::unique("Child raised overlay")); - let order = h.host.borrow().child_layer_order.get(&viewport).cloned().expect("child layer order"); - let field_index = order.iter().position(|layer| *layer == field_layer).expect("font field in layer order"); - let overlay_index = order.iter().position(|layer| *layer == overlay_layer).expect("overlay in layer order"); - assert!(field_index > overlay_index, "the font field must be above the overlay before the wheel event: {order:?}"); - let before = h.app.run("text.fonts", json!({})).unwrap(); - h.host.borrow_mut().child_raise_overlay = Some((viewport, field.expand(10.0), true)); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(pointer), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - ]); - h.frame(vec![]); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap(), before); - h.frame(vec![]); - h.frame(vec![]); - assert!( - h.ctx.input_for(viewport, |input| input.raw.events.iter().any(|event| matches!(event, Event::MouseWheel { .. }))), - "the raised overlay must receive the wheel event" - ); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap(), before); -} - -#[test] -fn a_closed_overlay_does_not_replay_its_wheel_into_the_panel() { - let mut h = Harness::new(); - h.app.run("text.create", json!({"x": 30, "y": 50, "text": "Hi", "font": "Source Sans 3"})).unwrap(); - let viewport = h.float("character"); - let field = h - .host - .borrow() - .windows - .get(&viewport) - .and_then(|w| w.texts.iter().find(|(text, _)| text == "Source Sans 3").map(|(_, rect)| *rect)) - .expect("the Character panel's font family field"); - let pointer = pos2(field.min.x + 4.0, field.center().y); - h.host.borrow_mut().child_raise_overlay = Some((viewport, field.expand(10.0), false)); - h.frames(2); - let field_layer = crate::font_menu::wheel_field_layer_for_test(&h.ctx, viewport, pointer).expect("the font field layer"); - h.host.borrow_mut().child_raise_layer = Some((viewport, field_layer)); - h.frame(vec![]); - h.host.borrow_mut().child_raise_layer = None; - h.frame(vec![]); - let before = h.app.run("text.fonts", json!({})).unwrap(); - h.host.borrow_mut().child_raise_overlay = Some((viewport, field.expand(10.0), true)); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(pointer), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - ]); - h.frame(vec![]); - h.host.borrow_mut().child_raise_overlay = None; - h.frame(vec![]); - h.frame(vec![]); - assert!( - !h.ctx.input_for(viewport, |input| input.raw.events.iter().any(|event| matches!(event, Event::MouseWheel { .. }))), - "a closed overlay cannot receive a later wheel event" - ); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap(), before); -} - -#[test] -fn a_wheel_before_opening_a_floating_font_menu_changes_the_font() { - let mut h = Harness::new(); - h.app.run("text.create", json!({"x": 30, "y": 50, "text": "Hi", "font": "Source Sans 3"})).unwrap(); - let viewport = h.float("character"); - let field = h - .host - .borrow() - .windows - .get(&viewport) - .and_then(|w| w.texts.iter().find(|(text, _)| text == "Source Sans 3").map(|(_, rect)| *rect)) - .expect("the Character panel's font family field"); - let pointer = pos2(field.min.x + 4.0, field.center().y); - let before = h.app.run("text.fonts", json!({})).unwrap(); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(pointer), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - Event::PointerButton { pos: pointer, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() }, - Event::PointerButton { pos: pointer, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() }, - ]); - h.frames(3); - assert_ne!(h.app.run("text.fonts", json!({})).unwrap(), before); -} - -#[test] -fn a_delayed_wheel_does_not_change_a_font_field_that_moved() { - let mut h = Harness::new(); - h.app.run("text.create", json!({"x": 30, "y": 50, "text": "Hi", "font": "Source Sans 3"})).unwrap(); - let viewport = h.float("character"); - let field = h - .host - .borrow() - .windows - .get(&viewport) - .and_then(|w| w.texts.iter().find(|(text, _)| text == "Source Sans 3").map(|(_, rect)| *rect)) - .expect("the Character panel's font family field"); - let pointer = pos2(field.min.x + 4.0, field.center().y); - let before = h.app.run("text.fonts", json!({})).unwrap(); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(pointer), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - ]); - h.frame(vec![]); - crate::font_menu::offset_wheel_fields_for_test(&h.ctx, viewport, vec2(300.0, 0.0)); - h.frame(vec![]); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap(), before); -} - -#[test] -fn wheel_events_on_both_sides_of_an_overlay_are_checked_separately() { - let mut h = Harness::new(); - h.app.run("text.create", json!({"x": 30, "y": 50, "text": "Hi", "font": "Source Sans 3"})).unwrap(); - let viewport = h.float("character"); - let field = h - .host - .borrow() - .windows - .get(&viewport) - .and_then(|w| w.texts.iter().find(|(text, _)| text == "Source Sans 3").map(|(_, rect)| *rect)) - .expect("the Character panel's font family field"); - let covered = pos2(field.min.x + 4.0, field.center().y); - let clear = pos2(field.max.x - 4.0, field.center().y); - let overlay = Rect::from_min_max(field.min - vec2(2.0, 2.0), pos2(field.center().x, field.max.y + 2.0)); - assert!(covered.x < overlay.max.x && clear.x > overlay.max.x); - h.host.borrow_mut().child_raise_overlay = Some((viewport, overlay, false)); - h.frames(2); - let field_layer = crate::font_menu::wheel_field_layer_for_test(&h.ctx, viewport, clear).expect("the font field layer"); - h.host.borrow_mut().child_raise_layer = Some((viewport, field_layer)); - h.frame(vec![]); - h.host.borrow_mut().child_raise_layer = None; - h.frame(vec![]); - let before = h.app.run("text.fonts", json!({})).unwrap(); - h.host.borrow_mut().child_raise_overlay = Some((viewport, overlay, true)); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(covered), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - Event::PointerMoved(clear), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - ]); - h.frame(vec![]); - h.frame(vec![]); - h.frame(vec![]); - assert_ne!(h.app.run("text.fonts", json!({})).unwrap(), before); - assert_eq!(h.ctx.input_for(viewport, |input| input.pointer.latest_pos()), Some(clear)); -} - -#[test] -fn a_wheel_stream_that_began_outside_a_floating_font_field_stays_outside() { - let mut h = Harness::new(); - h.app.run("text.create", json!({"x": 30, "y": 50, "text": "Hi", "font": "Source Sans 3"})).unwrap(); - let viewport = h.float("character"); - let field = h - .host - .borrow() - .windows - .get(&viewport) - .and_then(|w| w.texts.iter().find(|(text, _)| text == "Source Sans 3").map(|(_, rect)| *rect)) - .expect("the Character panel's font family field"); - let on = pos2(field.min.x + 4.0, field.center().y); - let off = pos2(field.min.x + 4.0, field.max.y + 60.0); - let turn = Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }; - let before = h.app.run("text.fonts", json!({})).unwrap(); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([Event::PointerMoved(off), turn.clone(), Event::PointerMoved(on), turn]); - h.frames(3); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap(), before); -} - -#[test] -fn a_wheel_stream_leaves_a_floating_font_field_that_an_overlay_covered_before_its_queued_turn() { - let mut h = Harness::new(); - h.app.run("text.create", json!({"x": 30, "y": 50, "text": "Hi", "font": "Source Sans 3"})).unwrap(); - let viewport = h.float("character"); - let field = h - .host - .borrow() - .windows - .get(&viewport) - .and_then(|w| w.texts.iter().find(|(text, _)| text == "Source Sans 3").map(|(_, rect)| *rect)) - .expect("the Character panel's font family field"); - let pointer = pos2(field.min.x + 4.0, field.center().y); - let turn = Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }; - h.host.borrow_mut().child_raise_overlay = Some((viewport, field.expand(10.0), false)); - h.frames(2); - let field_layer = crate::font_menu::wheel_field_layer_for_test(&h.ctx, viewport, pointer).expect("the font field layer"); - let raise_field = |h: &mut Harness| { - h.host.borrow_mut().child_raise_layer = Some((viewport, field_layer)); - h.frame(vec![]); - h.host.borrow_mut().child_raise_layer = None; - h.frame(vec![]); - }; - raise_field(&mut h); - let before = h.app.run("text.fonts", json!({})).unwrap(); - h.host.borrow_mut().child_raise_overlay = Some((viewport, field.expand(10.0), true)); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([Event::PointerMoved(pointer), turn.clone()]); - h.frames(2); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap(), before, "the overlay covered the field before its queued turn"); - h.host.borrow_mut().child_raise_overlay = Some((viewport, field.expand(10.0), false)); - raise_field(&mut h); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.push(turn); - h.frames(2); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap(), before, "the stream left the field when the overlay covered it"); -} - -#[test] -fn leaving_a_floating_font_field_resets_queued_fractional_wheel_input() { - let mut h = Harness::new(); - h.app.run("text.create", json!({"x": 30, "y": 50, "text": "Hi", "font": "Source Sans 3"})).unwrap(); - let viewport = h.float("character"); - let field = h - .host - .borrow() - .windows - .get(&viewport) - .and_then(|w| w.texts.iter().find(|(text, _)| text == "Source Sans 3").map(|(_, rect)| *rect)) - .expect("the Character panel's font family field"); - let inside = pos2(field.min.x + 4.0, field.center().y); - let outside = pos2(field.max.x + 500.0, field.center().y); - let before = h.app.run("text.fonts", json!({})).unwrap(); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(inside), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -0.6), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - Event::PointerMoved(outside), - ]); - h.frame(vec![]); - h.frame(vec![]); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(inside), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -0.6), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - ]); - h.frame(vec![]); - h.frame(vec![]); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap(), before); -} - -#[test] -fn a_floating_wheel_turn_precedes_a_pointer_leave_in_the_menu_order() { - let mut h = Harness::new(); - h.app.run("text.create", json!({"x": 30, "y": 50, "text": "Hi", "font": "Source Sans 3"})).unwrap(); - h.app.ui.recent_fonts = ["Source Sans 3", "Source Serif 4", "Inter"].map(str::to_string).into(); - let viewport = h.float("character"); - let field = h - .host - .borrow() - .windows - .get(&viewport) - .and_then(|w| w.texts.iter().find(|(text, _)| text == "Source Sans 3").map(|(_, rect)| *rect)) - .expect("the Character panel's font family field"); - let inside = pos2(field.min.x + 4.0, field.center().y); - let outside = pos2(field.max.x + 500.0, field.center().y); - let wheel = Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }; - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([Event::PointerMoved(inside), wheel.clone()]); - h.frame(vec![]); - h.frames(2); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap()[0]["family"], "Source Serif 4"); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([wheel, Event::PointerMoved(outside)]); - h.frame(vec![]); - h.frames(2); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap()[0]["family"], "Inter"); -} - -#[test] -fn moving_under_an_overlay_resets_queued_fractional_font_wheel_input() { - let mut h = Harness::new(); - h.app.run("text.create", json!({"x": 30, "y": 50, "text": "Hi", "font": "Source Sans 3"})).unwrap(); - let viewport = h.float("character"); - let field = h - .host - .borrow() - .windows - .get(&viewport) - .and_then(|w| w.texts.iter().find(|(text, _)| text == "Source Sans 3").map(|(_, rect)| *rect)) - .expect("the Character panel's font family field"); - let covered = pos2(field.min.x + 4.0, field.center().y); - let clear = pos2(field.max.x - 4.0, field.center().y); - let overlay = Rect::from_min_max(field.min - vec2(2.0, 2.0), pos2(field.center().x, field.max.y + 2.0)); - h.host.borrow_mut().child_raise_overlay = Some((viewport, overlay, true)); - h.frames(2); - let before = h.app.run("text.fonts", json!({})).unwrap(); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([ - Event::PointerMoved(clear), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -0.6), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - Event::PointerMoved(covered), - Event::PointerMoved(clear), - Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -0.6), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }, - ]); - h.frame(vec![]); - h.frame(vec![]); - h.frame(vec![]); - assert_eq!(h.app.run("text.fonts", json!({})).unwrap(), before); -} - -#[test] -fn wheel_without_a_move_uses_the_floating_viewports_pointer() { - let mut h = Harness::new(); - h.app.run("text.create", json!({"x": 30, "y": 50, "text": "Hi", "font": "Source Sans 3"})).unwrap(); - let viewport = h.float("character"); - let field = h - .host - .borrow() - .windows - .get(&viewport) - .and_then(|w| w.texts.iter().find(|(text, _)| text == "Source Sans 3").map(|(_, rect)| *rect)) - .expect("the Character panel's font family field"); - let pointer = pos2(field.min.x + 4.0, field.center().y); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.push(Event::PointerMoved(pointer)); - h.frame(vec![Event::PointerMoved(pos2(1200.0, 800.0))]); - h.app.run("text.create", json!({"x": 30, "y": 90, "text": "New", "font": "Source Sans 3"})).unwrap(); - h.frames(2); - let before = h.app.run("text.fonts", json!({})).unwrap(); - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.push(Event::MouseWheel { - unit: egui::MouseWheelUnit::Line, - delta: vec2(0.0, -1.0), - phase: egui::TouchPhase::Move, - modifiers: Default::default(), - source: egui::MouseWheelSource::Unknown, - }); - h.frame(vec![]); - h.frame(vec![]); - assert_ne!(h.app.run("text.fonts", json!({})).unwrap(), before); -} - -#[test] -fn floating_collapse_arrow_in_a_native_window_moves_the_group_to_the_rail() { - let mut h = Harness::new(); - h.app.run("window.panel.float", json!({"panel": "layers", "group": true})).unwrap(); - h.app.run("window.panel.activate", json!({"panel": "layers"})).unwrap(); - h.frames(3); - let viewport = h.viewport("layers"); - h.host.borrow_mut().focus = viewport; - let arrow = h.host.borrow().windows[&viewport] - .texts - .iter() - .find(|(text, _)| text == "»") - .map(|(_, rect)| rect.center()) - .expect("the native floating header paints a collapse arrow"); - for pressed in [true, false] { - h.host.borrow_mut().windows.get_mut(&viewport).unwrap().events.extend([Event::PointerMoved(arrow), button(arrow, pressed)]); - h.frames(1); - } - h.frames(3); - let layout = layout_of(&h.app.ui); - assert!(floating_window(&layout, "layers").is_none()); - assert_eq!(group_of(&layout, &"layers".into()), ["properties", "layers", "libraries"]); - assert!(["properties", "layers", "libraries"].iter().all(|p| in_iconic_column(&layout, p))); - let frames = h.host.borrow().windows[&viewport].frames; - h.frames(3); - assert_eq!(h.host.borrow().windows[&viewport].frames, frames, "the emptied palette is no longer rendered"); -} diff --git a/crates/ui-egui/src/panel_docking/tests.rs b/crates/ui-egui/src/panel_docking/tests.rs index 2c90c7cd54..897c396112 100644 --- a/crates/ui-egui/src/panel_docking/tests.rs +++ b/crates/ui-egui/src/panel_docking/tests.rs @@ -1,9 +1,11 @@ //! The dock area as the user sees it: whole-window frames driven by pointer and key events. -use craft_ui::docking::Hit; -use egui::{Event, PointerButton, Pos2, Rect, pos2, vec2}; +use egui::{Event, PointerButton, Pos2, Rect, ViewportId, pos2, vec2}; use vectorcraft_engine::Session; +// PLAN: assumed path of the chrome toggle request (ch.K3, ch.K4); the framework's `runtime::frame` module. +use craft_ui::runtime::frame::{ChromeToggle, request_chrome_toggle}; + use super::*; const SCREEN: Rect = Rect::from_min_max(Pos2::ZERO, pos2(1600.0, 1000.0)); @@ -12,7 +14,6 @@ struct Harness { app: VectorcraftApp, ctx: egui::Context, time: f64, - cursor: egui::CursorIcon, nodes: Vec<(egui::accesskit::NodeId, egui::accesskit::Node)>, } @@ -21,10 +22,10 @@ impl Harness { fn new() -> Self { let mut app = VectorcraftApp::new(Session::new(), Default::default()); app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); - let mut h = Self { app, ctx: egui::Context::default(), time: 0.0, cursor: egui::CursorIcon::Default, nodes: Vec::new() }; - for _ in 0..3 { - h.frame(vec![]); - } + let ctx = egui::Context::default(); + ctx.enable_accesskit(); + let mut h = Self { app, ctx, time: 0.0, nodes: Vec::new() }; + h.frames(3); h } @@ -34,16 +35,13 @@ impl Harness { self.run(input); } - /// A frame whose input first goes through [`VectorcraftApp::release_lost_buttons`], with the - /// system reporting a mouse button down or not. - fn frame_with_system_buttons(&mut self, events: Vec, system_held: bool) { - self.time += 0.05; - let mut input = egui::RawInput { time: Some(self.time), events, screen_rect: Some(SCREEN), ..Default::default() }; - self.app.release_lost_buttons(&self.ctx, &mut input, system_held); - self.run(input); + fn frames(&mut self, n: usize) { + for _ in 0..n { + self.frame(vec![]); + } } - /// Draw one application frame and retain its cursor and accessibility output for assertions. + /// Draw one application frame and retain its accessibility output for assertions. fn run(&mut self, input: egui::RawInput) { let app = &mut self.app; let mut out = self.ctx.run_ui(input, |ui| { @@ -51,16 +49,32 @@ impl Harness { app.ui(ui); }); out.textures_delta.clear(); - self.cursor = out.platform_output.cursor_icon; self.nodes = out.platform_output.accesskit_update.map_or_else(Vec::new, |update| update.nodes); } - fn hit(&self, hit: &Hit) -> Option { - hit_rects(&self.ctx).into_iter().find(|(h, _)| h == hit).map(|(_, r)| r) + /// The rect of the node of `role` named like `panel` (its title, ignoring case and spaces) in the last frame. + // PLAN: assumed the dock names a tab and an icon by the panel's title, with the roles Tab and Button. + fn named(&self, panel: &str, role: egui::accesskit::Role) -> Option { + let wanted = panel.to_lowercase().replace(' ', ""); + self.nodes + .iter() + .filter(|(_, node)| node.role() == role && node.label().is_some_and(|l| l.to_lowercase().replace(' ', "") == wanted)) + .find_map(|(_, node)| node.bounds()) + .map(|b| Rect::from_min_max(pos2(b.x0 as f32, b.y0 as f32), pos2(b.x1 as f32, b.y1 as f32))) } fn tab(&self, panel: &str) -> Option { - self.hit(&Hit::Tab(panel.into())) + self.named(panel, egui::accesskit::Role::Tab) + } + + fn icon(&self, panel: &str) -> Option { + self.named(panel, egui::accesskit::Role::Button) + } + + /// The canvas rect as the last frame drew it. + // PLAN: assumed `app.canvas_view()` returns the `View` with a `canvas: Rect` field (hub-w1 M12). + fn canvas(&self) -> Rect { + self.app.canvas_view().canvas } fn click(&mut self, at: Pos2) { @@ -74,6 +88,10 @@ impl Harness { self.frame(vec![Event::Key { key, physical_key: None, pressed: false, repeat: false, modifiers }]); } + fn f7(&mut self) { + self.press(egui::Key::F7, egui::Modifiers::NONE); + } + /// Press at `from` and move to `to` in steps, holding the button. fn press_and_move(&mut self, from: Pos2, to: Pos2) { self.frame(vec![Event::PointerMoved(from), button(from, true)]); @@ -86,12 +104,23 @@ impl Harness { fn drag(&mut self, from: Pos2, to: Pos2) { self.press_and_move(from, to); self.frame(vec![button(to, false)]); - self.frame(vec![]); - self.frame(vec![]); + self.frames(2); } fn layout(&self) -> Layout { - layout_of(&self.app.ui) + layout_of(&self.app.ui).clone() + } + + /// Whether the workspace remembers `panel` as hidden. + fn hidden(&self, panel: &str) -> bool { + self.app.ui.dock_workspace.hidden.iter().any(|(id, _)| id == panel) + } + + /// Hides the panels the way Tab (`Panels`) or Shift+Tab (`PanelsKeepTools`) does. + fn hide_panels(&mut self, toggle: ChromeToggle) { + request_chrome_toggle(&self.ctx, ViewportId::ROOT, toggle); + self.frames(2); + assert!(!panels_shown(&self.app), "the panels are hidden"); } } @@ -103,21 +132,26 @@ fn main_panels(layout: &Layout) -> Vec { dock_columns(layout).first().map(|c| c.groups.iter().flat_map(|g| g.panels.clone()).collect()).unwrap_or_default() } +fn first_column_iconic(h: &Harness) -> bool { + let layout = h.layout(); + dock_columns(&layout).first().is_some_and(|c| c.iconic) +} + #[test] fn the_default_layout_is_the_canvas_beside_the_main_column_and_the_icon_column() { let mut h = Harness::new(); let layout = h.layout(); - assert!(h.app.ui.docking.is_none(), "an untouched workspace has no saved layout"); + assert!(layout.contains(&CANVAS.to_string()), "the canvas is in the layout"); let columns = dock_columns(&layout); assert_eq!(main_panels(&layout), ["properties", "layers", "libraries"]); let icons = columns.get(1).expect("an icon column"); assert!(icons.iconic && !columns[0].iconic); assert_eq!(icons.groups.len(), ICON_PANEL_GROUPS.len()); let properties = h.tab("properties").expect("the main column's tabs are drawn"); - let swatches = h.hit(&Hit::Icon("swatches".into())).expect("the icon column is drawn"); + let swatches = h.icon("swatches").expect("the icon column is drawn"); assert!(swatches.right() <= properties.left(), "the icon column is inside the main column"); // The canvas takes the rest and gets the pointer: the Rectangle tool draws on it. - let canvas = h.app.canvas_rect.expect("the canvas is drawn"); + let canvas = h.canvas(); assert!(canvas.right() <= swatches.left() + 1.0 && canvas.width() > 800.0); h.app.select_tool("rectangle"); let count = |h: &Harness| h.app.session.active().unwrap().doc.node_count(); @@ -126,1044 +160,438 @@ fn the_default_layout_is_the_canvas_beside_the_main_column_and_the_icon_column() assert_eq!(count(&h), before + 1); } -#[test] -fn an_icon_opens_its_flyout_and_open_panel_follows() { - let mut h = Harness::new(); - let icon = h.hit(&Hit::Icon("swatches".into())).unwrap(); - h.click(icon.center()); - assert_eq!(h.app.ui.open_panel.as_deref(), Some("swatches")); - assert!(h.tab("swatches").is_some(), "the flyout shows its group's tabs"); - // Window › Brushes switches the flyout on the next frame. - h.app.run("window.panel", json!({"panel": "brushes"})).unwrap(); - h.frame(vec![]); - h.frame(vec![]); - assert_eq!(craft_ui::docking::flyout::(&h.ctx, area_id()).as_deref(), Some("brushes")); - assert_eq!(h.app.ui.open_panel.as_deref(), Some("brushes")); - h.app.run("window.panel", json!({"panel": "brushes"})).unwrap(); - h.frame(vec![]); - assert_eq!(craft_ui::docking::flyout::(&h.ctx, area_id()), None); +/// The floating window holding `panel`, by its panels. +fn window_of(layout: &Layout, panel: &str) -> Vec { + floating_window(layout, panel).map(window_panels).unwrap_or_default() } -/// #5713: real Glyphs and Artboards bodies must retain a flyout resize through -/// content fitting, closing/reopening and serialization of the workspace layout. +fn column_panels(layout: &Layout) -> Vec> { + end_columns(layout).iter().map(|c| c.groups.iter().flat_map(|g| g.panels.clone()).collect()).collect() +} + +/// Window › Layers: the menu item's command and params. +fn window_layers() -> Value { + json!({"panel": "layers", "toggle": true}) +} + +fn checked(h: &Harness, params: &Value) -> Option { + crate::menus::checked(&h.app, "window.panel", params) +} + +fn group_hidden_all(h: &Harness) -> bool { + ["properties", "layers", "libraries"].iter().all(|panel| !h.layout().contains(&panel.to_string())) +} + +type Case = (&'static str, fn(&mut Harness)); + +/// Each panel command (and key) gives the placement it gave before the dock was the framework's: the +/// command goes in, the placement comes out of the layout. #[test] -fn glyphs_and_artboards_flyouts_keep_user_sizes() { - for panel in ["glyphs", "artboards"] { - let mut h = Harness::new(); - let icon = h.hit(&Hit::Icon(panel.into())).unwrap().center(); - h.click(icon); - // Glyphs reserves space from the available height, so its untouched auto-fit - // converges over several frames before the first user resize. - for _ in 0..30 { - h.frame(vec![]); - } - // Area memory can include a panel body's minimum allocation beyond its clip. - // The frame's actual edge widgets describe the visible, resizable rectangle. - let rect = |h: &Harness| { - let id = area_id().with("flyout"); - let left = h.ctx.read_response(id.with("resize-left")).unwrap().rect; - let bottom = h.ctx.read_response(id.with("resize-bottom")).unwrap().rect; - Rect::from_min_max(pos2(left.left(), left.top() - 12.0), pos2(bottom.right() + 12.0, bottom.bottom())) - }; - let before = rect(&h); - for (at, cursor) in [ - (before.left_center() + vec2(2.0, 0.0), egui::CursorIcon::ResizeHorizontal), - (before.center_bottom() - vec2(0.0, 2.0), egui::CursorIcon::ResizeVertical), - ] { - h.frame(vec![Event::PointerMoved(at)]); - h.frame(vec![]); - assert_eq!(h.cursor, cursor, "{panel} at {at:?}, frame {before:?}"); - } - let from = before.left_bottom() + vec2(2.0, -2.0); - h.drag(from, from + vec2(-100.0, 150.0)); - for _ in 0..3 { - h.frame(vec![]); - } - let grown = rect(&h); - assert!((grown.width() - before.width() - 100.0).abs() < 1.0, "{panel}: {before:?} -> {grown:?}"); - assert!((grown.height() - before.height() - 150.0).abs() < 1.0, "{panel}: {before:?} -> {grown:?}"); - assert_eq!(grown.right(), before.right(), "the icon-column edge stays anchored"); - h.click(icon); - assert_eq!(craft_ui::docking::flyout::(&h.ctx, area_id()), None); - h.click(icon); - for _ in 0..3 { - h.frame(vec![]); - } - assert_eq!(rect(&h).size(), grown.size()); - let saved = serde_json::to_string(&h.layout()).unwrap(); - let mut restored = Harness::new(); - restored.app.ui.docking = Some(serde_json::from_str(&saved).unwrap()); - for _ in 0..3 { - restored.frame(vec![]); - } - restored.click(restored.hit(&Hit::Icon(panel.into())).unwrap().center()); - for _ in 0..3 { - restored.frame(vec![]); - } - assert_eq!(rect(&restored).size(), grown.size(), "{panel} saved layout"); +fn panel_commands_give_the_placement_the_old_tests_checked() { + let cases: [Case; 21] = [ + ("reveal brings the panel to the front of its open flyout", |h| { + h.app.run("window.collapseDock", json!({"collapsed": true})).unwrap(); + h.app.run("window.panel", json!({"panel": "properties"})).unwrap(); + h.frames(1); + apply(&mut h.app, &h.ctx, vec![Action::activate("layers".into())]).unwrap(); + h.frames(1); + assert_eq!(h.app.ui.open_panel.as_deref(), Some("properties"), "the flyout is still named for its opening icon"); + assert_eq!(flyout_tab(&h.layout(), "properties").as_deref(), Some("layers"), "Layers is in front"); + reveal_panel(&mut h.app, "properties").unwrap(); + h.frames(1); + assert_eq!(h.app.ui.open_panel.as_deref(), Some("properties"), "the flyout stays open"); + assert_eq!(flyout_tab(&h.layout(), "properties").as_deref(), Some("properties"), "Properties is in front"); + reveal_panel(&mut h.app, "properties").unwrap(); + assert_eq!(h.app.ui.open_panel.as_deref(), Some("properties"), "revealed while in front, it stays"); + }), + ("a character field shortcut raises the floating character panel", |h| { + let id = h.app.run("text.create", json!({"x": 20, "y": 50, "text": "AVAX"})).unwrap()["id"].clone(); + h.app.run("select.set", json!({"ids": [id]})).unwrap(); + h.app.run("window.panel.float", json!({"panel": "character"})).unwrap(); + h.frames(1); + assert!(showing(&h.app.ui, "character")); + assert_eq!(h.app.dock_rt.raise, None); + for command in ["type.highlightKerning", "type.highlightTracking", "type.highlightFontName", "type.highlightFontSize"] { + h.app.run(command, json!({})).unwrap(); + assert_eq!(h.app.dock_rt.raise.as_deref(), Some("character"), "{command}"); + h.frames(1); + assert!(showing(&h.app.ui, "character"), "{command} leaves the panel showing"); + } + }), + ("a character field shortcut ends the hide of the panels", |h| { + let id = h.app.run("text.create", json!({"x": 20, "y": 50, "text": "AVAX"})).unwrap()["id"].clone(); + h.app.run("select.set", json!({"ids": [id]})).unwrap(); + h.app.run("window.panel.float", json!({"panel": "character"})).unwrap(); + h.frames(1); + let rect = floating_window(&h.layout(), "character").map(|w| w.rect); + h.hide_panels(ChromeToggle::Panels); + h.app.run("type.highlightKerning", json!({})).unwrap(); + h.frames(2); + assert!(panels_shown(&h.app) && visible(&h.app.ui, "character"), "the panel shows"); + assert_eq!(floating_window(&h.layout(), "character").map(|w| w.rect), rect, "where it was"); + // In Presentation Mode the shortcut still focuses the field, and the panel shows when the mode ends. + h.app.ui.screen_mode = 3; + assert_eq!(h.app.run("type.highlightTracking", json!({})).unwrap(), json!({"field": "tracking"})); + h.app.ui.screen_mode = 0; + assert!(visible(&h.app.ui, "character"), "the panel stays in place"); + }), + ("the dock collapses to icons and back", |h| { + assert!(!first_column_iconic(h)); + h.app.run("window.collapseDock", json!({"collapsed": true})).unwrap(); + h.frames(1); + assert!(first_column_iconic(h) && h.icon("properties").is_some()); + h.app.run("window.collapseDock", json!({"collapsed": false})).unwrap(); + h.frames(1); + assert!(!first_column_iconic(h) && h.tab("properties").is_some()); + }), + ("float onto, above and below stack with a floating group", |h| { + h.app.run("window.panel.float", json!({"panel": "swatches"})).unwrap(); + h.app.run("window.panel.float", json!({"panel": "stroke", "onto": "Swatches"})).unwrap(); + assert_eq!(group_of(&h.layout(), &"stroke".into()), vec!["swatches".to_string(), "stroke".into()], "a tab of its group"); + h.app.run("window.panel.float", json!({"panel": "color", "below": "swatches"})).unwrap(); + h.app.run("window.panel.float", json!({"panel": "gradient", "above": "swatches"})).unwrap(); + let layout = h.layout(); + let window = window_of(&layout, "swatches"); + for panel in ["color", "gradient", "stroke"] { + assert!(window.contains(&panel.to_string()), "{panel} in the window: {window:?}"); + } + assert_eq!(group_of(&layout, &"color".into()), vec!["color".to_string()], "a group of its own below"); + assert_eq!(group_of(&layout, &"gradient".into()), vec!["gradient".to_string()]); + h.frames(1); + let (top, middle, bottom) = (h.tab("gradient").unwrap(), h.tab("swatches").unwrap(), h.tab("color").unwrap()); + assert!(top.top() < middle.top() && middle.top() < bottom.top(), "gradient above, color below"); + // `onto` names a floating group; a docked one is refused. + assert!(h.app.run("window.panel.float", json!({"panel": "stroke", "onto": "layers"})).is_err()); + }), + ("float collapsed minimizes its window", |h| { + h.app.run("window.panel.float", json!({"panel": "swatches", "collapsed": true})).unwrap(); + assert!(floating_window(&h.layout(), "swatches").unwrap().minimized); + h.app.run("window.panel.float", json!({"panel": "swatches", "collapsed": false})).unwrap(); + assert!(!floating_window(&h.layout(), "swatches").unwrap().minimized); + let r = h.app.run("window.panel.collapse", json!({"panel": "Layers"})).unwrap(); + assert_eq!((r["panel"].clone(), r["collapsed"].clone()), (json!("layers"), json!(true)), "a docked group minimizes too"); + }), + ("dock column n docks a column at that place", |h| { + let before = end_columns(&h.layout()).len(); + h.app.run("window.panel.float", json!({"panel": "swatches"})).unwrap(); + h.app.run("window.panel.float", json!({"panel": "color", "below": "swatches"})).unwrap(); + h.app.run("window.panel.dock", json!({"panel": "swatches", "column": 1})).unwrap(); + let layout = h.layout(); + let columns = end_columns(&layout); + assert_eq!(columns.len(), before + 1); + let panels: Vec<&String> = columns[2].groups.iter().flat_map(|g| &g.panels).collect(); + assert_eq!(panels, ["swatches", "color"], "the whole floating window, next to the icon column"); + assert!(layout.floating.is_empty()); + // `true` docks at the innermost place; without `group`, the panel leaves its group. + h.app.run("window.panel.dock", json!({"panel": "layers", "column": true})).unwrap(); + let layout = h.layout(); + let columns = end_columns(&layout); + assert_eq!(columns.len(), before + 2); + assert_eq!(columns.last().unwrap().groups[0].panels, vec!["layers".to_string()]); + assert_eq!(main_panels(&layout), ["properties", "libraries"]); + }), + ("dock icon puts a panel in the icon column", |h| { + h.app.run("window.panel.dock", json!({"panel": "layers", "icon": true})).unwrap(); + let layout = h.layout(); + assert!(in_iconic_column(&layout, "layers")); + assert!(!main_panels(&layout).contains(&"layers".to_string())); + assert_eq!(group_of(&layout, &"layers".into()), vec!["layers".to_string()], "a row of its own"); + }), + ("a docked column moves to a later place and back", |h| { + h.app.run("window.panel.dock", json!({"panel": "swatches", "column": 1})).unwrap(); + h.app.run("window.panel.dock", json!({"panel": "color", "column": 2})).unwrap(); + assert_eq!(column_panels(&h.layout())[2..], [vec!["swatches".to_string()], vec!["color".to_string()]]); + h.app.run("window.panel.dock", json!({"panel": "swatches", "column": 2})).unwrap(); + assert_eq!(column_panels(&h.layout())[2..], [vec!["color".to_string()], vec!["swatches".to_string()]], "swatches moved inwards"); + h.app.run("window.panel.dock", json!({"panel": "swatches", "column": 1})).unwrap(); + assert_eq!(column_panels(&h.layout())[2..], [vec!["swatches".to_string()], vec!["color".to_string()]], "and back"); + let before = h.layout(); + h.app.run("window.panel.dock", json!({"panel": "swatches", "column": 1})).unwrap(); + assert_eq!(h.layout(), before, "already there"); + }), + ("column places and icon count from the icon column", |h| { + // A layout converted from an old dock split side by side, with the dock collapsed: two + // collapsed tree columns before the icon column. No command makes it, so it is set directly. + let mut layout = h.layout(); + if let Some(Node::Split { second, .. }) = layout.root.as_mut() + && let Node::Dock { columns, .. } = second.as_mut() + { + let libraries = columns[0].groups[0].panels.pop().unwrap(); + assert_eq!(libraries, "libraries"); + columns.insert(1, Column { groups: vec![Group::new(vec![libraries], 0)], width: 200.0, iconic: true, iconic_width: Some(ICON_WIDTH) }); + columns[0].iconic = true; + } + let width: f32 = end_columns(&layout).iter().map(|c| c.iconic_width.unwrap_or(ICON_WIDTH)).sum(); + if let Some(Node::Split { size, .. }) = layout.root.as_mut() { + *size = craft_ui::layout::SplitSize::FixedSecond(width); + } + assert!(layout.validate().is_ok()); + h.app.ui.dock_workspace.layout = layout; + h.app.run("window.panel.dock", json!({"panel": "layers", "icon": true})).unwrap(); + let columns = column_panels(&h.layout()); + assert!(columns[2].contains(&"layers".to_string()), "{columns:?}"); + h.app.run("window.panel.dock", json!({"panel": "swatches", "column": 1})).unwrap(); + let columns = column_panels(&h.layout()); + assert_eq!(columns[..2], [vec!["properties".to_string()], vec!["libraries".to_string()]]); + assert_eq!(columns[3], ["swatches"], "next to the icon column: {columns:?}"); + + // No icon column: `icon` makes one. + let mut layout = h.layout(); + if let Some(Node::Split { second, .. }) = layout.root.as_mut() + && let Node::Dock { columns, .. } = second.as_mut() + { + columns.remove(2); + } + h.app.ui.dock_workspace.layout = layout; + assert_eq!(icon_column(&end_columns(&h.layout())), None); + h.app.run("window.panel.dock", json!({"panel": "properties", "icon": true})).unwrap(); + let layout = h.layout(); + assert!(in_iconic_column(&layout, "properties")); + assert!(icon_column(&end_columns(&layout)).is_none_or(|k| column_panels(&layout)[k].contains(&"properties".to_string()))); + }), + ("F7 hides and restores the expanded Layers tab", |h| { + h.f7(); + assert!(showing(&h.app.ui, "layers"), "the first press activates the tab"); + h.f7(); + assert!(!showing(&h.app.ui, "layers"), "a second press hides the active tab"); + assert!(h.hidden("layers")); + h.f7(); + assert!(showing(&h.app.ui, "layers"), "the next press restores it"); + assert_eq!(main_panels(&h.layout()), ["properties", "layers", "libraries"]); + + h.app.run("window.panel.collapse", json!({"panel": "layers", "collapsed": true})).unwrap(); + let group_minimized = |h: &Harness| { + let layout = h.layout(); + dock_columns(&layout).iter().flat_map(|c| &c.groups).find(|g| g.panels.contains(&"layers".to_string())).unwrap().minimized + }; + assert!(group_minimized(h)); + h.f7(); + assert!(!group_minimized(h), "the first press expands a collapsed group"); + h.f7(); + assert!(!showing(&h.app.ui, "layers"), "the following press hides the expanded group"); + }), + ("F7 activates a background tab before hiding it", |h| { + h.app.run("window.panel.move", json!({"panel": "layers", "anchor": "properties"})).unwrap(); + h.app.run("window.panel.activate", json!({"panel": "properties"})).unwrap(); + let before = group_of(&h.layout(), &"layers".into()); + assert!(!showing(&h.app.ui, "layers")); + h.f7(); + assert!(showing(&h.app.ui, "layers")); + h.f7(); + assert!(group_hidden_all(h)); + h.f7(); + assert!(showing(&h.app.ui, "layers")); + assert_eq!(group_of(&h.layout(), &"layers".into()), before); + }), + ("F7 hides and restores the whole expanded group", |h| { + h.f7(); + h.f7(); + assert!(group_hidden_all(h)); + h.f7(); + assert_eq!(group_of(&h.layout(), &"layers".into()), ["properties", "layers", "libraries"]); + assert!(showing(&h.app.ui, "layers")); + h.f7(); + h.app.run("window.panel", json!({"panel": "properties", "toggle": true})).unwrap(); + assert_eq!(group_of(&h.layout(), &"properties".into()), ["properties", "layers", "libraries"]); + assert!(showing(&h.app.ui, "properties")); + }), + ("a tool's panel request shows the panel and a second keeps it shown", |h| { + // A tool's request for the Transform panel (a double-click on a shape's corner widget) shows and + // selects it wherever it is: closed, floating, behind another tab, in an icon column or in a hidden dock. + let ask_twice = |h: &mut Harness, case: &str| { + for n in 1..=2 { + crate::canvas::apply_requests(&mut h.app, Ok(vec![vectorcraft_engine::UiRequest::Panel("transform".into())])); + h.frames(2); + assert!(visible(&h.app.ui, "transform"), "{case}: request {n}"); + } + }; + if h.layout().contains(&"transform".to_string()) { + h.app.run("window.panel.close", json!({"panel": "transform"})).unwrap(); + } + assert!(!visible(&h.app.ui, "transform")); + ask_twice(h, "closed"); + h.app.run("window.panel.float", json!({"panel": "transform"})).unwrap(); + h.frames(1); + ask_twice(h, "floating"); + h.app.run("window.panel.move", json!({"panel": "transform", "anchor": "properties"})).unwrap(); + h.app.run("window.panel.activate", json!({"panel": "properties"})).unwrap(); + h.frames(1); + assert!(!visible(&h.app.ui, "transform")); + ask_twice(h, "behind another tab"); + h.app.run("window.collapseDock", json!({"collapsed": true})).unwrap(); + h.frames(1); + assert!(in_iconic_column(&h.layout(), "transform")); + ask_twice(h, "in an icon column"); + h.hide_panels(ChromeToggle::Panels); + ask_twice(h, "in a hidden dock"); + assert!(panels_shown(&h.app), "the request ends the hide"); + }), + ("F7 hides and restores a floating tab group", |h| { + h.app.run("window.panel.float", json!({"panel": "layers", "group": true})).unwrap(); + h.app.run("window.panel.activate", json!({"panel": "layers"})).unwrap(); + h.frames(1); + let before = h.layout(); + let rect = floating_window(&before, "layers").unwrap().rect; + assert_eq!(group_of(&before, &"layers".into()), ["properties", "layers", "libraries"]); + h.f7(); + assert!(floating_window(&h.layout(), "layers").is_none()); + assert!(group_hidden_all(h)); + h.f7(); + assert_eq!(floating_window(&h.layout(), "layers").unwrap().rect, rect); + assert_eq!(group_of(&h.layout(), &"layers".into()), ["properties", "layers", "libraries"]); + assert!(showing(&h.app.ui, "layers")); + }), + ("F7 toggles a multi-tab icon flyout", |h| { + h.app.run("window.collapseDock", json!({"collapsed": true})).unwrap(); + h.frames(1); + assert!(in_iconic_column(&h.layout(), "layers")); + assert_eq!(group_of(&h.layout(), &"layers".into()), ["properties", "layers", "libraries"]); + h.f7(); + assert_eq!(h.app.ui.open_panel.as_deref(), Some("layers")); + h.f7(); + assert_eq!(h.app.ui.open_panel, None); + assert!(in_iconic_column(&h.layout(), "layers")); + assert_eq!(group_of(&h.layout(), &"layers".into()), ["properties", "layers", "libraries"]); + }), + ("panel function keys toggle icon flyouts and floating groups", |h| { + h.press(egui::Key::F5, egui::Modifiers::NONE); + assert_eq!(h.app.ui.open_panel.as_deref(), Some("brushes")); + h.press(egui::Key::F5, egui::Modifiers::NONE); + assert_eq!(h.app.ui.open_panel, None); + h.press(egui::Key::F5, egui::Modifiers::NONE); + assert_eq!(h.app.ui.open_panel.as_deref(), Some("brushes")); + + h.app.run("window.panel.float", json!({"panel": "layers"})).unwrap(); + h.frames(1); + let rect = floating_window(&h.layout(), "layers").unwrap().rect; + h.f7(); + assert!(floating_window(&h.layout(), "layers").is_none()); + h.f7(); + assert_eq!(floating_window(&h.layout(), "layers").unwrap().rect, rect); + h.app.run("window.panel.dock", json!({"panel": "layers"})).unwrap(); + h.f7(); + assert!(!showing(&h.app.ui, "layers"), "the expanded dock also toggles"); + }), + ("Window > Layers from a Tab-hidden dock reveals the named front tab", |h| { + // Choosing it shows the dock with Layers where it was instead of closing the tab. + let item = window_layers(); + h.app.run("window.panel", json!({"panel": "layers"})).unwrap(); + assert_eq!(checked(h, &item), Some(true)); + let before = h.layout(); + h.hide_panels(ChromeToggle::Panels); + assert_eq!(checked(h, &item), Some(false), "no check mark while Tab hides the panels"); + h.app.run("window.panel", item.clone()).unwrap(); + h.frames(2); + assert!(panels_shown(&h.app) && visible(&h.app.ui, "layers"), "Layers shows"); + assert_eq!(h.layout(), before, "in its place"); + assert_eq!(checked(h, &item), Some(true)); + h.app.run("window.panel", item.clone()).unwrap(); + assert!(!h.layout().contains(&"layers".to_string()), "a visible panel closes"); + assert!(h.layout().contains(&"properties".to_string()), "only the panel named"); + assert_eq!(checked(h, &item), Some(false)); + }), + ("Window > Layers from a Shift+Tab-hidden floating window reveals it", |h| { + let item = window_layers(); + h.app.run("window.panel.float", json!({"panel": "layers"})).unwrap(); + h.frames(1); + let rect = floating_window(&h.layout(), "layers").map(|w| w.rect); + assert!(rect.is_some()); + for params in [item.clone(), json!({"panel": "layers"})] { + h.hide_panels(ChromeToggle::PanelsKeepTools); + assert_eq!(checked(h, &item), Some(false), "{params}: no check mark while Shift+Tab hides the panels"); + h.app.run("window.panel", params.clone()).unwrap(); + h.frames(2); + assert!(panels_shown(&h.app) && visible(&h.app.ui, "layers"), "{params}: Layers shows"); + assert_eq!(floating_window(&h.layout(), "layers").map(|w| w.rect), rect, "{params}: where it was"); + } + }), + ("Window > Layers with a hidden iconic or closed panel", |h| { + let item = window_layers(); + h.app.run("window.collapseDock", json!({"collapsed": true})).unwrap(); + h.app.run("window.panel", item.clone()).unwrap(); + assert_eq!(h.app.ui.open_panel.as_deref(), Some("layers")); + assert_eq!(checked(h, &item), Some(true)); + h.hide_panels(ChromeToggle::Panels); + assert_eq!(checked(h, &item), Some(false), "no check mark with the dock hidden"); + h.app.run("window.panel", item.clone()).unwrap(); + h.frames(2); + assert!(panels_shown(&h.app), "the dock shows"); + assert_eq!(h.app.ui.open_panel.as_deref(), Some("layers"), "the flyout stays open"); + assert_eq!(checked(h, &item), Some(true)); + h.app.run("window.panel", item.clone()).unwrap(); + assert_eq!(h.app.ui.open_panel, None, "a visible flyout puts away"); + + *h = Harness::new(); + let libraries = json!({"panel": "libraries", "toggle": true}); + h.app.run("window.panel.close", json!({"panel": "libraries"})).unwrap(); + h.app.run("window.panel.activate", json!({"panel": "properties"})).unwrap(); + h.hide_panels(ChromeToggle::Panels); + assert_eq!((checked(h, &libraries), checked(h, &item)), (Some(false), Some(false))); + h.app.run("window.panel", libraries.clone()).unwrap(); + h.frames(2); + assert!(panels_shown(&h.app) && visible(&h.app.ui, "libraries"), "a closed panel comes back"); + h.app.run("window.panel", item.clone()).unwrap(); + assert!(visible(&h.app.ui, "layers") && !visible(&h.app.ui, "properties"), "a background tab comes to the front"); + assert_eq!((checked(h, &libraries), checked(h, &item)), (Some(false), Some(true)), "the tab in front is checked"); + }), + ("F7 from a Tab-hidden dock reveals the named group", |h| { + h.f7(); + assert!(visible(&h.app.ui, "layers")); + let before = h.layout(); + h.hide_panels(ChromeToggle::Panels); + h.f7(); + h.frames(2); + assert!(panels_shown(&h.app) && visible(&h.app.ui, "layers"), "F7 shows the hidden group"); + assert_eq!(h.layout(), before); + h.f7(); + assert!(group_hidden_all(h), "the next press hides the group"); + }), + ]; + for (name, case) in cases { + eprintln!("case: {name}"); + case(&mut Harness::new()); } } -/// An icon flyout's top edge and its free top corner (the canvas-side one) resize it upward: -/// the bottom and the icon-column edge stay, and the new top is kept when the flyout closes -/// and reopens and in a saved layout. The docking action that saves it is one VectorCraft -/// accepts (#1094). -#[test] -fn an_icon_flyout_grows_upward_from_its_top_edge_and_free_corner() { - assert!(known(&Action::MoveFlyoutTop { panel: "glyphs".into(), offset: Some(-40.0) })); - let mut h = Harness::new(); - let icon = h.hit(&Hit::Icon("glyphs".into())).unwrap().center(); - h.click(icon); - for _ in 0..30 { - h.frame(vec![]); - } - let rect = |h: &Harness| { - let id = area_id().with("flyout"); - let left = h.ctx.read_response(id.with("resize-left")).unwrap().rect; - let bottom = h.ctx.read_response(id.with("resize-bottom")).unwrap().rect; - let top = h.ctx.read_response(id.with("resize-top")).unwrap().rect; - Rect::from_min_max(pos2(left.left(), top.top()), pos2(bottom.right() + 12.0, bottom.bottom())) - }; - let before = rect(&h); - assert!(before.top() > 200.0, "room above the Glyphs flyout: {before:?}"); - for (at, cursor) in [ - (pos2(before.center().x, before.top() + 2.0), egui::CursorIcon::ResizeVertical), - (before.left_top() + vec2(2.0, 2.0), egui::CursorIcon::ResizeNwSe), - ] { - h.frame(vec![Event::PointerMoved(at)]); - h.frame(vec![]); - assert_eq!(h.cursor, cursor, "at {at:?}, frame {before:?}"); - } - let from = pos2(before.center().x, before.top() + 2.0); - h.drag(from, from - vec2(0.0, 80.0)); - for _ in 0..3 { - h.frame(vec![]); - } - let raised = rect(&h); - assert!((raised.top() - (before.top() - 80.0)).abs() < 1.0, "{before:?} -> {raised:?}"); - assert_eq!(raised.bottom(), before.bottom()); - assert_eq!(raised.right(), before.right(), "the icon-column edge stays anchored"); - let from = raised.left_top() + vec2(2.0, 2.0); - h.drag(from, from + vec2(-60.0, -20.0)); - for _ in 0..3 { - h.frame(vec![]); - } - let grown = rect(&h); - assert!((grown.width() - raised.width() - 60.0).abs() < 1.0, "{raised:?} -> {grown:?}"); - assert!((grown.top() - (raised.top() - 20.0)).abs() < 1.0, "{raised:?} -> {grown:?}"); - assert_eq!(grown.bottom(), before.bottom()); - assert_eq!(grown.right(), before.right()); - h.click(icon); - assert_eq!(craft_ui::docking::flyout::(&h.ctx, area_id()), None); - h.click(icon); - for _ in 0..3 { - h.frame(vec![]); - } - assert_eq!(rect(&h), grown, "reopened where it was"); - let saved = serde_json::to_string(&h.layout()).unwrap(); - assert!(saved.contains("flyout_offset")); - let mut restored = Harness::new(); - restored.app.ui.docking = Some(serde_json::from_str(&saved).unwrap()); - for _ in 0..3 { - restored.frame(vec![]); - } - restored.click(restored.hit(&Hit::Icon("glyphs".into())).unwrap().center()); - for _ in 0..3 { - restored.frame(vec![]); - } - assert_eq!(rect(&restored), grown, "saved layout"); -} - -/// Footer controls stay at the resized panel's bottom, including empty or short lists. -/// The same shared footer is used by the other icon panels and library windows. -#[test] -fn expanded_panel_footers_stay_at_the_bottom_with_short_and_long_lists() { - for (panel, label, artboards) in [ - ("artboards", "New Artboard", 1), - ("artboards", "New Artboard", 30), - ("brushes", "New Brush", 1), - ("swatches", "New Swatch", 1), - ("symbols", "New Symbol", 1), - ("graphicStyles", "New Graphic Style (Alt-click to name it)", 1), - ("appearance", "Add New Stroke", 1), - ("charStyles", "Create New Character Style", 1), - ("paraStyles", "Create New Paragraph Style", 1), - ("colorGuide", "Save color group to Swatch panel", 1), - ("links", "Show Link Info", 1), - ("actions", "Create New Action", 1), - ("variables", "Capture Data Set", 1), - ("assetExport", "Export for Screens…", 1), - ("cssProperties", "Generate CSS: the whole document's CSS while nothing is selected", 1), - ("svgInteractivity", "Link JavaScript Files…", 1), - ("colorThemes", "Add to Swatches", 1), - ("glyphs", "Zoom In", 1), - ] { - let mut h = Harness::new(); - h.ctx.enable_accesskit(); - for _ in 1..artboards { - h.app.run("artboard.new", json!({})).unwrap(); - } - h.app.run("window.panel.dock", json!({"panel":panel,"icon":true})).unwrap(); - let mut layout = h.layout(); - layout.apply(Action::ResizeGroup { panel: panel.into(), height: Some(540.0) }).unwrap(); - layout.apply(Action::ResizeFlyoutWidth { panel: panel.into(), width: Some(420.0) }).unwrap(); - h.app.ui.docking = Some(layout); - h.app.ui.open_panel = Some(panel.into()); - for _ in 0..5 { - h.frame(vec![]); - } - let bottom = h.ctx.read_response(area_id().with("flyout").with("resize-bottom")).unwrap().rect.bottom(); - let control = h - .nodes - .iter() - .find(|(_, node)| node.label() == Some(label)) - .and_then(|(_, node)| node.bounds()) - .unwrap_or_else(|| panic!("missing {label} in {panel}")); - assert!((control.y1 as f32 - (bottom - 10.0)).abs() < 2.0, "{panel}/{artboards}: footer {control:?}, bottom {bottom}"); - if panel == "artboards" && artboards == 1 { - let at = pos2(((control.x0 + control.x1) * 0.5) as f32, ((control.y0 + control.y1) * 0.5) as f32); - h.click(at); - assert_eq!(h.app.session.active().unwrap().doc.artboards.len(), 2, "the pinned New button works"); - } - } -} - -#[test] -fn a_tab_dragged_out_floats_and_comes_back_where_it_was_after_its_window_closes() { - let mut h = Harness::new(); - let tab = h.tab("layers").unwrap(); - let canvas = h.app.canvas_rect.unwrap(); - h.drag(tab.center(), canvas.center()); - let layout = h.layout(); - let window = floating_window(&layout, "layers").expect("Layers floats"); - assert_eq!(window_panels(window), ["layers"]); - let rect = window.rect; - assert_eq!(main_panels(&layout), ["properties", "libraries"]); - let close = h.hit(&Hit::Close("layers".into())).expect("the floating window has a close button"); - h.click(close.center()); - assert!(!h.layout().contains(&"layers".to_string()), "× hides its panels"); - assert!(h.app.ui.docking_hidden.contains_key("layers")); - h.app.run("window.panel", json!({"panel": "layers"})).unwrap(); - h.frame(vec![]); - let layout = h.layout(); - let window = floating_window(&layout, "layers").expect("Window › Layers brings it back where it was"); - assert_eq!(window.rect, rect); - assert!(h.tab("layers").is_some()); -} - -#[test] -fn thumbnail_work_is_needed_only_while_layers_or_links_can_be_seen() { - let mut h = Harness::new(); - h.app.run("window.panel", json!({"panel": "layers"})).unwrap(); - assert!(thumbnail_panel_showing(&h.app.ui, &h.ctx)); - h.app.ui.dock = false; - assert!(!thumbnail_panel_showing(&h.app.ui, &h.ctx), "Tab hides every panel"); - h.app.ui.dock = true; - h.app.ui.screen_mode = 3; - assert!(!thumbnail_panel_showing(&h.app.ui, &h.ctx), "presentation mode hides panels"); - h.app.ui.screen_mode = 0; - h.app.run("window.panel.float", json!({"panel": "layers"})).unwrap(); - assert!(thumbnail_panel_showing(&h.app.ui, &h.ctx), "a floating Layers panel still needs thumbnails"); - h.app.run("window.panel", json!({"panel": "layers", "toggle": true})).unwrap(); - assert!(!thumbnail_panel_showing(&h.app.ui, &h.ctx)); - h.app.run("window.panel", json!({"panel": "links"})).unwrap(); - h.frame(vec![]); - assert!(thumbnail_panel_showing(&h.app.ui, &h.ctx), "the Links flyout also uses row thumbnails"); -} - -#[test] -fn a_collapsed_dock_uses_the_flyouts_active_tab_for_thumbnail_work() { - let mut h = Harness::new(); - h.app.run("window.collapseDock", json!({"collapsed": true})).unwrap(); - // A customized layout keeps the group's active tab independent of the icon that opened it. - h.app.ui.docking = Some(layout_of(&h.app.ui)); - h.app.run("window.panel", json!({"panel": "properties"})).unwrap(); - h.frame(vec![]); - assert!(!thumbnail_panel_showing(&h.app.ui, &h.ctx), "Properties does not use row thumbnails"); - // A tab press emits Activate without revealing a different flyout anchor. - gesture(&mut h.app, Action::Activate { panel: "layers".into() }).unwrap(); - h.frame(vec![]); - assert_eq!(h.app.ui.open_panel.as_deref(), Some("properties"), "the flyout is still named for its opening icon"); - assert!(thumbnail_panel_showing(&h.app.ui, &h.ctx), "the active Layers tab keeps thumbnail jobs"); - gesture(&mut h.app, Action::Activate { panel: "properties".into() }).unwrap(); - h.frame(vec![]); - assert!(!thumbnail_panel_showing(&h.app.ui, &h.ctx), "returning to Properties drops hidden Layers jobs"); - h.app.run("window.panel", json!({"panel": "layers"})).unwrap(); - h.frame(vec![]); - assert!(thumbnail_panel_showing(&h.app.ui, &h.ctx)); - gesture(&mut h.app, Action::Activate { panel: "properties".into() }).unwrap(); - h.frame(vec![]); - assert_eq!(h.app.ui.open_panel.as_deref(), Some("layers")); - assert!(!thumbnail_panel_showing(&h.app.ui, &h.ctx), "a Properties tab under the Layers icon does not need thumbnails"); -} - -/// Revealing a panel whose flyout is open with another tab of its group in front brings that panel -/// to the front and keeps the flyout open. -#[test] -fn reveal_brings_the_panel_to_the_front_of_its_open_flyout() { - let mut h = Harness::new(); - h.app.run("window.collapseDock", json!({"collapsed": true})).unwrap(); - h.app.ui.docking = Some(layout_of(&h.app.ui)); - h.app.run("window.panel", json!({"panel": "properties"})).unwrap(); - h.frame(vec![]); - gesture(&mut h.app, Action::Activate { panel: "layers".into() }).unwrap(); - h.frame(vec![]); - assert_eq!(h.app.ui.open_panel.as_deref(), Some("properties"), "the flyout is still named for its opening icon"); - assert_eq!(flyout_tab(&layout_of(&h.app.ui), "properties").as_deref(), Some("layers"), "Layers is in front"); - reveal_panel(&mut h.app, "properties").unwrap(); - h.frame(vec![]); - assert_eq!(h.app.ui.open_panel.as_deref(), Some("properties"), "the flyout stays open"); - assert_eq!(flyout_tab(&layout_of(&h.app.ui), "properties").as_deref(), Some("properties"), "Properties is in front"); - // Revealed again while in front, the panel stays as it is. - reveal_panel(&mut h.app, "properties").unwrap(); - assert_eq!(h.app.ui.open_panel.as_deref(), Some("properties")); -} - -/// A Character field shortcut with the Character panel in front of its floating window brings -/// that window forward and leaves the panel showing. -#[test] -fn a_character_field_shortcut_brings_the_floating_character_panel_forward() { - let mut h = Harness::new(); - let id = h.app.run("text.create", json!({"x": 20, "y": 50, "text": "AVAX"})).unwrap()["id"].clone(); - h.app.run("select.set", json!({"ids": [id]})).unwrap(); - h.app.run("window.panel.float", json!({"panel": "character"})).unwrap(); - h.frame(vec![]); - assert!(showing(&h.app.ui, "character")); - assert_eq!(h.app.dock_rt.raise, None); - for command in ["type.highlightKerning", "type.highlightTracking", "type.highlightFontName", "type.highlightFontSize"] { - h.app.run(command, json!({})).unwrap(); - assert_eq!(h.app.dock_rt.raise.as_deref(), Some("character"), "{command}"); - h.frame(vec![]); - assert!(showing(&h.app.ui, "character"), "{command} leaves the panel showing"); - } -} - -/// With the panels hidden by Tab, a Character field shortcut shows the dock with the floating -/// Character panel, so the field it focuses can be seen. -#[test] -fn a_character_field_shortcut_shows_the_panels_tab_hid() { - let mut h = Harness::new(); - let id = h.app.run("text.create", json!({"x": 20, "y": 50, "text": "AVAX"})).unwrap()["id"].clone(); - h.app.run("select.set", json!({"ids": [id]})).unwrap(); - h.app.run("window.panel.float", json!({"panel": "character"})).unwrap(); - h.frame(vec![]); - let rect = floating_window(&h.layout(), "character").map(|w| w.rect); - h.app.run("window.dock", json!({})).unwrap(); - assert!(!visible(&h.app.ui, "character")); - h.app.run("type.highlightKerning", json!({})).unwrap(); - assert!(h.app.ui.dock && visible(&h.app.ui, "character"), "the panel shows"); - assert_eq!(floating_window(&h.layout(), "character").map(|w| w.rect), rect, "where it was"); - // In Presentation Mode the shortcut still focuses the field, and the panel shows when the mode ends. - h.app.ui.screen_mode = 3; - assert_eq!(h.app.run("type.highlightTracking", json!({})).unwrap(), json!({"field": "tracking"})); - h.app.ui.screen_mode = 0; - assert!(visible(&h.app.ui, "character"), "the panel stays in place"); -} - -#[test] -fn the_dock_collapses_to_icons_and_back() { - let mut h = Harness::new(); - let collapse = h.hit(&Hit::Collapse("properties".into())).expect("the main column's double arrow"); - h.click(collapse.center()); - assert!(dock_collapsed(&h.app.ui) && h.app.ui.dock_collapsed); - assert!(h.app.ui.docking.is_none(), "collapsing keeps the workspace's default layout"); - assert!(h.tab("properties").is_none() && h.hit(&Hit::Icon("properties".into())).is_some()); - assert!(h.app.run("window.collapseDock", json!({"collapsed": false})).is_ok()); - h.frame(vec![]); - assert!(!dock_collapsed(&h.app.ui)); - assert!(h.tab("properties").is_some()); -} - -#[test] -fn tab_hides_the_panels_and_the_canvas_keeps_drawing() { - let mut h = Harness::new(); - let narrow = h.app.canvas_rect.unwrap(); - h.app.run("window.dock", json!({})).unwrap(); - h.frame(vec![]); - h.frame(vec![]); - assert!(h.tab("properties").is_none() && h.hit(&Hit::Icon("swatches".into())).is_none()); - let wide = h.app.canvas_rect.expect("the canvas still draws"); - assert!(wide.width() > narrow.width(), "the canvas takes the panels' room"); - h.app.run("window.dock", json!({})).unwrap(); - h.frame(vec![]); - h.frame(vec![]); - assert!(h.tab("properties").is_some()); -} - -/// Hide the panels with Tab and settle; returns the canvas rect with every panel hidden. -fn hide_with_tab(h: &mut Harness) -> Rect { - h.app.run("window.dock", json!({})).unwrap(); - h.frame(vec![]); - h.frame(vec![]); - assert!(h.tab("properties").is_none()); - h.app.canvas_rect.expect("the canvas draws") -} - -/// Frames with the pointer still, for `secs` of the harness clock. -fn wait(h: &mut Harness, secs: f64) { - let until = h.time + secs; - while h.time < until { - h.frame(vec![]); - } -} - -#[test] -fn resting_on_the_right_edge_shows_the_hidden_panels_over_the_canvas_until_the_pointer_leaves() { - let mut h = Harness::new(); - let wide = hide_with_tab(&mut h); - let edge = pos2(SCREEN.right() - 2.0, 500.0); - h.frame(vec![Event::PointerMoved(edge)]); - wait(&mut h, peek::DELAY / 2.0); - assert!(h.tab("properties").is_none(), "the panels wait for the pointer to rest"); - wait(&mut h, peek::DELAY); - let tab = h.tab("properties").expect("resting on the edge shows the right-hand panels"); - assert!(h.hit(&Hit::Icon("swatches".into())).is_some(), "the icon column shows with them"); - assert!(!h.app.ui.dock, "the panels stay hidden for good"); - assert_eq!(h.app.canvas_rect, Some(wide), "the canvas keeps its size, so the art stays where it is"); - assert_eq!(inspect(&h.app, &h.ctx)["peek"], json!("right")); - h.frame(vec![Event::PointerMoved(tab.center())]); - wait(&mut h, peek::GRACE * 2.0); - assert!(h.tab("properties").is_some(), "they stay while the pointer is over them"); - h.frame(vec![Event::PointerMoved(wide.center())]); - wait(&mut h, peek::GRACE / 2.0); - assert!(h.tab("properties").is_some(), "a short trip away keeps them"); - wait(&mut h, peek::GRACE); - assert!(h.tab("properties").is_none(), "away from them, they hide again"); - assert_eq!(inspect(&h.app, &h.ctx)["peek"], Value::Null); -} - -#[test] -fn a_press_on_the_canvas_while_the_panels_show_from_the_edge_does_not_reach_the_art_under_them() { - let mut h = Harness::new(); - let wide = hide_with_tab(&mut h); - h.app.run("shape.rectangle", json!({"x": 100, "y": 100, "width": 100, "height": 100})).unwrap(); - h.frame(vec![]); - fn selected(app: &VectorcraftApp) -> bool { - !app.session.active().unwrap().selection.is_empty() - } - assert!(selected(&h.app)); - assert_eq!(h.app.run("window.panels.peek", json!({"side": "right"})).unwrap(), json!("right")); - h.frame(vec![]); - h.frame(vec![]); - let tab = h.tab("properties").expect("the right-hand panels show"); - assert!(tab.left() < wide.right(), "they cover the canvas's right edge"); - assert_eq!(h.app.canvas_rect, Some(wide)); - // A click in the panels, where the canvas lies under them, leaves the selection. - h.click(pos2(tab.center().x, wide.bottom() - 40.0)); - assert!(selected(&h.app), "the canvas under the panels takes no click"); - // A click on the uncovered canvas away from the art deselects it, as it always does. - h.click(pos2(wide.left() + 40.0, wide.bottom() - 40.0)); - assert!(!selected(&h.app)); -} - -#[test] -fn the_canvas_keeps_the_hidden_panels_size_when_the_window_resizes_or_the_panels_show_at_once() { - let mut h = Harness::new(); - // Shown before any frame has drawn the panels hidden. - h.app.run("window.dock", json!({})).unwrap(); - assert_eq!(h.app.run("window.panels.peek", json!({"side": "right"})).unwrap(), json!("right")); - h.frame(vec![]); - h.frame(vec![]); - assert!(h.tab("properties").is_some()); - let wide = h.app.canvas_rect.unwrap(); - h.app.run("window.panels.peek", json!({"side": null})).unwrap(); - h.frame(vec![]); - h.frame(vec![]); - assert_eq!(h.app.canvas_rect, Some(wide), "the canvas had the size it has with every panel hidden"); - // A narrower window while they show. - h.app.run("window.panels.peek", json!({"side": "right"})).unwrap(); - let narrow = Rect::from_min_max(SCREEN.min, pos2(SCREEN.right() - 200.0, SCREEN.bottom())); - for _ in 0..3 { - h.time += 0.05; - h.run(egui::RawInput { time: Some(h.time), screen_rect: Some(narrow), ..Default::default() }); - } - assert!(h.tab("properties").is_some()); - let canvas = h.app.canvas_rect.unwrap(); - assert!((canvas.width() - (wide.width() - 200.0)).abs() < 1.0, "{canvas:?} follows the window, under the panels"); -} - -#[test] -fn panels_held_by_a_press_hide_after_its_release_away_from_them() { - let mut h = Harness::new(); - let wide = hide_with_tab(&mut h); - h.frame(vec![Event::PointerMoved(pos2(SCREEN.right() - 2.0, 500.0))]); - wait(&mut h, peek::DELAY * 2.0); - let tab = h.tab("properties").expect("the panels show"); - let body = pos2(tab.center().x, wide.bottom() - 40.0); - h.frame(vec![Event::PointerMoved(body), button(body, true)]); - h.frame(vec![Event::PointerMoved(wide.center())]); - wait(&mut h, peek::GRACE * 2.0); - assert!(h.tab("properties").is_some(), "a held button keeps them"); - h.frame(vec![button(wide.center(), false)]); - wait(&mut h, peek::GRACE * 2.0); - assert!(h.tab("properties").is_none(), "released away from them, they hide"); -} - -#[test] -fn a_drag_to_the_edge_or_a_pointer_on_the_left_edge_shows_nothing() { - let mut h = Harness::new(); - let wide = hide_with_tab(&mut h); - // The default layout docks no panel on the left. - h.frame(vec![Event::PointerMoved(pos2(SCREEN.left() + 2.0, 500.0))]); - wait(&mut h, peek::DELAY * 2.0); - assert!(h.tab("properties").is_none()); - // A drag that reaches the right edge holds a button down. - h.frame(vec![Event::PointerMoved(wide.center()), button(wide.center(), true)]); - h.frame(vec![Event::PointerMoved(pos2(SCREEN.right() - 2.0, 500.0))]); - wait(&mut h, peek::DELAY * 2.0); - assert!(h.tab("properties").is_none(), "a drag never shows them"); - h.frame(vec![button(pos2(SCREEN.right() - 2.0, 500.0), false)]); -} - -#[test] -fn tab_while_the_panels_show_from_the_edge_shows_them_for_good() { - let mut h = Harness::new(); - let wide = hide_with_tab(&mut h); - h.frame(vec![Event::PointerMoved(pos2(SCREEN.right() - 2.0, 500.0))]); - wait(&mut h, peek::DELAY * 2.0); - assert!(h.tab("properties").is_some()); - h.app.run("window.dock", json!({})).unwrap(); - h.frame(vec![Event::PointerMoved(wide.center())]); - wait(&mut h, peek::GRACE * 2.0); - assert!(h.app.ui.dock && h.tab("properties").is_some()); - assert_eq!(inspect(&h.app, &h.ctx)["peek"], Value::Null); -} - -#[test] -fn shift_tab_hidden_panels_show_from_the_edge_too() { - let mut h = Harness::new(); - h.app.run("window.panels", json!({})).unwrap(); - h.frame(vec![]); - h.frame(vec![]); - assert!(h.tab("properties").is_none()); - h.frame(vec![Event::PointerMoved(pos2(SCREEN.right() - 2.0, 500.0))]); - wait(&mut h, peek::DELAY * 2.0); - assert!(h.tab("properties").is_some()); -} - #[test] fn window_panels_peek_rejects_what_it_cannot_show_and_changes_nothing() { let mut h = Harness::new(); assert_eq!(h.app.run("window.panels.peek", json!({"side": "right"})).unwrap_err(), "the panels are not hidden"); - hide_with_tab(&mut h); + h.hide_panels(ChromeToggle::Panels); for side in [json!("up"), json!(1), json!(true), json!("Right")] { assert_eq!(h.app.run("window.panels.peek", json!({"side": side})).unwrap_err(), "`side` must be left, right or null", "{side}"); } assert_eq!(h.app.run("window.panels.peek", json!({"side": "left"})).unwrap_err(), "no panels are docked on the left side"); assert_eq!(h.app.run("window.panels.peek", json!({})).unwrap(), Value::Null, "nothing to hide"); - h.frame(vec![]); - assert!(h.tab("properties").is_none() && !h.app.ui.dock); + h.frames(2); + assert!(h.tab("properties").is_none() && !panels_shown(&h.app), "the refusals changed nothing"); assert_eq!(h.app.run("window.panels.peek", json!({"side": "right"})).unwrap(), json!("right")); - h.frame(vec![]); - assert!(h.tab("properties").is_some(), "the panels stay while the pointer has not moved"); assert_eq!(h.app.run("window.panels.peek", json!({"side": null})).unwrap(), Value::Null); - h.frame(vec![]); - assert!(h.tab("properties").is_none()); h.app.ui.screen_mode = 3; assert_eq!(h.app.run("window.panels.peek", json!({"side": "right"})).unwrap_err(), "panels can't be shown in Presentation Mode"); h.app.ui.screen_mode = 0; } -#[test] -fn panels_shown_by_window_panels_peek_stay_until_the_pointer_moves_or_leaves_the_window() { - let mut h = Harness::new(); - let wide = hide_with_tab(&mut h); - assert_eq!(h.app.run("window.panels.peek", json!({"side": "right"})).unwrap(), json!("right")); - wait(&mut h, peek::GRACE * 2.0); - assert!(h.tab("properties").is_some(), "with no pointer movement, the panels stay"); - assert_eq!(inspect(&h.app, &h.ctx)["peek"], json!("right")); - h.frame(vec![Event::PointerMoved(wide.center())]); - wait(&mut h, peek::GRACE * 2.0); - assert!(h.tab("properties").is_none(), "the pointer moved away from them, so they hide"); - // The pointer leaving the window counts as moving away. - h.app.run("window.panels.peek", json!({"side": "right"})).unwrap(); - wait(&mut h, peek::GRACE * 2.0); - assert!(h.tab("properties").is_some()); - h.frame(vec![Event::PointerGone]); - wait(&mut h, peek::GRACE * 2.0); - assert!(h.tab("properties").is_none(), "the pointer left the window, so they hide"); - assert_eq!(inspect(&h.app, &h.ctx)["peek"], Value::Null); -} - -#[test] -fn panels_held_by_a_flyout_hide_after_it_closes_with_the_pointer_still_away_from_them() { - let mut h = Harness::new(); - let wide = hide_with_tab(&mut h); - // The pointer rests on the canvas, then outside the window, while a flyout holds the panels. - for away in [vec![Event::PointerMoved(wide.center())], vec![Event::PointerGone]] { - h.app.run("window.panels.peek", json!({"side": "right"})).unwrap(); - h.app.ui.open_panel = Some("swatches".into()); - h.frame(away); - wait(&mut h, peek::GRACE * 2.0); - assert!(h.tab("properties").is_some(), "an open flyout keeps them"); - h.app.ui.open_panel = None; - wait(&mut h, peek::GRACE * 2.0); - assert!(h.tab("properties").is_none(), "the flyout closed with the pointer away from them, so they hide"); - assert_eq!(inspect(&h.app, &h.ctx)["peek"], Value::Null); - } -} - #[test] fn edge_panels_are_the_docked_panels_on_each_side_of_the_canvas() { - let layout = default_from(DockTab::ALL[0], &[], false, None, None); + let layout = default_workspace(None).layout; let right = peek::edge_panels(&layout, peek::Edge::Right); assert!(right.contains(&"properties".to_string()) && right.contains(&"swatches".to_string())); assert!(!right.contains(&CANVAS.to_string())); assert!(peek::edge_panels(&layout, peek::Edge::Left).is_empty()); - let mirrored = Layout { - root: Some(Node::Split { + let mirrored = Layout::new( + Some(Node::Split { axis: craft_ui::layout::SplitAxis::Horizontal, size: craft_ui::layout::SplitSize::FixedFirst(310.0), first: Box::new(Node::Tabs { panels: vec!["layers".to_string()], active: 0 }), second: Box::new(Node::Tabs { panels: vec![CANVAS.to_string()], active: 0 }), }), - floating: Vec::new(), - }; + Vec::new(), + ); assert_eq!(peek::edge_panels(&mirrored, peek::Edge::Left), ["layers"]); assert!(peek::edge_panels(&mirrored, peek::Edge::Right).is_empty()); } -#[test] -fn switching_workspace_during_a_tab_drag_keeps_the_new_workspace_layout() { - let mut h = Harness::new(); - let tab = h.tab("layers").unwrap(); - let canvas = h.app.canvas_rect.unwrap(); - h.press_and_move(tab.center(), canvas.center()); - h.app.run("window.workspace", json!({"name": "Layout"})).unwrap(); - h.frame(vec![Event::PointerMoved(canvas.center())]); - h.frame(vec![button(canvas.center(), false)]); - h.frame(vec![]); - assert!(h.app.ui.docking.is_none(), "the drag ended with the switch"); - assert!(h.layout().floating.is_empty()); - assert_eq!(main_panels(&h.layout()), ["properties", "layers", "libraries"]); -} - -#[test] -fn the_tools_panel_floats_by_its_title_bar_and_docks_on_the_left_edge() { - let mut h = Harness::new(); - let zone = docked_tools(&h); - let title = zone.min + vec2(20.0, 12.0); - h.drag(title, pos2(600.0, 400.0)); - let [x, y] = h.app.ui.toolbar_pos.expect("the Tools panel floats"); - assert!((300.0..700.0).contains(&x) && (250.0..450.0).contains(&y), "{x} {y}"); - let title = pos2(x + 20.0, y + 7.0); - h.drag(title, zone.center()); - assert_eq!(h.app.ui.toolbar_pos, None, "dropped on the left edge, it docks"); -} - -/// The Tools panel also docks on the right, left of the panels: dropped on the window's right edge -/// it stands between the canvas and the panels, its flyouts open toward the canvas, the side is -/// kept with the preferences, and it drags back to the left edge. -#[test] -fn the_tools_panel_docks_on_the_right_left_of_the_panels() { - let mut h = Harness::new(); - let zone = docked_tools(&h); - h.drag(zone.min + vec2(20.0, 12.0), pos2(600.0, 400.0)); - let [x, y] = h.app.ui.toolbar_pos.expect("the Tools panel floats"); - h.drag(pos2(x + 20.0, y + 7.0), pos2(SCREEN.right() - 10.0, 500.0)); - assert_eq!((h.app.ui.toolbar_pos, h.app.ui.toolbar_right), (None, true), "dropped on the right edge, it docks on the right"); - settle(&mut h); - let bar = docked_tools(&h); - let canvas = h.app.canvas_rect.expect("the canvas"); - let layers = h.tab("layers").expect("the Layers panel, docked right"); - assert!(canvas.right() <= bar.left() + 1.0 && bar.right() <= layers.left() + 1.0, "canvas {canvas:?}, Tools {bar:?}, Layers {layers:?}"); - assert!(SCREEN.right() - bar.right() > 100.0, "the panels keep the window's right edge: {bar:?}"); - // Its flyouts and drawer open toward the canvas, not over the panels. - let room = crate::toolbar::room(&h.app, &h.ctx); - assert!(room.right() <= bar.right() + 0.5, "{room:?}"); - let left = crate::toolbar::beside(bar, 160.0, 8.0, room); - assert!(left + 160.0 <= bar.left() - 7.0, "left of the toolbar: {left}"); - let prefs: crate::state::UiState = serde_json::from_slice(&serde_json::to_vec(&h.app.ui).unwrap()).unwrap(); - assert!(prefs.toolbar_right, "kept with the preferences"); - // Dragged back to the window's left edge, it docks there again. - h.drag(bar.min + vec2(20.0, 12.0), pos2(600.0, 400.0)); - let [x, y] = h.app.ui.toolbar_pos.expect("floats again"); - h.drag(pos2(x + 20.0, y + 7.0), pos2(12.0, 500.0)); - assert_eq!((h.app.ui.toolbar_pos, h.app.ui.toolbar_right), (None, false)); - settle(&mut h); - assert!(docked_tools(&h).left() < 10.0, "at the window's left edge again"); - // Agents: window.panel.dock takes the side (left/right, or Start/End as panels do). - let r = h.app.run("window.panel.dock", json!({"panel": "tools", "side": "End"})).unwrap(); - assert_eq!((r["side"].as_str(), h.app.ui.toolbar_right), (Some("right"), true)); - h.app.run("window.panel.float", json!({"panel": "tools"})).unwrap(); - assert_eq!(h.app.run("window.panel.dock", json!({"panel": "tools"})).unwrap()["side"], "right", "back on the side it docked at last"); - assert!(h.app.run("window.panel.dock", json!({"panel": "tools", "side": "top"})).is_err()); - // A side that isn't text is an error too, not the last side: it leaves Tools where it is. - h.app.run("window.panel.float", json!({"panel": "tools"})).unwrap(); - for side in [json!(5), json!(true), json!(["left"])] { - assert!(h.app.run("window.panel.dock", json!({"panel": "tools", "side": side})).is_err(), "{side}"); - assert!(h.app.ui.toolbar_pos.is_some(), "still floating after side {side}"); - } - assert_eq!(h.app.run("window.panel.dock", json!({"panel": "tools", "side": null})).unwrap()["side"], "right", "null: the last side"); -} - -/// The docked Tools panel as last drawn. -fn docked_tools(h: &Harness) -> Rect { - h.ctx.data(|d| d.get_temp::(crate::toolbar::docked_rect_id())).expect("the docked Tools panel") -} - -fn settle(h: &mut Harness) { - for _ in 0..3 { - h.frame(vec![]); - } -} - -fn keep_tools_beside_canvas(h: &mut Harness, on: bool) { - assert_eq!(h.app.run("window.toolsBesideCanvas", json!({"on": on})).unwrap(), json!(on)); - settle(h); -} - -/// Where docked Tools sits with a left-side dock: `Some(true)` between the dock and the canvas, -/// `Some(false)` at the window's edge outside the dock. -fn tools_place(h: &Harness) -> Option { - let tools = docked_tools(h); - let layers = h.tab("layers")?; - let canvas = h.app.canvas_rect?; - if layers.right() <= tools.left() && tools.right() <= canvas.left() && canvas.left() - tools.right() < 40.0 { - Some(true) - } else if tools.left() < 10.0 && tools.right() <= layers.left() { - Some(false) - } else { - None - } -} - -/// Off by default: docked Tools stays at the window's edge, outside a left-side dock, and the -/// setting moves it between the dock and the canvas and back. -#[test] -fn docked_tools_keep_the_window_edge_unless_kept_beside_the_canvas() { - let mut h = Harness::new(); - assert!(!h.app.ui.tools_beside_canvas); - assert_eq!(crate::menus::checked(&h.app, "window.toolsBesideCanvas", &json!({})), Some(false)); - h.app.run("window.panel.dock", json!({"panel": "layers", "side": "Start"})).unwrap(); - settle(&mut h); - assert_eq!(tools_place(&h), Some(false), "Tools, then the left dock, then the canvas"); - assert_eq!(crate::toolbar::docked_width(&h.app.ui), 0.0, "the canvas reserves no room for Tools"); - keep_tools_beside_canvas(&mut h, true); - assert_eq!(crate::menus::checked(&h.app, "window.toolsBesideCanvas", &json!({})), Some(true)); - assert_eq!(tools_place(&h), Some(true), "the left dock, then Tools, then the canvas"); - keep_tools_beside_canvas(&mut h, false); - assert_eq!(tools_place(&h), Some(false), "back at the window's edge"); -} - -#[test] -fn keep_tools_beside_the_canvas_toggles_and_rejects_a_value_that_is_not_true_or_false() { - let mut h = Harness::new(); - assert_eq!(h.app.run("window.toolsBesideCanvas", json!({})).unwrap(), json!(true)); - assert_eq!(h.app.run("window.toolsBesideCanvas", json!({})).unwrap(), json!(false)); - assert!(h.app.run("window.toolsBesideCanvas", json!({"on": "yes"})).is_err()); - assert!(!h.app.ui.tools_beside_canvas, "a bad value changes nothing"); -} - -/// Whichever way the setting is, the canvas's left edge, right of a left-side dock, docks a -/// floating Tools panel beside the canvas and turns the setting on, and the window's left edge -/// docks it outside the dock and turns the setting off; the menu item's check mark follows. -#[test] -fn tools_dropped_on_a_left_edge_dock_there_and_the_setting_follows() { - for beside in [false, true] { - let mut h = Harness::new(); - keep_tools_beside_canvas(&mut h, beside); - h.app.run("window.panel.dock", json!({"panel": "layers", "side": "Start"})).unwrap(); - for at_window_edge in [false, true, false] { - h.app.run("window.panel.float", json!({"panel": "tools", "x": 600, "y": 300})).unwrap(); - settle(&mut h); - let canvas = h.app.canvas_rect.unwrap(); - let canvas_edge = h.ctx.data(|d| d.get_temp::(crate::tools_float::tools_zone_id())); - let canvas_edge = canvas_edge.expect("the canvas's left edge is a target whichever way the setting is"); - assert!(canvas_edge.left() > 60.0, "right of the left-side dock: {canvas_edge:?}"); - let target = if at_window_edge { pos2(12.0, canvas.center().y) } else { canvas_edge.center() }; - h.drag(pos2(620.0, 307.0), target); - let case = format!("set before: {beside}, at the window edge: {at_window_edge}"); - assert_eq!(h.app.ui.toolbar_pos, None, "{case}"); - assert_eq!(h.app.ui.tools_beside_canvas, !at_window_edge, "{case}"); - assert_eq!(crate::menus::checked(&h.app, "window.toolsBesideCanvas", &json!({})), Some(!at_window_edge), "{case}"); - settle(&mut h); - assert_eq!(tools_place(&h), Some(!at_window_edge), "{case}"); - } - } -} - -/// With no panels docked on the left, the canvas's left edge and the window's left edge overlap, -/// and a drop there docks Tools where the setting puts it, leaving the setting as it is. -#[test] -fn tools_dropped_where_the_left_edges_overlap_keep_the_setting() { - for beside in [false, true] { - let mut h = Harness::new(); - keep_tools_beside_canvas(&mut h, beside); - h.app.run("window.panel.float", json!({"panel": "tools", "x": 600, "y": 300})).unwrap(); - settle(&mut h); - let canvas_edge = h.ctx.data(|d| d.get_temp::(crate::tools_float::tools_zone_id())).expect("the canvas-edge target"); - assert!(canvas_edge.left() < 30.0, "no panels on the left: {canvas_edge:?}"); - h.drag(pos2(620.0, 307.0), pos2(canvas_edge.left() + 4.0, canvas_edge.center().y)); - assert_eq!(h.app.ui.toolbar_pos, None, "set before: {beside}"); - assert_eq!(h.app.ui.tools_beside_canvas, beside, "the setting is unchanged"); - settle(&mut h); - let tools = docked_tools(&h); - assert!(tools.left() < 10.0, "docked at the left edge, set before: {beside}: {tools:?}"); - assert!(tools.right() <= h.app.canvas_rect.unwrap().left() + 1.0, "left of the canvas: {tools:?}"); - } -} - -/// A toolbar made with New Toolbar docked on the left, and no panels there: Tools docks left of the -/// toolbar's lane either way, so a drop on the canvas's edge, right of the lane, keeps the setting. -#[test] -fn tools_dropped_beside_a_left_toolbar_lane_keep_the_setting() { - for beside in [false, true] { - let mut h = Harness::new(); - keep_tools_beside_canvas(&mut h, beside); - h.app.run("window.toolbar.new", json!({"name": "Mine"})).unwrap(); - h.app.run("window.toolbar.dock", json!({"name": "Mine", "side": "left"})).unwrap(); - h.app.run("window.panel.float", json!({"panel": "tools", "x": 600, "y": 300})).unwrap(); - settle(&mut h); - let canvas_edge = h.ctx.data(|d| d.get_temp::(crate::tools_float::tools_zone_id())).expect("the canvas-edge target"); - assert!(canvas_edge.left() > 30.0, "right of the toolbar lane: {canvas_edge:?}"); - let at = pos2(canvas_edge.right() - 4.0, canvas_edge.center().y); - let lit = crate::tools_float::drop_highlight(&h.ctx, at, beside).expect("a strip lights up"); - assert!(lit.left() < 10.0, "the window's edge, where Tools docks, lights up: {lit:?}"); - h.drag(pos2(620.0, 307.0), at); - assert_eq!(h.app.ui.toolbar_pos, None, "set before: {beside}"); - assert_eq!(h.app.ui.tools_beside_canvas, beside, "the setting is unchanged"); - settle(&mut h); - assert!(docked_tools(&h).left() < 10.0, "left of the lane, set before: {beside}"); - } -} - -/// Dragged out of the panel docked at the window's edge and released in the next frame, right of -/// where the panel was, Tools stays floating and the setting stays off. -#[test] -fn tools_released_as_they_float_from_the_window_edge_keep_floating() { - let mut h = Harness::new(); - let docked = docked_tools(&h); - let title = docked.min + vec2(20.0, 12.0); - h.frame(vec![Event::PointerMoved(title), button(title, true)]); - h.frame(vec![Event::PointerMoved(title + vec2(0.0, 10.0))]); - let out = pos2(docked.right().max(crate::toolbar::WIDTH) + 8.0, title.y + 10.0); - h.frame(vec![Event::PointerMoved(out)]); - assert!(h.app.ui.toolbar_pos.is_some(), "dragged out of the docked panel, Tools floats"); - h.frame(vec![button(out, false)]); - settle(&mut h); - assert!(h.app.ui.toolbar_pos.is_some(), "the strip laid out right of the docked panel docks nothing"); - assert!(!h.app.ui.tools_beside_canvas); -} - -/// A drop on the right edge leaves the setting as it is, and so does a move of Tools floating over -/// the canvas's left edge that ends within a few points of where it started. -#[test] -fn tools_docked_on_the_right_or_clicked_leave_the_setting() { - for beside in [false, true] { - let mut h = Harness::new(); - keep_tools_beside_canvas(&mut h, beside); - h.app.run("window.panel.dock", json!({"panel": "layers", "side": "Start"})).unwrap(); - h.app.run("window.panel.float", json!({"panel": "tools", "x": 600, "y": 300})).unwrap(); - settle(&mut h); - h.drag(pos2(620.0, 307.0), pos2(SCREEN.right() - 10.0, 500.0)); - assert_eq!((h.app.ui.toolbar_pos, h.app.ui.toolbar_right), (None, true), "set before: {beside}"); - assert_eq!(h.app.ui.tools_beside_canvas, beside, "docked on the right, the setting is unchanged"); - let canvas_edge = h.ctx.data(|d| d.get_temp::(crate::tools_float::tools_zone_id())).expect("the canvas-edge target"); - h.app.run("window.panel.float", json!({"panel": "tools", "x": canvas_edge.left(), "y": 300})).unwrap(); - settle(&mut h); - let [x, y] = h.app.ui.toolbar_pos.unwrap(); - let title = pos2(x + 12.0, y + 7.0); - assert!(canvas_edge.contains(title), "the title bar is over the canvas's edge: {title:?} {canvas_edge:?}"); - h.press_and_move(title, title + vec2(30.0, 0.0)); - let moved = h.app.ui.toolbar_pos.map(|[px, _]| px - x); - assert!(moved.is_some_and(|dx| dx > 10.0), "the move started, set before: {beside}: {moved:?}"); - let back = title + vec2(2.0, 0.0); - h.frame(vec![Event::PointerMoved(back)]); - h.frame(vec![button(back, false)]); - settle(&mut h); - assert!(h.app.ui.toolbar_pos.is_some(), "released where it started, Tools docks nowhere, set before: {beside}"); - assert_eq!(h.app.ui.tools_beside_canvas, beside, "the setting is unchanged"); - } -} - -/// A left-side dock collapsed to icons is narrower than the window's strip: only the column the -/// two strips share keeps the setting, and each strip's own part docks at its place. -#[test] -fn tools_dropped_beside_a_collapsed_left_dock_follow_the_strip_dropped_on() { - let mut h = Harness::new(); - h.app.run("window.panel.dock", json!({"panel": "layers", "side": "Start"})).unwrap(); - h.app.run("window.collapseDock", json!({"collapsed": true})).unwrap(); - let float = |h: &mut Harness| { - h.app.run("window.panel.float", json!({"panel": "tools", "x": 600, "y": 300})).unwrap(); - settle(h); - h.ctx.data(|d| d.get_temp::(crate::tools_float::tools_zone_id())).expect("the canvas-edge target") - }; - let zone = float(&mut h); - let window_strip = crate::toolbar::WIDTH; - assert!(zone.left() > 10.0 && zone.left() < window_strip - 4.0, "an icon column narrower than the window's strip: {zone:?}"); - let y = zone.center().y; - // Beside the canvas: right of the icon column; at the window's edge: left of it. - let placed_beside = |h: &mut Harness, icons: Rect| { - settle(h); - let tools = docked_tools(h); - if tools.left() >= icons.left() - 1.0 { - Some(true) - } else if tools.left() < 10.0 { - Some(false) - } else { - None - } - }; - // Right of the window's strip: beside the canvas. - h.drag(pos2(620.0, 307.0), pos2(window_strip + 4.0, y)); - assert_eq!((h.app.ui.toolbar_pos, h.app.ui.tools_beside_canvas), (None, true)); - assert_eq!(placed_beside(&mut h, zone), Some(true)); - // In the shared column: where the setting puts it, the setting unchanged. - let zone = float(&mut h); - h.drag(pos2(620.0, 307.0), pos2(zone.left() + 2.0, y)); - assert_eq!((h.app.ui.toolbar_pos, h.app.ui.tools_beside_canvas), (None, true)); - assert_eq!(placed_beside(&mut h, zone), Some(true)); - // Left of the icon column: at the window's edge. - let zone = float(&mut h); - h.drag(pos2(620.0, 307.0), pos2(zone.left() / 2.0, y)); - assert_eq!((h.app.ui.toolbar_pos, h.app.ui.tools_beside_canvas), (None, false)); - assert_eq!(placed_beside(&mut h, zone), Some(false)); -} - -/// Turning the setting on or off while Tools floats leaves it floating. -#[test] -fn toggling_while_tools_floats_keeps_it_floating() { - let mut h = Harness::new(); - h.app.run("window.panel.dock", json!({"panel": "layers", "side": "Start"})).unwrap(); - keep_tools_beside_canvas(&mut h, true); - h.app.run("window.panel.float", json!({"panel": "tools", "x": 600, "y": 300})).unwrap(); - settle(&mut h); - let [x, y] = h.app.ui.toolbar_pos.unwrap(); - keep_tools_beside_canvas(&mut h, false); - assert_eq!(h.app.ui.toolbar_pos, Some([x, y]), "turning it off leaves Tools floating where it was"); - keep_tools_beside_canvas(&mut h, true); - assert_eq!(h.app.ui.toolbar_pos, Some([x, y]), "turning it on leaves Tools floating where it was"); -} - -/// The setting is kept with a workspace; state and workspaces saved without it keep Tools at the -/// window's edge. -#[test] -fn keep_tools_beside_the_canvas_is_saved_and_off_in_state_saved_without_it() { - let mut saved = serde_json::to_value(crate::state::UiState { tools_beside_canvas: true, ..Default::default() }).unwrap(); - assert_eq!(saved["tools_beside_canvas"], json!(true)); - let reloaded: crate::state::UiState = serde_json::from_value(saved.clone()).unwrap(); - assert!(reloaded.tools_beside_canvas); - saved.as_object_mut().unwrap().remove("tools_beside_canvas"); - let old: crate::state::UiState = serde_json::from_value(saved).unwrap(); - assert!(!old.tools_beside_canvas && old.toolbar_pos.is_none(), "older state docks Tools at the window's edge"); - - let mut h = Harness::new(); - h.app.run("window.panel.dock", json!({"panel": "layers", "side": "Start"})).unwrap(); - keep_tools_beside_canvas(&mut h, true); - let mut workspace = serde_json::to_value(crate::workspaces::capture(&h.app.ui, "Mine")).unwrap(); - assert_eq!(workspace["toolsBesideCanvas"], json!(true)); - workspace.as_object_mut().unwrap().remove("toolsBesideCanvas"); - let old: crate::workspaces::Workspace = serde_json::from_value(workspace).unwrap(); - assert!(!old.tools_beside_canvas, "a workspace saved without it docks Tools at the window's edge"); - - h.app.run("window.workspace.new", json!({"name": "Mine"})).unwrap(); - h.app.run("window.workspace", json!({"name": "Essentials"})).unwrap(); - settle(&mut h); - assert!(!h.app.ui.tools_beside_canvas, "built-in workspaces keep Tools at the window's edge"); - h.app.run("window.workspace", json!({"name": "Mine"})).unwrap(); - settle(&mut h); - assert!(h.app.ui.tools_beside_canvas); - assert_eq!(tools_place(&h), Some(true), "the saved workspace brings Tools back beside the canvas"); -} - -#[test] -fn tools_follow_left_panels_when_resized_hidden_and_restored() { - for (beside, double) in [(false, false), (false, true), (true, false), (true, true)] { - let mut h = Harness::new(); - keep_tools_beside_canvas(&mut h, beside); - h.app.run("window.panel.dock", json!({"panel": "layers", "side": "Start"})).unwrap(); - gesture(&mut h.app, Action::ResizeColumn { panel: "layers".into(), width: 430.0 }).unwrap(); - h.app.ui.toolbar_double = double; - let width = if double { 80.0 } else { 48.0 }; - for hidden in [false, true, false] { - if hidden == h.app.ui.dock { - h.app.run("window.panels", json!({})).unwrap(); - } - settle(&mut h); - let tools = docked_tools(&h); - assert!((tools.width() - (width + 6.0)).abs() < 1.0, "{} columns: {tools:?}", if double { 2 } else { 1 }); - if hidden { - assert!(h.tab("layers").is_none()); - assert!(tools.left() < 10.0); - } else { - assert_eq!(tools_place(&h), Some(beside), "beside: {beside}, double: {double}"); - assert!(h.tab("layers").unwrap().width() > 0.0); - } - let canvas = h.app.canvas_rect.unwrap(); - assert!(canvas.left() - tools.left() >= width, "Tools has its own room: {tools:?}, {canvas:?}"); - assert!(canvas.width() > 200.0); - } - } -} - -/// A layout with panels above or below the canvas: the full-height strip along the window's left -/// edge docks Tools in both placements; off, Tools runs the window's height beside those panels, -/// and on, it stays in the canvas's row. -#[test] -fn tools_redock_at_window_edge_above_and_below_canvas() { - for (beside, above) in [(false, true), (false, false), (true, true), (true, false)] { - let mut h = Harness::new(); - keep_tools_beside_canvas(&mut h, beside); - let tabs = |panel: &str| Box::new(Node::Tabs { panels: vec![panel.to_string()], active: 0 }); - h.app.ui.docking = Some(Layout { - root: Some(Node::Split { - axis: craft_ui::layout::SplitAxis::Vertical, - size: craft_ui::layout::SplitSize::FixedFirst(180.0), - first: tabs("layers"), - second: Box::new(Node::Split { - axis: craft_ui::layout::SplitAxis::Vertical, - size: craft_ui::layout::SplitSize::FixedSecond(180.0), - first: tabs(CANVAS), - second: tabs("properties"), - }), - }), - floating: vec![], - }); - assert!(valid(&h.layout())); - h.app.run("window.panel.float", json!({"panel": "tools", "x": 600, "y": 300})).unwrap(); - for _ in 0..3 { - h.frame(vec![]); - } - let panel = h.tab(if above { "layers" } else { "properties" }).unwrap(); - let canvas = h.app.canvas_rect.unwrap(); - assert!(panel.center().y < canvas.top() || panel.center().y > canvas.bottom()); - h.drag(pos2(620.0, 307.0), pos2(12.0, panel.center().y)); - assert_eq!(h.app.ui.toolbar_pos, None, "window-edge drop, beside: {beside}, above the canvas: {above}"); - assert_eq!(h.app.ui.tools_beside_canvas, beside, "no panels on the left: the setting is unchanged"); - settle(&mut h); - let tools = docked_tools(&h); - let panel = h.tab(if above { "layers" } else { "properties" }).unwrap(); - assert!(tools.left() < 10.0 && tools.right() <= panel.left() + 60.0, "{tools:?}"); - match (beside, above) { - (false, true) => assert!(tools.top() <= panel.top(), "Tools runs past the panel above: {tools:?}, {panel:?}"), - (false, false) => assert!(tools.bottom() >= panel.bottom(), "Tools runs past the panel below: {tools:?}, {panel:?}"), - (true, true) => assert!(tools.top() >= panel.bottom(), "Tools stays below the panel above: {tools:?}, {panel:?}"), - (true, false) => assert!(tools.bottom() <= panel.top(), "Tools stays above the panel below: {tools:?}, {panel:?}"), - } - } -} - -#[test] -#[ignore = "manual visual check; set LEFT_TOOLS_SCREENSHOTS to an existing harness directory"] -fn capture_tools_beside_left_panels() { - let dir = std::path::PathBuf::from(std::env::var_os("LEFT_TOOLS_SCREENSHOTS").unwrap()); - let mut app = VectorcraftApp::new(Session::new(), Default::default()); - app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); - app.run("window.toolsBesideCanvas", json!({"on": true})).unwrap(); - app.run("window.panel.dock", json!({"panel": "layers", "side": "Start"})).unwrap(); - let mut h = egui_kittest::Harness::builder() - .with_size(vec2(1200.0, 800.0)) - .with_accessibility_check(false) - .wgpu() - .build_ui_state(|ui, app: &mut VectorcraftApp| app.ui(ui), app); - h.run_steps(6); - h.render().unwrap().save(dir.join("tools-inside-left-dock.png")).unwrap(); - h.state_mut().ui.toolbar_double = true; - h.run_steps(3); - h.render().unwrap().save(dir.join("tools-inside-left-dock-double.png")).unwrap(); -} - #[test] fn a_library_panel_opens_renames_and_closes() { let mut h = Harness::new(); @@ -1182,146 +610,6 @@ fn a_library_panel_opens_renames_and_closes() { assert!(!h.layout().contains(&web)); } -/// The floating window holding `panel`, by its panels. -fn window_of(layout: &Layout, panel: &str) -> Vec { - floating_window(layout, panel).map(window_panels).unwrap_or_default() -} - -#[test] -fn float_onto_above_and_below_stack_with_a_floating_group() { - let mut h = Harness::new(); - h.app.run("window.panel.float", json!({"panel": "swatches"})).unwrap(); - h.app.run("window.panel.float", json!({"panel": "stroke", "onto": "Swatches"})).unwrap(); - let layout = h.layout(); - assert_eq!(group_of(&layout, &"stroke".into()), vec!["swatches".to_string(), "stroke".into()], "a tab of its group"); - - h.app.run("window.panel.float", json!({"panel": "color", "below": "swatches"})).unwrap(); - h.app.run("window.panel.float", json!({"panel": "gradient", "above": "swatches"})).unwrap(); - let layout = h.layout(); - let window = window_of(&layout, "swatches"); - for panel in ["color", "gradient", "stroke"] { - assert!(window.contains(&panel.to_string()), "{panel} in the window: {window:?}"); - } - assert_eq!(group_of(&layout, &"color".into()), vec!["color".to_string()], "a group of its own below"); - assert_eq!(group_of(&layout, &"gradient".into()), vec!["gradient".to_string()]); - h.frame(vec![]); - let (top, middle, bottom) = (h.tab("gradient").unwrap(), h.tab("swatches").unwrap(), h.tab("color").unwrap()); - assert!(top.top() < middle.top() && middle.top() < bottom.top(), "gradient above, color below"); - - // `onto` names a floating group; a docked one is refused. - assert!(h.app.run("window.panel.float", json!({"panel": "stroke", "onto": "layers"})).is_err()); -} - -#[test] -fn float_collapsed_minimizes_its_window() { - let mut h = Harness::new(); - h.app.run("window.panel.float", json!({"panel": "swatches", "collapsed": true})).unwrap(); - assert!(floating_window(&h.layout(), "swatches").unwrap().minimized); - h.app.run("window.panel.float", json!({"panel": "swatches", "collapsed": false})).unwrap(); - assert!(!floating_window(&h.layout(), "swatches").unwrap().minimized); - let r = h.app.run("window.panel.collapse", json!({"panel": "Layers"})).unwrap(); - assert_eq!((r["panel"].clone(), r["collapsed"].clone()), (json!("layers"), json!(true)), "a docked group minimizes too"); -} - -#[test] -fn dock_column_n_docks_a_column_at_that_place() { - let mut h = Harness::new(); - let before = end_columns(&h.layout()).len(); - h.app.run("window.panel.float", json!({"panel": "swatches"})).unwrap(); - h.app.run("window.panel.float", json!({"panel": "color", "below": "swatches"})).unwrap(); - h.app.run("window.panel.dock", json!({"panel": "swatches", "column": 1})).unwrap(); - let layout = h.layout(); - let columns = end_columns(&layout); - assert_eq!(columns.len(), before + 1); - let panels: Vec<&String> = columns[2].groups.iter().flat_map(|g| &g.panels).collect(); - assert_eq!(panels, ["swatches", "color"], "the whole floating window, next to the icon column"); - assert!(layout.floating.is_empty()); - - // `true` docks at the innermost place; without `group`, the panel leaves its group. - h.app.run("window.panel.dock", json!({"panel": "layers", "column": true})).unwrap(); - let layout = h.layout(); - let columns = end_columns(&layout); - assert_eq!(columns.len(), before + 2); - assert_eq!(columns.last().unwrap().groups[0].panels, vec!["layers".to_string()]); - assert_eq!(main_panels(&layout), ["properties", "libraries"]); -} - -#[test] -fn dock_icon_puts_a_panel_in_the_icon_column() { - let mut h = Harness::new(); - h.app.run("window.panel.dock", json!({"panel": "layers", "icon": true})).unwrap(); - let layout = h.layout(); - assert!(in_iconic_column(&layout, "layers")); - assert!(!main_panels(&layout).contains(&"layers".to_string())); - assert_eq!(group_of(&layout, &"layers".into()), vec!["layers".to_string()], "a row of its own"); -} - -fn column_panels(layout: &Layout) -> Vec> { - end_columns(layout).iter().map(|c| c.groups.iter().flat_map(|g| g.panels.clone()).collect()).collect() -} - -#[test] -fn a_docked_column_moves_to_a_later_place_and_back() { - let mut h = Harness::new(); - h.app.run("window.panel.dock", json!({"panel": "swatches", "column": 1})).unwrap(); - h.app.run("window.panel.dock", json!({"panel": "color", "column": 2})).unwrap(); - let columns = column_panels(&h.layout()); - assert_eq!(columns[2..], [vec!["swatches".to_string()], vec!["color".to_string()]]); - h.app.run("window.panel.dock", json!({"panel": "swatches", "column": 2})).unwrap(); - let columns = column_panels(&h.layout()); - assert_eq!(columns[2..], [vec!["color".to_string()], vec!["swatches".to_string()]], "swatches moved inwards"); - h.app.run("window.panel.dock", json!({"panel": "swatches", "column": 1})).unwrap(); - let columns = column_panels(&h.layout()); - assert_eq!(columns[2..], [vec!["swatches".to_string()], vec!["color".to_string()]], "and back"); - let before = h.layout(); - h.app.run("window.panel.dock", json!({"panel": "swatches", "column": 1})).unwrap(); - assert_eq!(h.layout(), before, "already there"); -} - -/// A layout converted from an old dock split side by side, with the dock collapsed: two -/// collapsed tree columns before the icon column. -#[test] -fn column_places_and_icon_count_from_the_icon_column() { - let mut h = Harness::new(); - let mut layout = h.layout(); - if let Some(Node::Split { second, .. }) = layout.root.as_mut() - && let Node::Dock { columns, .. } = second.as_mut() - { - let libraries = columns[0].groups[0].panels.pop().unwrap(); - assert_eq!(libraries, "libraries"); - columns.insert(1, Column { groups: vec![Group::new(vec![libraries], 0)], width: 200.0, iconic: true, iconic_width: Some(ICON_WIDTH) }); - columns[0].iconic = true; - } - let width: f32 = end_columns(&layout).iter().map(|c| c.iconic_width.unwrap_or(ICON_WIDTH)).sum(); - if let Some(Node::Split { size, .. }) = layout.root.as_mut() { - *size = craft_ui::layout::SplitSize::FixedSecond(width); - } - assert!(valid(&layout)); - h.app.ui.docking = Some(layout); - h.app.run("window.panel.dock", json!({"panel": "layers", "icon": true})).unwrap(); - let layout = h.layout(); - let columns = column_panels(&layout); - assert!(columns[2].contains(&"layers".to_string()), "{columns:?}"); - h.app.run("window.panel.dock", json!({"panel": "swatches", "column": 1})).unwrap(); - let columns = column_panels(&h.layout()); - assert_eq!(columns[..2], [vec!["properties".to_string()], vec!["libraries".to_string()]]); - assert_eq!(columns[3], ["swatches"], "next to the icon column: {columns:?}"); - - // No icon column: `icon` makes one. - let mut layout = h.layout(); - if let Some(Node::Split { second, .. }) = layout.root.as_mut() - && let Node::Dock { columns, .. } = second.as_mut() - { - columns.remove(2); - } - h.app.ui.docking = Some(layout); - assert_eq!(icon_column(&end_columns(&h.layout())), None); - h.app.run("window.panel.dock", json!({"panel": "properties", "icon": true})).unwrap(); - let layout = h.layout(); - assert!(in_iconic_column(&layout, "properties")); - assert!(icon_column(&end_columns(&layout)).is_none_or(|k| column_panels(&layout)[k].contains(&"properties".to_string()))); -} - #[test] fn invalid_docking_parameters_change_nothing() { let mut h = Harness::new(); @@ -1345,7 +633,7 @@ fn invalid_docking_parameters_change_nothing() { } #[test] -fn ui_docking_reports_the_layout_and_hit_rects() { +fn ui_docking_reports_the_layout_and_the_dock_state() { let mut h = Harness::new(); h.app.run("window.panel.float", json!({"panel": "swatches"})).unwrap(); h.frame(vec![]); @@ -1354,68 +642,20 @@ fn ui_docking_reports_the_layout_and_hit_rects() { let report = inspect(&h.app, &h.ctx); assert_eq!(report["layout"], serde_json::to_value(h.layout()).unwrap()); assert_eq!(report["focused"], json!("layers")); - let hits = report["hitRects"].as_array().unwrap(); - for hit in [json!({"Tab": "layers"}), json!({"Title": "swatches"})] { - let found = hits.iter().find(|h| h["hit"] == hit).unwrap_or_else(|| panic!("{hit} in {hits:?}")); - assert_eq!(found["rect"].as_array().unwrap().len(), 4); + for key in ["flyout", "hidden", "columns", "windows", "peek", "panelsShown"] { + assert!(report.get(key).is_some(), "{key} in the report"); } -} - -/// Press on the main column's divider and drag it 60 pt toward the canvas, holding the button. -fn press_on_the_main_column_divider(h: &mut Harness) -> f32 { - let edge = - hit_rects(&h.ctx).into_iter().filter(|(hit, _)| matches!(hit, Hit::Divider(_))).map(|(_, r)| r.center().x).fold(f32::NEG_INFINITY, f32::max); - h.press_and_move(pos2(edge, 600.0), pos2(edge - 60.0, 600.0)); - dock_columns(&h.layout())[0].width -} - -#[test] -fn a_column_resize_whose_release_was_lost_ends_when_the_system_reports_no_button_down() { - let mut h = Harness::new(); - let width = press_on_the_main_column_divider(&mut h); - for x in [900.0, 1000.0, 850.0, 1100.0] { - h.frame_with_system_buttons(vec![Event::PointerMoved(pos2(x, 600.0))], false); - assert_eq!(dock_columns(&h.layout())[0].width, width, "the column followed the pointer to {x}"); - } - assert!(!h.ctx.input(|i| i.pointer.any_down())); -} - -#[test] -fn a_column_resize_continues_while_the_system_reports_the_button_down() { - let mut h = Harness::new(); - let width = press_on_the_main_column_divider(&mut h); - h.frame_with_system_buttons(vec![Event::PointerMoved(pos2(900.0, 600.0))], true); - assert_ne!(dock_columns(&h.layout())[0].width, width); - assert!(h.ctx.input(|i| i.pointer.primary_down())); -} - -#[test] -fn a_lost_release_after_the_pointer_moved_away_is_not_a_click() { - let mut h = Harness::new(); - h.frame(vec![Event::PointerMoved(pos2(800.0, 600.0)), button(pos2(800.0, 600.0), true)]); - h.frame_with_system_buttons(vec![Event::PointerMoved(pos2(1000.0, 600.0))], false); - assert!(!h.ctx.input(|i| i.pointer.any_down())); - assert!(!h.ctx.input(|i| i.pointer.any_click()), "the release at the press position made a click"); -} - -#[test] -fn control_channel_drags_keep_their_button_between_steps() { - let mut h = Harness::new(); - h.frame(vec![Event::PointerMoved(pos2(800.0, 600.0)), button(pos2(800.0, 600.0), true)]); - h.app.synthetic.push(button(pos2(820.0, 600.0), false)); - let mut input = egui::RawInput { events: vec![Event::PointerMoved(pos2(810.0, 600.0))], ..Default::default() }; - h.app.release_lost_buttons(&h.ctx, &mut input, false); - assert_eq!(input.events, [Event::PointerMoved(pos2(810.0, 600.0))]); + assert!(report.get("hitRects").is_none() && report.get("customized").is_none(), "the dock-only hit list is gone"); + assert!(report["columns"].as_array().is_some_and(|columns| !columns.is_empty()), "the columns follow the layout"); + assert_eq!(report["panelsShown"], json!(panels_shown(&h.app))); } #[test] fn window_menu_panels_without_a_place_open_in_their_icon_group_or_float() { let mut h = Harness::new(); // A saved layout that lost Swatches without remembering where it was. - let mut layout = h.layout(); - layout.apply(Action::Close { panel: "swatches".into() }).unwrap(); - h.app.ui.docking = Some(layout); - h.app.ui.docking_hidden.clear(); + h.app.ui.dock_workspace.layout.apply(Action::close("swatches".into())).unwrap(); + h.app.ui.dock_workspace.hidden.clear(); h.app.run("window.panel", json!({"panel": "Swatches"})).unwrap(); let layout = h.layout(); assert!(in_iconic_column(&layout, "swatches"), "back in the icon column"); @@ -1429,105 +669,15 @@ fn window_menu_panels_without_a_place_open_in_their_icon_group_or_float() { assert!(h.tab("openType").is_some()); } -#[test] -fn f7_hides_and_restores_the_expanded_layers_tab() { - let mut h = Harness::new(); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert!(showing(&h.app.ui, "layers"), "the first press activates the tab"); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert!(!showing(&h.app.ui, "layers"), "a second press hides the active tab"); - assert!(h.app.ui.docking_hidden.contains_key("layers")); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert!(showing(&h.app.ui, "layers"), "the next press restores it"); - assert_eq!(main_panels(&h.layout()), ["properties", "layers", "libraries"]); - - h.app.run("window.panel.collapse", json!({"panel": "layers", "collapsed": true})).unwrap(); - let layout = h.layout(); - let group = dock_columns(&layout).iter().flat_map(|c| &c.groups).find(|g| g.panels.contains(&"layers".to_string())).unwrap(); - assert!(group.minimized); - h.press(egui::Key::F7, egui::Modifiers::NONE); - let layout = h.layout(); - let group = dock_columns(&layout).iter().flat_map(|c| &c.groups).find(|g| g.panels.contains(&"layers".to_string())).unwrap(); - assert!(!group.minimized, "the first press expands a collapsed group"); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert!(!showing(&h.app.ui, "layers"), "the following press hides the expanded group"); -} - -#[test] -fn f7_activates_a_background_tab_before_hiding_it() { - let mut h = Harness::new(); - h.app.run("window.panel.move", json!({"panel": "layers", "anchor": "properties"})).unwrap(); - h.app.run("window.panel.activate", json!({"panel": "properties"})).unwrap(); - let before = group_of(&h.layout(), &"layers".into()); - assert!(!showing(&h.app.ui, "layers")); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert!(showing(&h.app.ui, "layers")); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert!(["properties", "layers", "libraries"].iter().all(|panel| !h.layout().contains(&panel.to_string()))); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert!(showing(&h.app.ui, "layers")); - assert_eq!(group_of(&h.layout(), &"layers".into()), before); -} - -#[test] -fn f7_hides_and_restores_the_whole_expanded_group() { - let mut h = Harness::new(); - h.press(egui::Key::F7, egui::Modifiers::NONE); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert!(["properties", "layers", "libraries"].iter().all(|panel| !h.layout().contains(&panel.to_string()))); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert_eq!(group_of(&h.layout(), &"layers".into()), ["properties", "layers", "libraries"]); - assert!(showing(&h.app.ui, "layers")); - h.press(egui::Key::F7, egui::Modifiers::NONE); - h.app.run("window.panel", json!({"panel": "properties", "toggle": true})).unwrap(); - assert_eq!(group_of(&h.layout(), &"properties".into()), ["properties", "layers", "libraries"]); - assert!(showing(&h.app.ui, "properties")); -} - -/// A tool's request for the Transform panel (a double-click on a shape's corner widget) shows and -/// selects it wherever it is: closed, floating, behind another tab, in an icon column or in a dock -/// hidden by Tab. Asking again keeps it on show. -#[test] -fn a_tools_panel_request_shows_the_panel_and_a_second_keeps_it_shown() { - let mut h = Harness::new(); - let ask_twice = |h: &mut Harness, case: &str| { - for n in 1..=2 { - crate::canvas::apply_requests(&mut h.app, Ok(vec![vectorcraft_engine::UiRequest::Panel("transform".into())])); - h.frame(vec![]); - assert!(visible(&h.app.ui, "transform"), "{case}: request {n}"); - } - }; - if h.layout().contains(&"transform".to_string()) { - h.app.run("window.panel.close", json!({"panel": "transform"})).unwrap(); - } - assert!(!visible(&h.app.ui, "transform")); - ask_twice(&mut h, "closed"); - h.app.run("window.panel.float", json!({"panel": "transform"})).unwrap(); - h.frame(vec![]); - ask_twice(&mut h, "floating"); - h.app.run("window.panel.move", json!({"panel": "transform", "anchor": "properties"})).unwrap(); - h.app.run("window.panel.activate", json!({"panel": "properties"})).unwrap(); - h.frame(vec![]); - assert!(!visible(&h.app.ui, "transform")); - ask_twice(&mut h, "behind another tab"); - h.app.run("window.collapseDock", json!({"collapsed": true})).unwrap(); - h.frame(vec![]); - assert!(in_iconic_column(&h.layout(), "transform")); - ask_twice(&mut h, "in an icon column"); - h.app.ui.dock = false; - assert!(!visible(&h.app.ui, "transform")); - ask_twice(&mut h, "in a hidden dock"); -} - #[test] fn f7_restores_only_tabs_hidden_with_the_group() { let mut h = Harness::new(); h.app.run("window.panel.close", json!({"panel": "libraries"})).unwrap(); - assert!(h.app.ui.docking_hidden.contains_key("libraries")); - h.press(egui::Key::F7, egui::Modifiers::NONE); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert!(["properties", "layers"].iter().all(|panel| h.app.ui.docking_hidden.contains_key(*panel))); - h.press(egui::Key::F7, egui::Modifiers::NONE); + assert!(h.hidden("libraries")); + h.f7(); + h.f7(); + assert!(["properties", "layers"].iter().all(|panel| h.hidden(panel))); + h.f7(); assert_eq!(group_of(&h.layout(), &"layers".into()), ["properties", "layers"]); assert!(!h.layout().contains(&"libraries".to_string())); assert!(showing(&h.app.ui, "layers")); @@ -1536,43 +686,24 @@ fn f7_restores_only_tabs_hidden_with_the_group() { #[test] fn independently_reopened_tab_is_not_restored_by_the_group_key() { let mut h = Harness::new(); - h.press(egui::Key::F7, egui::Modifiers::NONE); + h.f7(); assert!(showing(&h.app.ui, "layers")); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert!(["properties", "layers", "libraries"].iter().all(|panel| h.app.ui.docking_hidden.contains_key(*panel))); - assert!(["properties", "layers", "libraries"].iter().all(|panel| !h.layout().contains(&panel.to_string()))); + h.f7(); + assert!(["properties", "layers", "libraries"].iter().all(|panel| h.hidden(panel))); + assert!(group_hidden_all(&h)); h.app.run("window.panel.activate", json!({"panel": "libraries"})).unwrap(); assert!(h.layout().contains(&"libraries".to_string())); - assert!(!h.app.ui.docking_hidden.contains_key("libraries")); - assert!(["properties", "layers"].iter().all(|panel| h.app.ui.docking_hidden.contains_key(*panel))); + assert!(!h.hidden("libraries")); + assert!(["properties", "layers"].iter().all(|panel| h.hidden(panel))); h.app.run("window.panel.close", json!({"panel": "libraries"})).unwrap(); - assert!(h.app.ui.docking_hidden.contains_key("libraries")); + assert!(h.hidden("libraries")); assert!(!h.layout().contains(&"libraries".to_string())); - h.press(egui::Key::F7, egui::Modifiers::NONE); + h.f7(); assert_eq!(group_of(&h.layout(), &"layers".into()), ["properties", "layers"]); assert!(!h.layout().contains(&"libraries".to_string())); assert!(showing(&h.app.ui, "layers")); } -#[test] -fn f7_hides_and_restores_a_floating_tab_group() { - let mut h = Harness::new(); - h.app.run("window.panel.float", json!({"panel": "layers", "group": true})).unwrap(); - h.app.run("window.panel.activate", json!({"panel": "layers"})).unwrap(); - h.frame(vec![]); - let before = h.layout(); - let window = floating_window(&before, "layers").unwrap(); - let rect = window.rect; - assert_eq!(group_of(&before, &"layers".into()), ["properties", "layers", "libraries"]); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert!(floating_window(&h.layout(), "layers").is_none()); - assert!(["properties", "layers", "libraries"].iter().all(|panel| !h.layout().contains(&panel.to_string()))); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert_eq!(floating_window(&h.layout(), "layers").unwrap().rect, rect); - assert_eq!(group_of(&h.layout(), &"layers".into()), ["properties", "layers", "libraries"]); - assert!(showing(&h.app.ui, "layers")); -} - #[test] fn showing_a_hidden_floating_group_returns_its_window_panels() { let mut h = Harness::new(); @@ -1580,157 +711,12 @@ fn showing_a_hidden_floating_group_returns_its_window_panels() { h.app.run("window.panel.float", json!({"panel": "layers", "group": true})).unwrap(); h.app.run("window.panel.activate", json!({"panel": "layers"})).unwrap(); h.frame(vec![]); - h.press(egui::Key::F7, egui::Modifiers::NONE); + h.f7(); assert!(floating_window(&h.layout(), "layers").is_none()); let reply = h.app.run("window.panel", json!({"panel": "layers"})).unwrap(); assert_eq!(reply, json!({"floating": ["properties", "layers", "libraries"]}), "only the restored window's panels"); } -#[test] -fn f7_toggles_a_multi_tab_icon_flyout() { - let mut h = Harness::new(); - h.app.run("window.collapseDock", json!({"collapsed": true})).unwrap(); - h.frame(vec![]); - assert!(in_iconic_column(&h.layout(), "layers")); - assert_eq!(group_of(&h.layout(), &"layers".into()), ["properties", "layers", "libraries"]); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert_eq!(h.app.ui.open_panel.as_deref(), Some("layers")); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert_eq!(h.app.ui.open_panel, None); - assert!(in_iconic_column(&h.layout(), "layers")); - assert_eq!(group_of(&h.layout(), &"layers".into()), ["properties", "layers", "libraries"]); -} - -#[test] -fn panel_function_keys_toggle_icon_flyouts_and_floating_groups() { - let mut h = Harness::new(); - h.press(egui::Key::F5, egui::Modifiers::NONE); - assert_eq!(h.app.ui.open_panel.as_deref(), Some("brushes")); - h.press(egui::Key::F5, egui::Modifiers::NONE); - assert_eq!(h.app.ui.open_panel, None); - h.press(egui::Key::F5, egui::Modifiers::NONE); - assert_eq!(h.app.ui.open_panel.as_deref(), Some("brushes")); - - h.app.run("window.panel.float", json!({"panel": "layers"})).unwrap(); - h.frame(vec![]); - let rect = floating_window(&h.layout(), "layers").unwrap().rect; - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert!(floating_window(&h.layout(), "layers").is_none()); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert_eq!(floating_window(&h.layout(), "layers").unwrap().rect, rect); - h.app.run("window.panel.dock", json!({"panel": "layers"})).unwrap(); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert!(!showing(&h.app.ui, "layers"), "the expanded dock also toggles"); -} - -/// Window › Layers: the menu item's command and params. -fn window_layers() -> Value { - json!({"panel": "layers", "toggle": true}) -} - -fn checked(h: &Harness, params: &Value) -> Option { - crate::menus::checked(&h.app, "window.panel", params) -} - -/// With the panels hidden by Tab, Window › Layers has no check mark, and choosing it shows the dock -/// with Layers where it was instead of closing the tab; the toolbar and the Control bar stay hidden -/// until the next Tab shows them. -#[test] -fn window_panel_from_tab_hidden_dock_reveals_named_front_tab() { - let mut h = Harness::new(); - let item = window_layers(); - h.app.run("window.panel", json!({"panel": "layers"})).unwrap(); - assert_eq!(checked(&h, &item), Some(true)); - let before = h.layout(); - h.app.ui.toolbar = true; - h.app.ui.control_bar = true; - h.app.run("window.dock", json!({})).unwrap(); - assert!(!h.app.ui.dock && !h.app.ui.toolbar && !h.app.ui.control_bar); - assert_eq!(checked(&h, &item), Some(false), "no check mark while Tab hides the panels"); - h.app.run("window.panel", item.clone()).unwrap(); - assert!(h.app.ui.dock && visible(&h.app.ui, "layers"), "Layers shows"); - assert_eq!(h.layout(), before, "in its place"); - assert!(!h.app.ui.toolbar && !h.app.ui.control_bar, "the toolbar and the Control bar stay hidden"); - assert_eq!(checked(&h, &item), Some(true)); - h.app.run("window.dock", json!({})).unwrap(); - assert!(h.app.ui.dock && h.app.ui.toolbar && h.app.ui.control_bar, "the next Tab shows the bars the first Tab hid"); - h.app.run("window.panel", item.clone()).unwrap(); - assert!(!h.layout().contains(&"layers".to_string()), "a visible panel closes"); - assert!(h.layout().contains(&"properties".to_string()), "only the panel named"); - assert_eq!(checked(&h, &item), Some(false)); -} - -/// With the panels hidden by Shift+Tab, Window › Layers and its plain command show a floating -/// Layers panel again where it was instead of closing it. -#[test] -fn window_panel_from_shift_tab_hidden_floating_window_reveals_it() { - let mut h = Harness::new(); - let item = window_layers(); - h.app.run("window.panel.float", json!({"panel": "layers"})).unwrap(); - h.frame(vec![]); - let rect = floating_window(&h.layout(), "layers").map(|w| w.rect); - assert!(rect.is_some()); - h.app.ui.toolbar = true; - h.app.ui.control_bar = true; - for params in [item.clone(), json!({"panel": "layers"})] { - h.app.run("window.panels", json!({})).unwrap(); - assert!(!h.app.ui.dock && h.app.ui.toolbar && h.app.ui.control_bar); - assert_eq!(checked(&h, &item), Some(false), "{params}: no check mark while Shift+Tab hides the panels"); - h.app.run("window.panel", params.clone()).unwrap(); - assert!(h.app.ui.dock && visible(&h.app.ui, "layers"), "{params}: Layers shows"); - assert_eq!(floating_window(&h.layout(), "layers").map(|w| w.rect), rect, "{params}: where it was"); - } -} - -/// While the dock is hidden, Window › Layers opens a flyout that was open in the icon column -/// instead of putting it away; a closed panel comes back and a background tab comes to the front. -/// With the dock showing, choosing the checked item again hides only that panel. -#[test] -fn window_panel_hidden_iconic_and_closed_cases() { - let mut h = Harness::new(); - let item = window_layers(); - h.app.run("window.collapseDock", json!({"collapsed": true})).unwrap(); - h.app.run("window.panel", item.clone()).unwrap(); - assert_eq!(h.app.ui.open_panel.as_deref(), Some("layers")); - assert_eq!(checked(&h, &item), Some(true)); - h.app.ui.dock = false; - assert_eq!(checked(&h, &item), Some(false), "no check mark with the dock hidden"); - h.app.run("window.panel", item.clone()).unwrap(); - assert!(h.app.ui.dock, "the dock shows"); - assert_eq!(h.app.ui.open_panel.as_deref(), Some("layers"), "the flyout stays open"); - assert_eq!(checked(&h, &item), Some(true)); - h.app.run("window.panel", item.clone()).unwrap(); - assert_eq!(h.app.ui.open_panel, None, "a visible flyout puts away"); - - let mut h = Harness::new(); - let libraries = json!({"panel": "libraries", "toggle": true}); - h.app.run("window.panel.close", json!({"panel": "libraries"})).unwrap(); - h.app.run("window.panel.activate", json!({"panel": "properties"})).unwrap(); - h.app.run("window.dock", json!({})).unwrap(); - assert_eq!((checked(&h, &libraries), checked(&h, &item)), (Some(false), Some(false))); - h.app.run("window.panel", libraries.clone()).unwrap(); - assert!(h.app.ui.dock && visible(&h.app.ui, "libraries"), "a closed panel comes back"); - h.app.run("window.panel", item.clone()).unwrap(); - assert!(visible(&h.app.ui, "layers") && !visible(&h.app.ui, "properties"), "a background tab comes to the front"); - assert_eq!((checked(&h, &libraries), checked(&h, &item)), (Some(false), Some(true)), "the tab in front is checked"); -} - -/// F7 with the panels hidden by Tab shows the dock with the Layers group instead of hiding the -/// group; the next F7 hides it. -#[test] -fn function_key_from_tab_hidden_dock_reveals_named_group() { - let mut h = Harness::new(); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert!(visible(&h.app.ui, "layers")); - let before = h.layout(); - h.app.run("window.dock", json!({})).unwrap(); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert!(h.app.ui.dock && visible(&h.app.ui, "layers"), "F7 shows the hidden group"); - assert_eq!(h.layout(), before); - h.press(egui::Key::F7, egui::Modifiers::NONE); - assert!(["properties", "layers", "libraries"].iter().all(|panel| !h.layout().contains(&panel.to_string())), "the next press hides the group"); -} - /// Presentation Mode hides every panel: no panel is checked, `window.panel` fails and changes /// nothing, and F7 is ignored. The other full screen modes show the panels as Normal does. #[test] @@ -1742,13 +728,14 @@ fn presentation_panel_command_reports_unavailable_without_mutating_layout() { h.app.run("view.presentation", json!({})).unwrap(); assert_eq!(h.app.ui.screen_mode, 3); assert_eq!(checked(&h, &item), Some(false), "no check mark in Presentation Mode"); - let before = (h.layout(), h.app.ui.dock, h.app.ui.open_panel.clone(), h.app.ui.docking_hidden.len()); + let state = |h: &Harness| (h.layout(), panels_shown(&h.app), h.app.ui.open_panel.clone(), h.app.ui.dock_workspace.hidden.len()); + let before = state(&h); for params in [item.clone(), json!({"panel": "layers"}), json!({"panel": "swatches"}), json!({"panel": "brushes"})] { let error = h.app.run("window.panel", params.clone()).unwrap_err(); assert_eq!(error, "panels can't be shown in Presentation Mode", "{params}"); - assert_eq!((h.layout(), h.app.ui.dock, h.app.ui.open_panel.clone(), h.app.ui.docking_hidden.len()), before, "{params} changed nothing"); + assert_eq!(state(&h), before, "{params} changed nothing"); } - h.press(egui::Key::F7, egui::Modifiers::NONE); + h.f7(); assert_eq!(h.app.ui.screen_mode, 3); assert_eq!(h.layout(), before.0, "F7 is ignored"); for mode in [1, 2] { @@ -1772,21 +759,22 @@ fn other_function_keys_and_keyboard_guards_keep_their_routes() { } h.app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap(); h.press(egui::Key::F8, egui::Modifiers::NONE); - assert_eq!(h.app.ui.dialog.as_ref().map(|d| d.kind.as_str()), Some("symbolOptions")); + assert!(h.app.dialogs().any_dialog_open(), "F8 opens the Symbol Options dialog"); crate::dialogs::confirm(&mut h.app).unwrap(); assert_eq!(h.app.session.doc().unwrap().doc.symbols.len(), 1); crate::menus::invoke(&mut h.app, "view.rotate", json!({})); - assert!(h.app.ui.dialog.is_some()); - h.press(egui::Key::F7, egui::Modifiers::NONE); + assert!(h.app.dialogs().any_dialog_open()); + h.f7(); assert!(showing(&h.app.ui, "layers"), "F7 does nothing while a dialog is open"); - h.app.ui.dialog = None; + // PLAN: assumed `Dialogs::close_all` (hub-w1 M10) closes whatever dialog is up. + h.app.dialogs().close_all(); let field = egui::Id::unique("panel-shortcut-text-focus"); h.ctx.memory_mut(|m| m.request_focus(field)); - h.press(egui::Key::F7, egui::Modifiers::NONE); + h.f7(); assert!(showing(&h.app.ui, "layers"), "F7 does nothing while a text field has focus"); h.ctx.memory_mut(|m| m.surrender_focus(field)); - h.press(egui::Key::F7, egui::Modifiers::NONE); + h.f7(); assert!(!showing(&h.app.ui, "layers"), "F7 toggles when the field releases focus"); let dir = vectorcraft_testkit::temp_dir("panel-shortcut-revert"); @@ -1807,166 +795,23 @@ fn other_function_keys_and_keyboard_guards_keep_their_routes() { ctx.run_ui(egui::RawInput { events: vec![key], ..Default::default() }, |ui| crate::shortcuts::handle(&mut saved, ui.ctx())) .textures_delta .clear(); - assert_eq!(saved.ui.dialog.as_ref().map(|d| d.kind.as_str()), Some(crate::dialogs::confirm::KIND)); + assert!(saved.dialogs().any_dialog_open(), "F12 asks before reverting"); crate::dialogs::confirm(&mut saved).unwrap(); assert_eq!(saved.session.doc().unwrap().doc.node_count(), 2, "F12 reloads the saved document"); std::fs::remove_dir_all(dir).unwrap(); } -fn collapse_to_icons_button(h: &Harness, panel: &str) -> Option { - let panel = panel.to_string(); - h.ctx.read_response(area_id().with(("header", &panel)).with(("collapse-to-icons", &panel))).map(|r| r.rect) -} - +/// Each panel with display sizes steps through them and the smallest size goes back to the largest. #[test] -fn floating_collapse_arrow_moves_the_whole_group_into_the_icon_rail() { - let mut h = Harness::new(); - assert!(collapse_to_icons_button(&h, "properties").is_none(), "docked headers have no extra button"); - h.app.run("window.panel.float", json!({"panel": "layers", "group": true})).unwrap(); - h.app.run("window.panel.activate", json!({"panel": "layers"})).unwrap(); - for _ in 0..3 { - h.frame(vec![]); - } - let before = h.layout(); - let members = group_of(&before, &"layers".into()); - let columns = end_columns(&before).len(); - let arrow = collapse_to_icons_button(&h, "layers").expect("floating header offers collapse to icons"); - assert!(arrow.width() <= 24.0 && arrow.height() <= 24.0, "a compact header control: {arrow:?}"); - h.click(arrow.center()); - let layout = h.layout(); - assert!(floating_window(&layout, "layers").is_none(), "the old floating window is removed"); - assert_eq!(group_of(&layout, &"layers".into()), members, "all tabs move together"); - assert!(members.iter().all(|p| in_iconic_column(&layout, p))); - assert_eq!(end_columns(&layout).len(), columns, "reuse the existing icon rail"); - assert!(h.app.ui.open_panel.is_none(), "the group stays collapsed until its icon is clicked"); - let icon = h.hit(&Hit::Icon("layers".into())).expect("Layers now has a rail icon"); - h.click(icon.center()); - assert_eq!(h.app.ui.open_panel.as_deref(), Some("layers")); - assert!(h.tab("layers").is_some(), "the collapsed group can reopen as a flyout"); - assert!(collapse_to_icons_button(&h, "layers").is_some(), "the reopened flyout also offers collapse to icons"); -} - -#[test] -fn floating_collapse_arrow_leaves_other_floating_groups_alone() { - let mut h = Harness::new(); - h.app.run("window.panel.float", json!({"panel": "layers"})).unwrap(); - h.app.run("window.panel.float", json!({"panel": "color", "below": "layers"})).unwrap(); - for _ in 0..3 { - h.frame(vec![]); - } - let arrow = collapse_to_icons_button(&h, "layers").expect("floating group has its own arrow"); - h.click(arrow.center()); - let layout = h.layout(); - assert!(in_iconic_column(&layout, "layers")); - assert!(floating_window(&layout, "color").is_some(), "another group in the same window stays floating"); - assert!(!in_iconic_column(&layout, "color")); - assert_eq!(main_panels(&layout), ["properties", "libraries"]); -} - -#[test] -fn floating_collapse_arrow_creates_a_rail_when_none_exists_and_keeps_the_menu() { - let mut h = Harness::new(); - h.app.run("window.panel.float", json!({"panel": "layers"})).unwrap(); - let mut layout = h.layout(); - if let Some(Node::Split { second, .. }) = layout.root.as_mut() - && let Node::Dock { columns, .. } = second.as_mut() - { - columns.retain(|column| !column.iconic); - } - assert!(icon_column(&end_columns(&layout)).is_none()); - h.app.ui.docking = Some(layout); - for _ in 0..3 { - h.frame(vec![]); - } - let panel = "layers".to_string(); - let menu = - h.ctx.read_response(area_id().with(("header", &panel)).with(("panel-menu", &panel))).expect("the existing panel menu remains available").rect; - let arrow = collapse_to_icons_button(&h, "layers").expect("floating group offers collapse"); - assert!(!menu.intersects(arrow), "menu and arrow have separate hit targets"); - h.click(menu.center()); - assert!(egui::Popup::is_any_open(&h.ctx), "the existing menu still opens"); - h.press(egui::Key::Escape, egui::Modifiers::NONE); - h.click(arrow.center()); - let layout = h.layout(); - assert!(in_iconic_column(&layout, "layers")); - assert!(floating_window(&layout, "layers").is_none()); - assert!(h.app.ui.open_panel.is_none(), "collapse does not immediately open a flyout"); - assert_eq!(main_panels(&layout), ["properties", "libraries"]); -} - -#[test] -fn rail_flyout_collapse_arrow_folds_the_panel_back_without_moving_it() { - let mut h = Harness::new(); - let icon = h.hit(&Hit::Icon("swatches".into())).unwrap(); - h.click(icon.center()); - let before = h.layout(); - let arrow = collapse_to_icons_button(&h, "swatches").expect("opened rail panels offer collapse to icons"); - h.click(arrow.center()); - assert!(h.app.ui.open_panel.is_none()); - assert!(craft_ui::docking::flyout::(&h.ctx, area_id()).is_none()); - assert_eq!(h.layout(), before, "folding a flyout leaves the dock layout intact"); - let icon = h.hit(&Hit::Icon("swatches".into())).expect("the rail icon stays available"); - h.click(icon.center()); - assert_eq!(h.app.ui.open_panel.as_deref(), Some("swatches")); - assert!(h.tab("swatches").is_some()); -} - -#[test] -fn rail_flyout_collapse_arrow_closes_the_opening_icon_after_switching_tabs() { - let mut h = Harness::new(); - h.app.ui.docking = Some(h.layout()); - let icon = h.hit(&Hit::Icon("swatches".into())).unwrap(); - h.click(icon.center()); - let brushes = h.tab("brushes").unwrap(); - h.click(brushes.center()); - assert_eq!(h.app.ui.open_panel.as_deref(), Some("swatches")); - let before = h.layout(); - let arrow = collapse_to_icons_button(&h, "brushes").expect("the active tab has a collapse arrow"); - h.click(arrow.center()); - assert!(h.app.ui.open_panel.is_none()); - assert!(craft_ui::docking::flyout::(&h.ctx, area_id()).is_none()); - assert_eq!(h.layout(), before, "all tabs and their active state stay in the rail"); -} - -/// The texts `panel`'s menu draws, in the harness's context. -fn panel_menu_texts(h: &mut Harness, panel: &str) -> Vec { - fn texts(s: &egui::Shape, out: &mut Vec) { - match s { - egui::Shape::Text(t) => out.push(t.galley.text().trim().to_string()), - egui::Shape::Vec(v) => v.iter().for_each(|s| texts(s, out)), - _ => {} - } - } - let app = &mut h.app; - let mut out = h.ctx.run_ui(egui::RawInput { screen_rect: Some(SCREEN), ..Default::default() }, |ui| { - crate::panels::panel_menu_items(app, ui, panel); - }); - out.textures_delta.clear(); - let mut v = vec![]; - out.shapes.iter().for_each(|c| texts(&c.shape, &mut v)); - v -} - -fn cycle_button(h: &Harness, panel: &str) -> Option { - let panel = panel.to_string(); - h.ctx.read_response(area_id().with(("header", &panel)).with(("cycle-display-sizes", &panel))).map(|r| r.rect) -} - -/// Each panel with display sizes steps between the states its panel menu's Show/Hide Options -/// item sets, and the smallest size goes back to the largest. -#[test] -fn cycle_options_steps_through_the_states_the_panel_menu_sets() { +fn cycle_options_steps_through_the_sizes_and_returns_to_the_largest() { let mut h = Harness::new(); for panel in ["align", "attributes", "character", "color", "gradient", "info", "paragraph", "stroke", "transform"] { - assert!(panel_menu_texts(&mut h, panel).iter().any(|t| t == "Hide Options"), "{panel} starts with its options"); let r = h.app.run("window.panel.cycleOptions", json!({"panel": panel})).unwrap(); assert_eq!(r, json!({"panel": panel, "size": 1, "sizes": 2}), "{panel}"); - assert!(panel_menu_texts(&mut h, panel).iter().any(|t| t == "Show Options"), "{panel} hides its options"); let r = h.app.run("window.panel.cycleOptions", json!({"panel": panel})).unwrap(); assert_eq!(r["size"], 0, "{panel}"); - assert!(panel_menu_texts(&mut h, panel).iter().any(|t| t == "Hide Options"), "{panel} shows them again"); } - // The Flattener Preview's options start hidden: its first step shows them, as Show Options does. + // The Flattener Preview's options start hidden: its first step shows them. assert!(!h.app.ui.flattener_preview.show_options); let r = h.app.run("window.panel.cycleOptions", json!({"panel": "Flattener Preview"})).unwrap(); assert_eq!(r, json!({"panel": crate::panels::flattener_preview::ID, "size": 0, "sizes": 2})); @@ -1974,20 +819,15 @@ fn cycle_options_steps_through_the_states_the_panel_menu_sets() { h.app.run("window.panel.cycleOptions", json!({"panel": crate::panels::flattener_preview::ID})).unwrap(); assert!(!h.app.ui.flattener_preview.show_options); // Transparency: thumbnails and options, thumbnails alone, neither, and back. - let shown = |h: &mut Harness| { - let t = panel_menu_texts(h, "transparency"); - (t.iter().any(|t| t == "Hide Thumbnails"), t.iter().any(|t| t == "Hide Options")) - }; - assert_eq!(shown(&mut h), (true, true)); - for (size, want) in [(1, (true, false)), (2, (false, false)), (0, (true, true))] { + for size in [1, 2, 0] { let r = h.app.run("window.panel.cycleOptions", json!({"panel": "transparency"})).unwrap(); assert_eq!((r["size"].clone(), r["sizes"].clone()), (json!(size), json!(3))); - assert_eq!(shown(&mut h), want, "size {size}"); } - // Options hidden with the thumbnails shown matches no size, so the next step is the largest. - crate::panels::set_pstate(&h.ctx, "tr-hide-thumbs", true); + // PLAN: assumed `panels::{part_hidden, set_part_hidden}(ctx, key[, hidden])` (hub-w2, op.A3). + // Thumbnails hidden with the options shown matches no size, so the next step is the largest. + crate::panels::set_part_hidden(&h.ctx, "tr-hide-thumbs", true); assert_eq!(h.app.run("window.panel.cycleOptions", json!({"panel": "transparency"})).unwrap()["size"], 0); - assert_eq!(shown(&mut h), (true, true)); + assert!(!crate::panels::part_hidden(&h.ctx, "tr-hide-thumbs"), "the largest size shows the thumbnails"); } /// An unknown panel, a panel without display sizes or a missing `panel` is an error, and the @@ -1995,6 +835,7 @@ fn cycle_options_steps_through_the_states_the_panel_menu_sets() { #[test] fn cycle_options_rejects_panels_without_sizes() { let mut h = Harness::new(); + let before = h.layout(); for (params, error) in [ (json!({"panel": "nope"}), "unknown panel `nope`"), (json!({}), "unknown panel ``"), @@ -2004,39 +845,5 @@ fn cycle_options_rejects_panels_without_sizes() { ] { assert_eq!(h.app.run("window.panel.cycleOptions", params.clone()).unwrap_err(), error, "{params}"); } - assert!(panel_menu_texts(&mut h, "stroke").iter().any(|t| t == "Hide Options")); - assert!(!h.app.ui.color_hide_options); -} - -/// The double triangle shows on the tab strip of a panel with display sizes, docked or floating, -/// and a click on it shows the panel's next size; other panels have none. -#[test] -fn the_double_triangle_on_the_tab_strip_cycles_the_display_sizes() { - let mut h = Harness::new(); - h.ctx.enable_accesskit(); - h.frame(vec![]); - assert!(cycle_button(&h, "properties").is_none() && cycle_button(&h, "layers").is_none(), "the main column's panels have no sizes"); - h.app.run("window.panel.float", json!({"panel": "stroke"})).unwrap(); - for _ in 0..3 { - h.frame(vec![]); - } - let control = cycle_button(&h, "stroke").expect("a floating Stroke panel has the double triangle"); - assert!(h.nodes.iter().any(|(_, node)| node.label() == Some("Cycle Display Sizes")), "screen readers name it"); - let menu = h.ctx.read_response(area_id().with(("header", &"stroke".to_string())).with(("panel-menu", &"stroke".to_string()))).unwrap().rect; - assert!(!menu.intersects(control), "the double triangle and the menu have separate hit targets"); - h.click(control.center()); - assert!(crate::panels::pstate::(&h.ctx, "stroke-hide-options"), "a click hides the options"); - assert!(floating_window(&h.layout(), "stroke").is_some(), "the panel stays where it is"); - h.click(control.center()); - assert!(!crate::panels::pstate::(&h.ctx, "stroke-hide-options"), "a second click shows them again"); - // A docked panel's tab strip has it too, and a floating panel without sizes does not. - h.app.run("window.panel.dock", json!({"panel": "color", "anchor": "layers", "zone": "below"})).unwrap(); - h.app.run("window.panel.float", json!({"panel": "swatches"})).unwrap(); - for _ in 0..3 { - h.frame(vec![]); - } - let control = cycle_button(&h, "color").expect("a docked Color panel has the double triangle"); - h.click(control.center()); - assert!(h.app.ui.color_hide_options); - assert!(cycle_button(&h, "swatches").is_none()); + assert_eq!(h.layout(), before, "a refusal changes nothing"); }