mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 01:44:25 +00:00
A side label lines up with the first line of its control
The label scope took any control's baseline over every centre, so text on a later line (AppearancePage's "Active" after a wrapped row of theme chips) pulled the label down. A baseline now counts only from a control that starts on the first reporting control's line.
This commit is contained in:
@@ -1350,10 +1350,8 @@ pub(super) fn run(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, mut tip: Option<T
|
||||
// A control painting text through its faces (a select, a segmented control) reports that baseline too.
|
||||
let core = e.core_ref();
|
||||
let baseline = core.paint.borrow().baseline;
|
||||
if let Some(baseline) = baseline {
|
||||
scope::record_baseline(&ctx, baseline);
|
||||
} else if shape == ShapeKind::Leaf {
|
||||
scope::record_centre(&ctx, core.rect.center().y);
|
||||
if baseline.is_some() || shape == ShapeKind::Leaf {
|
||||
scope::record_line(&ctx, core.rect, baseline);
|
||||
}
|
||||
}
|
||||
real_pass = real;
|
||||
|
||||
@@ -135,7 +135,9 @@ struct Scope {
|
||||
/// The screen y of the top of the rect the label step placed the controls in; the recorded baseline and centre
|
||||
/// are stored measured from it.
|
||||
control_top: f32,
|
||||
/// The screen y of the first baseline of the first control.
|
||||
/// The screen y of the bottom of the first control that reported its line: the label's line is that control's.
|
||||
first_bottom: Option<f32>,
|
||||
/// The screen y of the first baseline of the first control on the first line.
|
||||
baseline: Option<f32>,
|
||||
/// The screen y of the centre line of the first control, for a control that paints no text.
|
||||
centre: Option<f32>,
|
||||
@@ -195,6 +197,7 @@ pub(crate) fn begin(ctx: &Context, container: Id, owner: Id, kind: Kind) {
|
||||
nodes: Vec::new(),
|
||||
open: Vec::new(),
|
||||
control_top: 0.0,
|
||||
first_bottom: None,
|
||||
baseline: None,
|
||||
centre: None,
|
||||
reset: None,
|
||||
@@ -266,21 +269,23 @@ pub(crate) fn place_controls(ctx: &Context, container: Id, top: f32) {
|
||||
});
|
||||
}
|
||||
|
||||
/// Records the screen y of a control's first baseline in the innermost label scope. The first control to report wins.
|
||||
pub(crate) fn record_baseline(ctx: &Context, baseline: f32) {
|
||||
/// Records the line of a control at `rect` in the innermost label scope: its first baseline, or for a control that
|
||||
/// paints no text its centre line. The label lines up with the first line of what it labels: a baseline counts from a
|
||||
/// control that starts above the bottom of the first control that reported, so text that wraps onto a later line (a
|
||||
/// note after a row of chips) does not move the label down; otherwise the label centres on the first control.
|
||||
pub(crate) fn record_line(ctx: &Context, rect: Rect, baseline: Option<f32>) {
|
||||
pass_state::with::<Stack, _>(ctx, |stack| {
|
||||
if let Some(scope) = stack.0.iter_mut().rev().find(|scope| matches!(scope.kind, Kind::Label(_))) {
|
||||
scope.baseline.get_or_insert(baseline);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Records the screen y of the centre line of a control that paints no text (a box, a track) in the innermost label
|
||||
/// scope; the label centres on it. The first control to report wins.
|
||||
pub(crate) fn record_centre(ctx: &Context, centre: f32) {
|
||||
pass_state::with::<Stack, _>(ctx, |stack| {
|
||||
if let Some(scope) = stack.0.iter_mut().rev().find(|scope| matches!(scope.kind, Kind::Label(_))) {
|
||||
scope.centre.get_or_insert(centre);
|
||||
let first_bottom = *scope.first_bottom.get_or_insert(rect.bottom());
|
||||
match baseline {
|
||||
Some(baseline) if rect.top() < first_bottom => {
|
||||
scope.baseline.get_or_insert(baseline);
|
||||
}
|
||||
Some(_) => {}
|
||||
None => {
|
||||
scope.centre.get_or_insert(rect.center().y);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -50,3 +50,37 @@ fn a_side_label_lines_up_with_the_text_of_a_piece_a_row_centres() {
|
||||
let (label, field) = (baseline_of(&fixture, "Quality"), baseline_of(&fixture, "80"));
|
||||
assert!((label - field).abs() <= SLACK, "the label lines up with the number of the field beside the check box: label {label}, field {field}");
|
||||
}
|
||||
|
||||
/// A control whose text sits on a later line than its first piece (a note under a swatch, "Active" after a wrapped
|
||||
/// row of chips): the label stays level with the first line.
|
||||
#[trial]
|
||||
fn a_side_label_stays_on_the_first_line_of_its_control() {
|
||||
use craft_theme::Srgba;
|
||||
use craft_ui::choice::Swatch;
|
||||
use craft_ui::display::Text;
|
||||
use craft_ui::layout_components::Stack;
|
||||
const GREY: Srgba = Srgba { r: 0.5, g: 0.5, b: 0.5, a: 1.0 };
|
||||
let fixture = settled_in(|ui| {
|
||||
Field::new("Light theme").show(ui, |ui, _| {
|
||||
Stack::new().show(ui, |ui, _| {
|
||||
Swatch::new("Light", GREY).show(ui);
|
||||
Text::new("Active").show(ui);
|
||||
});
|
||||
});
|
||||
});
|
||||
let swatch = fixture.by_label("Light").expect("the swatch is shown").rect;
|
||||
let label = fixture.texts().iter().find(|run| run.text == "Light theme").map(|run| run.rect).expect("the label is painted");
|
||||
assert!(
|
||||
label.center().y >= swatch.top() && label.center().y <= swatch.bottom(),
|
||||
"the label sits level with the swatch, not with the note below it: label {label:?}, swatch {swatch:?}"
|
||||
);
|
||||
}
|
||||
|
||||
/// `body` in a field group, settled.
|
||||
fn settled_in(mut body: impl FnMut(&mut Ui) + 'static) -> Fixture {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
FieldGroup::new("Appearance").show(ui, |ui, _| body(ui));
|
||||
});
|
||||
fixture.settle();
|
||||
fixture
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user