diff --git a/crates/color/src/ui/wheel.rs b/crates/color/src/ui/wheel.rs index c6beb6f13..b2c3b0ca1 100644 --- a/crates/color/src/ui/wheel.rs +++ b/crates/color/src/ui/wheel.rs @@ -317,6 +317,7 @@ impl Component for ColorWheel<'_> { fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) { let geometry = ColorWheel::geometry(cx.style, rect); let reach = cx.style.grab; + let hue = finite(cx.value().copied().unwrap_or_else(blank).values[0]); // The ring takes every press within its reach; the triangle takes the circle inside the ring's reach. cx.part(RING, HitShape::circle(geometry.center, geometry.outer + reach)) .sense(Sense::DRAG | Sense::SETS_ON_PRESS | Sense::CLICK) @@ -326,7 +327,8 @@ impl Component for ColorWheel<'_> { .sense(Sense::DRAG | Sense::SETS_ON_PRESS | Sense::CLICK) .priority(HitPriority::new(HitLevel::Handle, 0)) .focusable() - .focus_shape(FocusShape::Circle); + // The triangle turns with the hue, so its ring is rebuilt each pass. + .focus_shape(FocusShape::Polygon(geometry.corners(hue, 0.0).to_vec())); } fn bind(&mut self) -> Option> { diff --git a/crates/ui-contract/src/primitives/focus_ring.rs b/crates/ui-contract/src/primitives/focus_ring.rs index 92e81d5c7..a3bfdeac9 100644 --- a/crates/ui-contract/src/primitives/focus_ring.rs +++ b/crates/ui-contract/src/primitives/focus_ring.rs @@ -3,7 +3,8 @@ use super::draw::PaintShape; use crate::style::{Flags, FocusLook}; use egui::emath::GuiRounding as _; -use egui::{CornerRadius, Shape, Stroke, StrokeKind}; +use crate::runtime::focus::FocusShape; +use egui::{CornerRadius, Pos2, Rect, Shape, Stroke, StrokeKind, Vec2}; /// How a focus indication is drawn. #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] @@ -28,10 +29,12 @@ impl FocusRing { /// The shape the ring is drawn around for a part declared with `shape` and laid out in `rect`: the rect, the circle /// inside it, or the polygon's points. - #[allow(dead_code)] // P3(framework-01-w6): fx.F33 - pub(crate) fn outline(shape: &crate::runtime::focus::FocusShape, rect: egui::Rect) -> PaintShape { - let _ = (shape, rect); - todo!("fx.F33") + pub(crate) fn outline(shape: &FocusShape, rect: Rect) -> PaintShape { + match shape { + FocusShape::Rect => PaintShape::Rect(rect, CornerRadius::ZERO), + FocusShape::Circle => PaintShape::Circle(rect.center(), rect.width().min(rect.height()) / 2.0), + FocusShape::Polygon(points) => PaintShape::Polygon(points.clone()), + } } /// The shapes of the ring around `shape`, grown by `look.offset`. A halo adds a stroke twice as wide at a @@ -60,8 +63,34 @@ impl FocusRing { Shape::rect_stroke(grown, radius, stroke, StrokeKind::Inside) } PaintShape::Circle(center, r) => Shape::circle_stroke(*center, r + offset, stroke), - PaintShape::Polygon(points) => Shape::closed_line(points.clone(), stroke), + PaintShape::Polygon(points) => Shape::closed_line(grow_polygon(points, offset), stroke), PaintShape::Path(points) => Shape::line(points.clone(), stroke), } } } + +/// `points` moved outward by `offset`: each vertex slides along the bisector of its two edge normals by +/// `offset / cos(half angle)`, so every edge ends up `offset` from where it was (a mitred outline). Fewer than three +/// points, or an offset of zero, leave the polygon as it is. +fn grow_polygon(points: &[Pos2], offset: f32) -> Vec { + let count = points.len(); + if count < 3 || offset == 0.0 { + return points.to_vec(); + } + // The sign of the area says which side of an edge is outside, whatever the winding. + let area: f32 = (0..count).map(|i| points[i].x * points[(i + 1) % count].y - points[(i + 1) % count].x * points[i].y).sum(); + let side = if area < 0.0 { -1.0 } else { 1.0 }; + let normal = |from: Pos2, to: Pos2| -> Vec2 { + let along = (to - from).normalized(); + Vec2::new(along.y, -along.x) * side + }; + (0..count) + .map(|i| { + let (before, here, after) = (points[(i + count - 1) % count], points[i], points[(i + 1) % count]); + let (n1, n2) = (normal(before, here), normal(here, after)); + // 1 + n1.n2 = 2 cos^2(half angle); it vanishes only where the edges fold back on themselves. + let miter = 1.0 + n1.dot(n2); + if miter < 1e-3 { here + n1 * offset } else { here + (n1 + n2) * (offset / miter) } + }) + .collect() +} diff --git a/crates/ui-widgets/tests/it/text/edit_menu.rs b/crates/ui-widgets/tests/it/text/edit_menu.rs index 622cd75e2..66adef1a5 100644 --- a/crates/ui-widgets/tests/it/text/edit_menu.rs +++ b/crates/ui-widgets/tests/it/text/edit_menu.rs @@ -32,7 +32,10 @@ fn rig() -> Rig { } impl Rig { + /// A right click well after the click that focused the field: egui counts presses of any button into one click + /// run, and a second press of a run selects the word under it. fn right_click_in_the_field(&mut self) { + self.fixture.advance(1.0); let at = self.fixture.by_label("Name").expect("the field").center(); self.fixture.frame(vec![pointer::secondary(at, true)]); self.fixture.frame(vec![pointer::secondary(at, false)]); @@ -109,16 +112,31 @@ fn an_edit_command_fired_from_elsewhere_reaches_the_focused_field() { assert_eq!(*rig.text.borrow(), ""); } -/// The Delete row deletes the field's selection: the command reaches the focused text part as its edit, not the app. +/// Cut, Copy and Delete need a selection; Paste, Undo and Select All do not. #[trial] -fn the_edit_menus_delete_deletes_the_selection_in_the_field() { +fn the_edit_menu_offers_cut_copy_and_delete_only_with_a_selection() { let mut rig = rig(); + let selection_rows = ["Cut", "Copy", "Delete"]; + let always_rows = ["Paste", "Undo", "Select All"]; + let disabled = |rig: &Rig, row: &str| { + let target = rig.fixture.by_label(row).unwrap_or_else(|| panic!("the edit menu has {row}")); + rig.fixture.tree().is_disabled_key(target.node) + }; + rig.right_click_in_the_field(); + assert!(rig.menu_open(), "the edit menu is open"); + for row in selection_rows { + assert!(disabled(&rig, row), "{row} is disabled while the selection is empty"); + } + for row in always_rows { + assert!(!disabled(&rig, row), "{row} stays enabled without a selection"); + } + rig.fixture.key(Key::Escape, Modifiers::NONE); + rig.fixture.settle(); rig.fixture.key(Key::A, Modifiers::COMMAND); rig.fixture.settle(); rig.right_click_in_the_field(); - let delete = rig.fixture.by_label("Delete").expect("the Delete row"); - rig.fixture.click(&delete); - rig.fixture.settle(); - assert_eq!(*rig.text.borrow(), "", "Delete takes the selection out of the field"); - assert!(rig.focus_in_the_field(), "the focus is back in the field"); + assert!(rig.menu_open(), "the edit menu opens again"); + for row in selection_rows { + assert!(!disabled(&rig, row), "{row} is enabled with a selection"); + } }