mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 16:59:56 +01:00
ui-widgets list rows: TreeIndent, a scoped style value for how far a tree level steps in
A tree row's indent was the size set's alone (an icon slot, or ThumbRows::Tight's small icon and tight gap), so a host whose tree has to narrow its step as it deepens could not say so. TreeIndent is that value: a host sets it on the list's scope (`with_env`), 0 or no value keeps the size set's step, and Sizes::indent reads it, so the label, the icon, the disclosure and the AccessKit tree's level all follow it together. Trial: a level under TreeIndent(10.0) steps in exactly 10 pt; no value, and TreeIndent(0.0), keep the size set's own step, which is wider.
This commit is contained in:
1 parent
90e14375a4
commit
3e2f031e25
5 files changed
+53
-6
No files matched your search
@@ -22,8 +22,8 @@ pub use predicate::{DropTest, RowTest, RowText};
|
||||
pub(crate) use row::RowThumb;
|
||||
pub use row::{
|
||||
CellStatus, CommandHighlight, HueTarget, LabelUnderline, ListRow, ListRowStyle, RowDragSpot, RowEvent, RowInk, RowLines, RowOutput, RowRun,
|
||||
RowStyleCx, ThumbAspect, ThumbPaint, ThumbRows, ThumbSize, ThumbTarget, ThumbTargets, TrailingFace, TrailingPart, TreeLeaves, label_color_label,
|
||||
tree_steps,
|
||||
RowStyleCx, ThumbAspect, ThumbPaint, ThumbRows, ThumbSize, ThumbTarget, ThumbTargets, TrailingFace, TrailingPart, TreeIndent, TreeLeaves,
|
||||
label_color_label, tree_steps,
|
||||
};
|
||||
pub use toggle::{ToggleCell, ToggleFace, ToggleState};
|
||||
pub use tree::OpenSet;
|
||||
|
||||
@@ -41,7 +41,7 @@ pub use placement::indent_steps;
|
||||
#[doc(hidden)]
|
||||
pub use placement::placement_under_fixed_sizes;
|
||||
pub use placement::{TreeLeaves, tree_steps};
|
||||
pub use style::{CommandHighlight, ListRowStyle, RowStyleCx, ThumbRows};
|
||||
pub use style::{CommandHighlight, ListRowStyle, RowStyleCx, ThumbRows, TreeIndent};
|
||||
pub(crate) use thumb::RowThumb;
|
||||
pub(super) use thumb::Thumb;
|
||||
use thumb::ThumbPayload;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use super::ListRow;
|
||||
use super::show::named;
|
||||
use super::style::{ListRowStyle, ThumbRows};
|
||||
use super::style::{ListRowStyle, ThumbRows, TreeIndent};
|
||||
use super::thumb::{Thumb, ThumbSize};
|
||||
use super::trailing::{TrailingFace, TrailingItem, trailing_run};
|
||||
use crate::buttons::Link;
|
||||
@@ -26,7 +26,7 @@ pub(super) struct Sizes {
|
||||
pub(super) row: f32,
|
||||
/// The slot of an icon or a toggle cell's mark.
|
||||
pub(super) icon: f32,
|
||||
/// A level of indent (`ThumbRows`).
|
||||
/// A level of indent (`TreeIndent`, else `ThumbRows`).
|
||||
indent: f32,
|
||||
/// The side the row's icon and its toggle marks draw at in their slots: the control's icon side, the small
|
||||
/// control's in a sub-row.
|
||||
@@ -59,10 +59,13 @@ impl Sizes {
|
||||
let small = cx.control_of(ControlSize::Sm);
|
||||
let row = if sub_row { small.height } else { rows.of(cx) };
|
||||
let tight = cx.env::<ThumbRows>() == ThumbRows::Tight;
|
||||
let own_indent = if tight { small.icon + tokens.space.gap.tight } else { control.icon };
|
||||
// A host's own step (`TreeIndent`) wins over the size set's; 0, or no value, keeps the size set's.
|
||||
let set_indent = cx.env::<TreeIndent>().0;
|
||||
Sizes {
|
||||
row,
|
||||
icon: control.icon,
|
||||
indent: if tight { small.icon + tokens.space.gap.tight } else { control.icon },
|
||||
indent: if set_indent > 0.0 { set_indent } else { own_indent },
|
||||
mark: if sub_row { small.icon } else { control.icon },
|
||||
pad: control.pad_x,
|
||||
gap: control.gap,
|
||||
|
||||
@@ -102,6 +102,21 @@ pub enum ThumbRows {
|
||||
Tight,
|
||||
}
|
||||
|
||||
/// How far a tree row indents per level, in points: a host whose tree narrows its step as it deepens sets it on the
|
||||
/// scope of the lists (`with_env`), and changes it when the room its names need does. `0`, the default, keeps the size
|
||||
/// set's own step: an icon slot, or `ThumbRows::Tight`'s small icon and `space.gap.tight`. The label, the icon and the
|
||||
/// disclosure all follow it, and so does the `AccessKit` tree's level: only the step changes.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, crate::runtime::env::EnvValue, crate::contract::MemoProps)]
|
||||
#[env(style)]
|
||||
pub struct TreeIndent(pub f32);
|
||||
|
||||
impl std::hash::Hash for TreeIndent {
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
// (a length folded into the style key by its bits: `f32` has no `Hash` of its own)
|
||||
std::hash::Hash::hash(&self.0.to_bits(), state);
|
||||
}
|
||||
}
|
||||
|
||||
/// What the row fill depends on.
|
||||
#[derive(Clone, Copy)]
|
||||
pub(super) struct FillKey {
|
||||
|
||||
@@ -1951,3 +1951,32 @@ fn tight_thumbnail_rows_stand_lower_and_indent_less() {
|
||||
assert!(tight_height < roomy_height, "the tight row ({tight_height}) is lower than the roomy one ({roomy_height})");
|
||||
assert!(0.0 < tight_indent && tight_indent < roomy_indent, "a level indents less ({tight_indent}) than an icon slot ({roomy_indent})");
|
||||
}
|
||||
|
||||
/// How far a row two levels down starts after the one a level down, under `indent` (a leaf inside a group steps one
|
||||
/// level past its parent's disclosure column, so two leaves one level apart differ by exactly one indent step).
|
||||
fn tree_indent_step(indent: Option<craft_ui::list::TreeIndent>) -> f32 {
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
let keys = [0_usize, 1, 2];
|
||||
let source = keyed(&keys, |key: &usize| *key);
|
||||
let _ = List::new("Folders").show_items(ui, &source, &mut |key, _| ListRow::new(NAMES[*key]).depth(*key));
|
||||
});
|
||||
if let Some(indent) = indent {
|
||||
fixture.root_value(indent);
|
||||
}
|
||||
fixture.room(Vec2::new(300.0, 200.0)).settle();
|
||||
let label_x = |name: &str| fixture.texts().iter().find(|text| text.text == name).expect("the row paints its label").rect.left();
|
||||
label_x(NAMES[2]) - label_x(NAMES[1])
|
||||
}
|
||||
|
||||
/// A list whose scope sets `TreeIndent` indents a level by that many points, whatever the size set's own step; with no
|
||||
/// value (or `TreeIndent(0.0)`) a level keeps the size set's step, so no other list changes.
|
||||
#[trial]
|
||||
fn a_scope_tree_indent_sets_how_far_a_level_steps_in() {
|
||||
use craft_ui::list::TreeIndent;
|
||||
let own = tree_indent_step(None);
|
||||
let unset = tree_indent_step(Some(TreeIndent(0.0)));
|
||||
let set = tree_indent_step(Some(TreeIndent(10.0)));
|
||||
assert!((unset - own).abs() < 0.01, "a zero value keeps the size set's step ({} with none, {} with zero)", own, unset);
|
||||
assert!((set - 10.0).abs() < 0.01, "a level steps in by the value the scope sets: {set}");
|
||||
assert!(own > set, "the size set's own step ({own}) is wider than the 10 pt the scope sets");
|
||||
}
|
||||
Reference in new issue
Block a user