Dots are one shared paint: Rule::dotted

The collapsed icon group grip is a faint dotted line: hairline dots one
dot apart, as the reference apps' grips. Guides' dotted line and the
grid's dots draw through the same paint, so each dot covers whole
physical pixels and a one-pixel dot no longer vanishes. The guides
stories show a dotted guide.
This commit is contained in:
aiongg committed 2026-11-21 14:46:57 +00:00
1 parent 7bf0a9b6dd
commit e5e007233a
7 files changed
+112 -35

No files matched your search

@@ -526,10 +526,11 @@ impl Component for Guides<'_> {
for guide in guides {
let Some([a, b]) = place::segment(&space.view, guide.along, guide.position, guide.span, cx.rect) else { continue };
let color = if self.selected == Some(guide.key) { style.selected.egui() } else { guide.color.map_or(style.line.egui(), data_color) };
let look = RuleLook { color, width: RuleWidth::Points(style.width) };
match guide.line {
GuideLine::Solid => painter.painter.extend(Rule::shapes(&RuleLook { color, width: RuleWidth::Points(style.width) }, a, b, ppp)),
GuideLine::Solid => painter.painter.extend(Rule::shapes(&look, a, b, ppp)),
GuideLine::Dashed => painter.dashed(&[a, b], Dashes::new(style.dash, style.gap), StrokeWidth::Thin, color),
GuideLine::Dots => painter.dashed(&[a, b], Dashes::new(style.width, style.gap), StrokeWidth::Thin, color),
GuideLine::Dots => painter.painter.extend(Rule::dotted(&look, a, b, style.gap, ppp)),
}
if focused == Some(guide.key.get()) && (b - a).length() > 0.0 {
painter.painter.extend(FocusRing::shapes(&style.focus, FocusKind::Halo, &footprint(&space, a, b, style.width), ppp));
@@ -26,12 +26,15 @@ fn on_guides_canvas(ui: &mut Ui, mut control: impl FnMut(&mut Ui, Rect, Id) -> G
on_canvas_with(ui, room, Canvas::new("Document").id(canvas).view(view), Some(document), |ui, at| control(ui, at, canvas))
}
/// Every line a guide draws: solid, dotted and dashed, and a locked one.
fn guides() -> Vec<Guide> {
let mut dotted = Guide::new(GuideKey::new(2), Axis::X, 120.0);
dotted.line = GuideLine::Dots;
let mut dashed = Guide::new(GuideKey::new(3), Axis::X, 280.0);
dashed.line = GuideLine::Dashed;
let mut locked = Guide::new(GuideKey::new(4), Axis::Y, 480.0);
locked.locked = true;
vec![Guide::new(GuideKey::new(1), Axis::Y, 160.0), Guide::new(GuideKey::new(2), Axis::X, 120.0), dashed, locked]
vec![Guide::new(GuideKey::new(1), Axis::Y, 160.0), dotted, dashed, locked]
}
/// Drags the first guide across itself by twice the drag threshold.
@@ -487,6 +487,11 @@ impl<'a> Draw<'a> {
self.painter.extend(super::rule::Rule::shapes(look, from, to, self.painter.pixels_per_point()));
}
/// Paints the line `look` from `from` to `to` as dots `gap` points apart (`Rule::dotted`): a grip's dotted line.
pub fn dotted_rule_with(&self, look: &crate::style::RuleLook, from: Pos2, to: Pos2, gap: f32) {
self.painter.extend(super::rule::Rule::dotted(look, from, to, gap, self.painter.pixels_per_point()));
}
/// Paints a surface of `kind` (tracks, plot wells).
pub fn surface(&self, rect: Rect, kind: SurfaceKind, corners: Corners) {
let style = surface_style(self.env.tokens, kind);
+1 -4
View File
@@ -469,9 +469,6 @@ const MINOR_SHARE: f32 = 0.45;
/// How many times a thinned grid doubles its step before it gives up.
const MAX_DOUBLINGS: u32 = 32;
/// The radius of a dot, in screen points.
const DOT_RADIUS: f32 = 0.6;
/// What a grid does when its lines come closer than the minimum step.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
@@ -693,7 +690,7 @@ pub fn grid_lines_with(look: &GridLook, grid: &GridLines, ink: &GridInk, pixels_
match ink.stroke {
GridStroke::Solid => shapes.extend(Rule::shapes(&RuleLook { color, width: look.width }, a, b, pixels_per_point)),
GridStroke::Dashed => shapes.extend(dashed(&[a, b], Dashes::new(look.dash, look.gap), look.width.points(pixels_per_point), color)),
GridStroke::Dots => shapes.extend(Shape::dotted_line(&[a, b], color, look.gap, DOT_RADIUS)),
GridStroke::Dots => shapes.extend(Rule::dotted(&RuleLook { color, width: look.width }, a, b, look.gap, pixels_per_point)),
}
}
}
+39 -6
View File
@@ -1,7 +1,7 @@
//! `Rule`: dividers, edges, grid lines and drop marks, with one pixel-snap rule.
use crate::style::RuleLook;
use egui::{Pos2, Rect, Shape, Stroke};
use egui::{Mesh, Pos2, Rect, Shape, Stroke, Vec2};
pub use crate::style::RuleKind;
@@ -47,16 +47,20 @@ impl RectSide {
}
}
/// `v` (points) snapped for a line `physical` pixels wide: an odd width on a physical pixel centre, an even one on a
/// pixel edge.
fn snap(v: f32, physical: f32, pixels_per_point: f32) -> f32 {
let px = v * pixels_per_point;
let snapped = if physical % 2.0 == 1.0 { px.floor() + 0.5 } else { px.round() };
snapped / pixels_per_point
}
impl Rule {
/// The line from `from` to `to`, snapped across its direction: a line whose physical width is odd sits on a
/// pixel centre, an even one on a pixel edge, and no line is thinner than one physical pixel.
pub fn shapes(look: &RuleLook, from: Pos2, to: Pos2, pixels_per_point: f32) -> Vec<Shape> {
let physical = look.width.pixels(pixels_per_point);
let snap = |v: f32| {
let px = v * pixels_per_point;
let snapped = if physical % 2.0 == 1.0 { px.floor() + 0.5 } else { px.round() };
snapped / pixels_per_point
};
let snap = |v: f32| snap(v, physical, pixels_per_point);
let (from, to) = if (from.y - to.y).abs() <= (from.x - to.x).abs() {
(Pos2::new(from.x, snap(from.y)), Pos2::new(to.x, snap(to.y)))
} else {
@@ -65,6 +69,35 @@ impl Rule {
vec![Shape::line_segment([from, to], Stroke::new(physical / pixels_per_point, look.color))]
}
/// The dotted form of the line from `from` to `to`: square dots as wide as the line, `gap` points apart, the run
/// of whole dots centred between the ends (a grip, a dotted guide or grid line). The gap is whole physical pixels,
/// at least one, and each dot's centre is snapped on both axes by the rule `shapes` snaps across with, so every dot
/// covers whole physical pixels and dots on a level or upright line are evenly spaced. The dots are one mesh.
pub fn dotted(look: &RuleLook, from: Pos2, to: Pos2, gap: f32, pixels_per_point: f32) -> Vec<Shape> {
let ppp = pixels_per_point.max(f32::EPSILON);
let side = look.width.pixels(ppp);
let gap = (gap * ppp).round().max(1.0);
let length = (to - from).length() * ppp;
// The run leaves at least a physical pixel of the line free, so snapping, which moves a dot at most half a
// pixel, keeps every dot between the ends.
let count = ((length - 1.0 + gap) / (side + gap)).floor();
if !count.is_finite() || count < 1.0 {
return Vec::new();
}
let direction = (to - from).normalized();
let first = (length - (count * side + (count - 1.0) * gap)) / 2.0 + side / 2.0;
let dot = Vec2::splat(side / ppp);
// One mesh of plain quads: a filled rect two pixels wide or less is tessellated as a line segment, whose ends
// the tessellator rounds inward, and a one-pixel dot drawn that way vanishes.
let mut mesh = Mesh::default();
for i in 0..count as usize {
let centre = from + direction * ((first + i as f32 * (side + gap)) / ppp);
let centre = Pos2::new(snap(centre.x, side, ppp), snap(centre.y, side, ppp));
mesh.add_colored_rect(Rect::from_center_size(centre, dot), look.color);
}
vec![Shape::mesh(mesh)]
}
/// The line along the `side` edge of `rect`, on the physical pixels just inside it: a row's bottom rule or a
/// cell's gridline, which a line centred on the edge would put on the first pixel outside, under the next row's
/// clip or the next cell's fill.
@@ -130,6 +130,45 @@ fn an_edge_rule_lies_on_the_pixels_just_inside_its_side_at_any_scale() {
}
}
#[trial]
fn a_dotted_rule_is_whole_pixel_dots_at_one_pitch_between_its_ends() {
use craft_ui::primitives::Rule;
use craft_ui::style::{RuleLook, RuleWidth};
use egui::{Color32, Rect, Shape};
let whole = |v: f32| (v - v.round()).abs() < EPS;
for ppp in [1.0, 1.5, 2.0] {
for width in [RuleWidth::Hairline, RuleWidth::Points(1.0), RuleWidth::Points(2.0)] {
let look = RuleLook { color: Color32::WHITE, width };
// Ends off the pixel grid, level and upright.
for (from, to) in [(pos2(10.3, 20.6), pos2(41.1, 20.6)), (pos2(7.7, 3.2), pos2(7.7, 29.9))] {
let shapes = Rule::dotted(&look, from, to, 3.0, ppp);
let [Shape::Mesh(mesh)] = shapes.as_slice() else { panic!("the dots are one mesh") };
let dots: Vec<Rect> =
mesh.vertices.chunks(4).map(|quad| Rect::from_points(&quad.iter().map(|v| v.pos).collect::<Vec<_>>())).collect();
assert!(dots.len() > 2, "{width:?} at {ppp}x: several dots");
let level = from.y == to.y;
let across = width.points(ppp);
let span = if level {
Rect::from_two_pos(from, to).expand2(egui::vec2(0.0, across))
} else {
Rect::from_two_pos(from, to).expand2(egui::vec2(across, 0.0))
};
for dot in &dots {
let px = *dot * ppp;
assert!([px.min.x, px.min.y, px.max.x, px.max.y].into_iter().all(whole), "{width:?} at {ppp}x: {px:?} is not whole pixels");
assert!((dot.width() - dot.height()).abs() < EPS, "{width:?} at {ppp}x: a dot is square");
assert!(span.expand(EPS).contains_rect(*dot), "{width:?} at {ppp}x: {dot:?} leaves the line {from:?}..{to:?}");
}
let starts: Vec<f32> = dots.iter().map(|dot| if level { dot.min.x } else { dot.min.y }).collect();
let pitch = starts[1] - starts[0];
assert!(pitch > dots[0].width(), "{width:?} at {ppp}x: dots are apart");
assert!(starts.windows(2).all(|w| (w[1] - w[0] - pitch).abs() < EPS), "{width:?} at {ppp}x: one pitch: {starts:?}");
}
}
}
}
#[trial]
fn ant_edges_are_whole_device_pixels_at_a_fractional_scale() {
use craft_theme::{Contrast, Mode};
+21 -22
View File
@@ -13,15 +13,15 @@ use crate::contract::{
WordSetCx as _,
};
use crate::menu::GroupItem;
use crate::primitives::{IconSpec, PaintShape, TextRun};
use crate::primitives::{IconSpec, TextRun};
use crate::runtime::env::Label;
use crate::runtime::focus::KeyUse;
use crate::style::{ComponentStyle, Corners, GlyphRole, StyleColor, StyleCx, SurfaceKind, UiTokens};
use crate::style::{ComponentStyle, Corners, GlyphRole, RuleLook, RuleWidth, StyleColor, StyleCx, SurfaceKind};
use craft_behavior::dock::{Column, DockEdge, Group};
use craft_behavior::overlay::Side;
use craft_behavior::{Axis, CursorIcon, DockSide, Metrics, PartKey, PressPhase, Sense, Value};
use craft_theme::ControlSize;
use egui::{CornerRadius, Id, Pos2, Rect, Response, Ui, Vec2};
use egui::{Id, Pos2, Rect, Response, Ui, Vec2};
use std::fmt::Debug;
use std::hash::Hash;
@@ -54,11 +54,11 @@ pub struct IconicSizes {
/// The column width, in rail widths, from which the cells show their panels' names: 96 points at the Compact size.
const LABELS_IN_RAIL_WIDTHS: f32 = 2.4;
/// The length of a group's grip bar, in icon cell heights: half a cell, 15 points at the Compact size (18 at
/// Comfortable). That is several times the bar's thickness, so it reads as a handle and not as a dot, and in a
/// rail-wide column (40 points at Compact) it leaves more than a quarter of the width free on each side, so it does
/// not read as a rule between the groups.
const GRIP_BAR_IN_CELLS: f32 = 0.5;
/// The length of a group's grip line, in icon cell heights: half a cell, 15 points at the Compact size (18 at
/// Comfortable). That holds several dots, so it reads as a handle and not as a dot, and in a rail-wide column (40
/// points at Compact) it leaves more than a quarter of the width free on each side, so it does not read as a rule
/// between the groups.
const GRIP_LINE_IN_CELLS: f32 = 0.5;
impl IconicSizes {
/// The sizes of the style context `cx`.
@@ -149,22 +149,21 @@ pub struct IconicColumnStyle {
name_open: StyleColor,
/// The room between an icon and its name.
name_gap: f32,
/// The ink of a group's grip bar.
/// The ink of a group's grip line.
grip: StyleColor,
/// The ink of a group's grip bar while the pointer is over it or drags it.
/// The ink of a group's grip line while the pointer is over it or drags it.
grip_active: StyleColor,
/// The grip bar's length across the group and its thickness.
/// The grip line's length across the group.
grip_length: f32,
grip_thickness: f32,
/// The grip bar's corners: the theme's pill rounding.
grip_radius: CornerRadius,
/// The side of the grip line's dots, and the gap between two of them.
grip_dot: f32,
}
/// A column collapsed to icons: the column bar over icon groups, one cell per panel. A panel without an icon shows the
/// first letter of its name in a box. A column widened past `IconicSizes::labels` shows each panel's name beside its
/// icon, cut with an ellipsis to the cell; a click or drag on the name acts as one on the icon. A click or Enter on an icon
/// opens the flyout of its panel beside the column, switches it to that panel or closes it; one flyout is open at a
/// time. A drag of an icon starts a move of its panel, a drag of the grip bar on top of a group a move of the group, a
/// time. A drag of an icon starts a move of its panel, a drag of the grip line on top of a group a move of the group, a
/// drag of the bar beside its button a move of the column; each starts through the drag session, and a press outside
/// the flyout closes it. Escape and a press
/// outside close the flyout; with `auto_collapse` the focus leaving it does too.
@@ -423,13 +422,12 @@ impl<P: Value + Hash + Debug + 'static> Component for IconicColumn<'_, P> {
name: cx.color(colors.text.normal),
name_open: cx.color(colors.accent.text),
name_gap: cx.tokens.space.gap.item,
// A handle that is found when looked for, as the reference's faint grips: the faint ink in a hairline,
// the normal ink under the pointer.
// A handle that is found when looked for, as the references' faint grips: hairline dots one dot apart in
// the faint ink, the normal ink under the pointer.
grip: cx.color(colors.text.faint),
grip_active: cx.color(colors.text.normal),
grip_length: cx.tokens.size.rail.button * GRIP_BAR_IN_CELLS,
grip_thickness: cx.tokens.stroke.thin,
grip_radius: UiTokens::corner(cx.tokens.shape.radius.pill),
grip_length: cx.tokens.size.rail.button * GRIP_LINE_IN_CELLS,
grip_dot: cx.tokens.stroke.thin,
}
}
@@ -582,8 +580,9 @@ impl<P: Value + Hash + Debug + 'static> Component for IconicColumn<'_, P> {
for (group, strip) in self.laid.as_ref().map_or(&[][..], |plan| &plan.grips[..]) {
let part = cx.part(GRIP.at(*group as u64));
let ink = if part.hovered || part.pressed() { style.grip_active } else { style.grip };
let bar = Rect::from_center_size(strip.center(), Vec2::new(style.grip_length.min(strip.width()), style.grip_thickness));
ui.draw().fill(&PaintShape::Rect(bar, style.grip_radius), ink);
let half = Vec2::new(style.grip_length.min(strip.width()) / 2.0, 0.0);
let look = RuleLook { color: ink.egui(), width: RuleWidth::Points(style.grip_dot) };
ui.draw().dotted_rule_with(&look, strip.center() - half, strip.center() + half, style.grip_dot);
}
// The edge's line lies on the column's inner edge; its handle stays inside the column. A floating window's
// edges are the window's.