mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 00:34:25 +00:00
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:
@@ -118,8 +118,8 @@ pub struct CellGridStyle {
|
||||
pub(crate) background: StyleColor,
|
||||
pub(crate) header: Stateful<StyleColor>,
|
||||
pub(crate) header_text: Stateful<StyleColor>,
|
||||
pub(crate) gridline: StyleColor,
|
||||
pub(crate) header_line: StyleColor,
|
||||
/// The line along the frozen panes' edge.
|
||||
pub(crate) freeze_line: StyleColor,
|
||||
pub(crate) accent: StyleColor,
|
||||
pub(crate) preview: StyleColor,
|
||||
pub(crate) selection: StyleColor,
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
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, 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::motion::Animate as _;
|
||||
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),
|
||||
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)),
|
||||
gridline: cx.color(c.line.divider),
|
||||
header_line: cx.color(c.line.border),
|
||||
freeze_line: cx.color(c.line.border),
|
||||
accent: cx.color(c.accent.solid),
|
||||
preview: cx.color(c.text.muted),
|
||||
selection: cx.color(c.state.text_selection),
|
||||
@@ -297,13 +296,13 @@ impl CellGrid<'_> {
|
||||
let (cells, split) = (geometry.cells(), geometry.split());
|
||||
if frozen_columns > 0 {
|
||||
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 {
|
||||
draw.stroke(
|
||||
&PaintShape::Path(vec![Pos2::new(cells.left(), split.y), Pos2::new(cells.right(), split.y)]),
|
||||
StrokeWidth::Medium,
|
||||
style.header_line,
|
||||
style.freeze_line,
|
||||
);
|
||||
}
|
||||
if self.headings {
|
||||
@@ -334,7 +333,7 @@ impl CellGrid<'_> {
|
||||
}
|
||||
let marks = Marks::of(selection, geometry.rows().count(), geometry.columns().count());
|
||||
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));
|
||||
for &column in &frame.columns {
|
||||
let at = Self::column_header(geometry, column);
|
||||
|
||||
@@ -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, StrokeWidth, TextRun};
|
||||
use craft_ui::primitives::{PaintShape, 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};
|
||||
@@ -180,7 +180,6 @@ pub struct OutlineBandStyle {
|
||||
hit: f32,
|
||||
radius: CornerRadius,
|
||||
background: StyleColor,
|
||||
edge: StyleColor,
|
||||
button: StyleColor,
|
||||
ink: StyleColor,
|
||||
}
|
||||
@@ -274,7 +273,6 @@ impl Component for OutlineBand<'_> {
|
||||
hit: size.hit.slop,
|
||||
radius: CornerRadius::same(tokens.shape.radius.sm.round() as u8),
|
||||
background: cx.color(colors.surface.background),
|
||||
edge: cx.color(colors.line.border),
|
||||
button: cx.color(colors.surface.chrome),
|
||||
ink,
|
||||
}
|
||||
@@ -345,13 +343,15 @@ 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;
|
||||
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 {
|
||||
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 {
|
||||
draw.stroke(&line(bracket.line[0], bracket.line[1]), StrokeWidth::Thin, style.ink);
|
||||
|
||||
@@ -21,7 +21,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, StrokeWidth};
|
||||
use craft_ui::primitives::{IconSpec, PaintShape, 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};
|
||||
@@ -398,8 +398,6 @@ pub struct TrackRowStyle {
|
||||
/// A switch while off.
|
||||
#[style(icon)]
|
||||
off: IconLook,
|
||||
/// The line under the header.
|
||||
divider: StyleColor,
|
||||
/// The colour strip while the track has no label and the pointer is over it.
|
||||
hover: StyleColor,
|
||||
/// 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))),
|
||||
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))),
|
||||
divider: cx.color(color.line.divider),
|
||||
hover: cx.color(color.state.hover),
|
||||
pad: cx.tokens.space.pad.regular,
|
||||
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) {
|
||||
draw.fill(&PaintShape::Rect(strip, CornerRadius::ZERO), style.hover);
|
||||
}
|
||||
let divider = [Pos2::new(headers.left(), headers.bottom()), Pos2::new(headers.right(), headers.bottom())];
|
||||
draw.stroke(&PaintShape::Path(divider.to_vec()), StrokeWidth::Hairline, style.divider);
|
||||
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));
|
||||
if let Some(cell) = self.cell(&NAME) {
|
||||
let indent = f32::from(self.track.depth) * style.pad;
|
||||
let flow = craft_behavior::Flow::new(cell, rtl);
|
||||
|
||||
@@ -289,13 +289,6 @@ impl<'a> Draw<'a> {
|
||||
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`.
|
||||
pub fn rule(&self, kind: RuleKind, from: Pos2, to: Pos2) {
|
||||
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())
|
||||
}
|
||||
|
||||
/// Paints a line of theme width `width` and `color` from `from` to `to`, snapped to physical pixels as `rule` is.
|
||||
#[doc(hidden)]
|
||||
pub fn line_snapped(&self, width: StrokeWidth, color: Color32, 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()));
|
||||
/// Paints the line `look` from `from` to `to`, snapped to physical pixels as `rule` is: a rule in a component's own
|
||||
/// colour or width.
|
||||
pub fn rule_with(&self, look: &crate::style::RuleLook, from: Pos2, to: Pos2) {
|
||||
self.painter.extend(super::rule::Rule::shapes(look, from, to, self.painter.pixels_per_point()));
|
||||
}
|
||||
|
||||
/// Paints a surface of `kind` (tracks, plot wells).
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use crate::contract::{Access, Common, Component, Cx, MeasureCx, PaintUi, Pointer as _, Role, Styled as _};
|
||||
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_theme::variant::Tone;
|
||||
use craft_theme::{Contrast, States};
|
||||
@@ -120,7 +120,7 @@ impl Component for Link {
|
||||
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.
|
||||
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 {
|
||||
|
||||
@@ -6,9 +6,9 @@ use crate::contract::{
|
||||
ShowContainer as _, Styled as _, WordSetCx as _,
|
||||
};
|
||||
use crate::menu::{self, Menu};
|
||||
use crate::primitives::{IconSpec, PaintShape, StrokeWidth};
|
||||
use crate::primitives::{IconSpec, PaintShape};
|
||||
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_theme::{
|
||||
States, Variant,
|
||||
@@ -43,6 +43,7 @@ pub struct SplitButtonStyle {
|
||||
chevron: Stateful<StyleColor>,
|
||||
/// The line between the parts, in the variant's edge colour.
|
||||
divider: Stateful<StyleColor>,
|
||||
divider_width: RuleWidth,
|
||||
/// The divider's gap from the top and bottom edges.
|
||||
divider_inset: f32,
|
||||
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)),
|
||||
chevron,
|
||||
divider,
|
||||
divider_width: RuleWidth::Points(cx.tokens.stroke.thin),
|
||||
divider_inset: cx.tokens.space.gap.inline,
|
||||
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));
|
||||
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 = draw.pixel_centre(x);
|
||||
let inset = cx.style.divider_inset;
|
||||
draw.stroke(
|
||||
&PaintShape::Path(vec![Pos2::new(x, cx.rect.top() + inset), Pos2::new(x, cx.rect.bottom() - inset)]),
|
||||
StrokeWidth::Thin,
|
||||
cx.resolve(&cx.style.divider),
|
||||
);
|
||||
let divider = RuleLook { color: cx.resolve(&cx.style.divider).egui(), width: cx.style.divider_width };
|
||||
draw.rule_with(÷r, Pos2::new(x, cx.rect.top() + inset), Pos2::new(x, cx.rect.bottom() - inset));
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> SplitOutput<T> {
|
||||
|
||||
Reference in New Issue
Block a user