mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 10:48:15 +01:00
ui-widgets: a segmented strip in a row or bar keeps one line
In a row, a bar included, the strip's least is its whole line again, so the row's shrink step does not wrap it. It still wraps in a column narrower than the strip. A rect with room for one line only keeps the segments on that line. - ui-contract: `MeasureCx::in_row` says whether a component is a direct child of a `Row` body.
This commit is contained in:
1 parent
cc2b8a3c24
commit
bdccf96831
4 files changed
+66
-11
No files matched your search
@@ -83,6 +83,12 @@ impl<C: Component> MeasureCx<'_, C> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the component is a direct child of a `Row` body, a bar's included. A component that could wrap into
|
||||
/// lines keeps its one-line least there, and the row's shrink step decides who gives way.
|
||||
pub fn in_row(&self) -> bool {
|
||||
crate::layout::row_alone_room(self.ui).is_some()
|
||||
}
|
||||
|
||||
/// The width a band picture sizes at (a plot, a strip), one whose height is its own whatever its width: it takes
|
||||
/// the room it is offered, up to `most`. At its least, where the width is unbounded, and in a measuring run of a
|
||||
/// column that sizes its width from its content (which offers no room), it is `least`: a picture needs no width of
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! `Segmented`: one of a few options as a strip of segments. A leaf with a part per segment; each segment wears a
|
||||
//! face that is flat and muted, with a hover fill, and takes the selection fill and edge while it is picked. The row is one Tab stop over its segments: arrows, Home and End move focus between them, Space,
|
||||
//! Enter or a click picks the focused one. In a room narrower than the strip, the segments that do not fit move to
|
||||
//! the next line.
|
||||
//! Enter or a click picks the focused one. In a column narrower than the strip, the segments that do not fit move to
|
||||
//! the next line; in a row or bar the strip keeps one line.
|
||||
|
||||
use crate::buttons::ActiveLook;
|
||||
use crate::buttons::face::{self, FaceLook, FaceSpec};
|
||||
@@ -68,10 +68,17 @@ impl<'a, T: Value + 'static> Segmented<'a, T> {
|
||||
}
|
||||
|
||||
/// The segments measured and flowed into lines `room` wide. At the least proposal the room is the widest segment,
|
||||
/// so each segment keeps its whole face.
|
||||
/// so each segment keeps its whole face. In a row or bar the strip keeps one line, so its least is the whole line
|
||||
/// and the row's shrink step never wraps it.
|
||||
fn arrange(&self, cx: &MeasureCx<'_, Self>, room: f32) -> Placement {
|
||||
let wants = self.widths(cx);
|
||||
let room = if cx.proposal == Proposal::Min { wants.iter().copied().fold(0.0, f32::max).min(room) } else { room };
|
||||
let room = if cx.in_row() {
|
||||
f32::INFINITY
|
||||
} else if cx.proposal == Proposal::Min {
|
||||
wants.iter().copied().fold(0.0, f32::max).min(room)
|
||||
} else {
|
||||
room
|
||||
};
|
||||
place(&wants, room, cx.style.gap)
|
||||
}
|
||||
}
|
||||
@@ -145,7 +152,7 @@ impl<T: Value + 'static> Component for Segmented<'_, T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The lines the segments take in the column's room: one line when the strip fits it.
|
||||
/// The lines the segments take in the column's room: one line when the strip fits it, and always in a row.
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
self.arrange(cx, cx.column_room().unwrap_or(f32::INFINITY)).size(cx.style.height, cx.style.gap)
|
||||
}
|
||||
@@ -154,11 +161,12 @@ impl<T: Value + 'static> Component for Segmented<'_, T> {
|
||||
Sense::HOVER
|
||||
}
|
||||
|
||||
/// A part per segment, flowed into the lines of `rect`. A segment with an icon, or one cut to its line, shows its
|
||||
/// label as its tooltip.
|
||||
/// A part per segment, flowed into the lines of `rect`. A `rect` with room for one line only keeps the segments on
|
||||
/// that line. A segment with an icon, or one cut to its line, shows its label as its tooltip.
|
||||
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
|
||||
let placement = self.arrange(cx, rect.width());
|
||||
let (gap, rtl) = (cx.style.gap, cx.flow.is_rtl());
|
||||
let one_line = rect.height() < 2.0 * cx.style.height + gap - 0.5;
|
||||
let placement = if one_line { place(&self.widths(cx), f32::INFINITY, gap) } else { self.arrange(cx, rect.width()) };
|
||||
let current = self.position_of(cx.value());
|
||||
for (at, entry) in self.entries.iter().enumerate() {
|
||||
let mut part = cx.part(Self::part(at), placement.rect(at, rect, gap, rtl));
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
//! A segmented strip in a room narrower than its one line moves the segments that do not fit to the next line, so
|
||||
//! it stays inside the room; in a room it fits it keeps one line.
|
||||
//! A segmented strip in a column narrower than its one line moves the segments that do not fit to the next line, so
|
||||
//! it stays inside the room; in a room it fits, and in a row or bar, it keeps one line.
|
||||
|
||||
use craft_i18n::Direction;
|
||||
use craft_ui::bars::{Bar, BarKind};
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::choice::Segmented;
|
||||
use craft_ui::contract::{Bind, Entry, Show as _};
|
||||
use craft_ui::contract::{Bind, Entry, Show as _, ShowContainer as _};
|
||||
use craft_ui::layout_components::Row;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Key, Modifiers, Rect, Vec2};
|
||||
|
||||
@@ -67,6 +70,43 @@ fn a_strip_that_fits_its_room_keeps_one_line() {
|
||||
assert!((group.width() - line).abs() < 0.5, "the strip is as wide as its one line ({} against {line})", group.width());
|
||||
}
|
||||
|
||||
/// The strip of the five modes followed by three buttons, in a row (`bar` false) or an options bar, `width` wide.
|
||||
fn strip_in_row(width: f32, bar: bool) -> Fixture {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let entries: Vec<Entry<u8>> = (0..).zip(MODES).map(|(at, mode)| Entry::new(at, mode)).collect();
|
||||
let mut picked = 0_u8;
|
||||
let mut members = |ui: &mut egui::Ui| {
|
||||
Segmented::new("Combine", Bind::bound(&mut picked), &entries).show(ui);
|
||||
for at in 0..3 {
|
||||
Button::new(format!("Button {at}")).show(ui);
|
||||
}
|
||||
};
|
||||
if bar {
|
||||
Bar::new("Options", BarKind::Options).show(ui, |ui, _| members(ui));
|
||||
} else {
|
||||
Row::new().show(ui, |ui, _| members(ui));
|
||||
}
|
||||
});
|
||||
fixture.room(Vec2::new(width, 300.0)).settle();
|
||||
fixture
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_strip_in_a_row_or_bar_keeps_one_line_under_width_pressure() {
|
||||
let line = one_line_width();
|
||||
for bar in [false, true] {
|
||||
let (_, wide) = rects(&strip_in_row(2000.0, bar));
|
||||
let (_, narrow) = rects(&strip_in_row(line * 0.6, bar));
|
||||
assert!(
|
||||
narrow.iter().all(|segment| (segment.top() - narrow[0].top()).abs() < 0.5),
|
||||
"bar {bar}: the segments share one line in a room narrower than the strip"
|
||||
);
|
||||
for ((mode, wide), narrow) in MODES.iter().zip(&wide).zip(&narrow) {
|
||||
assert!(narrow.width() >= wide.width() - 0.5, "bar {bar}: {mode} keeps its whole face ({narrow:?} against {wide:?})");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn arrows_move_across_the_lines_of_a_wrapped_strip() {
|
||||
let mut fixture = strip(one_line_width() * 0.6, Direction::Ltr);
|
||||
|
||||
@@ -2440,6 +2440,7 @@ The component traits, context types, `PaintUi`, `Looks`, the pipeline and its st
|
||||
- `pub fn command(&self) -> Option<&ResolvedCommand>`. The bound command as step 1 resolved it, with the scoped bindings of the scopes above the component.
|
||||
- `pub fn resolve_command(&self, command: impl Into<CommandRef>) -> Option<std::sync::Arc<ResolvedCommand>>`. `command` as this component shows it: the label, icon and chord with the scoped bindings of the scopes above
|
||||
- `pub fn column_room(&self) -> Option<f32>`. The width a component whose height follows its width (wrapped text, a square plot) sizes at: the room the
|
||||
- `pub fn in_row(&self) -> bool`. Whether the component is a direct child of a `Row` body, a bar's included. A component that could wrap into
|
||||
- `pub fn picture_width(&self, least: f32, most: f32) -> f32`. The width a band picture sizes at (a plot, a strip), one whose height is its own whatever its width: it takes
|
||||
- `pub fn picture_size(&self, least: f32, most: f32, height: impl FnOnce(f32) -> f32) -> Vec2`. The size of a picture whose height follows from its width (a square graph, a wheel, an image at its aspect):
|
||||
- `pub fn label(&self) -> Label`. The label as given.
|
||||
|
||||
Reference in new issue
Block a user