ui-contract: the color range types move to a layer-2 module

craft-design-lint's mapped_crates_have_no_findings failed on main after
#3401. runtime::egui::text_edit (layer 2) imported COLORED_TEXT_LIMIT,
ColorRange and RangeInk from primitives::text_input (layer 3), and
ColorRange had public fields without #[non_exhaustive].

The four items move from primitives::text_input to the new module
primitives::color_ranges, which depends only on std and is mapped at
layer 2 in design/layers/craft-ui-contract/primitives.toml, as
contract::memo is in contract.toml. craft_ui::primitives still exports
them under the same names. ColorRange gains #[non_exhaustive]; callers
outside the crate already build it with ColorRange::new.
This commit is contained in:
dotCooCoo committed 2026-11-27 18:42:01 -08:00
1 parent 6bfe8e7678
commit 4f779c88fe
4 files changed
+60 -48

No files matched your search

@@ -0,0 +1,50 @@
//! The color ranges of a field's text: the inks a [`TextOptions::color_ranges`](crate::primitives::TextOptions::color_ranges)
//! gives stretches of the text, and the longest text a part colors.
use std::ops::Range;
/// The colour ranges of a text ([`TextOptions::color_ranges`](crate::primitives::TextOptions::color_ranges)): called with
/// the text each time the part lays it out, which is every frame the part shows. An app whose ranges are costly to find
/// keeps its own cache.
pub type ColorRanges = dyn Fn(&str) -> Vec<ColorRange>;
/// The longest text, in bytes, a part colours: a longer one is laid out in its plain ink, and its
/// [`ColorRanges`] is not called. It bounds the cost of an app's colouring (a highlighter whose time grows with the
/// square of the length on crafted input) at the size of a long script.
pub const COLORED_TEXT_LIMIT: usize = 16 * 1024;
/// A theme colour for a stretch of a field's text. Each is the token's colour moved, where it has to be, to the text
/// contrast minimum on the field's fill, so every ink reads in every theme and contrast.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum RangeInk {
/// `text.muted`.
Muted,
/// `accent.text`.
Accent,
/// `status.good.fg`.
Good,
/// `status.warn.fg`.
Warn,
/// `status.bad.fg`.
Bad,
/// `status.info.fg`.
Info,
}
/// One stretch of a text in an ink of its own: `range` is in bytes of the text the [`ColorRanges`] was given.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ColorRange {
/// The bytes the ink covers.
pub range: Range<usize>,
/// Their ink.
pub ink: RangeInk,
}
impl ColorRange {
/// The bytes `range` in `ink`.
pub fn new(range: Range<usize>, ink: RangeInk) -> Self {
ColorRange { range, ink }
}
}
+3 -2
View File
@@ -2,6 +2,7 @@
mod auto_scroll_mark;
pub mod backdrop;
mod color_ranges;
mod content;
mod draw;
mod face;
@@ -35,6 +36,7 @@ pub mod textures;
mod tool;
pub use auto_scroll_mark::AutoScrollMark;
pub use color_ranges::{COLORED_TEXT_LIMIT, ColorRange, ColorRanges, RangeInk};
pub use content::{ContentPainterCx, DocRun, DocText, FaceRun};
pub use draw::{Dashes, DataStrokeWidth, DataWidth, Draw, PaintShape, StrokeColor, StrokePlace, StrokeWidth, WellFill};
pub use face::FacePaint;
@@ -65,8 +67,7 @@ pub(crate) use svg_set::Layer;
pub use svg_set::{AppIcons, register_app_icons};
pub use text::{ChordText, HAlign, LayoutRun, LeastLayout, ReadingMeasure, Span, TextLayout, TextRun, Wrap};
pub use text_input::{
COLORED_TEXT_LIMIT, ColorRange, ColorRanges, Completer, Completion, EditorLine, EditorPurpose, EditorRun, EditorState, EditorSurrounding,
ImeStamp, RangeInk, SubmitKey, TabKey, TextKey, TextOptions,
Completer, Completion, EditorLine, EditorPurpose, EditorRun, EditorState, EditorSurrounding, ImeStamp, SubmitKey, TabKey, TextKey, TextOptions,
};
pub use textures::TextureCache;
pub use tool::ToolIcon;
@@ -1,6 +1,6 @@
//! The types of text parts and editors (18.2, 18.4): keys, options, completion and the own-caret editor state.
use crate::primitives::TextLayout;
use crate::primitives::{ColorRanges, TextLayout};
use crate::runtime::env::{Label, Message};
use crate::style::Ink;
use craft_behavior::{PartKey, TextPos};
@@ -52,50 +52,6 @@ pub enum TabKey {
/// Whether a pasted text is whole ([`TextOptions::whole_paste`]).
type WholePaste = dyn Fn(&str) -> bool;
/// The colour ranges of a text ([`TextOptions::color_ranges`]): called with the text each time the part lays it out,
/// which is every frame the part shows. An app whose ranges are costly to find keeps its own cache.
pub type ColorRanges = dyn Fn(&str) -> Vec<ColorRange>;
/// The longest text, in bytes, a part colours: a longer one is laid out in its plain ink, and its
/// [`ColorRanges`] is not called. It bounds the cost of an app's colouring (a highlighter whose time grows with the
/// square of the length on crafted input) at the size of a long script.
pub const COLORED_TEXT_LIMIT: usize = 16 * 1024;
/// A theme colour for a stretch of a field's text. Each is the token's colour moved, where it has to be, to the text
/// contrast minimum on the field's fill, so every ink reads in every theme and contrast.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum RangeInk {
/// `text.muted`.
Muted,
/// `accent.text`.
Accent,
/// `status.good.fg`.
Good,
/// `status.warn.fg`.
Warn,
/// `status.bad.fg`.
Bad,
/// `status.info.fg`.
Info,
}
/// One stretch of a text in an ink of its own: `range` is in bytes of the text the [`ColorRanges`] was given.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ColorRange {
/// The bytes the ink covers.
pub range: Range<usize>,
/// Their ink.
pub ink: RangeInk,
}
impl ColorRange {
/// The bytes `range` in `ink`.
pub fn new(range: Range<usize>, ink: RangeInk) -> Self {
ColorRange { range, ink }
}
}
/// How a text part behaves beyond its codec.
#[derive(Clone)]
pub struct TextOptions {
@@ -188,7 +144,7 @@ pub struct TextOptions {
/// Colours stretches of the text: the ranges it gives, in order, take their inks and the rest the part's ink. A range
/// that is empty, overlaps the one before, ends past the text or splits a character is skipped. Colour is display
/// only: editing, the caret, selection, wrapping and what assistive technology reads are as without it. A masked or
/// disabled part, and a text past [`COLORED_TEXT_LIMIT`], show no colours.
/// disabled part, and a text past [`COLORED_TEXT_LIMIT`](crate::primitives::COLORED_TEXT_LIMIT), show no colours.
pub color_ranges: Option<Arc<ColorRanges>>,
}
@@ -4,6 +4,11 @@ config_version = 1
path = "primitives"
layer = 3
# color_ranges depends only on std, so it sits with runtime::egui's text editor, which paints its inks.
[[module]]
path = "primitives::color_ranges"
layer = 2
[[module]]
path = "primitives::textures"
layer = 3