Merge rework-d2d: Info panel, host-labelled rows, page label column, egui text band width

This commit is contained in:
aiongg
2026-11-21 09:23:35 +00:00
16 changed files with 300 additions and 108 deletions
Generated
+14 -14
View File
@@ -5449,7 +5449,7 @@ dependencies = [
[[package]]
name = "ecolor"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6525c87923ca47465aac244460fe4b1139349060#6525c87923ca47465aac244460fe4b1139349060"
source = "git+https://github.com/storytold/egui.git?rev=72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1#72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1"
dependencies = [
"bytemuck",
"emath",
@@ -5505,7 +5505,7 @@ dependencies = [
[[package]]
name = "eframe"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6525c87923ca47465aac244460fe4b1139349060#6525c87923ca47465aac244460fe4b1139349060"
source = "git+https://github.com/storytold/egui.git?rev=72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1#72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1"
dependencies = [
"ahash",
"bytemuck",
@@ -5540,7 +5540,7 @@ dependencies = [
[[package]]
name = "egui"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6525c87923ca47465aac244460fe4b1139349060#6525c87923ca47465aac244460fe4b1139349060"
source = "git+https://github.com/storytold/egui.git?rev=72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1#72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1"
dependencies = [
"accesskit",
"ahash",
@@ -5559,7 +5559,7 @@ dependencies = [
[[package]]
name = "egui-wgpu"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6525c87923ca47465aac244460fe4b1139349060#6525c87923ca47465aac244460fe4b1139349060"
source = "git+https://github.com/storytold/egui.git?rev=72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1#72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1"
dependencies = [
"ahash",
"bytemuck",
@@ -5578,7 +5578,7 @@ dependencies = [
[[package]]
name = "egui-winit"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6525c87923ca47465aac244460fe4b1139349060#6525c87923ca47465aac244460fe4b1139349060"
source = "git+https://github.com/storytold/egui.git?rev=72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1#72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1"
dependencies = [
"block2 0.6.2",
"bytemuck",
@@ -5598,7 +5598,7 @@ dependencies = [
[[package]]
name = "egui_glow"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6525c87923ca47465aac244460fe4b1139349060#6525c87923ca47465aac244460fe4b1139349060"
source = "git+https://github.com/storytold/egui.git?rev=72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1#72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1"
dependencies = [
"bytemuck",
"egui",
@@ -5611,7 +5611,7 @@ dependencies = [
[[package]]
name = "egui_kittest"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6525c87923ca47465aac244460fe4b1139349060#6525c87923ca47465aac244460fe4b1139349060"
source = "git+https://github.com/storytold/egui.git?rev=72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1#72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1"
dependencies = [
"eframe",
"egui",
@@ -5688,7 +5688,7 @@ dependencies = [
[[package]]
name = "emath"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6525c87923ca47465aac244460fe4b1139349060#6525c87923ca47465aac244460fe4b1139349060"
source = "git+https://github.com/storytold/egui.git?rev=72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1#72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1"
dependencies = [
"bytemuck",
]
@@ -5761,7 +5761,7 @@ dependencies = [
[[package]]
name = "epaint"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6525c87923ca47465aac244460fe4b1139349060#6525c87923ca47465aac244460fe4b1139349060"
source = "git+https://github.com/storytold/egui.git?rev=72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1#72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1"
dependencies = [
"ahash",
"bytemuck",
@@ -5787,7 +5787,7 @@ dependencies = [
[[package]]
name = "epaint_default_fonts"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6525c87923ca47465aac244460fe4b1139349060#6525c87923ca47465aac244460fe4b1139349060"
source = "git+https://github.com/storytold/egui.git?rev=72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1#72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1"
[[package]]
name = "equator"
@@ -15118,19 +15118,19 @@ dependencies = [
[[patch.unused]]
name = "egui_demo_lib"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6525c87923ca47465aac244460fe4b1139349060#6525c87923ca47465aac244460fe4b1139349060"
source = "git+https://github.com/storytold/egui.git?rev=72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1#72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1"
[[patch.unused]]
name = "egui_extras"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6525c87923ca47465aac244460fe4b1139349060#6525c87923ca47465aac244460fe4b1139349060"
source = "git+https://github.com/storytold/egui.git?rev=72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1#72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1"
[[patch.unused]]
name = "egui_inspection"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6525c87923ca47465aac244460fe4b1139349060#6525c87923ca47465aac244460fe4b1139349060"
source = "git+https://github.com/storytold/egui.git?rev=72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1#72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1"
[[patch.unused]]
name = "egui_system_font_provider"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=6525c87923ca47465aac244460fe4b1139349060#6525c87923ca47465aac244460fe4b1139349060"
source = "git+https://github.com/storytold/egui.git?rev=72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1#72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1"
+14 -14
View File
@@ -310,20 +310,20 @@ hayro-postscript = { path = "vendor/hayro-postscript" }
linesweeper = { path = "vendor/linesweeper" }
vello_common = { path = "vendor/vello_common" }
vello_cpu = { path = "vendor/vello_cpu" }
ecolor = { git = "https://github.com/storytold/egui.git", rev = "6525c87923ca47465aac244460fe4b1139349060" }
emath = { git = "https://github.com/storytold/egui.git", rev = "6525c87923ca47465aac244460fe4b1139349060" }
epaint = { git = "https://github.com/storytold/egui.git", rev = "6525c87923ca47465aac244460fe4b1139349060" }
epaint_default_fonts = { git = "https://github.com/storytold/egui.git", rev = "6525c87923ca47465aac244460fe4b1139349060" }
egui = { git = "https://github.com/storytold/egui.git", rev = "6525c87923ca47465aac244460fe4b1139349060" }
egui-winit = { git = "https://github.com/storytold/egui.git", rev = "6525c87923ca47465aac244460fe4b1139349060" }
egui-wgpu = { git = "https://github.com/storytold/egui.git", rev = "6525c87923ca47465aac244460fe4b1139349060" }
egui_glow = { git = "https://github.com/storytold/egui.git", rev = "6525c87923ca47465aac244460fe4b1139349060" }
egui_extras = { git = "https://github.com/storytold/egui.git", rev = "6525c87923ca47465aac244460fe4b1139349060" }
egui_kittest = { git = "https://github.com/storytold/egui.git", rev = "6525c87923ca47465aac244460fe4b1139349060" }
egui_system_font_provider = { git = "https://github.com/storytold/egui.git", rev = "6525c87923ca47465aac244460fe4b1139349060" }
eframe = { git = "https://github.com/storytold/egui.git", rev = "6525c87923ca47465aac244460fe4b1139349060" }
egui_demo_lib = { git = "https://github.com/storytold/egui.git", rev = "6525c87923ca47465aac244460fe4b1139349060" }
egui_inspection = { git = "https://github.com/storytold/egui.git", rev = "6525c87923ca47465aac244460fe4b1139349060" }
ecolor = { git = "https://github.com/storytold/egui.git", rev = "72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1" }
emath = { git = "https://github.com/storytold/egui.git", rev = "72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1" }
epaint = { git = "https://github.com/storytold/egui.git", rev = "72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1" }
epaint_default_fonts = { git = "https://github.com/storytold/egui.git", rev = "72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1" }
egui = { git = "https://github.com/storytold/egui.git", rev = "72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1" }
egui-winit = { git = "https://github.com/storytold/egui.git", rev = "72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1" }
egui-wgpu = { git = "https://github.com/storytold/egui.git", rev = "72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1" }
egui_glow = { git = "https://github.com/storytold/egui.git", rev = "72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1" }
egui_extras = { git = "https://github.com/storytold/egui.git", rev = "72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1" }
egui_kittest = { git = "https://github.com/storytold/egui.git", rev = "72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1" }
egui_system_font_provider = { git = "https://github.com/storytold/egui.git", rev = "72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1" }
eframe = { git = "https://github.com/storytold/egui.git", rev = "72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1" }
egui_demo_lib = { git = "https://github.com/storytold/egui.git", rev = "72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1" }
egui_inspection = { git = "https://github.com/storytold/egui.git", rev = "72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1" }
winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" }
# symphonia's FLAC decoder restores LPC samples as `residual + (prediction as i32)`. In a valid
+57 -51
View File
@@ -4,8 +4,10 @@ use craft_behavior::ColumnWidth;
use craft_cms::convert::CmykConverter;
use craft_theme::Srgba;
use craft_theme::variant::Tone;
use craft_ui::contract::{Outcome as _, Pattern, Props as _, Show as _, ShowContainer as _};
use craft_ui::display::Text;
use craft_ui::contract::{Gap, Outcome as _, Pattern, Props as _, Show as _, ShowContainer as _};
use craft_ui::display::{Divider, Text};
use craft_ui::form::{Field, FieldGroup};
use craft_ui::layout::{CellAlign, GridFit};
use craft_ui::layout_components::{Grid, Stack};
use craft_ui::notices::{Notice, NoticeLayout};
use craft_ui::runtime::env::{Env, Label};
@@ -19,9 +21,8 @@ pub(crate) mod stories;
/// What a missing value shows.
const DASH: &str = "–";
/// The grid's columns: a name and its value, twice, so that the two readouts of a line stand side by side and each
/// column lines up down the grid.
const COLUMNS: [ColumnWidth; 4] = [ColumnWidth::Content, ColumnWidth::Content, ColumnWidth::Content, ColumnWidth::Content];
/// A block's two halves, equal in width.
const HALVES: [ColumnWidth; 2] = [ColumnWidth::fill(1.0), ColumnWidth::fill(1.0)];
craft_ui::words! {
/// The words of the Info panel, in the installed language unless set.
@@ -179,9 +180,10 @@ impl<'a> InfoReadings<'a> {
}
}
/// A read-only grid: RGB beside CMYK, the Ruler tool's measure (the angle A beside the length L, or a protractor's arms
/// L1 and L2 stacked beside it), X/Y beside W/H, the document size, then one row group per sampler; a dash for a
/// missing value; `document: None` shows "No document" as a centred notice in place of the grid.
/// A read-only panel of blocks, a rule between them, each block two equal halves of labelled readouts: RGB beside CMYK
/// (in percent), the Ruler tool's measure (the angle A beside the length L, or a protractor's arms L1 and L2 stacked
/// beside it), X/Y beside W/H, the document size, then a position block and a colour block per sampler; a dash for a
/// missing value; `document: None` shows "No document" as a centred notice in place of the blocks.
#[must_use = "show the panel with `show`"]
#[derive(Pattern)]
pub struct InfoPanel<'a> {
@@ -192,43 +194,38 @@ pub struct InfoPanel<'a> {
/// A name and its value, or `None` for a dash.
type Cell = (Label, Option<Label>);
/// One block: the readouts of its left and right halves.
type Block = [Vec<Cell>; 2];
/// The value of a number, or `None` when it is missing.
fn number(value: Option<impl ToString>) -> Option<Label> {
value.map(|value| Label::from(value.to_string()))
}
/// A name and its value in two grid cells, or two empty cells.
fn pair(ui: &mut Ui, cell: Option<Cell>) {
match cell {
Some((name, value)) => {
Text::new(name).show(ui);
Text::new(value.unwrap_or_else(|| Label::from(DASH))).show(ui);
}
None => {
Text::new(Label::from("")).show(ui);
Text::new(Label::from("")).show(ui);
}
}
/// The value of a CMYK percent, or `None` when it is missing.
fn percent(value: Option<u8>) -> Option<Label> {
value.map(|value| Label::from(format!("{value}%")))
}
/// A grid of lines, each two name and value pairs.
fn table(ui: &mut Ui, lines: Vec<[Option<Cell>; 2]>) {
let _ = Grid::new(&COLUMNS).show(ui, |ui, _| {
for [left, right] in lines.iter().cloned() {
pair(ui, left);
pair(ui, right);
/// A half of a block: its readouts as the fields of one form group, so the names carry their colons and share a
/// column and the values line up.
fn half(ui: &mut Ui, cells: &[Cell]) {
let _ = FieldGroup::new(Label::from("")).gap(Gap::Tight).show(ui, |ui, _| {
for (name, value) in cells {
let value = value.clone().unwrap_or_else(|| Label::from(DASH));
let _ = Field::new(name.clone()).value(value.as_str()).show(ui, |ui, _| {
Text::new(value.clone()).show(ui);
});
}
});
}
/// `cells` two to a line.
fn in_pairs(cells: Vec<Cell>) -> Vec<[Option<Cell>; 2]> {
let mut cells = cells.into_iter();
let mut lines = Vec::new();
while let Some(left) = cells.next() {
lines.push([Some(left), cells.next()]);
}
lines
/// A block: two equal halves, which stay beside each other in a narrow panel.
fn block(ui: &mut Ui, [left, right]: &Block) {
let _ = Grid::new(&HALVES).fit(GridFit::Fixed).cells(CellAlign::Top).show(ui, |ui, _| {
half(ui, left);
half(ui, right);
});
}
impl<'a> InfoPanel<'a> {
@@ -250,38 +247,47 @@ impl<'a> InfoPanel<'a> {
Stack::new()
.label(label)
.show(ui, |ui, _| {
let mut lines = Vec::new();
for (at, cmyk_name) in cmyk_names.iter().enumerate() {
let rgb = rgb_names.get(at).map(|rgb_name| (rgb_name.clone(), number(readings.color.map(|color| color.rgb[at]))));
lines.push([rgb, Some((cmyk_name.clone(), number(readings.color.map(|color| color.cmyk[at]))))]);
}
let color = readings.color;
let rgb = rgb_names.iter().enumerate().map(|(at, name)| (name.clone(), number(color.map(|color| color.rgb[at])))).collect();
let cmyk = cmyk_names.iter().enumerate().map(|(at, name)| (name.clone(), percent(color.map(|color| color.cmyk[at])))).collect();
let mut blocks = vec![[rgb, cmyk]];
match &readings.measure {
Some(InfoMeasure::Line { angle, length }) => {
lines.push([Some((name(&words.angle), Some(angle.clone()))), Some((name(&words.length), Some(length.clone())))]);
blocks.push([vec![(name(&words.angle), Some(angle.clone()))], vec![(name(&words.length), Some(length.clone()))]]);
}
Some(InfoMeasure::Protractor { angle, first, second }) => {
lines.push([Some((name(&words.angle), Some(angle.clone()))), Some((name(&words.length1), Some(first.clone())))]);
lines.push([None, Some((name(&words.length2), Some(second.clone())))]);
blocks.push([
vec![(name(&words.angle), Some(angle.clone()))],
vec![(name(&words.length1), Some(first.clone())), (name(&words.length2), Some(second.clone()))],
]);
}
None => {}
}
let (position, extent) = (readings.position.as_ref(), readings.extent.as_ref());
let axes = [(name(&words.x), name(&words.w)), (name(&words.pos_y), name(&words.h))];
let (mut at, mut size) = (Vec::new(), Vec::new());
for (axis, (position_name, extent_name)) in axes.into_iter().enumerate() {
lines.push([
Some((position_name, position.map(|at| at[axis].clone()))),
Some((extent_name, extent.map(|size| size[axis].clone()))),
]);
at.push((position_name, position.map(|at| at[axis].clone())));
size.push((extent_name, extent.map(|size| size[axis].clone())));
}
blocks.push([at, size]);
for (at, readings) in blocks.iter().enumerate() {
if at > 0 {
Divider::horizontal().show(ui);
}
block(ui, readings);
}
table(ui, lines);
let size = words.document.fill(&[("width", &width.to_string()), ("height", &height.to_string())]);
Divider::horizontal().show(ui);
Text::new(Label::from(size)).show(ui);
for (readout, position) in readings.samplers {
Divider::horizontal().show(ui);
Text::new(Label::from(format!("#{}", readout.number))).show(ui);
let mut lines = vec![[Some((name(&words.x), Some(position[0].clone()))), Some((name(&words.pos_y), Some(position[1].clone())))]];
lines.extend(in_pairs(rgb_names.iter().zip(readout.rgb).map(|(name, value)| (name.clone(), number(Some(value)))).collect()));
lines.extend(in_pairs(cmyk_names.iter().zip(readout.cmyk).map(|(name, value)| (name.clone(), number(Some(value)))).collect()));
table(ui, lines);
let place = [vec![(name(&words.x), Some(position[0].clone()))], vec![(name(&words.pos_y), Some(position[1].clone()))]];
let rgb = rgb_names.iter().zip(readout.rgb).map(|(name, value)| (name.clone(), number(Some(value)))).collect();
let cmyk = cmyk_names.iter().zip(readout.cmyk).map(|(name, value)| (name.clone(), percent(Some(value)))).collect();
block(ui, &place);
block(ui, &[rgb, cmyk]);
}
})
.output
+24
View File
@@ -421,6 +421,30 @@ fn measure_names(measure: Option<craft_color_ui::ui::InfoMeasure>) -> [bool; 4]
["A", "L", "L1", "L2"].map(|name| fixture.by_label(name).is_some())
}
/// The painted run whose text is `text`, without a colon.
fn run(fixture: &Fixture, text: &str) -> egui::Rect {
fixture.texts().iter().find(|run| run.text.trim_end_matches(':') == text).unwrap_or_else(|| panic!("no run \"{text}\"")).rect
}
#[trial]
fn the_info_panel_shows_rgb_and_cmyk_as_two_aligned_columns() {
let mut fixture = Fixture::new(|ui| {
let color = craft_color_ui::ui::InfoColor::from_parts([10, 200, 30], [1, 22, 33, 44]);
let readings = craft_color_ui::ui::InfoReadings::new().color(Some(color)).document(Some([640, 480]));
let _ = craft_color_ui::ui::InfoPanel::new("Info", &readings).show(ui);
});
fixture.settle();
let [r, g, b] = ["10", "200", "30"].map(|value| run(&fixture, value));
let [c, m, y, k] = ["1%", "22%", "33%", "44%"].map(|value| run(&fixture, value));
assert!(c.left() > r.right().max(g.right()).max(b.right()), "CMYK starts right of RGB: {r:?} {c:?}");
assert!(r.left() == g.left() && g.left() == b.left(), "the RGB values share a column");
assert!(c.left() == m.left() && m.left() == y.left() && y.left() == k.left(), "the CMYK values share a column");
let [rn, gn, bn] = ["R", "G", "B"].map(|name| run(&fixture, name));
assert!(rn.right() == gn.right() && gn.right() == bn.right(), "the RGB labels end at one x");
assert!(rn.right() < r.left(), "the labels sit left of their values");
assert!(fixture.texts().iter().any(|run| run.text == ":"), "a label carries its colon");
}
#[trial]
fn the_info_panel_shows_the_rulers_measure_while_it_has_one() {
use craft_color_ui::ui::InfoMeasure;
+1 -1
View File
@@ -3,7 +3,7 @@
/// The egui fork the library is built and tested with. Apps patch egui to exactly this.
pub const GIT: &str = "https://github.com/storytold/egui.git";
/// The commit of [`GIT`].
pub const REV: &str = "6525c87923ca47465aac244460fe4b1139349060";
pub const REV: &str = "72cb386ac73dc0ade2b2d15cc8314e236a8e5eb1";
/// The packages the workspace patches to the fork (the `[patch.crates-io]` entries that name [`GIT`]).
pub const PACKAGES: &[&str] = &[
+3 -3
View File
@@ -8,8 +8,8 @@ use craft_theme::{ContrastChoice, ModeChoice, MotionChoice, Size, TextSize, Them
use craft_ui::choice::RadioGroup;
use craft_ui::contract::{Bind, Entry, Held, NoValue, Outcome, Props as _, Show as _, ShowContainer as _};
use craft_ui::display::Text;
use craft_ui::form::{Field, FieldGroup};
use craft_ui::layout_components::Row;
use craft_ui::form::Field;
use craft_ui::layout_components::{Row, Stack};
use craft_ui::menu::Menu;
use craft_ui::pickers::Select;
use craft_ui::runtime::env::{Env, Label};
@@ -110,7 +110,7 @@ impl<'a> AppearancePage<'a> {
let mut held = Held::new(draft);
let mut toggled = None;
let mut changed = false;
let mut shown = FieldGroup::new(words.appearance.clone()).show(ui, |ui, cx| {
let mut shown = Stack::new().label(words.appearance.clone()).show(ui, |ui, cx| {
let before = held.read(cx).unwrap_or_default();
let mut now = before.clone();
let shown = now.applied_to(&suite.shown);
+8 -2
View File
@@ -5,6 +5,7 @@ use crate::{SelectionChange, SuiteClient};
use craft_theme::{ThemeId, builtins};
use craft_ui::choice::RadioGroup;
use craft_ui::contract::{Props as _, Show as _, ShowContainer as _};
use craft_ui::form::FieldGroup;
use craft_ui::layout_components::Row;
use craft_ui::testing::{Drive, StoryData, StoryOutput, story};
use egui::Ui;
@@ -18,13 +19,18 @@ fn favorites() -> Vec<ThemeId> {
fn appearance_page(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let client = SuiteClient::memory().view();
let favorites = favorites();
AppearancePage::new(&client, data.bind("draft", SelectionChange::default())).favorites(&favorites).show(ui).into()
let mut draft = data.bind("draft", SelectionChange::default());
FieldGroup::new("Appearance").show(ui, |ui, _| AppearancePage::new(&client, draft.reborrowed()).favorites(&favorites).show(ui)).output.into()
}
#[story(AppearancePage<'static>, covers = [], drive = Drive::Never)]
fn appearance_page_without_text_size(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let client = SuiteClient::memory().view();
AppearancePage::new(&client, data.bind("plain", SelectionChange::default())).text_size(false).system_dark(true).show(ui).into()
let mut draft = data.bind("plain", SelectionChange::default());
FieldGroup::new("Appearance")
.show(ui, |ui, _| AppearancePage::new(&client, draft.reborrowed()).text_size(false).system_dark(true).show(ui))
.output
.into()
}
/// One theme's chips in a group: plain, chosen, favourite, and both. A click on a star toggles the chip's favourite.
+75
View File
@@ -0,0 +1,75 @@
//! A run laid out in room equal to its own width.
use craft_behavior::Sense;
use craft_ui::contract::{Common, Component, Cx, MeasureCx, Props as _, Role, Show as _};
use craft_ui::primitives::{TextRun, Wrap};
use craft_ui::style::StyleCx;
use craft_ui::testing::{Fixture, trial};
use craft_ui_test_kit::contract::PlainStyle;
use egui::{Response, Vec2};
use std::cell::Cell;
use std::rc::Rc;
const WORDS: [&str; 9] = ["200", "100", "44%", "40%", "201", "45%", "Hello", "Width", "mm"];
/// Lays each word out unbounded and again in room equal to that width, and counts the words whose width is less than
/// half a point past a whole point: the widths a whole-point wrap width would round down.
#[derive(Component)]
struct OwnWidth {
#[common]
common: Common,
below_half: Rc<Cell<usize>>,
}
impl Component for OwnWidth {
const ROLE: Role = Role::GROUP;
type Style = PlainStyle;
type Output = Response;
fn style(_: &StyleCx<'_>, _: OwnWidthStyleKey) -> PlainStyle {
PlainStyle::default()
}
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
let mut below_half = 0;
for word in WORDS {
for wrap in [Wrap::Wrap, Wrap::Elide] {
let run = TextRun::new(word).wrap(wrap);
let whole = cx.layout_at(&run, f32::INFINITY);
let width = whole.size().x;
let fitted = cx.layout_at(&run, width);
assert_eq!(fitted.galley.rows.len(), 1, "{word:?} {wrap:?}");
assert!(!fitted.elided, "{word:?} {wrap:?}");
assert_eq!(fitted.size().x, width, "{word:?} {wrap:?}: room equal to its width keeps its width");
}
let width = cx.layout_at(&TextRun::new(word), f32::INFINITY).size().x;
if width.fract() > 0.0 && width.fract() < 0.5 {
below_half += 1;
}
}
self.below_half.set(below_half);
Vec2::ZERO
}
fn sense(&self) -> Sense {
Sense::empty()
}
fn finish(self, cx: Cx<'_, Self>) -> Response {
cx.response.clone()
}
}
#[trial]
fn room_equal_to_its_width_keeps_a_word_whole() {
for pixels_per_point in [1.0, 2.0] {
let below_half = Rc::new(Cell::new(0));
let output = below_half.clone();
let mut fixture = Fixture::new(move |ui| {
OwnWidth { common: Common::new(), below_half: output.clone() }.id_salt("own-width").show(ui);
});
fixture.pixels_per_point(pixels_per_point);
fixture.frame(Vec::new());
assert!(below_half.get() > 0, "at {pixels_per_point}x some word must be less than half a point past a whole point");
}
}
+1
View File
@@ -1,3 +1,4 @@
pub(crate) mod band;
pub(crate) mod editor;
pub(crate) mod input_cap;
pub(crate) mod memo;
@@ -91,7 +91,7 @@ impl<T: Serialize + DeserializeOwned + Default + Clone + PartialEq, C: CustomSet
(PrefValue::Int(n), Value::Float(_)) => Value::Float(clamp_float(n as f64)),
(PrefValue::Float(x), Value::Float(_)) if x.is_finite() => Value::Float(clamp_float(x)),
(PrefValue::Float(x), Value::Integer(_)) if x.is_finite() => Value::Integer(clamp_int(x.round() as i64)),
(PrefValue::Text(text), Value::String(_)) if matches!(kind, None | Some(RuleKind::Custom)) => Value::String(text),
(PrefValue::Text(text), Value::String(_)) if matches!(kind, None | Some(RuleKind::Custom | RuleKind::CustomRows)) => Value::String(text),
(PrefValue::Choice(id), Value::String(_) | Value::Integer(_)) => match kind {
Some(RuleKind::Choice(options)) if options.iter().any(|(option, _)| *option == id) => match id.parse::<i64>() {
Ok(n) if self.rules.get(key).is_some_and(|rule| rule.integer) => Value::Integer(n),
@@ -71,8 +71,10 @@ pub enum RuleKind {
Choice(Vec<(String, String)>),
/// A `#rrggbb` string as a swatch and hex field.
Color,
/// The app draws it ([`CustomSettings::row`]).
/// The app draws it ([`CustomSettings::row`]); the window labels it.
Custom,
/// The app draws whole rows, labels included ([`CustomSettings::row`]).
CustomRows,
}
impl SettingRule {
@@ -158,13 +160,21 @@ impl SettingRule {
self
}
/// The app draws the control ([`CustomSettings::row`]).
/// The app draws the control ([`CustomSettings::row`]); the window labels it beside the control.
#[must_use]
pub fn custom(mut self) -> Self {
self.kind = Some(RuleKind::Custom);
self
}
/// The app draws whole rows with their labels ([`CustomSettings::row`]): the window adds none. The rule's label
/// stays the row's search text and its group's name.
#[must_use]
pub fn custom_rows(mut self) -> Self {
self.kind = Some(RuleKind::CustomRows);
self
}
/// Shows the row only while the setting at `key` has `value`. Called again, the row shows only while every
/// condition holds.
#[must_use]
@@ -57,6 +57,7 @@ enum Shape {
Choice(Vec<(String, Label)>, Option<UnitKind>),
Color,
Custom,
CustomRows,
}
/// The range of a number without a rule.
@@ -65,8 +66,10 @@ pub(super) const OPEN_RANGE: f64 = 1e9;
fn shape_of(rule: Option<&SettingRule>, value: Option<&toml::Value>) -> Option<Shape> {
use toml::Value;
let kind = rule.and_then(|rule| rule.kind.as_ref());
if matches!(kind, Some(RuleKind::Custom)) {
return Some(Shape::Custom);
match kind {
Some(RuleKind::Custom) => return Some(Shape::Custom),
Some(RuleKind::CustomRows) => return Some(Shape::CustomRows),
_ => {}
}
Some(match (kind, value?) {
(Some(RuleKind::Choice(options)), Value::String(_) | Value::Integer(_)) => {
@@ -169,7 +172,7 @@ impl<T: Serialize + DeserializeOwned + Default + Clone + PartialEq, C: CustomSet
let prefix = format!("{}.", page.id);
for (path, rule) in self.rules.range(prefix.clone()..).take_while(|(path, _)| path.starts_with(&prefix)) {
let key = &path[prefix.len()..];
if matches!(rule.kind, Some(RuleKind::Custom)) && !keys.iter().any(|k| k == key) {
if matches!(rule.kind, Some(RuleKind::Custom | RuleKind::CustomRows)) && !keys.iter().any(|k| k == key) {
keys.push(key.to_owned());
}
}
@@ -235,6 +238,7 @@ impl<T: Serialize + DeserializeOwned + Default + Clone + PartialEq, C: CustomSet
(Shape::Choice(options, counted), Some(Value::String(id))) => choice_kind(options, *counted, id),
(Shape::Color, Some(Value::String(text))) => RowKind::Color(Rgba::from_hex(text).unwrap_or(Rgba::rgb(128, 128, 128))),
(Shape::Custom, _) => RowKind::Custom,
(Shape::CustomRows, _) => RowKind::CustomRows,
_ => return None,
};
let spec = RowSpec::new(&row.key, &row.label, kind).keywords(&row.keywords).visible(self.row_visible(&row.key));
@@ -204,8 +204,11 @@ pub enum RowKind<'a> {
},
/// A text field.
Text(&'a str),
/// The host draws the control ([`PreferencesModel::custom_row`]).
/// The host draws the control ([`PreferencesModel::custom_row`]); the window labels it beside the control.
Custom,
/// The host draws whole rows, labels included ([`PreferencesModel::custom_row`]): the window adds no label. The
/// row's label stays its search text and its group's name.
CustomRows,
}
impl<'a> RowKind<'a> {
@@ -284,21 +284,24 @@ fn show_page<H: PreferencesModel + ?Sized>(
} else if page.groups.is_empty() && !searching {
Notice::new(Tone::Info, words.empty_page.clone()).layout(NoticeLayout::Centred).show(ui);
}
for (at, group) in page.groups.iter().enumerate() {
let mut rows = |ui: &mut Ui| {
let name = group.title.clone().unwrap_or_else(|| page.title.clone());
FieldGroup::new(name).show(ui, |ui, _| {
for row in &group.rows {
show_row(ui, host, row, units, &mut out.rows);
if !page.groups.is_empty() {
FieldGroup::new(page.title.clone()).show(ui, |ui, _| {
for (at, group) in page.groups.iter().enumerate() {
let mut rows = |ui: &mut Ui| {
Stack::new().id_salt(at).show(ui, |ui, _| {
for row in &group.rows {
show_row(ui, host, row, units, &mut out.rows);
}
});
};
match &group.title {
Some(title) => {
Section::fixed(title.clone()).id_salt(at).show(ui, |ui, _| rows(ui));
}
None => rows(ui),
}
});
};
match &group.title {
Some(title) => {
Section::fixed(title.clone()).id_salt(at).show(ui, |ui, _| rows(ui));
}
None => rows(ui),
}
});
}
if let Some(note) = &page.footnote {
Text::new(note.clone()).role(TypeRole::Caption).shade(Shade::Muted).show(ui);
@@ -35,6 +35,7 @@ pub(super) enum Kind {
Float { value: f64, range: RangeInclusive<f64>, decimals: u8, step: Option<f64>, unit: Option<UnitKind> },
Text(String),
Custom,
CustomRows,
}
impl ShownRow {
@@ -59,6 +60,7 @@ impl ShownRow {
}
RowKind::Text(text) => Kind::Text((*text).to_owned()),
RowKind::Custom => Kind::Custom,
RowKind::CustomRows => Kind::CustomRows,
};
Self {
key: row.key.to_owned(),
@@ -176,7 +178,7 @@ pub(super) fn show_row<H: PreferencesModel + ?Sized>(ui: &mut Ui, host: &mut H,
out.pick_color = ask;
}
}
Kind::Custom if label.as_str().is_empty() => {
Kind::CustomRows => {
Stack::new().id_salt(key).show(ui, |ui, _| host.custom_row(ui, key));
}
Kind::Custom => {
@@ -1,7 +1,11 @@
//! `PreferencesPage` over a model with no draft: each edit reaches the model in the pass it lands in.
use craft_ui::contract::{Bind, Role};
use craft_ui::patterns::preferences::{PageSpec, PrefValue, PreferencesModel, PreferencesPage, PreferencesState, PreferencesWords, RowKind, RowSpec};
use craft_ui::contract::{Bind, Role, Show as _, ShowContainer as _};
use craft_ui::display::Text;
use craft_ui::form::Field;
use craft_ui::patterns::preferences::{
GroupSpec, PageSpec, PrefValue, PreferencesModel, PreferencesPage, PreferencesState, PreferencesWords, RowKind, RowSpec,
};
use craft_ui::runtime::env::Env;
use craft_ui::runtime::words::CommonWords;
use craft_ui::testing::pointer::{moved, press, release};
@@ -83,3 +87,57 @@ fn a_page_edit_reaches_the_model_in_the_same_pass_and_no_draft_method_runs() {
assert_eq!(live.borrow().sets.len(), 1, "later passes apply nothing again");
assert!(live.borrow().snap, "the model holds the value");
}
/// A page of two titled groups with labels of different lengths, and a host-labelled row that draws its own field.
struct Grouped;
impl PreferencesModel for Grouped {
fn pages(&self) -> Vec<PageSpec<'_>> {
let text = |key, label| RowSpec::new(key, label, RowKind::Text("v"));
let mut page = PageSpec::new("general", "General", Vec::new());
page.groups = vec![
GroupSpec::new(Some("First"), vec![text("general.a", "Aa"), RowSpec::new("general.host", "Hosted", RowKind::CustomRows)]),
GroupSpec::new(Some("Second"), vec![text("general.b", "A much longer label")]),
];
vec![page]
}
fn set(&mut self, _: &str, _: PrefValue) {}
fn custom_row(&mut self, ui: &mut Ui, _: &str) {
let _ = Field::new("Hosted").show(ui, |ui, _| {
Text::new("hosted value").show(ui);
});
}
fn custom_page(&mut self, _: &mut Ui, _: &str) {}
fn reset_page(&mut self, _: &str) {}
}
fn grouped_page() -> Fixture {
let mut state = PreferencesState::default();
let mut fixture = Fixture::new(move |ui| {
PreferencesPage::new(Bind::bound(&mut state)).show(ui, &mut Grouped);
});
fixture.settle();
fixture
}
#[trial]
fn the_rows_of_every_group_of_a_page_share_one_label_column() {
let fixture = grouped_page();
let end = |label: &str| {
let run = fixture.texts().iter().find(|run| run.text.trim_end_matches(':') == label).unwrap_or_else(|| panic!("no label {label}"));
run.rect.right()
};
assert_eq!(end("Aa"), end("A much longer label"), "the labels of both groups end at one x");
}
#[trial]
fn a_host_labelled_custom_row_shows_its_label_once() {
let fixture = grouped_page();
let tree = fixture.tree();
let fields = tree.keys().into_iter().filter(|key| tree.name_of(*key) == Some("Hosted") && tree.role_of(*key) == Some(Role::GROUP)).count();
assert_eq!(fields, 1, "the host's field is the row's only labelled group");
}