Merge pull request #1288 from ramox81/feat/auto-constraint-complete

# Conflicts:
#	src/ui/overlay.rs
This commit is contained in:
Hakan Seven committed 2026-09-15 14:41:43 +03:00
commit cb4c4cef65
3 files changed
+264 -29

No files matched your search

+74 -21
View File
@@ -27,6 +27,7 @@ const CONSTRAINT_GLYPH_PAD_X: f32 = 7.0;
const CONSTRAINT_GLYPH_PAD_Y: f32 = 4.0;
const CONSTRAINT_GLYPH_GAP: f32 = 6.0;
const CONSTRAINT_GLYPH_ROW_GAP: f32 = 4.0;
const CONSTRAINT_HOVER_MARKER_RADIUS: f32 = 7.0;
fn constraint_glyph_size(label: &str) -> Size {
let w = label.chars().count() as f32 * CONSTRAINT_GLYPH_SIZE * 0.62
@@ -35,6 +36,22 @@ fn constraint_glyph_size(label: &str) -> Size {
Size::new(w, h)
}
fn draw_tangent_constraint_glyph(
frame: &mut canvas::Frame,
center: Point,
color: Color,
) {
let radius = 4.5;
let circle_center = Point::new(center.x - 0.3, center.y + 2.75);
let diagonal = radius * std::f32::consts::FRAC_1_SQRT_2;
let contact = Point::new(circle_center.x - diagonal, circle_center.y - diagonal);
let tangent_end = Point::new(contact.x + 8.0, contact.y - 8.0);
let stroke = canvas::Stroke::default().with_color(color).with_width(1.35);
frame.stroke(&canvas::Path::circle(circle_center, radius), stroke.clone());
frame.stroke(&canvas::Path::line(contact, tangent_end), stroke);
}
fn constraint_glyph_box(
anchor: Point,
outward: [f32; 2],
@@ -749,7 +766,7 @@ pub fn selection_overlay<'a>(
crosshair_bg: [f32; 4],
crosshair: CrosshairOptions,
selection_visual: SelectionVisualOptions,
constraint_glyphs: Vec<(Point, [f32; 2], String, bool, bool)>,
constraint_glyphs: Vec<(Point, [f32; 2], String, bool, bool, Vec<Point>)>,
) -> Element<'a, Message> {
canvas(SelectionCanvas {
selection,
@@ -833,9 +850,9 @@ struct SelectionCanvas {
crosshair: CrosshairOptions,
selection_visual: SelectionVisualOptions,
/// Constraint glyph anchor, outward screen direction, label, conflict
/// state, and whether the pill itself is the current click-to-select
/// target (`Scene::selected_constraint`) — for the current scope.
constraint_glyphs: Vec<(Point, [f32; 2], String, bool, bool)>,
/// state, whether the pill itself is the current click-to-select target,
/// and the points to mark while the pill is hovered.
constraint_glyphs: Vec<(Point, [f32; 2], String, bool, bool, Vec<Point>)>,
}
fn draw_grip_marker(
@@ -1021,7 +1038,7 @@ impl canvas::Program<Message> for SelectionCanvas {
let glyphs: Vec<_> = self
.constraint_glyphs
.iter()
.map(|(anchor, outward, label, conflict, _)| {
.map(|(anchor, outward, label, conflict, _, _)| {
(*anchor, *outward, label.clone(), *conflict)
})
.collect();
@@ -1747,15 +1764,18 @@ impl canvas::Program<Message> for SelectionCanvas {
if !self.constraint_glyphs.is_empty() {
// `constraint_glyph_offsets` only needs the anchor/outward/label/
// conflict quadruple it was written against; project away the
// trailing `is_selected` flag rather than widen its signature.
// trailing display fields rather than widen its signature.
let glyphs_for_offsets: Vec<(Point, [f32; 2], String, bool)> = self
.constraint_glyphs
.iter()
.map(|(anchor, outward, label, is_conflicting, _)| {
.map(|(anchor, outward, label, is_conflicting, _, _)| {
(*anchor, *outward, label.clone(), *is_conflicting)
})
.collect();
let offsets = constraint_glyph_offsets(&glyphs_for_offsets);
let hovered = cursor
.position_in(bounds)
.and_then(|point| constraint_glyph_hit_test(&glyphs_for_offsets, point));
let normal_bg = theme.palette().primary.base.color;
let normal_fg = theme.palette().primary.base.text;
// A redundant or conflicting constraint gets the danger palette
@@ -1765,7 +1785,7 @@ impl canvas::Program<Message> for SelectionCanvas {
let conflict_bg = theme.palette().danger.base.color;
let conflict_fg = theme.palette().danger.base.text;
let selected_ring = theme.palette().primary.strong.color;
for ((anchor, outward, label, is_conflicting, is_selected), tangent_offset) in
for ((anchor, outward, label, is_conflicting, is_selected, _), tangent_offset) in
self.constraint_glyphs.iter().zip(offsets)
{
if !anchor.x.is_finite() || !anchor.y.is_finite() {
@@ -1786,19 +1806,52 @@ impl canvas::Program<Message> for SelectionCanvas {
canvas::Stroke::default().with_color(selected_ring).with_width(2.0),
);
}
frame.fill_text(canvas::Text {
content: label.clone(),
position: Point::new(
top_left.x + size.width * 0.5,
top_left.y + size.height * 0.5,
),
color: fg,
size: iced::Pixels(CONSTRAINT_GLYPH_SIZE),
align_x: iced::alignment::Horizontal::Center.into(),
align_y: iced::alignment::Vertical::Center,
shaping: iced::advanced::text::Shaping::Advanced,
..Default::default()
});
let glyph_center = Point::new(
top_left.x + size.width * 0.5,
top_left.y + size.height * 0.5,
);
if label == "T" {
draw_tangent_constraint_glyph(&mut frame, glyph_center, fg);
} else {
frame.fill_text(canvas::Text {
content: label.clone(),
position: glyph_center,
color: fg,
size: iced::Pixels(CONSTRAINT_GLYPH_SIZE),
align_x: iced::alignment::Horizontal::Center.into(),
align_y: iced::alignment::Vertical::Center,
shaping: iced::advanced::text::Shaping::Advanced,
..Default::default()
});
}
}
if let Some(index) = hovered {
let red = Color::from_rgb(1.0, 0.0, 0.0);
let stroke = canvas::Stroke::default().with_color(red).with_width(1.5);
for point in &self.constraint_glyphs[index].5 {
let first = canvas::Path::line(
Point::new(
point.x - CONSTRAINT_HOVER_MARKER_RADIUS,
point.y - CONSTRAINT_HOVER_MARKER_RADIUS,
),
Point::new(
point.x + CONSTRAINT_HOVER_MARKER_RADIUS,
point.y + CONSTRAINT_HOVER_MARKER_RADIUS,
),
);
let second = canvas::Path::line(
Point::new(
point.x - CONSTRAINT_HOVER_MARKER_RADIUS,
point.y + CONSTRAINT_HOVER_MARKER_RADIUS,
),
Point::new(
point.x + CONSTRAINT_HOVER_MARKER_RADIUS,
point.y - CONSTRAINT_HOVER_MARKER_RADIUS,
),
);
frame.stroke(&first, stroke.clone());
frame.stroke(&second, stroke.clone());
}
}
}
// Small cross at each acquired tracking point.