mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 04:04:25 +00:00
148 lines
4.7 KiB
Rust
148 lines
4.7 KiB
Rust
//! `Proposal`, `Metrics` and `ControlMetrics`: what a measure call is asked and what a style gives it to add.
|
|
|
|
use crate::flow::Insets;
|
|
use craft_theme::{BySize, ControlSize};
|
|
use emath::Vec2;
|
|
|
|
/// The question one measure call answers.
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
#[non_exhaustive]
|
|
pub enum Proposal {
|
|
/// The size the content wants, unwrapped.
|
|
Ideal,
|
|
/// The smallest size the content can take.
|
|
Min,
|
|
/// The height at this width.
|
|
Width(f32),
|
|
/// The size in a room this wide and tall, as the component takes it shown at a rect of that size: it takes no share
|
|
/// of the room of the body it is measured in. A container's content that fills the room takes it; other content
|
|
/// its own size.
|
|
Size(Vec2),
|
|
}
|
|
|
|
/// What the pipeline adds around a component's measured content.
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
|
#[non_exhaustive]
|
|
pub struct Metrics {
|
|
/// Space between the component's edge and its content.
|
|
pub padding: Insets,
|
|
/// The least size of the component.
|
|
pub min_size: Vec2,
|
|
/// The wrap width of text.
|
|
pub max_width: Option<f32>,
|
|
/// Space between content parts.
|
|
pub gap: f32,
|
|
/// The least text width before eliding.
|
|
pub min_text: f32,
|
|
/// The width the component takes where its room allows, though its content needs less: its ideal measure is at
|
|
/// least this wide, up to the room it is offered, and its least measure is its content's.
|
|
pub ideal_width: f32,
|
|
/// The least width of the component, padding included, where its least measure (`Proposal::Min`) floors at it in
|
|
/// place of `min_size`'s width: a field that is at least `min_size` wide where it takes its own width, and narrows
|
|
/// past that only where a row shrinks it.
|
|
pub least_width: Option<f32>,
|
|
}
|
|
|
|
impl Metrics {
|
|
/// No padding, no minimum, no wrap width.
|
|
pub const fn new() -> Self {
|
|
Metrics {
|
|
padding: Insets::new(0.0, 0.0, 0.0, 0.0),
|
|
min_size: Vec2::ZERO,
|
|
max_width: None,
|
|
gap: 0.0,
|
|
min_text: 0.0,
|
|
ideal_width: 0.0,
|
|
least_width: None,
|
|
}
|
|
}
|
|
|
|
/// Sets the padding.
|
|
pub const fn padding(mut self, padding: Insets) -> Self {
|
|
self.padding = padding;
|
|
self
|
|
}
|
|
|
|
/// Sets the least size.
|
|
pub const fn min_size(mut self, min_size: Vec2) -> Self {
|
|
self.min_size = min_size;
|
|
self
|
|
}
|
|
|
|
/// Sets the wrap width.
|
|
pub const fn max_width(mut self, width: f32) -> Self {
|
|
self.max_width = Some(width);
|
|
self
|
|
}
|
|
|
|
/// Sets the gap between content parts.
|
|
pub const fn gap(mut self, gap: f32) -> Self {
|
|
self.gap = gap;
|
|
self
|
|
}
|
|
|
|
/// Sets the least text width.
|
|
pub const fn min_text(mut self, width: f32) -> Self {
|
|
self.min_text = width;
|
|
self
|
|
}
|
|
|
|
/// Sets the width the component takes where its room allows.
|
|
pub const fn ideal_width(mut self, width: f32) -> Self {
|
|
self.ideal_width = width;
|
|
self
|
|
}
|
|
|
|
/// Sets the least width, padding included, that the least measure floors at in place of `min_size`'s width.
|
|
pub const fn least_width(mut self, width: f32) -> Self {
|
|
self.least_width = Some(width);
|
|
self
|
|
}
|
|
}
|
|
|
|
/// The sizes a control takes from its `ControlSize`: height, icon side, horizontal padding, gap and the widths of a
|
|
/// field and a number field.
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
|
#[non_exhaustive]
|
|
pub struct ControlMetrics {
|
|
/// The control's height.
|
|
pub height: f32,
|
|
/// The side of an icon inside the control.
|
|
pub icon: f32,
|
|
/// The padding at the start and the end.
|
|
pub pad_x: f32,
|
|
/// The gap between content parts.
|
|
pub gap: f32,
|
|
/// The width of a text or value field where the room allows.
|
|
pub field: f32,
|
|
/// The width of a number field.
|
|
pub number: f32,
|
|
}
|
|
|
|
impl ControlMetrics {
|
|
/// The entries of the six tables at `size`.
|
|
pub fn at(
|
|
size: ControlSize,
|
|
height: &BySize<f32>,
|
|
icon: &BySize<f32>,
|
|
pad_x: &BySize<f32>,
|
|
gap: &BySize<f32>,
|
|
field: &BySize<f32>,
|
|
number: &BySize<f32>,
|
|
) -> Self {
|
|
ControlMetrics {
|
|
height: height.get(size),
|
|
icon: icon.get(size),
|
|
pad_x: pad_x.get(size),
|
|
gap: gap.get(size),
|
|
field: field.get(size),
|
|
number: number.get(size),
|
|
}
|
|
}
|
|
|
|
/// The `Metrics` of a control of this height: horizontal padding, the height as the least height, the gap.
|
|
pub const fn metrics(&self) -> Metrics {
|
|
Metrics::new().padding(Insets::horizontal(self.pad_x)).min_size(Vec2::new(0.0, self.height)).gap(self.gap)
|
|
}
|
|
}
|