Hand-stroked dividers and edges use the rule painter

The split button's divider, the timeline track header's divider and the table
outline and grid header edges paint through Draw::rule and Draw::rule_with, the
one pixel-snapped line, instead of their own strokes.
This commit is contained in:
aiongg
2026-11-12 14:32:41 +00:00
parent a53d901967
commit 5d8b0a94de
7 changed files with 27 additions and 41 deletions
+2 -2
View File
@@ -118,8 +118,8 @@ pub struct CellGridStyle {
pub(crate) background: StyleColor, pub(crate) background: StyleColor,
pub(crate) header: Stateful<StyleColor>, pub(crate) header: Stateful<StyleColor>,
pub(crate) header_text: Stateful<StyleColor>, pub(crate) header_text: Stateful<StyleColor>,
pub(crate) gridline: StyleColor, /// The line along the frozen panes' edge.
pub(crate) header_line: StyleColor, pub(crate) freeze_line: StyleColor,
pub(crate) accent: StyleColor, pub(crate) accent: StyleColor,
pub(crate) preview: StyleColor, pub(crate) preview: StyleColor,
pub(crate) selection: StyleColor, pub(crate) selection: StyleColor,
+5 -6
View File
@@ -3,7 +3,7 @@
use craft_behavior::{CursorIcon, HitLevel, HitPriority, Metrics, PartKey, ScrollPos, Sense, TextPos}; use craft_behavior::{CursorIcon, HitLevel, HitPriority, Metrics, PartKey, ScrollPos, Sense, TextPos};
use craft_ui::contract::{Access, Cx, Focus as _, Name, PaintUi, PartsCx, Remember as _, Role, Styled as _}; use craft_ui::contract::{Access, Cx, Focus as _, Name, PaintUi, PartsCx, Remember as _, Role, Styled as _};
use craft_ui::primitives::{Dashes, Draw, EditorLine, EditorPurpose, EditorRun, EditorState, HAlign, PaintShape, StrokeWidth, TextRun}; use craft_ui::primitives::{Dashes, Draw, EditorLine, EditorPurpose, EditorRun, EditorState, HAlign, PaintShape, RuleKind, StrokeWidth, TextRun};
use craft_ui::runtime::env::Label; use craft_ui::runtime::env::Label;
use craft_ui::runtime::motion::Animate as _; use craft_ui::runtime::motion::Animate as _;
use craft_ui::style::{Flags, Stateful, StyleCx, TypeRole}; use craft_ui::style::{Flags, Stateful, StyleCx, TypeRole};
@@ -51,8 +51,7 @@ pub(super) fn style(cx: &StyleCx<'_>, key: CellGridStyleKey) -> CellGridStyle {
background: cx.color(c.surface.background), background: cx.color(c.surface.background),
header: Stateful::new(cx.color(c.surface.chrome)).when(Flags::SELECTED, cx.color(c.state.selected)), header: Stateful::new(cx.color(c.surface.chrome)).when(Flags::SELECTED, cx.color(c.state.selected)),
header_text: Stateful::new(cx.color(c.text.muted)).when(Flags::SELECTED, cx.color(c.text.on_selected)), header_text: Stateful::new(cx.color(c.text.muted)).when(Flags::SELECTED, cx.color(c.text.on_selected)),
gridline: cx.color(c.line.divider), freeze_line: cx.color(c.line.border),
header_line: cx.color(c.line.border),
accent: cx.color(c.accent.solid), accent: cx.color(c.accent.solid),
preview: cx.color(c.text.muted), preview: cx.color(c.text.muted),
selection: cx.color(c.state.text_selection), selection: cx.color(c.state.text_selection),
@@ -297,13 +296,13 @@ impl CellGrid<'_> {
let (cells, split) = (geometry.cells(), geometry.split()); let (cells, split) = (geometry.cells(), geometry.split());
if frozen_columns > 0 { if frozen_columns > 0 {
let x = geometry.mirror_x(split.x); let x = geometry.mirror_x(split.x);
draw.stroke(&PaintShape::Path(vec![Pos2::new(x, cells.top()), Pos2::new(x, cells.bottom())]), StrokeWidth::Medium, style.header_line); draw.stroke(&PaintShape::Path(vec![Pos2::new(x, cells.top()), Pos2::new(x, cells.bottom())]), StrokeWidth::Medium, style.freeze_line);
} }
if frozen_rows > 0 { if frozen_rows > 0 {
draw.stroke( draw.stroke(
&PaintShape::Path(vec![Pos2::new(cells.left(), split.y), Pos2::new(cells.right(), split.y)]), &PaintShape::Path(vec![Pos2::new(cells.left(), split.y), Pos2::new(cells.right(), split.y)]),
StrokeWidth::Medium, StrokeWidth::Medium,
style.header_line, style.freeze_line,
); );
} }
if self.headings { if self.headings {
@@ -334,7 +333,7 @@ impl CellGrid<'_> {
} }
let marks = Marks::of(selection, geometry.rows().count(), geometry.columns().count()); let marks = Marks::of(selection, geometry.rows().count(), geometry.columns().count());
let rtl = geometry.mirror_x(0.0) > 0.0; let rtl = geometry.mirror_x(0.0) > 0.0;
let line = |from: Pos2, to: Pos2| draw.stroke(&PaintShape::Path(vec![from, to]), StrokeWidth::Hairline, style.header_line); let line = |from: Pos2, to: Pos2| draw.rule(RuleKind::Edge, from, to);
let look = |part: craft_behavior::PartRef| (cx.resolve_part(part, &style.header), cx.resolve_part(part, &style.header_text)); let look = |part: craft_behavior::PartRef| (cx.resolve_part(part, &style.header), cx.resolve_part(part, &style.header_text));
for &column in &frame.columns { for &column in &frame.columns {
let at = Self::column_header(geometry, column); let at = Self::column_header(geometry, column);
+5 -5
View File
@@ -5,7 +5,7 @@ use craft_theme::{Stateful, TypeRole};
use craft_ui::contract::{ use craft_ui::contract::{
Access, Common, Component, Cx, MeasureCx, Name, NoValue, Outcome, PaintUi, PartsCx, Props as _, Role, Styled as _, WordSetCx as _, Access, Common, Component, Cx, MeasureCx, Name, NoValue, Outcome, PaintUi, PartsCx, Props as _, Role, Styled as _, WordSetCx as _,
}; };
use craft_ui::primitives::{PaintShape, StrokeWidth, TextRun}; use craft_ui::primitives::{PaintShape, RuleKind, StrokeWidth, TextRun};
use craft_ui::runtime::env::Label; use craft_ui::runtime::env::Label;
use craft_ui::style::{ComponentStyle, FocusLook, StyleColor, StyleCx, TextLook}; use craft_ui::style::{ComponentStyle, FocusLook, StyleColor, StyleCx, TextLook};
use egui::{CornerRadius, Pos2, Rect, Response, Vec2, pos2, vec2}; use egui::{CornerRadius, Pos2, Rect, Response, Vec2, pos2, vec2};
@@ -180,7 +180,6 @@ pub struct OutlineBandStyle {
hit: f32, hit: f32,
radius: CornerRadius, radius: CornerRadius,
background: StyleColor, background: StyleColor,
edge: StyleColor,
button: StyleColor, button: StyleColor,
ink: StyleColor, ink: StyleColor,
} }
@@ -274,7 +273,6 @@ impl Component for OutlineBand<'_> {
hit: size.hit.slop, hit: size.hit.slop,
radius: CornerRadius::same(tokens.shape.radius.sm.round() as u8), radius: CornerRadius::same(tokens.shape.radius.sm.round() as u8),
background: cx.color(colors.surface.background), background: cx.color(colors.surface.background),
edge: cx.color(colors.line.border),
button: cx.color(colors.surface.chrome), button: cx.color(colors.surface.chrome),
ink, ink,
} }
@@ -345,13 +343,15 @@ impl Component for OutlineBand<'_> {
draw.fill(&PaintShape::Rect(geometry.mirror_rect(band), CornerRadius::ZERO), style.background); draw.fill(&PaintShape::Rect(geometry.mirror_rect(band), CornerRadius::ZERO), style.background);
} }
} }
let half = draw.rule_width(RuleKind::Edge) / 2.0;
let edge = |a: Pos2, b: Pos2| draw.rule(RuleKind::Edge, geometry.mirror_pos(a), geometry.mirror_pos(b));
if frame.row_band.width() > 0.0 { if frame.row_band.width() > 0.0 {
let band = frame.row_band; let band = frame.row_band;
draw.stroke(&line(pos2(band.right(), band.top()), pos2(band.right(), band.bottom())), StrokeWidth::Hairline, style.edge); edge(pos2(band.right() - half, band.top()), pos2(band.right() - half, band.bottom()));
} }
if frame.column_band.height() > 0.0 { if frame.column_band.height() > 0.0 {
let band = frame.column_band; let band = frame.column_band;
draw.stroke(&line(pos2(band.left(), band.bottom()), pos2(band.right(), band.bottom())), StrokeWidth::Hairline, style.edge); edge(pos2(band.left(), band.bottom() - half), pos2(band.right(), band.bottom() - half));
} }
for bracket in &self.laid.brackets { for bracket in &self.laid.brackets {
draw.stroke(&line(bracket.line[0], bracket.line[1]), StrokeWidth::Thin, style.ink); draw.stroke(&line(bracket.line[0], bracket.line[1]), StrokeWidth::Thin, style.ink);
+3 -6
View File
@@ -21,7 +21,7 @@ use craft_ui::contract::{
ItemKeys, ItemOutput, MeasureCx, Name, Outcome, PaintUi, PartsCx, Pointer as _, Props, Remember as _, Role, Scroll, ScrollAnchor, SelectMode, ItemKeys, ItemOutput, MeasureCx, Name, Outcome, PaintUi, PartsCx, Pointer as _, Props, Remember as _, Role, Scroll, ScrollAnchor, SelectMode,
Sizing, Styled as _, ToggleColumn, WordSetCx as _, Sizing, Styled as _, ToggleColumn, WordSetCx as _,
}; };
use craft_ui::primitives::{IconSpec, PaintShape, StrokeWidth}; use craft_ui::primitives::{IconSpec, PaintShape, RuleKind};
use craft_ui::runtime::commands::FireSource; use craft_ui::runtime::commands::FireSource;
use craft_ui::runtime::env::Label; use craft_ui::runtime::env::Label;
use craft_ui::style::{ComponentStyle, DataColor, IconLook, Stateful, StyleColor, StyleCx, SurfaceLook, TextLook, TypeRole, UiTokens}; use craft_ui::style::{ComponentStyle, DataColor, IconLook, Stateful, StyleColor, StyleCx, SurfaceLook, TextLook, TypeRole, UiTokens};
@@ -398,8 +398,6 @@ pub struct TrackRowStyle {
/// A switch while off. /// A switch while off.
#[style(icon)] #[style(icon)]
off: IconLook, off: IconLook,
/// The line under the header.
divider: StyleColor,
/// The colour strip while the track has no label and the pointer is over it. /// The colour strip while the track has no label and the pointer is over it.
hover: StyleColor, hover: StyleColor,
/// Between the strip and the name, and the indent of one level of depth. /// Between the strip and the name, and the indent of one level of depth.
@@ -471,7 +469,6 @@ impl Component for TrackRow<'_> {
name: TextLook::new(TypeRole::Caption, Stateful::new(cx.color(color.text.normal))), name: TextLook::new(TypeRole::Caption, Stateful::new(cx.color(color.text.normal))),
on: IconLook::new(cx.control().icon, Stateful::new(cx.color(color.text.normal))), on: IconLook::new(cx.control().icon, Stateful::new(cx.color(color.text.normal))),
off: IconLook::new(cx.control().icon, Stateful::new(cx.color(color.text.faint))), off: IconLook::new(cx.control().icon, Stateful::new(cx.color(color.text.faint))),
divider: cx.color(color.line.divider),
hover: cx.color(color.state.hover), hover: cx.color(color.state.hover),
pad: cx.tokens.space.pad.regular, pad: cx.tokens.space.pad.regular,
edge: cx.tokens.size.hit.target / 2.0, edge: cx.tokens.size.hit.target / 2.0,
@@ -609,8 +606,8 @@ impl Component for TrackRow<'_> {
} else if let Some(strip) = self.cell(&STRIP).filter(|_| cx.part(LABEL).hovered) { } else if let Some(strip) = self.cell(&STRIP).filter(|_| cx.part(LABEL).hovered) {
draw.fill(&PaintShape::Rect(strip, CornerRadius::ZERO), style.hover); draw.fill(&PaintShape::Rect(strip, CornerRadius::ZERO), style.hover);
} }
let divider = [Pos2::new(headers.left(), headers.bottom()), Pos2::new(headers.right(), headers.bottom())]; let y = headers.bottom() - draw.rule_width(RuleKind::Divider) / 2.0;
draw.stroke(&PaintShape::Path(divider.to_vec()), StrokeWidth::Hairline, style.divider); draw.rule(RuleKind::Divider, Pos2::new(headers.left(), y), Pos2::new(headers.right(), y));
if let Some(cell) = self.cell(&NAME) { if let Some(cell) = self.cell(&NAME) {
let indent = f32::from(self.track.depth) * style.pad; let indent = f32::from(self.track.depth) * style.pad;
let flow = craft_behavior::Flow::new(cell, rtl); let flow = craft_behavior::Flow::new(cell, rtl);
+4 -12
View File
@@ -289,13 +289,6 @@ impl<'a> Draw<'a> {
self.painter.extend(super::ResetMark::shapes(look, center, differs)); self.painter.extend(super::ResetMark::shapes(look, center, differs));
} }
/// `v` moved to the centre of the physical pixel it lies in: a line one physical pixel wide placed there fills that
/// pixel, and on a pixel edge it would split into two paler ones.
pub fn pixel_centre(&self, v: f32) -> f32 {
let ppp = self.painter.pixels_per_point();
((v * ppp).floor() + 0.5) / ppp
}
/// Paints the line of `kind` from `from` to `to`. /// Paints the line of `kind` from `from` to `to`.
pub fn rule(&self, kind: RuleKind, from: Pos2, to: Pos2) { pub fn rule(&self, kind: RuleKind, from: Pos2, to: Pos2) {
let look = rule_look(self.env.tokens, kind); let look = rule_look(self.env.tokens, kind);
@@ -320,11 +313,10 @@ impl<'a> Draw<'a> {
rule_look(self.env.tokens, kind).width.points(self.painter.pixels_per_point()) rule_look(self.env.tokens, kind).width.points(self.painter.pixels_per_point())
} }
/// Paints a line of theme width `width` and `color` from `from` to `to`, snapped to physical pixels as `rule` is. /// Paints the line `look` from `from` to `to`, snapped to physical pixels as `rule` is: a rule in a component's own
#[doc(hidden)] /// colour or width.
pub fn line_snapped(&self, width: StrokeWidth, color: Color32, from: Pos2, to: Pos2) { pub fn rule_with(&self, look: &crate::style::RuleLook, from: Pos2, to: Pos2) {
let look = crate::style::RuleLook { color, width: crate::style::RuleWidth::Points(self.width(width)) }; self.painter.extend(super::rule::Rule::shapes(look, from, to, self.painter.pixels_per_point()));
self.painter.extend(super::rule::Rule::shapes(&look, from, to, self.painter.pixels_per_point()));
} }
/// Paints a surface of `kind` (tracks, plot wells). /// Paints a surface of `kind` (tracks, plot wells).
+2 -2
View File
@@ -2,7 +2,7 @@
use crate::contract::{Access, Common, Component, Cx, MeasureCx, PaintUi, Pointer as _, Role, Styled as _}; use crate::contract::{Access, Common, Component, Cx, MeasureCx, PaintUi, Pointer as _, Role, Styled as _};
use crate::primitives::{StrokeWidth, TextLayout, TextRun, Wrap}; use crate::primitives::{StrokeWidth, TextLayout, TextRun, Wrap};
use crate::style::{ComponentStyle, FocusLook, Stateful, StyleCx, TextLook, TypeRole}; use crate::style::{ComponentStyle, FocusLook, RuleLook, RuleWidth, Stateful, StyleCx, TextLook, TypeRole};
use craft_behavior::{Flow, Metrics, PointerButton, PressPhase, Sense}; use craft_behavior::{Flow, Metrics, PointerButton, PressPhase, Sense};
use craft_theme::variant::Tone; use craft_theme::variant::Tone;
use craft_theme::{Contrast, States}; use craft_theme::{Contrast, States};
@@ -120,7 +120,7 @@ impl Component for Link {
draw.galley(Pos2::new(line.left(), top), &layout); draw.galley(Pos2::new(line.left(), top), &layout);
// The stroke's top edge sits on the line box's bottom, so the whole stroke lies below every descender. // The stroke's top edge sits on the line box's bottom, so the whole stroke lies below every descender.
let y = top + line_bottom(&layout).unwrap_or(layout.size().y) + underline / 2.0; let y = top + line_bottom(&layout).unwrap_or(layout.size().y) + underline / 2.0;
draw.line_snapped(cx.style.underline, color.egui(), Pos2::new(line.left(), y), Pos2::new(line.right(), y)); draw.rule_with(&RuleLook { color: color.egui(), width: RuleWidth::Points(underline) }, Pos2::new(line.left(), y), Pos2::new(line.right(), y));
} }
fn finish(self, cx: Cx<'_, Self>) -> Response { fn finish(self, cx: Cx<'_, Self>) -> Response {
@@ -6,9 +6,9 @@ use crate::contract::{
ShowContainer as _, Styled as _, WordSetCx as _, ShowContainer as _, Styled as _, WordSetCx as _,
}; };
use crate::menu::{self, Menu}; use crate::menu::{self, Menu};
use crate::primitives::{IconSpec, PaintShape, StrokeWidth}; use crate::primitives::{IconSpec, PaintShape};
use crate::runtime::env::Label; use crate::runtime::env::Label;
use crate::style::{ComponentStyle, Corners, FocusLook, IconLook, Stateful, StyleColor, StyleCx, SurfaceLook, TextLook}; use crate::style::{ComponentStyle, Corners, FocusLook, IconLook, RuleLook, RuleWidth, Stateful, StyleColor, StyleCx, SurfaceLook, TextLook};
use craft_behavior::{Flow, HitLevel, HitPriority, Metrics, NavMove, PartKey, Sense, Value}; use craft_behavior::{Flow, HitLevel, HitPriority, Metrics, NavMove, PartKey, Sense, Value};
use craft_theme::{ use craft_theme::{
States, Variant, States, Variant,
@@ -43,6 +43,7 @@ pub struct SplitButtonStyle {
chevron: Stateful<StyleColor>, chevron: Stateful<StyleColor>,
/// The line between the parts, in the variant's edge colour. /// The line between the parts, in the variant's edge colour.
divider: Stateful<StyleColor>, divider: Stateful<StyleColor>,
divider_width: RuleWidth,
/// The divider's gap from the top and bottom edges. /// The divider's gap from the top and bottom edges.
divider_inset: f32, divider_inset: f32,
radius: CornerRadius, radius: CornerRadius,
@@ -121,6 +122,7 @@ impl<T: Value + 'static> Component for SplitButton<'_, T> {
part_fill: Stateful::new(clear).off(States::new(clear, fill.hover, fill.pressed, clear)), part_fill: Stateful::new(clear).off(States::new(clear, fill.hover, fill.pressed, clear)),
chevron, chevron,
divider, divider,
divider_width: RuleWidth::Points(cx.tokens.stroke.thin),
divider_inset: cx.tokens.space.gap.inline, divider_inset: cx.tokens.space.gap.inline,
radius: parts.radius, radius: parts.radius,
} }
@@ -201,13 +203,9 @@ impl<T: Value + 'static> Component for SplitButton<'_, T> {
let chevron = Rect::from_center_size(arrow.center(), Vec2::splat(side)); let chevron = Rect::from_center_size(arrow.center(), Vec2::splat(side));
draw.icon(chevron, &crate::controls::disclosure::DisclosureGlyph::menu_button().color(cx.resolve(&cx.style.chevron))); draw.icon(chevron, &crate::controls::disclosure::DisclosureGlyph::menu_button().color(cx.resolve(&cx.style.chevron)));
let x = if rtl { arrow.right() } else { arrow.left() }; let x = if rtl { arrow.right() } else { arrow.left() };
let x = draw.pixel_centre(x);
let inset = cx.style.divider_inset; let inset = cx.style.divider_inset;
draw.stroke( let divider = RuleLook { color: cx.resolve(&cx.style.divider).egui(), width: cx.style.divider_width };
&PaintShape::Path(vec![Pos2::new(x, cx.rect.top() + inset), Pos2::new(x, cx.rect.bottom() - inset)]), draw.rule_with(&divider, Pos2::new(x, cx.rect.top() + inset), Pos2::new(x, cx.rect.bottom() - inset));
StrokeWidth::Thin,
cx.resolve(&cx.style.divider),
);
} }
fn finish(self, cx: Cx<'_, Self>) -> SplitOutput<T> { fn finish(self, cx: Cx<'_, Self>) -> SplitOutput<T> {