macro_rules! example {
($name:literal) => {
concat!(
"\n# Example\n\n```no_run\n",
include_str!(concat!("../../examples/", $name, ".rs")),
"```"
)
};
}
mod builder;
mod button;
mod chip;
mod container;
mod icon;
mod image;
mod label;
mod list;
mod overlay;
mod page;
mod progress;
mod provide;
mod radio;
mod scroll;
mod segmented;
mod slider;
mod stateful;
mod table;
mod text_input;
mod toggle;
mod tree;
use taffy::{
AlignItems, Dimension, FlexWrap, JustifyContent, LengthPercentage, LengthPercentageAuto, Size,
Style,
};
pub use builder::{Builder, Seen, Snapshot, future_builder, stream_builder, watch};
pub use button::{Button, button};
pub use chip::{badge, chip, choice_chip};
pub use container::{Container, column, container, row, spacer};
pub use icon::{Icon, icon, icon_button};
pub use image::{Image, image};
pub use label::{Label, text};
pub use list::{List, list};
pub use overlay::{
Dropdown, OnBack, OnKey, Overlay, Overlays, Shown, backdrop, bottom_sheet, dialog, drawer,
dropdown, on_back, on_key, overlay, overlays, popup, screen, snackbar, tooltip,
};
pub use page::{Page, heading, page};
pub use progress::{Progress, Spinner, progress, spinner, stacked_progress, wait_bar};
pub use provide::{Provide, provide};
pub use radio::{Radio, radio};
pub use scroll::{Scroll, scroll};
pub use segmented::{Segmented, segmented, tabs};
pub use slider::{Slider, slider};
pub use stateful::{Stateful, stateful, stateful_with};
pub use table::{Column, TableSort, table};
pub use text_input::{TextInput, text_input};
pub use toggle::{Toggle, toggle};
pub use tree::{Expanded, tree_view};
pub trait Styled: Sized {
fn style_mut(&mut self) -> &mut Style;
fn padding(mut self, p: f32) -> Self {
self.style_mut().padding = taffy::Rect::length(p);
self
}
fn padding_xy(mut self, x: f32, y: f32) -> Self {
let (x, y) = (LengthPercentage::length(x), LengthPercentage::length(y));
self.style_mut().padding = taffy::Rect {
left: x,
right: x,
top: y,
bottom: y,
};
self
}
fn gap(mut self, g: f32) -> Self {
self.style_mut().gap = Size::length(g);
self
}
fn grow(mut self, g: f32) -> Self {
self.style_mut().flex_grow = g;
self
}
fn shrink(mut self, s: f32) -> Self {
self.style_mut().flex_shrink = s;
self
}
fn width(mut self, w: f32) -> Self {
self.style_mut().size.width = Dimension::length(w);
self
}
fn height(mut self, h: f32) -> Self {
self.style_mut().size.height = Dimension::length(h);
self
}
fn min_width(mut self, w: f32) -> Self {
self.style_mut().min_size.width = LengthPercentageAuto::length(w);
self
}
fn min_height(mut self, h: f32) -> Self {
self.style_mut().min_size.height = LengthPercentageAuto::length(h);
self
}
fn max_height(mut self, h: f32) -> Self {
self.style_mut().max_size.height = LengthPercentageAuto::length(h);
self
}
fn fill(mut self) -> Self {
let s = self.style_mut();
s.flex_grow = 1.0;
s.flex_basis = Dimension::length(0.0);
s.min_size = Size::length(0.0);
self
}
fn fill_width(mut self) -> Self {
self.style_mut().size.width = Dimension::percent(1.0);
self
}
fn align(mut self, a: AlignItems) -> Self {
self.style_mut().align_items = a;
self
}
fn justify(mut self, j: JustifyContent) -> Self {
self.style_mut().justify_content = j;
self
}
fn wrap(mut self) -> Self {
self.style_mut().flex_wrap = FlexWrap::Wrap;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Default)]
struct Probe(Style);
impl Styled for Probe {
fn style_mut(&mut self) -> &mut Style {
&mut self.0
}
}
#[test]
fn modifiers_write_style() {
let s = Probe::default()
.padding_xy(4.0, 2.0)
.gap(3.0)
.grow(1.0)
.width(10.0)
.wrap()
.0;
let (x, y) = (LengthPercentage::length(4.0), LengthPercentage::length(2.0));
assert_eq!(
s.padding,
taffy::Rect {
left: x,
right: x,
top: y,
bottom: y
}
);
assert_eq!(s.gap, Size::length(3.0));
assert_eq!(s.flex_grow, 1.0);
assert_eq!(s.size.width, Dimension::length(10.0));
assert_eq!(s.flex_wrap, FlexWrap::Wrap);
}
}