diff --git a/crates/font-manager/src/panel/browse/cards.rs b/crates/font-manager/src/panel/browse/cards.rs index 9ce4a50cf..9e87b0d25 100644 --- a/crates/font-manager/src/panel/browse/cards.rs +++ b/crates/font-manager/src/panel/browse/cards.rs @@ -1,6 +1,7 @@ //! The family card and the collection of cards the panel lists. use super::*; +use craft_ui::primitives::RectSide; /// What a family card asks the panel for. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -164,7 +165,6 @@ struct Placed { meta: Option, end: Vec<(End, Rect)>, sample: Rect, - rule: (Pos2, Pos2), } /// The sizes of the card's parts at one width. @@ -371,7 +371,6 @@ impl Component for FamilyCard { meta, end, sample, - rule: (rect.left_bottom(), rect.right_bottom()), }); } @@ -385,7 +384,7 @@ impl Component for FamilyCard { if let Some(meta) = placed.meta { draw.text(meta, &TextRun::new(self.data.meta.as_str()).role(style.meta.role).color(cx.resolve(&style.meta.color)).elide()); } - draw.rule(RuleKind::Divider, placed.rule.0, placed.rule.1); + draw.edge_rule(RuleKind::Divider, cx.rect, RectSide::Bottom); } for (piece, rect) in &placed.end { let pressed = with_end!(self, words, *piece, |child| clicked(&ui.child(("end", *piece as u8), *rect, child))); diff --git a/crates/table/src/ui/grid.rs b/crates/table/src/ui/grid.rs index 13c3c5069..b51122cca 100644 --- a/crates/table/src/ui/grid.rs +++ b/crates/table/src/ui/grid.rs @@ -121,6 +121,8 @@ pub struct CellGridStyle { /// The line along the frozen panes' edge. pub(crate) freeze_line: StyleColor, pub(crate) accent: StyleColor, + /// The thickness of the accent bar on a selected line's header edge. + pub(crate) accent_bar: f32, pub(crate) preview: StyleColor, pub(crate) selection: StyleColor, pub(crate) handle_fill: StyleColor, diff --git a/crates/table/src/ui/grid/geometry.rs b/crates/table/src/ui/grid/geometry.rs index cecbc9365..ecd646435 100644 --- a/crates/table/src/ui/grid/geometry.rs +++ b/crates/table/src/ui/grid/geometry.rs @@ -169,6 +169,11 @@ impl SheetGeometry { Self { layout, cells, header, frozen_size } } + /// Whether the sheet is mirrored for a right-to-left script. + pub fn right_to_left(&self) -> bool { + self.layout.right_to_left + } + /// The zoom the geometry was built at. pub fn zoom(&self) -> f32 { self.layout.zoom diff --git a/crates/table/src/ui/grid/paint.rs b/crates/table/src/ui/grid/paint.rs index 2731a6d28..16b23560d 100644 --- a/crates/table/src/ui/grid/paint.rs +++ b/crates/table/src/ui/grid/paint.rs @@ -3,7 +3,9 @@ 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::primitives::{Dashes, Draw, EditorLine, EditorPurpose, EditorRun, EditorState, HAlign, PaintShape, RuleKind, StrokeWidth, TextRun}; +use craft_ui::primitives::{ + Dashes, Draw, EditorLine, EditorPurpose, EditorRun, EditorState, HAlign, PaintShape, RectSide, RuleKind, StrokeWidth, TextRun, +}; use craft_ui::runtime::env::Label; use craft_ui::runtime::motion::Animate as _; use craft_ui::style::{Flags, Stateful, StyleCx, TypeRole}; @@ -53,6 +55,7 @@ pub(super) fn style(cx: &StyleCx<'_>, key: CellGridStyleKey) -> CellGridStyle { header_text: Stateful::new(cx.color(c.text.muted)).when(Flags::SELECTED, cx.color(c.text.on_selected)), freeze_line: cx.color(c.line.border), accent: cx.color(c.accent.solid), + accent_bar: cx.tokens.stroke.thick, preview: cx.color(c.text.muted), selection: cx.color(c.state.text_selection), handle_fill: cx.color(c.accent.solid), @@ -153,7 +156,7 @@ impl CellGrid<'_> { /// The fill handle's rect at the end-bottom corner of `outline` (screen coordinates). pub(super) fn handle_rect(geometry: &SheetGeometry, outline: Rect, size: f32) -> Rect { - let corner = if geometry.mirror_x(0.0) > 0.0 { outline.left_bottom() } else { outline.right_bottom() }; + let corner = if geometry.right_to_left() { outline.left_bottom() } else { outline.right_bottom() }; Rect::from_center_size(corner, Vec2::splat(size)) } @@ -163,7 +166,7 @@ impl CellGrid<'_> { let (columns, rows) = Self::visible_lines(&geometry); let sense = Sense::CLICK | Sense::DRAG | Sense::CONTEXT_CLICK | Sense::TAKE_FOCUS; let reach = craft_behavior::hit::LINE_REACH; - let rtl = geometry.mirror_x(0.0) > 0.0; + let rtl = geometry.right_to_left(); let marks = Marks::of(&self.snapshot, geometry.rows().count(), geometry.columns().count()); let flags = |held: Option| if held.is_some() { Flags::SELECTED } else { Flags::empty() }; if self.headings { @@ -332,8 +335,9 @@ impl CellGrid<'_> { draw.fill(&rect(geometry.mirror_rect(area)), plain); } let marks = Marks::of(selection, geometry.rows().count(), geometry.columns().count()); - let rtl = geometry.mirror_x(0.0) > 0.0; + let rtl = geometry.right_to_left(); let line = |from: Pos2, to: Pos2| draw.rule(RuleKind::Edge, from, to); + let bar = style.accent_bar; let look = |part: craft_behavior::PartRef| (cx.resolve_part(part, &style.header), cx.resolve_part(part, &style.header_text)); for &column in &frame.columns { let at = Self::column_header(geometry, column); @@ -342,10 +346,10 @@ impl CellGrid<'_> { draw.fill(&rect(at), fill); draw.text(at, &TextRun::new(self.model.column_label(column)).color(text).align(HAlign::Center).elide()); if state.is_some() { - draw.stroke(&PaintShape::Path(vec![at.left_bottom(), at.right_bottom()]), StrokeWidth::Thick, style.accent); + draw.fill(&rect(Rect::from_x_y_ranges(at.x_range(), at.bottom() - bar..=at.bottom())), style.accent); } + draw.edge_rule(RuleKind::Edge, at, RectSide::end(rtl)); let end = if rtl { at.left() } else { at.right() }; - line(Pos2::new(end, at.top()), Pos2::new(end, at.bottom())); if column + 1 < geometry.columns().count() && !geometry.columns().shown(column + 1) { let mark = if rtl { end - style.gap } else { end + style.gap }; line(Pos2::new(mark, at.top()), Pos2::new(mark, at.bottom())); @@ -363,14 +367,17 @@ impl CellGrid<'_> { draw.text(at, &number); } if state.is_some() { - let (from, to) = if rtl { (at.left_top(), at.left_bottom()) } else { (at.right_top(), at.right_bottom()) }; - draw.stroke(&PaintShape::Path(vec![from, to]), StrokeWidth::Thick, style.accent); + let across = if rtl { at.left()..=at.left() + bar } else { at.right() - bar..=at.right() }; + draw.fill(&rect(Rect::from_x_y_ranges(across, at.y_range())), style.accent); } - line(at.left_bottom(), at.right_bottom()); + draw.edge_rule(RuleKind::Edge, at, RectSide::Bottom); if row + 1 < geometry.rows().count() && !geometry.rows().shown(row + 1) { line(Pos2::new(at.left(), at.bottom() + style.gap), Pos2::new(at.right(), at.bottom() + style.gap)); } } + // The headers' edges toward the cells, across the corner too. + draw.edge_rule(RuleKind::Edge, geometry.mirror_rect(top.union(corner)), RectSide::Bottom); + draw.edge_rule(RuleKind::Edge, geometry.mirror_rect(left.union(corner)), RectSide::end(rtl)); } /// The open cell: its fill, edge, draft text and caret, and the editor part's report. @@ -400,7 +407,7 @@ impl CellGrid<'_> { fn place_chrome(&mut self, ui: &mut PaintUi<'_>, frame: &Frame, style: &CellGridStyle, outline: Label) { let geometry = &frame.geometry; let (side, gap) = (style.side, style.gap); - let rtl = geometry.mirror_x(0.0) > 0.0; + let rtl = geometry.right_to_left(); for filter in self.filters { if !frame.columns.contains(&filter.column) { continue; diff --git a/crates/table/src/ui/grid/row.rs b/crates/table/src/ui/grid/row.rs index c6081b9ea..688b4c05d 100644 --- a/crates/table/src/ui/grid/row.rs +++ b/crates/table/src/ui/grid/row.rs @@ -2,7 +2,7 @@ use craft_behavior::{ColumnKey, Metrics, PartKey, Sense}; use craft_ui::contract::{Access, Component, Cx, MeasureCx, Name, PaintUi, PartsCx, Role, Styled as _}; -use craft_ui::primitives::{PaintShape, RuleKind}; +use craft_ui::primitives::{PaintShape, RectSide, RuleKind}; use craft_ui::style::{ComponentStyle, Flags, Stateful, StyleColor, StyleCx}; use egui::{CornerRadius, Pos2, Rect, Response, Vec2}; @@ -114,9 +114,8 @@ impl Component for GridRow<'_> { draw.fill(&PaintShape::Rect(shape, CornerRadius::ZERO), cx.style.background); draw.fill(&PaintShape::Rect(shape, CornerRadius::ZERO), cx.resolve_part(PartKey::CELL.at(u64::from(column)), &cx.style.tint)); if gridlines { - let (top, bottom) = if rtl { (shape.left_top(), shape.left_bottom()) } else { (shape.right_top(), shape.right_bottom()) }; - draw.rule(RuleKind::Grid, shape.left_bottom(), shape.right_bottom()); - draw.rule(RuleKind::Grid, top, bottom); + draw.edge_rule(RuleKind::Divider, shape, RectSide::Bottom); + draw.edge_rule(RuleKind::Divider, shape, RectSide::end(rtl)); } } self.model.paint_cell(ui, shape, cell, self.zoom); diff --git a/crates/table/src/ui/outline.rs b/crates/table/src/ui/outline.rs index de020c046..40ea459f5 100644 --- a/crates/table/src/ui/outline.rs +++ b/crates/table/src/ui/outline.rs @@ -5,7 +5,7 @@ use craft_theme::{Stateful, TypeRole}; use craft_ui::contract::{ Access, Common, Component, Cx, MeasureCx, Name, NoValue, Outcome, PaintUi, PartsCx, Props as _, Role, Styled as _, WordSetCx as _, }; -use craft_ui::primitives::{PaintShape, RuleKind, StrokeWidth, TextRun}; +use craft_ui::primitives::{PaintShape, RectSide, RuleKind, StrokeWidth, TextRun}; use craft_ui::runtime::env::Label; use craft_ui::style::{ComponentStyle, FocusLook, StyleColor, StyleCx, TextLook}; use egui::{CornerRadius, Pos2, Rect, Response, Vec2, pos2, vec2}; @@ -343,15 +343,11 @@ impl Component for OutlineBand<'_> { 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 { - let band = frame.row_band; - edge(pos2(band.right() - half, band.top()), pos2(band.right() - half, band.bottom())); + draw.edge_rule(RuleKind::Edge, geometry.mirror_rect(frame.row_band), RectSide::end(geometry.right_to_left())); } if frame.column_band.height() > 0.0 { - let band = frame.column_band; - edge(pos2(band.left(), band.bottom() - half), pos2(band.right(), band.bottom() - half)); + draw.edge_rule(RuleKind::Edge, geometry.mirror_rect(frame.column_band), RectSide::Bottom); } for bracket in &self.laid.brackets { draw.stroke(&line(bracket.line[0], bracket.line[1]), StrokeWidth::Thin, style.ink); diff --git a/crates/timeline/src/ui/stack.rs b/crates/timeline/src/ui/stack.rs index 8f3adfa68..c99dcf6ba 100644 --- a/crates/timeline/src/ui/stack.rs +++ b/crates/timeline/src/ui/stack.rs @@ -22,7 +22,7 @@ use craft_ui::contract::{ ItemKeys, ItemOutput, MeasureCx, Name, Outcome, PaintUi, PartsCx, Pointer as _, Props, Remember as _, Role, Scroll, ScrollAnchor, SelectMode, Sizing, Styled as _, ToggleColumn, WordSetCx as _, }; -use craft_ui::primitives::{IconSpec, PaintShape, RuleKind}; +use craft_ui::primitives::{IconSpec, PaintShape, RectSide, RuleKind}; use craft_ui::runtime::commands::FireSource; use craft_ui::runtime::env::Label; use craft_ui::style::{ComponentStyle, DataColor, IconLook, Stateful, StyleColor, StyleCx, SurfaceLook, TextLook, TypeRole, UiTokens}; @@ -604,8 +604,7 @@ impl Component for TrackRow<'_> { } else if let Some(strip) = self.cell(&STRIP).filter(|_| cx.part(LABEL).hovered) { draw.fill(&PaintShape::Rect(strip, CornerRadius::ZERO), style.hover); } - let y = headers.bottom() - draw.rule_width(RuleKind::Divider) / 2.0; - draw.rule(RuleKind::Divider, Pos2::new(headers.left(), y), Pos2::new(headers.right(), y)); + draw.edge_rule(RuleKind::Divider, headers, RectSide::Bottom); if let Some(cell) = self.cell(&NAME) { let indent = f32::from(self.track.depth) * style.pad; let flow = craft_behavior::Flow::new(cell, rtl); diff --git a/crates/ui-canvas/src/canvas/rulers/mod.rs b/crates/ui-canvas/src/canvas/rulers/mod.rs index d55a79e55..723fca895 100644 --- a/crates/ui-canvas/src/canvas/rulers/mod.rs +++ b/crates/ui-canvas/src/canvas/rulers/mod.rs @@ -17,7 +17,7 @@ use crate::contract::{ PaintUi, PartsCx, Pointer as _, Props as _, Role, Styled as _, WordSetCx as _, }; use crate::menu::Menu; -use crate::primitives::{PaintShape, StrokeWidth, TextRun, Tick, TickEdge, TickRank}; +use crate::primitives::{PaintShape, RectSide, StrokeWidth, TextRun, Tick, TickEdge, TickRank}; use crate::runtime::env::{EnvView, Label, UnitKind}; use crate::runtime::focus::KeyUse; use crate::runtime::memory::Remember as _; @@ -558,8 +558,8 @@ impl Component for Rulers<'_> { } draw.fill(&rect(corner), style.fill); - draw.rule(RuleKind::Divider, Pos2::new(top.left(), top.bottom()), Pos2::new(top.right(), top.bottom())); - draw.rule(RuleKind::Divider, Pos2::new(left.right(), left.top()), Pos2::new(left.right(), left.bottom())); + draw.edge_rule(RuleKind::Divider, top, RectSide::Bottom); + draw.edge_rule(RuleKind::Divider, left, RectSide::Right); } fn access(&self, access: &mut Access, cx: &Cx<'_, Self>) { diff --git a/crates/ui-contract/src/contract/body/regions.rs b/crates/ui-contract/src/contract/body/regions.rs index bf75608da..cd50603d2 100644 --- a/crates/ui-contract/src/contract/body/regions.rs +++ b/crates/ui-contract/src/contract/body/regions.rs @@ -4,7 +4,7 @@ use super::BodyHooks; use crate::contract::cx::BodyCx; use crate::contract::{Container, ContainerProps, FooterPlace, Gap, HeaderParts, LeastWidth, Pad, PaneEdge, Sizing}; use crate::layout::ColumnGroup; -use crate::primitives::Rule; +use crate::primitives::{RectSide, Rule}; use crate::runtime::access::{self}; use crate::runtime::egui::{alloc, child, style}; use crate::runtime::env::Env; @@ -138,19 +138,16 @@ impl RegionFrame<'_> { /// edge toward the window's centre, mirrored for a start or end pane under right-to-left. pub(super) fn paint_edge_rule(ui: &mut Ui, props: &ContainerProps, env: &Env, rect: Rect) { let Some(edge) = env.pane_edge.filter(|_| props.edge_rule) else { return }; - let rule = rule_look(&env.tokens, RuleKind::Divider); - let half = rule.width.points(ui.ctx().pixels_per_point()) / 2.0; let rtl = env.direction.is_rtl(); - let (from, to) = match edge { - PaneEdge::Start if rtl => (Pos2::new(rect.min.x + half, rect.min.y), Pos2::new(rect.min.x + half, rect.max.y)), - PaneEdge::Start => (Pos2::new(rect.max.x - half, rect.min.y), Pos2::new(rect.max.x - half, rect.max.y)), - PaneEdge::End if rtl => (Pos2::new(rect.max.x - half, rect.min.y), Pos2::new(rect.max.x - half, rect.max.y)), - PaneEdge::End => (Pos2::new(rect.min.x + half, rect.min.y), Pos2::new(rect.min.x + half, rect.max.y)), - PaneEdge::Top => (Pos2::new(rect.min.x, rect.max.y - half), Pos2::new(rect.max.x, rect.max.y - half)), - PaneEdge::Bottom => (Pos2::new(rect.min.x, rect.min.y + half), Pos2::new(rect.max.x, rect.min.y + half)), + let side = match edge { + PaneEdge::Start => RectSide::end(rtl), + PaneEdge::End => RectSide::start(rtl), + PaneEdge::Top => RectSide::Bottom, + PaneEdge::Bottom => RectSide::Top, PaneEdge::Center => return, }; - ui.painter().extend(Rule::shapes(&rule, from, to, ui.ctx().pixels_per_point())); + let rule = rule_look(&env.tokens, RuleKind::Divider); + ui.painter().extend(Rule::edge(&rule, rect, side, ui.ctx().pixels_per_point())); } /// The divider rules above and under the header, above the footer and beside the end region. @@ -175,14 +172,13 @@ pub(super) fn paint_rules(ui: &mut Ui, props: &ContainerProps, env: &Env, inner: } // The end region's rule runs along its edge toward the body, beside a footer placed in it too. if props.end.is_some_and(|end| end.rule) && regions.end.width() > 0.0 { - let half = rule.width.points(ppp) / 2.0; - let x = if env.direction.is_rtl() { regions.end.max.x - half } else { regions.end.min.x + half }; let (top, bottom) = if footer_in_end(props) { (regions.end.min.y.min(regions.footer.min.y), regions.end.max.y.max(regions.footer.max.y)) } else { (regions.end.min.y, regions.end.max.y) }; - ui.painter().extend(Rule::shapes(&rule, Pos2::new(x, top), Pos2::new(x, bottom), ppp)); + let end = Rect::from_x_y_ranges(regions.end.x_range(), top..=bottom); + ui.painter().extend(Rule::edge(&rule, end, RectSide::start(env.direction.is_rtl()), ppp)); } } diff --git a/crates/ui-contract/src/primitives/draw.rs b/crates/ui-contract/src/primitives/draw.rs index 7e50fd96c..75ab0949f 100644 --- a/crates/ui-contract/src/primitives/draw.rs +++ b/crates/ui-contract/src/primitives/draw.rs @@ -2,7 +2,7 @@ use super::content::DocText; use super::record::PaintRecord; -use super::rule::RuleKind; +use super::rule::{RectSide, RuleKind}; use super::surface::Surface; use super::text::{TextEnv, TextLayout, TextRun}; use crate::fonts::stack_facts; @@ -322,6 +322,13 @@ impl<'a> Draw<'a> { self.painter.extend(super::rule::Rule::shapes(&look, from, to, self.painter.pixels_per_point())); } + /// Paints the line of `kind` along the `side` edge of `rect`, on the physical pixels just inside it + /// (`Rule::edge`): a row's bottom rule, a cell's gridline, a strip's edge toward its neighbour. + pub fn edge_rule(&self, kind: RuleKind, rect: Rect, side: RectSide) { + let look = rule_look(self.env.tokens, kind); + self.painter.extend(super::rule::Rule::edge(&look, rect, side, self.painter.pixels_per_point())); + } + /// Paints the scroll bar in `track` along `axis` for `span`, in `state` (`ScrollBar::shapes`). pub fn scroll_bar( &self, diff --git a/crates/ui-contract/src/primitives/mod.rs b/crates/ui-contract/src/primitives/mod.rs index 977d5ad2d..ef01b3fad 100644 --- a/crates/ui-contract/src/primitives/mod.rs +++ b/crates/ui-contract/src/primitives/mod.rs @@ -40,7 +40,7 @@ pub use kit::{ }; pub use marker::{Marker, MarkerSize}; pub use reset_mark::ResetMark; -pub use rule::{Rule, RuleKind}; +pub use rule::{RectSide, Rule, RuleKind}; pub use scroll_bar::{HiddenContent, ScrollBar}; pub use sensing::{PartResult, TextOutcome}; pub use state_layer::{StateLayer, StateLayerKind}; diff --git a/crates/ui-contract/src/primitives/rule.rs b/crates/ui-contract/src/primitives/rule.rs index d0e70bd01..c91ed542c 100644 --- a/crates/ui-contract/src/primitives/rule.rs +++ b/crates/ui-contract/src/primitives/rule.rs @@ -1,13 +1,39 @@ //! `Rule`: dividers, edges, grid lines and drop marks, with one pixel-snap rule. use crate::style::RuleLook; -use egui::{Pos2, Shape, Stroke}; +use egui::{Pos2, Rect, Shape, Stroke}; pub use crate::style::RuleKind; /// The rule painter. pub struct Rule; +/// A side of a rect, on screen: left and right stay put under right-to-left. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub enum RectSide { + /// The top edge. + Top, + /// The bottom edge. + Bottom, + /// The left edge. + Left, + /// The right edge. + Right, +} + +impl RectSide { + /// The start side in reading order: left, or right under right-to-left. + pub const fn start(rtl: bool) -> RectSide { + if rtl { RectSide::Right } else { RectSide::Left } + } + + /// The end side in reading order: right, or left under right-to-left. + pub const fn end(rtl: bool) -> RectSide { + if rtl { RectSide::Left } else { RectSide::Right } + } +} + 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. @@ -25,4 +51,18 @@ impl Rule { }; vec![Shape::line_segment([from, to], Stroke::new(physical / pixels_per_point, look.color))] } + + /// 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. + pub fn edge(look: &RuleLook, rect: Rect, side: RectSide, pixels_per_point: f32) -> Vec { + let half = look.width.points(pixels_per_point) / 2.0; + let (from, to) = match side { + RectSide::Top => (Pos2::new(rect.left(), rect.top() + half), Pos2::new(rect.right(), rect.top() + half)), + RectSide::Bottom => (Pos2::new(rect.left(), rect.bottom() - half), Pos2::new(rect.right(), rect.bottom() - half)), + RectSide::Left => (Pos2::new(rect.left() + half, rect.top()), Pos2::new(rect.left() + half, rect.bottom())), + RectSide::Right => (Pos2::new(rect.right() - half, rect.top()), Pos2::new(rect.right() - half, rect.bottom())), + }; + Self::shapes(look, from, to, pixels_per_point) + } } diff --git a/crates/ui-contract/tests/it/paint/painters.rs b/crates/ui-contract/tests/it/paint/painters.rs index bca2ac14a..e14fe04b9 100644 --- a/crates/ui-contract/tests/it/paint/painters.rs +++ b/crates/ui-contract/tests/it/paint/painters.rs @@ -58,3 +58,36 @@ fn a_rounded_ramp_keeps_inside_its_corners_and_a_square_one_fills_its_rect() { } } } + +#[trial] +fn an_edge_rule_lies_on_the_pixels_just_inside_its_side_at_any_scale() { + use craft_ui::primitives::{RectSide, Rule}; + use craft_ui::style::{RuleLook, RuleWidth}; + use egui::{Color32, Rect, Shape}; + + for ppp in [1.0, 1.5, 2.0] { + // Edges on physical pixel boundaries, as laid-out rows and cells have them. + let rect = Rect::from_min_max(pos2(30.0, 45.0), pos2(150.0, 90.0)) / ppp; + for width in [RuleWidth::Hairline, RuleWidth::Points(2.0)] { + let look = RuleLook { color: Color32::WHITE, width }; + for side in [RectSide::Top, RectSide::Bottom, RectSide::Left, RectSide::Right] { + let shapes = Rule::edge(&look, rect, side, ppp); + let [Shape::LineSegment { points: [from, to], stroke }] = shapes.as_slice() else { + panic!("one line segment for {side:?}"); + }; + let half = stroke.width / 2.0; + let horizontal = matches!(side, RectSide::Top | RectSide::Bottom); + let (at, low, high) = if horizontal { (from.y, rect.top(), rect.bottom()) } else { (from.x, rect.left(), rect.right()) }; + assert!(at - half >= low - EPS && at + half <= high + EPS, "{side:?} at {ppp}x: the band {at}±{half} leaves the rect"); + let edge = match side { + RectSide::Top => at - half - low, + RectSide::Left => at - half - low, + _ => high - (at + half), + }; + assert!(edge.abs() < EPS, "{side:?} at {ppp}x: the band does not touch its side"); + let along = if horizontal { (from.x, to.x, rect.left(), rect.right()) } else { (from.y, to.y, rect.top(), rect.bottom()) }; + assert!((along.0 - along.2).abs() < EPS && (along.1 - along.3).abs() < EPS, "{side:?} runs the whole side"); + } + } + } +} diff --git a/crates/ui-shell/src/shell/title_bar/bar.rs b/crates/ui-shell/src/shell/title_bar/bar.rs index 918ca1788..e9d88828e 100644 --- a/crates/ui-shell/src/shell/title_bar/bar.rs +++ b/crates/ui-shell/src/shell/title_bar/bar.rs @@ -11,7 +11,7 @@ use crate::contract::{ PaintUi, Role, ShowContainer as _, Sizing, WordSetCx as _, }; use crate::layout::{Placed, arrange}; -use crate::primitives::RuleKind; +use crate::primitives::{RectSide, RuleKind}; use crate::runtime::access::Name; use crate::runtime::egui::pass; use crate::runtime::env::Env; @@ -21,7 +21,7 @@ use crate::shell::{Caption, CaptionButton, CaptionIcons, Frame, ShellWords, Slot use crate::style::{StyleCx, SurfaceKind}; use craft_behavior::Sense; use craft_host::target::{DoubleClick, WindowState}; -use egui::{Response, Ui, pos2}; +use egui::{Response, Ui}; /// What a title bar reports: the caption clicked and whether the window was asked to close. #[derive(Debug)] @@ -243,9 +243,7 @@ impl Component for TitleBar<'_> { if !self.separator { return; } - let draw = ui.draw(); - let y = cx.rect.bottom() - draw.rule_width(RuleKind::Divider) / 2.0; - draw.rule(RuleKind::Divider, pos2(cx.rect.left(), y), pos2(cx.rect.right(), y)); + ui.draw().edge_rule(RuleKind::Divider, cx.rect, RectSide::Bottom); } fn finish(self, cx: Cx<'_, Self>) -> TitleBarOutput { diff --git a/crates/ui-widgets/src/list/row/show.rs b/crates/ui-widgets/src/list/row/show.rs index 84942163d..ae710b43e 100644 --- a/crates/ui-widgets/src/list/row/show.rs +++ b/crates/ui-widgets/src/list/row/show.rs @@ -16,7 +16,7 @@ use crate::controls::disclosure::Disclosure; use crate::display::Progress; use crate::list::toggle::{ToggleFace, ToggleState}; use crate::list::words::ListWords; -use crate::primitives::{RuleKind, StrokeWidth, TextRun, Wrap, glyph}; +use crate::primitives::{RectSide, RuleKind, StrokeWidth, TextRun, Wrap, glyph}; use crate::runtime::commands::FireSource; use crate::runtime::env::{Label, WordTemplate}; use crate::style::{Flags, IconLook, StyleCx, SurfaceLook, TextLook, UiTokens}; @@ -454,7 +454,7 @@ impl Component for ListRow<'_> { { let draw = ui.draw(); if self.rule || (self.layer && style.dividers) { - draw.rule(RuleKind::Divider, Pos2::new(rect.left(), rect.bottom()), Pos2::new(rect.right(), rect.bottom())); + draw.edge_rule(RuleKind::Divider, rect, RectSide::Bottom); } if self.layer && style.dividers { for x in &placed.dividers { diff --git a/crates/ui-widgets/src/tabs/tab.rs b/crates/ui-widgets/src/tabs/tab.rs index ea4bd2bd6..1a162b420 100644 --- a/crates/ui-widgets/src/tabs/tab.rs +++ b/crates/ui-widgets/src/tabs/tab.rs @@ -13,7 +13,7 @@ use crate::contract::{ Styled as _, WordSetCx as _, }; use crate::display::Progress; -use crate::primitives::{IconSpec, PaintShape, StrokeWidth, TextRun}; +use crate::primitives::{IconSpec, PaintShape, RectSide, StrokeWidth, TextRun}; use crate::runtime::env::Label; use crate::style::{ComponentStyle, Corners, Flags, IconLook, RuleKind, StyleColor, StyleCx, SurfaceLook, TextLook, UiTokens}; use craft_behavior::{Flow, IconId, Insets, Metrics, PartKey, PointerButton, PressPhase, Proposal, Sense}; @@ -408,8 +408,7 @@ impl Component for Tab<'_> { let selected = cx.resolved.selected == Some(true) || cx.state().flags.contains(Flags::SELECTED); let draw = ui.draw(); if style.divider { - let x = cx.flow.end_x(); - draw.rule(RuleKind::Divider, Pos2::new(x, rect.top()), Pos2::new(x, rect.bottom())); + draw.edge_rule(RuleKind::Divider, rect, RectSide::end(cx.flow.is_rtl())); } if let (Some(color), true) = (style.indicator, selected) { let bar = Rect::from_min_size(rect.min, Vec2::new(rect.width(), sizes.indicator.min(rect.height()))); diff --git a/crates/ui-widgets/src/tile_grid/grid.rs b/crates/ui-widgets/src/tile_grid/grid.rs index b7369205d..01b3c9344 100644 --- a/crates/ui-widgets/src/tile_grid/grid.rs +++ b/crates/ui-widgets/src/tile_grid/grid.rs @@ -20,7 +20,7 @@ use crate::contract::{ use crate::contract::{lead_position, section_part}; use crate::controls::disclosure::Disclosure; use crate::list::{DropTest, ListRow, RowTest, ThumbSize}; -use crate::primitives::PaintShape; +use crate::primitives::{PaintShape, RectSide}; use crate::runtime::access::Name; use crate::runtime::commands::FireSource; use crate::runtime::env::Label; @@ -646,7 +646,7 @@ impl Component for TileGrid<'_, K> if placed.pinned { let draw = ui.draw(); draw.fill(&PaintShape::Rect(rect, CornerRadius::ZERO), style.pinned); - draw.rule(RuleKind::Divider, rect.left_bottom(), rect.right_bottom()); + draw.edge_rule(RuleKind::Divider, rect, RectSide::Bottom); } let lead = RowState { emphasized: lead == Some(Slot::Header(placed.group)), ..RowState::default() }; let mut heading = ListRow::new(group.title.clone()).heading().node(false).state(lead);