diff --git a/assets/icons/ui/source/navigation.rs b/assets/icons/ui/source/navigation.rs index 54e6ca980..7192a1814 100644 --- a/assets/icons/ui/source/navigation.rs +++ b/assets/icons/ui/source/navigation.rs @@ -148,7 +148,8 @@ pub(crate) fn curved_arrow(painter: &egui::Painter, at: Pos2, clockwise: bool, h arrow(painter, *tip, vec2(-end.sin(), end.cos()) * (sign * scale), hover, t); } } -pub(crate) const AXIS_RADIUS: f32 = 11.0; +/// The pointer target around a compass end: larger than the drawn disc, so it is easy to hit. +pub(crate) const AXIS_RADIUS: f32 = 15.0; pub(crate) fn axis_tip(painter: &egui::Painter, c: Pos2, name: &str, d: &Vec3, at: Pos2, depth: f64, hovered: bool, t: Tokens) { let col = if d.x != 0.0 { t.triad[0] @@ -158,9 +159,56 @@ pub(crate) fn axis_tip(painter: &egui::Painter, c: Pos2, name: &str, d: &Vec3, a t.triad[2] }; let positive = !name.starts_with('-'); - let ink = if depth < -0.01 { col.gamma_multiply(0.55) } else { col }; - painter.line_segment([c, at], Stroke::new(if positive { 2.0 } else { 1.0 }, ink)); - painter.circle_filled(at, AXIS_RADIUS, if hovered { t.accent_soft } else { t.overlay }); - painter.circle_stroke(at, AXIS_RADIUS, Stroke::new(if hovered { 2.0 } else { 1.0 }, if hovered { t.accent } else { ink })); - painter.text(at, Align2::CENTER_CENTER, name.to_uppercase(), FontId::proportional(11.0), ink); + let light = t.panel.r() > 128; + // Positive shafts are the full axis colour; negative ones are a dimmed tint (and any end + // pointing away from the viewer dims further). + let ink = if !positive || depth < -0.01 { col.gamma_multiply(0.55) } else { col }; + let radius = if positive { 11.0 } else { 4.8 }; + let direction = (at - c).normalized(); + let width = 3.6; + // A negative shaft stops at the outer edge of its ring; a positive one runs under its disc. + let end = if positive { at - direction * radius } else { at - direction * (radius + 1.2) }; + painter.line_segment([c, end], Stroke::new(width, ink)); + // The round cap sits at the origin; the other end is covered by the disc or ring. + painter.circle_filled(c, width / 2.0, ink); + if positive { + painter.circle_filled(at, radius, col); + } + // Negative tips are hollow: neither the axis shaft nor a fill covers their + // centres, so the translucent compass background shows through. A hovered tip of either + // kind turns fully white. + if hovered { + // Same outer size as the resting tip: a ring's outline is half its stroke outside. + painter.circle_filled(at, if positive { radius } else { radius + 1.2 }, t.on_accent); + // On the light theme a white disc vanishes into the compass, so it carries an outline in + // the axis colour, drawn inside the disc so it does not grow. + if light && positive { + painter.circle_stroke(at, radius - 1.0, Stroke::new(2.0, col)); + } + } + if positive { + // The letters are heavy: egui's regular face gets a small overstrike for weight. On a + // hovered (white) disc the letter takes the axis colour. + let letter = if hovered { col } else { t.on_accent }; + let label = name.to_uppercase(); + for dx in [-0.45, 0.45] { + painter.text(at + vec2(dx, 0.0), Align2::CENTER_CENTER, &label, FontId::proportional(13.5), letter); + } + } + if !positive { + painter.circle_stroke( + at, + radius, + Stroke::new( + 2.4, + if !hovered { + ink + } else if light { + col + } else { + t.on_accent + }, + ), + ); + } } diff --git a/crates/ui-egui/src/sketch3d.rs b/crates/ui-egui/src/sketch3d.rs index 086ba813f..b4ab561d7 100644 --- a/crates/ui-egui/src/sketch3d.rs +++ b/crates/ui-egui/src/sketch3d.rs @@ -197,7 +197,7 @@ pub fn preview(app: &SolveApp, t: &Tool, painter: &egui::Painter, proj: &Proj) - return false; } let tk = Tokens::get(); - let stroke = Stroke::new(1.5, tk.rubber_band); + let stroke = Stroke::new(1.5, tk.sketch_accent); let pts = if CHAIN.with(|c| c.borrow().0) == Some(t.cmd) { chain() } else { Vec::new() }; let hover = HOVER.with(|h| *h.borrow()); let mut all: Vec = pts.iter().filter_map(|p| proj.to_screen(*p)).collect(); diff --git a/crates/ui-egui/src/sketch3d_frame_ui_tests.rs b/crates/ui-egui/src/sketch3d_frame_ui_tests.rs index 7662c6720..a89e9aba0 100644 --- a/crates/ui-egui/src/sketch3d_frame_ui_tests.rs +++ b/crates/ui-egui/src/sketch3d_frame_ui_tests.rs @@ -151,6 +151,10 @@ fn second_occurrence_drawn_wire_fit_picks_and_world_triad_drag_stay_aligned() { h.call("ui.view", json!({"view":"fit"})); h.app.start("sketch.line"); h.frames(2); + // The clickable Line instruction toast covers the fitted lower endpoint. This scenario + // tests model picks, so dismiss the transient instruction before clicking through that area. + h.app.status = None; + h.frames(2); for world in [Vec3::new(17.0, 32.0, 45.0), Vec3::new(17.0, 32.0, 55.0)] { let [x, y] = h.at(&json!({"world":[world.x,world.y,world.z]})).unwrap(); let proj = crate::viewport::projection(&h.app, h.app.viewport.rect.unwrap()); diff --git a/crates/ui-egui/src/sketch_tools.rs b/crates/ui-egui/src/sketch_tools.rs index 896561d6c..47555ee4d 100644 --- a/crates/ui-egui/src/sketch_tools.rs +++ b/crates/ui-egui/src/sketch_tools.rs @@ -1080,7 +1080,7 @@ pub fn preview(app: &SolveApp, t: &Tool, painter: &egui::Painter, proj: &Proj) { // In the error colour, thicker than a hover: it is what the click deletes. crate::tools::paint_sketch_path(&world, 0, painter, proj, Stroke::new(4.5, tk.error)); } - let stroke = Stroke::new(1.5, tk.rubber_band); + let stroke = Stroke::new(1.5, tk.sketch_accent); // A Trim drag: the pieces it will remove, then the path drawn so far. if t.cmd == "sketch.trim" && !t.pts.is_empty() { TRIM_REMOVED.with(|r| { @@ -1098,7 +1098,7 @@ pub fn preview(app: &SolveApp, t: &Tool, painter: &egui::Painter, proj: &Proj) { { painter.circle_stroke(sp, if snap.is_some() { 6.0 } else { 3.0 }, stroke); } - let faint = Stroke::new(1.0, tk.rubber_band.gamma_multiply(0.6)); + let faint = Stroke::new(1.0, tk.sketch_accent.gamma_multiply(0.6)); let mode = crate::sketch_palette::mode(app); for w in t.pts.windows(2) { crate::tools::paint_sketch_path(&[ss.plane.to_world(w[0].0), ss.plane.to_world(w[1].0)], mode, painter, proj, faint); diff --git a/crates/ui-egui/src/theme.rs b/crates/ui-egui/src/theme.rs index b3f62ce06..54e3a70ce 100644 --- a/crates/ui-egui/src/theme.rs +++ b/crates/ui-egui/src/theme.rs @@ -71,6 +71,7 @@ impl TabAccent { let accent = Self::for_tab(&Tokens::get(), &app.ui.tab); ctx.data_mut(|d| { d.insert_temp(egui::Id::new("sc_tab_accent"), accent); + d.insert_temp(egui::Id::new("sc_sketch_open"), app.session.active_sketch.is_some()); d.insert_temp(egui::Id::new("sc_tool_accent"), app.tool.is_some() || app.dialog.is_some()); }); } @@ -78,10 +79,11 @@ impl TabAccent { /// Paint only the focused numeric editor, without changing sibling buttons or checkboxes. pub fn number_focus(ui: &egui::Ui, response: &egui::Response) { let active = ui.ctx().data(|d| d.get_temp::(egui::Id::new("sc_tool_accent"))).unwrap_or(false); - let accent = Self::current(ui.ctx()); let t = Tokens::get(); - if active && response.has_focus() && accent.color != t.accent { - ui.painter().rect_stroke(response.rect, t.radius_sm, Stroke::new(1.0, accent.color), egui::StrokeKind::Inside); + let sketch = ui.ctx().data(|d| d.get_temp::(egui::Id::new("sc_sketch_open"))).unwrap_or(false); + let color = if sketch { t.sketch_accent } else { Self::current(ui.ctx()).color }; + if active && response.has_focus() && color != t.accent { + ui.painter().rect_stroke(response.rect, t.radius_sm, Stroke::new(1.0, color), egui::StrokeKind::Inside); } } } diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rs index 31e413f26..70c1a7387 100644 --- a/crates/ui-egui/src/tools.rs +++ b/crates/ui-egui/src/tools.rs @@ -850,7 +850,7 @@ pub fn preview(app: &SolveApp, t: &Tool, painter: &egui::Painter, proj: &Proj) { let Some(ss) = st.sketch(sid) else { return }; let to = |p: Vec2| proj.to_screen(ss.plane.to_world(p)); let tk = crate::theme::Tokens::get(); - let stroke = Stroke::new(1.5, tk.rubber_band); + let stroke = Stroke::new(1.5, tk.sketch_accent); if let Some((hp, snap)) = &t.hover && let Some(sp) = to(*hp) { diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 3e04865cf..e4c413c2c 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -20,6 +20,10 @@ use crate::{SolveApp, icons}; #[path = "viewport_appearance_tests.rs"] mod appearance_tests; +#[cfg(test)] +#[path = "viewport_overlays_tests.rs"] +mod overlays_tests; + #[cfg(test)] #[path = "viewport_removed_later_tests.rs"] mod removed_later_tests; @@ -2957,16 +2961,7 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) { ); } if let Some(bx) = app.viewport.boxsel { - let col = if bx.crossing { t.box_crossing } else { t.box_window }; - painter.rect_filled(bx.rect, 0.0, col.gamma_multiply(0.12)); - if bx.crossing { - let c = [bx.rect.left_top(), bx.rect.right_top(), bx.rect.right_bottom(), bx.rect.left_bottom()]; - for i in 0..4 { - painter.add(Shape::dashed_line(&[c[i], c[(i + 1) % 4]], Stroke::new(1.2, col), 6.0, 4.0)); - } - } else { - painter.rect_stroke(bx.rect, 0.0, Stroke::new(1.2, col), egui::StrokeKind::Inside); - } + paint_box_selection(&painter, bx, app.session.active_sketch.is_some()); } if let Some(tl) = app.tool.as_ref() { crate::tools::preview(app, tl, &painter, &proj); @@ -2977,37 +2972,94 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) { crate::select_other::show(app, ui.ctx()); axis_view(app, ui, rect); nav_bar(app, ui, rect); - // A command recomputing the history off the window thread (the marker moved, undo): the - // window goes on, and says so above the status chip. - if let Some(since) = app.computing_since() { - let seconds = ((crate::now_ms() - since) / 1000.0).floor().max(0.0); - let text = crate::i18n::format("dialog.computing", &[("seconds", &format!("{seconds:.0}"))]); - let pos = pos2(crate::shell::content_rect(app, rect).left() + 12.0, crate::shell::content_rect(app, rect).bottom() - 76.0); - let galley = painter.layout_no_wrap(text, FontId::proportional(t.font_label), t.text); - let r = Rect::from_min_size(pos, galley.size() + vec2(34.0, 8.0)); - painter.rect_filled(r, t.radius_sm, t.overlay); - egui::Spinner::new().size(12.0).paint_at(ui, Rect::from_center_size(pos2(r.left() + 13.0, r.center().y), vec2(12.0, 12.0))); - painter.galley(pos + vec2(26.0, 4.0), galley, t.text); - ui.ctx().request_repaint_after(std::time::Duration::from_millis(100)); + transient_messages(app, ui, rect); +} + +fn paint_box_selection(painter: &egui::Painter, bx: BoxSel, sketch: bool) { + let t = Tokens::get(); + let col = if sketch { + t.sketch_accent + } else if bx.crossing { + t.box_crossing + } else { + t.box_window + }; + painter.rect_filled(bx.rect, 0.0, col.gamma_multiply(0.12)); + if bx.crossing { + let c = [bx.rect.left_top(), bx.rect.right_top(), bx.rect.right_bottom(), bx.rect.left_bottom()]; + for i in 0..4 { + painter.add(Shape::dashed_line(&[c[i], c[(i + 1) % 4]], Stroke::new(1.2, col), 6.0, 4.0)); + } + } else { + painter.rect_stroke(bx.rect, 0.0, Stroke::new(1.2, col), egui::StrokeKind::Inside); } - // Status chip. - if let Some((msg, at, err)) = app.status.clone() { +} + +/// A single visual pattern for ongoing work and the existing six-second status. +fn transient_messages(app: &mut SolveApp, ui: &egui::Ui, viewport: Rect) { + if app.status.is_none() && app.computing_since().is_none() { + return; + } + let t = Tokens::get(); + let mut obstacles = crate::shell::covered_rects(app); + if let Some(layout) = &app.viewport.layout { + obstacles.extend([layout.navigation, layout.axis_view]); + } + let mut free = crate::viewport_layout::free_rect(crate::shell::content_rect(app, viewport), &obstacles).shrink(t.space_8); + if let Some((msg, at, error)) = app.status.clone() { if crate::now_ms() - at < 6000.0 { - let pos = pos2(crate::shell::content_rect(app, rect).left() + 12.0, crate::shell::content_rect(app, rect).bottom() - 46.0); - let galley = painter.layout_no_wrap(msg, FontId::proportional(t.font_label), if err { t.error } else { t.text }); - let r = Rect::from_min_size(pos, galley.size() + vec2(16.0, 8.0)); - painter.rect_filled(r, t.radius_sm, t.overlay); - painter.galley(pos + vec2(8.0, 4.0), galley, t.text); - // A click opens every message of the run, to read and copy (#2763). - crate::scenario::publish_handle("status:chip", r.center()); - if ui.interact(r, egui::Id::new("sc_status_chip"), Sense::click()).on_hover_text(crate::i18n::tr("messages.chip_hint")).clicked() { - app.messages.open = true; + if let Some(r) = paint_toast(ui, free, msg, error, false) { + crate::scenario::publish_rect("toast", r); + free.max.y = r.top() - t.space_8; + // A click opens every message of the run, to read and copy (#2763). + crate::scenario::publish_handle("status:chip", r.center()); + if ui.interact(r, egui::Id::new("sc_status_chip"), Sense::click()).on_hover_text(crate::i18n::tr("messages.chip_hint")).clicked() { + app.messages.open = true; + } } ui.ctx().request_repaint_after(std::time::Duration::from_millis(500)); } else { app.status = None; } } + if let Some(since) = app.computing_since() { + let seconds = ((crate::now_ms() - since) / 1000.0).floor().max(0.0); + let text = crate::i18n::format("dialog.computing", &[("seconds", &format!("{seconds:.0}"))]); + if let Some(r) = paint_toast(ui, free, text, false, true) { + crate::scenario::publish_rect("toast:computing", r); + if app.status.is_none() { + crate::scenario::publish_rect("toast", r); + } + } + ui.ctx().request_repaint_after(std::time::Duration::from_millis(100)); + } +} + +fn paint_toast(ui: &egui::Ui, free: Rect, text: String, error: bool, computing: bool) -> Option { + if !free.is_finite() || !free.is_positive() || free.width() <= 40.0 { + return None; + } + let t = Tokens::get(); + let painter = ui.painter().with_clip_rect(free); + let galley = painter.layout(text, FontId::proportional(t.font_label), t.text, (free.width() - 40.0).min(440.0)); + let size = galley.size() + vec2(40.0, 16.0); + if !size.is_finite() || size.x > free.width() || size.y > free.height() { + return None; + } + let r = Rect::from_center_size(pos2(free.center().x, free.bottom() - size.y * 0.5), size); + if !r.is_finite() { + return None; + } + let frame = egui::Frame::NONE.fill(t.panel).stroke(Stroke::new(1.0, t.border)).corner_radius(t.radius_md).shadow(t.shell_shadow()); + ui.painter().add(frame.paint(r.shrink(frame.stroke.width))); + let dot = pos2(r.left() + 14.0, r.center().y); + if computing { + egui::Spinner::new().size(12.0).color(t.text).paint_at(ui, Rect::from_center_size(dot, vec2(12.0, 12.0))); + } else { + painter.circle_filled(dot, 3.0, if error { t.error } else { t.text }); + } + painter.galley(r.min + vec2(28.0, 8.0), galley, t.text); + Some(r) } /// A click without a command: select the candidate (replacing the selection, or adding / @@ -3528,9 +3580,15 @@ pub(crate) fn refresh_layout(app: &mut SolveApp, scale: f32) { /// Orientation controls share the layout reservation used by model fitting. fn axis_center(app: &SolveApp, rect: Rect) -> Pos2 { - axis_rect(app, rect).center() + let c = axis_rect(app, rect).center(); + // The compass sits a little higher in its reserved box (less margin above, the same on the + // right); the box itself is unchanged. + if app.preferences.navigator_style == crate::prefs::NavigatorStyle::Axes { c - vec2(0.0, AXIS_CENTER_LIFT) } else { c } } +/// How far the signed-axis compass is lifted inside its reserved box. +pub(crate) const AXIS_CENTER_LIFT: f32 = 4.0; + pub(crate) fn axis_rect(app: &SolveApp, rect: Rect) -> Rect { app.viewport.layout.as_ref().filter(|l| l.viewport == rect).map(|l| l.axis_view).unwrap_or_else(|| { let size = axis_view_size(app.preferences.navigator_style); @@ -3539,6 +3597,8 @@ pub(crate) fn axis_rect(app: &SolveApp, rect: Rect) -> Rect { } const AXIS_LENGTH: f64 = 38.0; +/// The artwork sits inside the pointer reach so the compass keeps breathing room in its circle. +const AXIS_ART_SCALE: f64 = 0.84; use crate::icons::navigation::AXIS_RADIUS; /// Six signed world axes, sorted back to front so overlapping tips have one owner. @@ -3546,7 +3606,9 @@ fn axis_tips(cam: &Camera, c: Pos2) -> Vec<(&'static str, Vec3, Pos2, f64)> { let (r, u, b) = cam.basis(); let mut tips: Vec<_> = [("x", Vec3::X), ("-x", -Vec3::X), ("y", Vec3::Y), ("-y", -Vec3::Y), ("z", Vec3::Z), ("-z", -Vec3::Z)] .into_iter() - .map(|(name, d)| (name, d, c + vec2((d.dot(r) * AXIS_LENGTH) as f32, -(d.dot(u) * AXIS_LENGTH) as f32), d.dot(b))) + .map(|(name, d)| { + (name, d, c + vec2((d.dot(r) * AXIS_LENGTH * AXIS_ART_SCALE) as f32, -(d.dot(u) * AXIS_LENGTH * AXIS_ART_SCALE) as f32), d.dot(b)) + }) .collect(); tips.sort_by(|a, b| a.3.total_cmp(&b.3)); tips @@ -3596,7 +3658,7 @@ pub(crate) fn axis_view(app: &mut SolveApp, ui: &mut egui::Ui, rect: Rect) { } fn signed_axis_view(app: &mut SolveApp, ui: &mut egui::Ui, rect: Rect) { - if !axis_rect(app, rect).is_positive() { + if !axis_rect(app, rect).is_positive() || !axis_rect(app, rect).is_finite() { app.viewport.axis_drag = None; crate::scenario::publish_count("axis_view_highlights", 0.0); return; @@ -3609,10 +3671,15 @@ fn signed_axis_view(app: &mut SolveApp, ui: &mut egui::Ui, rect: Rect) { let hover = ui.input(|i| i.pointer.hover_pos()).filter(|p| ui.is_enabled() && ui.rect_contains_pointer(rect) && !crate::shell::covered(app, *p)); let hovered = if orbiting { None } else { hover.and_then(|p| axis_hit(&tips, p)) }; let painter = ui.painter_at(rect); - if orbiting || hover.is_some_and(|p| axis_rect(app, rect).contains(p)) { - painter.circle_filled(c, 55.0, t.overlay); - } + // Anywhere in the compass circle drags the view; the circle turns fully opaque under the pointer. + let in_circle = hover.is_some_and(|p| p.distance(c) <= 49.5); + let fill = if in_circle || orbiting { 1.0 } else { 0.35 }; + painter.circle(c, 49.5, t.overlay.gamma_multiply(fill), Stroke::new(1.0, t.border.gamma_multiply(0.75))); for (i, (name, d, at, depth)) in tips.iter().enumerate() { + if i == 3 { + // The origin dot sits behind the three front axes and over the three back ones. + painter.circle(c, 3.6, t.text.gamma_multiply(0.9), Stroke::new(1.4, t.panel)); + } crate::icons::navigation::axis_tip(&painter, c, name, d, *at, *depth, hovered == Some(i), t); crate::scenario::publish_handle(&format!("axis_view:{name}"), *at); } @@ -3641,11 +3708,22 @@ fn signed_axis_view(app: &mut SolveApp, ui: &mut egui::Ui, rect: Rect) { } } } - let home = Rect::from_center_size(pos2(c.x - 44.0, c.y - 44.0), vec2(18.0, 18.0)); + let home = Rect::from_center_size(pos2(c.x - 37.0, c.y - 37.0), vec2(18.0, 18.0)); crate::scenario::publish_rect("axis_view:home", home); crate::scenario::publish_handle("axis_view:home", home.center()); let hr = ui.interact(home, ui.id().with("axis_view_home"), Sense::click()); + // The home button sits in its own small round chip. + painter.circle(home.center(), 12.0, t.overlay.gamma_multiply(if hr.hovered() { 0.9 } else { 0.5 }), Stroke::new(1.0, t.border)); icons::paint(&painter, home, "home", t.icon, if hr.hovered() { t.accent_soft } else { t.overlay }, t.accent); + // Cursors: a grabbing hand while orbiting, an open hand over the circle, a pointer on the home + // chip (the axis ends keep their pointing hand). + if hr.hovered() { + ui.ctx().set_cursor_icon(egui::CursorIcon::PointingHand); + } else if orbiting { + ui.ctx().set_cursor_icon(egui::CursorIcon::Grabbing); + } else if in_circle && hovered.is_none() { + ui.ctx().set_cursor_icon(egui::CursorIcon::Grab); + } if hr.on_hover_text(crate::i18n::tr_label("Home view")).clicked() { app.animate_view("home"); } @@ -4278,17 +4356,19 @@ mod tests { for mode in [0, 1, 2] { crate::sketch_palette::set_mode(&mut h.app, mode); let ctx = egui::Context::default(); - let output = ctx.run_ui(egui::RawInput::default(), |ui| { + let mut output = ctx.run_ui(egui::RawInput::default(), |ui| { let painter = ui.ctx().layer_painter(egui::LayerId::background()); crate::tools::preview(&h.app, &draw, &painter, &proj); }); + output.textures_delta.clear(); let preview: f32 = output.shapes.iter().map(|s| length(&s.shape)).sum(); let r = h.app.run("sketch.line", json!({"points":[[0,0],[20,8]]})).unwrap(); let id = r["curves"][0].as_str().unwrap().to_string(); h.app.viewport.hover = Some(Hit::SketchCurve { sketch: h.app.session.active_sketch.unwrap(), id, straight: true }); - let output = ctx.run_ui(egui::RawInput::default(), |ui| { + let mut output = ctx.run_ui(egui::RawInput::default(), |ui| { hover_highlight(&h.app, &ui.ctx().layer_painter(egui::LayerId::background()), &proj); }); + output.textures_delta.clear(); let hover: f32 = output.shapes.iter().map(|s| length(&s.shape)).sum(); assert!((preview - hover).abs() < 0.01, "mode {mode}: preview {preview}, hover {hover}"); if mode == 0 { @@ -6005,44 +6085,50 @@ mod tests { #[test] fn floating_dialog_blocks_axis_view_highlights_and_clicks() { - let mut h = crate::scenario::Harness::new(); - h.call("ui.resize", json!({"width": 800, "height": 700})); - let p = crate::scenario::handle_at("axis_view:z").unwrap(); - h.call("ui.move", json!({"x": p.x, "y": p.y})); - assert_eq!(crate::scenario::count_of("axis_view_highlights"), Some(1.0)); - h.call("ui.prefs", json!({"open": "Navigation"})); - let window = h.ctx.memory(|m| m.area_rect(egui::Id::new("sc_prefs"))).unwrap(); - assert!(window.contains(p), "the floating dialog must cover the axis target"); - let camera = serde_json::to_value(h.app.cam).unwrap(); - h.call("ui.move", json!({"x": p.x, "y": p.y})); - assert_eq!(crate::scenario::count_of("axis_view_highlights"), Some(0.0)); - h.call("ui.click", json!({"x": p.x, "y": p.y})); - assert_eq!(serde_json::to_value(h.app.cam).unwrap(), camera, "dialog clicks must not turn the axis view"); - h.call("ui.drag", json!({"x0": p.x, "y0": p.y, "x1": p.x + 20.0, "y1": p.y, "steps": 8})); - assert_eq!(serde_json::to_value(h.app.cam).unwrap(), camera, "dialog drags must not orbit the axis gizmo"); - assert!(h.app.viewport.axis_drag.is_none()); - h.call("ui.prefs", json!({"button": "cancel"})); - h.call("ui.move", json!({"x": p.x, "y": p.y})); - assert_eq!(crate::scenario::count_of("axis_view_highlights"), Some(1.0)); - h.call("ui.click", json!({"x": p.x, "y": p.y})); - assert_ne!(serde_json::to_value(h.app.cam).unwrap(), camera); + for mode in [crate::shell::LayoutMode::Floating, crate::shell::LayoutMode::Docked] { + let mut h = crate::scenario::Harness::new(); + h.call("ui.set", json!({"layout":mode})); + h.call("ui.resize", json!({"width": 800, "height": 700})); + let p = crate::scenario::handle_at("axis_view:z").unwrap(); + h.call("ui.move", json!({"x": p.x, "y": p.y})); + assert_eq!(crate::scenario::count_of("axis_view_highlights"), Some(1.0)); + h.call("ui.prefs", json!({"open": "Navigation"})); + let window = h.ctx.memory(|m| m.area_rect(egui::Id::new("sc_prefs"))).unwrap(); + assert!(window.contains(p), "the floating dialog must cover the axis target"); + let camera = serde_json::to_value(h.app.cam).unwrap(); + h.call("ui.move", json!({"x": p.x, "y": p.y})); + assert_eq!(crate::scenario::count_of("axis_view_highlights"), Some(0.0)); + h.call("ui.click", json!({"x": p.x, "y": p.y})); + assert_eq!(serde_json::to_value(h.app.cam).unwrap(), camera, "dialog clicks must not turn the axis view"); + h.call("ui.drag", json!({"x0": p.x, "y0": p.y, "x1": p.x + 20.0, "y1": p.y, "steps": 8})); + assert_eq!(serde_json::to_value(h.app.cam).unwrap(), camera, "dialog drags must not orbit the axis gizmo"); + assert!(h.app.viewport.axis_drag.is_none()); + h.call("ui.prefs", json!({"button": "cancel"})); + h.call("ui.move", json!({"x": p.x, "y": p.y})); + assert_eq!(crate::scenario::count_of("axis_view_highlights"), Some(1.0)); + h.call("ui.click", json!({"x": p.x, "y": p.y})); + assert_ne!(serde_json::to_value(h.app.cam).unwrap(), camera); + } } #[test] fn view_axis_drag_owns_navigation_and_keeps_the_view_centre() { - for nav in [None, Some(NavMode::Orbit), Some(NavMode::Pan)] { - let mut h = crate::scenario::Harness::new(); - h.call("ui.view", json!({"view": "front"})); - h.app.viewport.nav = nav; - let before = h.app.cam; - let p = crate::scenario::handle_at("axis_view:-y").unwrap(); - h.call("ui.drag", json!({"x0": p.x, "y0": p.y, "x1": p.x + 48.0, "y1": p.y, "steps": 8})); - assert!((h.app.cam.yaw - 48.0 * 0.008).abs() < 1e-6, "{nav:?}: axis drag must orbit exactly once"); - assert_eq!(h.app.cam.target, before.target, "{nav:?}: axis drag must not pan"); - assert_eq!(h.app.cam.distance, before.distance); - assert_eq!(h.app.cam.pitch, before.pitch); - assert!(h.app.viewport.axis_drag.is_none(), "release clears the axis gesture"); - assert!(h.app.viewport.boxsel.is_none()); + for mode in [crate::shell::LayoutMode::Floating, crate::shell::LayoutMode::Docked] { + for nav in [None, Some(NavMode::Orbit), Some(NavMode::Pan)] { + let mut h = crate::scenario::Harness::new(); + h.call("ui.set", json!({"layout":mode})); + h.call("ui.view", json!({"view": "front"})); + h.app.viewport.nav = nav; + let before = h.app.cam; + let p = crate::scenario::handle_at("axis_view:-y").unwrap(); + h.call("ui.drag", json!({"x0": p.x, "y0": p.y, "x1": p.x + 48.0, "y1": p.y, "steps": 8})); + assert!((h.app.cam.yaw - 48.0 * 0.008).abs() < 1e-6, "{nav:?}: axis drag must orbit exactly once"); + assert_eq!(h.app.cam.target, before.target, "{nav:?}: axis drag must not pan"); + assert_eq!(h.app.cam.distance, before.distance); + assert_eq!(h.app.cam.pitch, before.pitch); + assert!(h.app.viewport.axis_drag.is_none(), "release clears the axis gesture"); + assert!(h.app.viewport.boxsel.is_none()); + } } } diff --git a/crates/ui-egui/src/viewport_layout.rs b/crates/ui-egui/src/viewport_layout.rs index 1e1c6d672..cf04bc81d 100644 --- a/crates/ui-egui/src/viewport_layout.rs +++ b/crates/ui-egui/src/viewport_layout.rs @@ -13,7 +13,10 @@ pub(crate) struct Layout { } /// Enumerate rectangles whose edges meet the viewport or a bounded number of overlays. -fn free_rect(view: Rect, obstacles: &[Rect]) -> Rect { +pub(crate) fn free_rect(view: Rect, obstacles: &[Rect]) -> Rect { + if !view.is_finite() || !view.is_positive() { + return Rect::NOTHING; + } let mut xs = vec![view.left(), view.right()]; let mut ys = vec![view.top(), view.bottom()]; let obstacles: Vec<_> = obstacles.iter().take(16).map(|r| r.intersect(view)).filter(|r| r.is_positive() && r.is_finite()).collect(); diff --git a/crates/ui-egui/src/viewport_overlays_tests.rs b/crates/ui-egui/src/viewport_overlays_tests.rs new file mode 100644 index 000000000..b07fbd424 --- /dev/null +++ b/crates/ui-egui/src/viewport_overlays_tests.rs @@ -0,0 +1,346 @@ +use super::*; +use crate::{scenario::Harness, shell::LayoutMode}; + +// These tests inspect shapes without submitting their texture uploads to a renderer. +fn draw(ctx: &egui::Context, paint: impl FnMut(&mut egui::Ui)) -> egui::FullOutput { + let mut output = ctx.run_ui(egui::RawInput::default(), paint); + output.textures_delta.clear(); + output +} + +fn sketch_stroke(shape: &Shape, color: Color32) -> bool { + match shape { + Shape::LineSegment { stroke, .. } => stroke.color == color, + Shape::Path(path) => matches!(path.stroke.color, egui::epaint::ColorMode::Solid(c) if c == color), + Shape::Vec(shapes) => shapes.iter().any(|s| sketch_stroke(s, color)), + _ => false, + } +} + +#[test] +fn compass_keeps_signed_targets_hover_and_clicks_at_both_scales() { + for mode in [LayoutMode::Floating, LayoutMode::Docked] { + for scale in [1.0, 2.0] { + let mut h = Harness::new(); + h.call("ui.set", json!({"layout": mode})); + h.call("ui.prefs", json!({"ui_scale": scale})); + h.app.preferences.look_at_anim = false; + h.settle(); + let outer = crate::scenario::rect_of("axis_view:outer").unwrap(); + assert_eq!(outer.size(), vec2(116.0, 116.0)); + for (name, direction) in [("x", Vec3::X), ("-x", -Vec3::X), ("y", Vec3::Y), ("-y", -Vec3::Y), ("z", Vec3::Z), ("-z", -Vec3::Z)] { + h.call("ui.view", json!({"view":"home"})); + h.settle(); + let centre = outer.center() - vec2(0.0, crate::viewport::AXIS_CENTER_LIFT); + let tips = axis_tips(&h.app.cam, centre); + let (r, u, _) = h.app.cam.basis(); + let p = centre + vec2((direction.dot(r) * 38.0 * 0.84) as f32, -(direction.dot(u) * 38.0 * 0.84) as f32); + assert_eq!(crate::scenario::handle_at(&format!("axis_view:{name}")), Some(p)); + for offset in [vec2(0.0, 0.0), vec2(14.9, 0.0), vec2(15.1, 0.0)] { + let sample = p + offset; + let expected = tips.iter().enumerate().rev().find(|(_, tip)| tip.2.distance(sample) <= 15.0).map(|(i, _)| i); + assert_eq!(axis_hit(&tips, sample), expected); + h.call("ui.move", json!({"x":sample.x,"y":sample.y})); + assert_eq!(crate::scenario::count_of("axis_view_highlights"), Some(f64::from(u8::from(expected.is_some())))); + if let Some(i) = expected { + assert!(crate::scenario::handle_at(&format!("axis_view_hover:{}", tips[i].0)).is_some()); + } + } + h.call("ui.click", json!({"x":p.x,"y":p.y})); + assert!(h.app.cam.back().dist(direction) < 1e-5, "{mode:?}, {scale}, {name}"); + let p = crate::scenario::handle_at(&format!("axis_view:{name}")).unwrap(); + h.call("ui.click", json!({"x":p.x,"y":p.y})); + assert!(h.app.cam.back().dist(-direction) < 1e-5); + assert_eq!(crate::scenario::rect_of("axis_view:outer"), Some(outer)); + } + } + } +} + +#[test] +fn compass_negative_ends_are_hollow_and_positive_ends_are_chunkier() { + for dark in [false, true] { + let t = Tokens::themed(dark, crate::theme::ColorVision::Off); + for (name, direction, positive) in [("x", Vec3::X, true), ("-x", -Vec3::X, false)] { + let ctx = egui::Context::default(); + let c = pos2(100.0, 100.0); + let original = c + vec2(38.0, 0.0); + let painted = c + (original - c) * 0.84; + let output = draw(&ctx, |ui| { + crate::icons::navigation::axis_tip(ui.painter(), c, name, &direction, painted, 0.5, false, t); + }); + let circles: Vec<_> = output.shapes.iter().filter_map(|s| if let Shape::Circle(c) = &s.shape { Some(c) } else { None }).collect(); + let end = circles.iter().find(|circle| circle.center == painted).unwrap(); + if positive { + assert_eq!(end.fill, t.triad[0]); + assert_eq!(end.radius, 11.0); + assert!(output.shapes.iter().any(|s| matches!(&s.shape, Shape::Text(text) if text.galley.text() == "X"))); + } else { + assert_eq!(end.fill, Color32::TRANSPARENT); + assert_eq!(end.stroke.width, 2.4); + assert_eq!(end.radius, 4.8); + assert!(!output.shapes.iter().any(|s| matches!(&s.shape, Shape::Text(_)))); + } + for shape in &output.shapes { + if let Shape::LineSegment { points, stroke } = &shape.shape { + assert!(points[1].distance(painted) >= end.radius - 0.001, "shaft must stop at the ring"); + assert!(stroke.width >= 2.5); + } + } + assert!(painted.distance(c) + end.radius < 49.5 - 3.0); + } + } +} + +#[test] +fn status_toast_keeps_the_messages_handle_and_opens_the_message_history() { + for mode in [LayoutMode::Floating, LayoutMode::Docked] { + for error in [false, true] { + for sketch in [false, true] { + let mut h = Harness::new(); + h.call("ui.set", json!({"layout": mode})); + if sketch { + h.app.run("sketch.create", json!({"plane": "XY"})).unwrap(); + h.app.start("sketch.line"); + } + h.app.set_status("Viewport message", error); + h.frames(2); + let toast = crate::scenario::rect_of("toast").unwrap(); + let chip = crate::scenario::handle_at("status:chip").unwrap(); + assert_eq!(chip, toast.center()); + h.call("ui.click", json!({"x": chip.x, "y": chip.y})); + h.frames(2); + assert!(h.app.messages.open, "{mode:?}, error={error}: the toast opens Messages"); + assert!(crate::scenario::handle_at("messages:copy").is_some()); + let out = h.call("ui.messages", json!({})); + let message = out["result"]["messages"].as_array().unwrap().last().unwrap(); + assert_eq!(message["text"], "Viewport message"); + assert_eq!(message["error"], error); + } + } + } +} + +#[test] +fn toast_stays_in_free_content_above_history_and_navigation() { + for mode in [LayoutMode::Floating, LayoutMode::Docked] { + for scale in [1.0, 2.0] { + let mut h = Harness::new(); + h.call("ui.set", json!({"layout": mode})); + h.call("ui.prefs", json!({"ui_scale": scale})); + h.app.run("solid.box", json!({"length":40,"width":30,"height":20})).unwrap(); + for (width, height) in [(1400, 900), (900, 720), (700, 475), (320, 240), (1, 1)] { + h.call("ui.resize", json!({"width":width,"height":height})); + for dialog in [false, true] { + if dialog { + h.app.start("solid.fillet"); + } else { + h.app.dialog = None; + } + h.app.status = Some(("Viewport message".into(), crate::now_ms(), false)); + h.frames(3); + let layout = h.app.viewport.layout.as_ref().unwrap(); + let content = crate::shell::content_rect(&h.app, layout.viewport); + let mut obstacles = crate::shell::covered_rects(&h.app); + obstacles.extend([layout.navigation, layout.axis_view]); + let free = crate::viewport_layout::free_rect(content, &obstacles).shrink(Tokens::get().space_8); + if let Some(toast) = crate::scenario::rect_of("toast") { + assert!(toast.is_finite() && toast.is_positive()); + assert!(content.contains_rect(toast), "{mode:?}, {scale}, {width}x{height}: {toast:?}"); + for obstacle in obstacles { + assert!(!toast.intersect(obstacle).is_positive(), "{toast:?} intersects {obstacle:?}"); + } + assert!((toast.center().x - free.center().x).abs() < 0.01); + assert!((toast.bottom() - free.bottom()).abs() < 0.01); + } else { + assert!(free.width() < 200.0 || free.height() < 40.0, "toast unexpectedly absent: {free:?}"); + } + } + } + h.app.status = Some(("Expired".into(), crate::now_ms() - 6001.0, true)); + h.frame(); + assert!(h.app.status.is_none()); + assert!(crate::scenario::rect_of("toast").is_none()); + } + } +} + +#[test] +fn toast_card_uses_shell_tokens_wraps_and_rejects_hostile_rects() { + let ctx = egui::Context::default(); + let t = Tokens::get(); + let free = Rect::from_min_size(pos2(30.0, 30.0), vec2(200.0, 200.0)); + for (error, computing) in [(false, false), (true, false), (false, true)] { + let output = draw(&ctx, |ui| { + let r = paint_toast(ui, free, "A message that wraps across several short lines".into(), error, computing).unwrap(); + assert!(free.contains_rect(r)); + assert!(r.height() > 35.0); + for bad in [ + Rect::NOTHING, + Rect::EVERYTHING, + Rect::from_min_size(Pos2::ZERO, vec2(1.0, 1.0)), + Rect::from_min_size(Pos2::ZERO, vec2(41.0, 1.0)), + Rect::from_min_max(pos2(f32::MAX * 0.75, 0.0), pos2(f32::MAX, 100.0)), + Rect::from_min_size(Pos2::ZERO, vec2(f32::NAN, 90.0)), + ] { + assert!(paint_toast(ui, bad, "Message".into(), error, computing).is_none()); + } + }); + fn card(shape: &Shape, t: Tokens) -> bool { + match shape { + Shape::Rect(r) => r.fill == t.panel && r.stroke.color == t.border && r.corner_radius == t.radius_md.into(), + Shape::Vec(shapes) => shapes.iter().any(|s| card(s, t)), + _ => false, + } + } + assert!(output.shapes.iter().any(|s| card(&s.shape, t))); + if !computing { + assert!( + output + .shapes + .iter() + .any(|s| matches!(&s.shape, Shape::Circle(c) if c.radius == 3.0 && c.fill == if error { t.error } else { t.text })) + ); + } + } +} + +#[test] +fn sketch_previews_selection_band_and_number_focus_follow_sketch_accent() { + for mode in [LayoutMode::Floating, LayoutMode::Docked] { + for dark in [false, true] { + let mut h = Harness::new(); + h.call("ui.set", json!({"layout":mode})); + h.call("ui.prefs", json!({"theme":if dark {"dark"} else {"light"}})); + h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); + h.call("ui.view", json!({"view":"top"})); + h.settle(); + let proj = display_proj(&h.app); + let t = Tokens::get(); + for cmd in ["sketch.line", "sketch.rectangle.two_point", "sketch.circle.center", "sketch.spline.fit_point"] { + let mut tool = crate::tools::Tool::for_command(cmd).unwrap(); + tool.pts.push((Vec2::ZERO, None)); + tool.hover = Some((Vec2::new(20.0, 8.0), None)); + let output = draw(&h.ctx, |ui| crate::tools::preview(&h.app, &tool, ui.painter(), &proj)); + assert!( + output.shapes.iter().any(|s| match &s.shape { + Shape::LineSegment { stroke, .. } => stroke.color == t.sketch_accent, + Shape::Path(p) => matches!(p.stroke.color, egui::epaint::ColorMode::Solid(color) if color == t.sketch_accent), + _ => false, + }), + "{cmd}: no Sketch preview stroke" + ); + } + let mut extended = crate::tools::Tool::for_command("sketch.line").unwrap(); + extended.kind = crate::tools::Kind::Ext; + extended.pts.push((Vec2::ZERO, None)); + extended.hover = Some((Vec2::new(20.0, 8.0), None)); + let output = draw(&h.ctx, |ui| crate::sketch_tools::preview(&h.app, &extended, ui.painter(), &proj)); + assert!(output.shapes.iter().any(|s| sketch_stroke(&s.shape, t.sketch_accent))); + for sketch in [false, true] { + for crossing in [false, true] { + let bx = BoxSel { rect: Rect::from_min_size(pos2(20.0, 20.0), vec2(100.0, 80.0)), crossing }; + let output = draw(&h.ctx, |ui| paint_box_selection(ui.painter(), bx, sketch)); + let color = if sketch { + t.sketch_accent + } else if crossing { + t.box_crossing + } else { + t.box_window + }; + assert!(output.shapes.iter().any(|s| match &s.shape { + Shape::Rect(r) => r.stroke.color == color, + Shape::Vec(shapes) => shapes.iter().any(|s| matches!(s, Shape::LineSegment { stroke, .. } if stroke.color == color)), + _ => false, + })); + } + } + h.app.ui.tab = "MODEL".into(); // A sketch remains open when another ribbon tab is selected. + h.app.tool = crate::tools::Tool::for_command("sketch.line"); + crate::theme::TabAccent::publish(&h.app, &h.ctx); + let output = draw(&h.ctx, |ui| { + let r = ui.add(egui::TextEdit::singleline(&mut String::from("20"))); + r.request_focus(); + crate::theme::TabAccent::number_focus(ui, &r); + }); + assert!(output.shapes.iter().any(|s| matches!(&s.shape, Shape::Rect(r) if r.stroke.color == t.sketch_accent))); + } + } +} + +#[test] +fn three_dimensional_sketch_rubber_band_uses_sketch_accent() { + for mode in [LayoutMode::Floating, LayoutMode::Docked] { + let mut h = Harness::new(); + h.call("ui.set", json!({"layout": mode})); + h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); + h.app.run("sketch.options", json!({"sketch_3d":true})).unwrap(); + h.call("ui.view", json!({"view":"top"})); + h.app.start("sketch.line"); + h.settle(); + h.step(&json!({"click":{"world":[0,0,0]}})).unwrap(); + h.step(&json!({"move":{"world":[20,8,0]}})).unwrap(); + let proj = display_proj(&h.app); + let output = draw(&h.ctx, |ui| { + assert!(crate::sketch3d::preview(&h.app, h.app.tool.as_ref().unwrap(), ui.painter(), &proj)); + }); + assert!(output.shapes.iter().any(|s| sketch_stroke(&s.shape, Tokens::get().sketch_accent))); + } +} + +#[test] +fn a_hovered_compass_end_turns_white_and_its_letter_takes_the_axis_colour() { + for dark in [false, true] { + let t = Tokens::themed(dark, crate::theme::ColorVision::Off); + for (name, direction, positive) in [("x", Vec3::X, true), ("-x", -Vec3::X, false)] { + let ctx = egui::Context::default(); + let c = pos2(100.0, 100.0); + let original = c + vec2(38.0, 0.0); + let painted = c + (original - c) * 0.84; + let output = draw(&ctx, |ui| { + crate::icons::navigation::axis_tip(ui.painter(), c, name, &direction, painted, 0.5, true, t); + }); + let circles: Vec<_> = output.shapes.iter().filter_map(|s| if let Shape::Circle(c) = &s.shape { Some(c) } else { None }).collect(); + assert!(circles.iter().any(|circle| circle.center == painted && circle.fill == t.on_accent), "{name}: the hovered end is filled white"); + if !dark { + // White would vanish on the light compass: an axis-coloured outline keeps it visible. + assert!( + circles.iter().any(|circle| circle.center == painted && circle.stroke.color == t.triad[0] && circle.stroke.width >= 2.0), + "{name}: the hovered end is outlined in its axis colour on the light theme" + ); + } + let letters: Vec<_> = output.shapes.iter().filter_map(|s| if let Shape::Text(text) = &s.shape { Some(text) } else { None }).collect(); + if positive { + assert!( + !letters.is_empty() + && letters.iter().all(|text| text.override_text_color == Some(t.triad[0]) + || text.galley.job.sections.iter().all(|s| s.format.color == t.triad[0])), + "the letter takes the axis colour" + ); + } else { + assert!(letters.is_empty()); + } + } + } +} + +#[test] +fn compass_circle_grabs_on_hover_and_the_home_chip_points() { + let mut h = Harness::new(); + h.call("ui.set", json!({"layout": "floating"})); + h.settle(); + let outer = crate::scenario::rect_of("axis_view:outer").unwrap(); + let centre = outer.center() - vec2(0.0, crate::viewport::AXIS_CENTER_LIFT); + // A point inside the circle that is clear of every axis end and of the home chip. + let probe = centre + vec2(-36.0, 30.0); + h.call("ui.move", json!({"x": probe.x, "y": probe.y})); + h.settle(); + assert_eq!(h.output.cursor_icon, egui::CursorIcon::Grab, "an open hand over the circle"); + let home = crate::scenario::handle_at("axis_view:home").unwrap(); + h.call("ui.move", json!({"x": home.x, "y": home.y})); + h.settle(); + assert_eq!(h.output.cursor_icon, egui::CursorIcon::PointingHand, "a pointer on the home chip"); + h.call("ui.move", json!({"x": centre.x - 200.0, "y": centre.y + 200.0})); + h.settle(); + assert_ne!(h.output.cursor_icon, egui::CursorIcon::Grab, "no hand away from the compass"); +}