Content painters paint data colours in the component's data mode

DataMode holds the one mapping of a data colour (DataMode::color) and maps
every colour of a shape with it (DataMode::shape). ContentPainterCx gets
data_shapes for shapes whose colours are data, and its data methods
(picture, checker, checker_with, ramp) paint through the mode, sharing
data_picture and data_checker with Draw::texture and Draw::checker. A
States component's ramps, pictures and checkers in a content painter dim
while it paints its Disabled state, as its Draw's data colours do.

The histogram's bins and outlines and the notes editor's document text
paint through data_shapes.
This commit is contained in:
aiongg
2026-11-12 14:32:51 +00:00
parent 67b6471bef
commit b19f52dde8
9 changed files with 91 additions and 46 deletions
+2 -2
View File
@@ -120,9 +120,9 @@ pub fn paint_edge(painter: &ContentPainterCx<'_>, rect: Rect, frame: &PlotFrame)
}
/// Draws `data` in `look` into `rect`: no frame, no layout, no interaction, nothing in the AccessKit tree. `data` that
/// doesn't fit the look (RGB data in the tone look) and a degenerate `rect` draw nothing.
/// doesn't fit the look (RGB data in the tone look) and a degenerate `rect` draw nothing. The bins are data.
pub fn paint_bins(painter: &ContentPainterCx<'_>, rect: Rect, data: &HistogramData, look: HistogramLook, inks: &HistogramInks) {
painter.painter.extend(plot_shapes(rect, data, look, inks));
painter.data_shapes(plot_shapes(rect, data, look, inks));
}
/// Whether `rect` is a finite rectangle with room to draw in.
+1 -1
View File
@@ -511,7 +511,7 @@ impl Component for Histogram<'_> {
let look = if self.ribbons { HistogramLook::Rgb } else { HistogramLook::Tone };
paint_bins(&painter, rect, self.data, look, &style.inks);
if let Some((original, channel)) = self.outline_bins() {
painter.painter.extend(outline_shapes(rect, self.data, original, channel, &style.inks));
painter.data_shapes(outline_shapes(rect, self.data, original, channel, &style.inks));
}
if let Some(range) = Self::current(cx) {
painter.painter.extend(selection_shapes(rect, bins, range, style.marker_width, style.selection));
+4 -3
View File
@@ -23,7 +23,7 @@ use craft_ui::runtime::focus::KeyUse;
use craft_ui::runtime::pending::Pending;
use craft_ui::style::{ComponentStyle, DataColor, StyleColor, StyleCx, SurfaceLook, TypeRole};
use egui::epaint::text::CharIndex;
use egui::{Pos2, Rect, Response, Vec2};
use egui::{Pos2, Rect, Response, Shape, Vec2};
use super::editor::word_boundary;
use crate::lists::line_starts;
@@ -426,9 +426,10 @@ impl Component for NotesEditor<'_> {
);
}
}
painter.painter.galley(top_left, layout.galley.clone(), ink_color);
// The notes' runs are document text, so they paint as data.
painter.data_shapes([Shape::galley(top_left, layout.galley.clone(), ink_color)]);
if let Some((x, marker)) = laid.marker {
painter.painter.galley(Pos2::new(area.left() + x, y), marker.galley, ink_color);
painter.data_shapes([Shape::galley(Pos2::new(area.left() + x, y), marker.galley, ink_color)]);
}
if (range.start..=range.end).contains(&cursor.caret.min(chars)) {
let cursor_pos = layout.galley.pos_from_cursor(egui::text::CCursor::new(cursor.caret.min(chars) - range.start));
+3 -2
View File
@@ -298,8 +298,9 @@ pub trait Pattern {}
#[non_exhaustive]
pub enum DisabledLook {
/// The style's Disabled state: `text.disabled` ink, the theme's disabled fills, and the data colours its `Draw`
/// paints (a tone dot, a swatch icon, a series) at `Opacity::DISABLED`, each keeping its hue. Controls whose look
/// is chrome (buttons, fields, sliders) take it.
/// and its content painter's data paths paint (a tone dot, a swatch icon, a gradient ramp, document text) at
/// `Opacity::DISABLED`, each keeping its hue. Controls whose look is chrome (buttons, fields, sliders, a gradient
/// bar's stops) take it.
States,
/// The component paints its enabled look at `Opacity::DISABLED`: its surface, its paint, its faces and
/// `paint_over`, not its focus ring. A component that shows data (a picture, a colour, a plot) takes it, so the
+2 -1
View File
@@ -273,7 +273,8 @@ impl PaintUi<'_> {
own.faces.push(record);
}
/// The raw painter for an allowlisted content painter, clipped to the component's own rect.
/// The raw painter for an allowlisted content painter, clipped to the component's own rect. Its data paths paint
/// data colours as [`draw`](Self::draw) does.
pub fn content_painter<C: ContentPainter>(&self, cx: &Cx<'_, C>) -> ContentPainterCx<'_> {
ContentPainterCx::new(self.ui.painter().with_clip_rect(cx.core.rect.intersect(self.ui.clip_rect())), self.env, self.ink).data(self.data)
}
+5 -3
View File
@@ -25,14 +25,16 @@ const BOLD_FROM: u16 = 600;
/// The raw painter of an allowlisted content painter, clipped to its rect.
#[non_exhaustive]
pub struct ContentPainterCx<'a> {
/// The painter.
/// The painter, for theme colours. Colours from data go through [`data_shapes`](Self::data_shapes), the kit's data
/// methods (`picture`, `checker`, `ramp`) or `with_draw`, which dim them while the component paints its Disabled
/// state; a component that paints data with this painter takes `DisabledLook::Faded`.
pub painter: Painter,
pub(super) tokens: &'a UiTokens,
pub(super) direction: Direction,
env: &'a Env,
ink: Ink,
/// How `with_draw` paints data colours, as the component's `PaintUi::draw` does.
data: super::DataMode,
/// How the data paths and `with_draw` paint data colours, as the component's `PaintUi::draw` does.
pub(super) data: super::DataMode,
}
impl<'a> ContentPainterCx<'a> {
+53 -25
View File
@@ -182,6 +182,31 @@ impl DataMode {
pub(crate) fn of(state: &crate::style::StateSet) -> Self {
if state.state == craft_theme::State::Disabled { DataMode::Dimmed } else { DataMode::Full }
}
/// A colour from data as this mode paints it.
pub(crate) fn color(self, tokens: &UiTokens, color: Color32) -> Color32 {
self.map(crate::style::rgba_color(tokens.color.state.disabled_fill), color)
}
/// Maps every colour of `shape` (fills, strokes, mesh vertices, glyphs) as [`Self::color`] maps one.
pub(crate) fn shape(self, tokens: &UiTokens, shape: &mut Shape) {
if self == DataMode::Full {
return;
}
let inactive = crate::style::rgba_color(tokens.color.state.disabled_fill);
egui::epaint::shape_transform::adjust_colors(shape, move |color| *color = self.map(inactive, *color));
}
/// The one mapping, with `inactive` the theme's disabled fill. `Color32::PLACEHOLDER` passes through, so a
/// galley's fallback ink still resolves when it is painted.
fn map(self, inactive: Color32, color: Color32) -> Color32 {
match self {
_ if color == Color32::PLACEHOLDER => color,
DataMode::Full => color,
DataMode::Dimmed => color.gamma_multiply(craft_theme::Opacity::DISABLED),
DataMode::Inactive => inactive,
}
}
}
/// The content-paint API `PaintUi::draw` gives.
@@ -445,31 +470,14 @@ impl<'a> Draw<'a> {
/// is data: a disabled component paints it at `Opacity::DISABLED`, and a `Draw` for an inactive part fills `rect`
/// with the theme's disabled fill instead, as `data_fill` does.
pub fn texture(&self, rect: Rect, texture: TextureId, uv: Rect) {
let tint = match self.data {
DataMode::Inactive => {
self.data_fill(&PaintShape::Rect(rect, CornerRadius::ZERO), DataColor::new(Color32::TRANSPARENT));
return;
}
DataMode::Dimmed => super::textures::UNTINTED.gamma_multiply(craft_theme::Opacity::DISABLED),
DataMode::Full => super::textures::UNTINTED,
};
self.painter.image(texture, rect, uv, tint);
data_picture(self.painter, self.env.tokens, self.data, texture, rect, uv, CornerRadius::ZERO);
}
/// Paints `checker` over `rect` with corners `radius`, a light cell at the rect's top left. It shows through data,
/// so it paints as the data over it: at `Opacity::DISABLED` for a disabled component, and for an inactive part as
/// the theme's disabled fill, as `data_fill` does.
pub fn checker(&self, checker: &super::Checker, rect: Rect, radius: CornerRadius) {
let shape = || checker.shape(self.painter.ctx(), self.env.tokens, super::CheckerArea::Rect(rect, radius), rect.min);
match self.data {
DataMode::Inactive => self.data_fill(&PaintShape::Rect(rect, radius), DataColor::new(Color32::TRANSPARENT)),
DataMode::Dimmed => {
faded_painter(self.painter, craft_theme::Opacity::DISABLED).add(shape());
}
DataMode::Full => {
self.painter.add(shape());
}
}
data_checker(self.painter, self.env.tokens, self.data, checker, rect, radius);
}
/// Paints the well of a colour swatch over `well` with corners `radius`: the colour, over a checkerboard where it is
@@ -493,13 +501,9 @@ impl<'a> Draw<'a> {
}
}
/// The one mapping of a data colour, by `self.data`.
/// A data colour as `self.data` paints it.
fn data_color(&self, color: DataColor) -> Color32 {
match self.data {
DataMode::Full => color.0,
DataMode::Dimmed => color.0.gamma_multiply(craft_theme::Opacity::DISABLED),
DataMode::Inactive => crate::style::rgba_color(self.env.tokens.color.state.disabled_fill),
}
self.data.color(self.env.tokens, color.0)
}
/// Strokes the open path `path` as dashes (copy marks, drag previews, reference lines), with the width and colour
@@ -571,6 +575,30 @@ impl<'a> Draw<'a> {
}
}
/// Paints the part `uv` of `texture` into `rect` with corners `radius` as data in `mode`: tinted as `mode` maps a
/// colour, or for an inactive part `rect` in the theme's disabled fill. `Draw::texture` and `ContentPainterCx::picture`
/// paint through it.
pub(super) fn data_picture(painter: &Painter, tokens: &UiTokens, mode: DataMode, texture: TextureId, rect: Rect, uv: Rect, radius: CornerRadius) {
if mode == DataMode::Inactive {
painter.rect_filled(rect, radius, mode.color(tokens, Color32::TRANSPARENT));
} else {
crate::runtime::egui::textures::paint(painter, texture, rect, uv, mode.color(tokens, super::textures::UNTINTED), radius);
}
}
/// Paints `checker` over `rect` with corners `radius`, a light cell at the rect's top left, as the data over it in
/// `mode`: mapped as `mode` maps a colour, or for an inactive part `rect` in the theme's disabled fill. `Draw::checker`
/// and `ContentPainterCx::checker_with` paint through it.
pub(super) fn data_checker(painter: &Painter, tokens: &UiTokens, mode: DataMode, checker: &super::Checker, rect: Rect, radius: CornerRadius) {
if mode == DataMode::Inactive {
painter.rect_filled(rect, radius, mode.color(tokens, Color32::TRANSPARENT));
} else {
let mut shape = checker.shape(painter.ctx(), tokens, super::CheckerArea::Rect(rect, radius), rect.min);
mode.shape(tokens, &mut shape);
painter.add(shape);
}
}
/// `painter` with its opacity multiplied by `share`: the one fade `Draw::faded` and `ContentPainterCx::faded` share.
pub(super) fn faded_painter(painter: &Painter, share: f32) -> Painter {
let mut painter = painter.clone();
+20 -8
View File
@@ -772,19 +772,30 @@ impl ContentPainterCx<'_> {
self.painter.pixels_per_point()
}
/// Paints the part `uv` of `texture` into `rect`, untinted, with corners of `radius` points (zero for square ones).
pub fn picture(&self, texture: egui::TextureId, rect: Rect, uv: Rect, radius: f32) {
crate::runtime::egui::textures::paint(&self.painter, texture, rect, uv, super::textures::UNTINTED, UiTokens::corner(radius));
/// Paints shapes whose colours are data (a colour mesh, a plot's series, document text laid out by `layout`): as
/// they are, at `Opacity::DISABLED` while the component paints its Disabled state (each colour keeps its hue), and
/// in the theme's disabled fill for an inactive part, as `Draw::data_fill` paints a data colour.
pub fn data_shapes(&self, shapes: impl IntoIterator<Item = Shape>) {
self.painter.extend(shapes.into_iter().map(|mut shape| {
self.data.shape(self.tokens, &mut shape);
shape
}));
}
/// Paints the theme's transparency checkerboard over `rect`.
/// Paints the part `uv` of `texture` into `rect`, untinted, with corners of `radius` points (zero for square ones).
/// The picture is data, painted as `Draw::texture` paints one.
pub fn picture(&self, texture: egui::TextureId, rect: Rect, uv: Rect, radius: f32) {
super::draw::data_picture(&self.painter, self.tokens, self.data, texture, rect, uv, UiTokens::corner(radius));
}
/// Paints the theme's transparency checkerboard over `rect`, as the data over it; see [`Self::checker_with`].
pub fn checker(&self, rect: Rect) {
self.checker_with(&Checker::new(), rect, CornerRadius::ZERO);
}
/// Paints a colour ramp over `rect` with corners of `radius` along `axis`; see [`ramp`].
/// Paints a colour ramp over `rect` with corners of `radius` along `axis`, as data; see [`ramp`].
pub fn ramp(&self, rect: Rect, radius: CornerRadius, stops: &[(f32, DataColor)], axis: Axis) {
self.painter.extend(ramp(rect, radius, stops, axis));
self.data_shapes(ramp(rect, radius, stops, axis));
}
/// Paints the well of a plot over `rect` and returns the rect inside its edge, where the plot draws.
@@ -815,9 +826,10 @@ impl ContentPainterCx<'_> {
self.painter.extend(grid_lines(self.tokens, grid, ink, self.ppp()));
}
/// Paints the checkerboard `checker` over `rect` with corners of `radius`.
/// Paints the checkerboard `checker` over `rect` with corners of `radius`. It shows through data, so it paints as
/// the data over it, as `Draw::checker` does.
pub fn checker_with(&self, checker: &Checker, rect: Rect, radius: CornerRadius) {
self.painter.add(checker.shape(self.painter.ctx(), self.tokens, CheckerArea::Rect(rect, radius), rect.min));
super::draw::data_checker(&self.painter, self.tokens, self.data, checker, rect, radius);
}
/// Paints tick marks on one edge of `rect`.
+1 -1
View File
@@ -467,7 +467,7 @@ Semantic tokens fall in eight groups: surface, text, line, accent, status, state
Accent fills that carry text use the strong pair: the accent moved in lightness only as far as white text needs.
A disabled component looks one of two ways (`Component::DISABLED`). A control whose look is chrome (a button, a field, a slider) takes its style's Disabled state: `text.disabled` ink and the disabled fills, and its `Draw` paints the data colours it shows (a tone dot on a picker, a swatch icon) at `Opacity::DISABLED`, so they dim and keep their hues. A component that shows data (a picture, a colour, a plot, a tile) declares `DisabledLook::Faded`: step 13 paints its enabled look, faces included, at `Opacity::DISABLED`, so its data dims with the rest. The children it shows stay outside its fade and show their own disabled look.
A disabled component looks one of two ways (`Component::DISABLED`). A control whose look is chrome (a button, a field, a slider) takes its style's Disabled state: `text.disabled` ink and the disabled fills, and the data colours it shows (a tone dot on a picker, a swatch icon, a gradient ramp) paint at `Opacity::DISABLED`, so they dim and keep their hues: its `Draw` paints them so, and so do a content painter's data paths (`ContentPainterCx::data_shapes`, `picture`, `checker`, `ramp`). A component that shows data (a picture, a colour, a plot, a tile), or paints data with a content painter's bare `painter`, declares `DisabledLook::Faded`: step 13 paints its enabled look, faces included, at `Opacity::DISABLED`, so its data dims with the rest. The children it shows stay outside its fade and show their own disabled look.
### 5.2 Typography