mod divider;
mod empty_view;
mod foreach;
mod hstack;
#[cfg(feature = "embedded-graphics")]
mod image;
pub mod match_view;
mod modifier;
pub mod shape;
mod spacer;
mod text;
mod view_that_fits;
mod vstack;
mod zstack;
pub use divider::Divider;
pub use empty_view::EmptyView;
pub use foreach::ForEach;
pub use hstack::HStack;
#[cfg(feature = "embedded-graphics")]
pub use image::Image;
pub use modifier::aspect_ratio;
pub use modifier::padding;
pub use spacer::Spacer;
pub(crate) use text::WhitespaceWrap;
pub use text::{HorizontalTextAlignment, Text};
pub use view_that_fits::{FitAxis, ViewThatFits};
pub use vstack::VStack;
pub use zstack::ZStack;
pub mod prelude {
pub use super::aspect_ratio::{ContentMode, Ratio};
pub use super::{padding::Edges, FitAxis, HorizontalTextAlignment};
pub use super::{
shape::*, Divider, EmptyView, ForEach, HStack, Spacer, Text, VStack, View, ViewExt,
ViewThatFits, ZStack,
};
#[cfg(feature = "embedded-graphics")]
pub use super::{AsDrawable, Image};
pub use crate::layout::{Alignment, HorizontalAlignment, VerticalAlignment};
}
use modifier::{
Animated, AspectRatio, BackgroundView, FixedFrame, FixedSize, FlexFrame, ForegroundStyle,
GeometryGroup, Hidden, Padding, Priority,
};
use crate::{
animation::Animation,
layout::{Alignment, HorizontalAlignment, VerticalAlignment},
render::{Render, Renderable},
};
#[cfg(feature = "embedded-graphics")]
use crate::{
environment::DefaultEnvironment,
primitives::{Interpolate, Point, ProposedDimensions},
};
pub trait View<C>: Renderable<Renderables: Render<C>> {}
impl<C, T: Renderable<Renderables: Render<C>>> View<C> for T {}
pub trait ViewExt: Sized {
fn aspect_ratio(
self,
aspect_ratio: aspect_ratio::Ratio,
content_mode: aspect_ratio::ContentMode,
) -> AspectRatio<Self> {
AspectRatio::new(self, aspect_ratio, content_mode)
}
fn padding(self, edges: padding::Edges, amount: u32) -> Padding<Self> {
Padding::new(edges, amount, self)
}
fn frame(self) -> FixedFrame<Self> {
FixedFrame::new(self)
}
fn frame_sized(self, width: u32, height: u32) -> FixedFrame<Self> {
FixedFrame::new(self).with_width(width).with_height(height)
}
fn flex_frame(self) -> FlexFrame<Self> {
FlexFrame::new(self)
}
fn flex_infinite_width(self, alignment: HorizontalAlignment) -> FlexFrame<Self> {
FlexFrame::new(self)
.with_infinite_max_width()
.with_horizontal_alignment(alignment)
}
fn flex_infinite_height(self, alignment: VerticalAlignment) -> FlexFrame<Self> {
FlexFrame::new(self)
.with_infinite_max_height()
.with_vertical_alignment(alignment)
}
fn fixed_size(self, horizontal: bool, vertical: bool) -> FixedSize<Self> {
FixedSize::new(horizontal, vertical, self)
}
fn priority(self, priority: i8) -> Priority<Self> {
Priority::new(priority, self)
}
fn animated<T: PartialEq + Clone>(self, animation: Animation, value: T) -> Animated<Self, T> {
Animated::new(self, animation, value)
}
fn geometry_group(self) -> GeometryGroup<Self> {
GeometryGroup::new(self)
}
fn background<U>(
self,
alignment: Alignment,
background: impl FnOnce() -> U,
) -> BackgroundView<Self, U> {
BackgroundView::new(self, background(), alignment)
}
fn hidden(self) -> Hidden<Self> {
Hidden::new(self)
}
fn foreground_color<C>(self, color: C) -> ForegroundStyle<Self, C> {
ForegroundStyle::new(color, self)
}
}
impl<T: crate::layout::Layout> ViewExt for T {}
#[deprecated(
since = "0.5.0",
note = "`RenderExtensions` has been replaced with `ViewExt`"
)]
pub trait RenderExtensions: ViewExt {
fn foreground_color<C>(self, color: C) -> ForegroundStyle<Self, C> {
<Self as ViewExt>::foreground_color(self, color)
}
}
#[allow(deprecated)]
impl<T: crate::layout::Layout> RenderExtensions for T {}
#[deprecated(
since = "0.5.0",
note = "`LayoutExtensions` has been replaced with `ViewExt`"
)]
pub trait LayoutExtensions: ViewExt {
fn padding(self, edges: padding::Edges, amount: u32) -> Padding<Self> {
<Self as ViewExt>::padding(self, edges, amount)
}
fn frame(self) -> FixedFrame<Self> {
<Self as ViewExt>::frame(self)
}
fn frame_sized(self, width: u32, height: u32) -> FixedFrame<Self> {
<Self as ViewExt>::frame_sized(self, width, height)
}
fn flex_frame(self) -> FlexFrame<Self> {
<Self as ViewExt>::flex_frame(self)
}
fn flex_infinite_width(self, alignment: HorizontalAlignment) -> FlexFrame<Self> {
<Self as ViewExt>::flex_infinite_width(self, alignment)
}
fn flex_infinite_height(self, alignment: VerticalAlignment) -> FlexFrame<Self> {
<Self as ViewExt>::flex_infinite_height(self, alignment)
}
fn fixed_size(self, horizontal: bool, vertical: bool) -> FixedSize<Self> {
<Self as ViewExt>::fixed_size(self, horizontal, vertical)
}
fn priority(self, priority: i8) -> Priority<Self> {
<Self as ViewExt>::priority(self, priority)
}
fn animated<T: PartialEq + Clone>(self, animation: Animation, value: T) -> Animated<Self, T> {
<Self as ViewExt>::animated(self, animation, value)
}
fn geometry_group(self) -> GeometryGroup<Self> {
<Self as ViewExt>::geometry_group(self)
}
fn background<U>(
self,
alignment: Alignment,
background: impl FnOnce() -> U,
) -> BackgroundView<Self, U> {
<Self as ViewExt>::background(self, alignment, background)
}
fn hidden(self) -> Hidden<Self> {
<Self as ViewExt>::hidden(self)
}
}
#[allow(deprecated)]
impl<T: crate::layout::Layout> LayoutExtensions for T {}
#[cfg(feature = "embedded-graphics")]
pub trait AsDrawable<Color> {
fn as_drawable(
&self,
size: impl Into<ProposedDimensions>,
default_color: Color,
) -> impl embedded_graphics_core::Drawable<Color = Color, Output = ()>;
}
#[cfg(feature = "embedded-graphics")]
impl<C: embedded_graphics_core::pixelcolor::PixelColor + Interpolate, T: View<C>> AsDrawable<C>
for T
{
fn as_drawable(
&self,
size: impl Into<ProposedDimensions>,
default_color: C,
) -> impl embedded_graphics_core::Drawable<Color = C, Output = ()> {
let env = DefaultEnvironment::non_animated();
let layout = self.layout(&size.into(), &env);
let render_tree = self.render_tree(&layout, Point::zero(), &env);
DrawableView {
render_tree,
default_color,
}
}
}
#[cfg(feature = "embedded-graphics")]
struct DrawableView<T: Render<C>, C> {
render_tree: T,
default_color: C,
}
#[cfg(feature = "embedded-graphics")]
impl<T: Render<C>, C: embedded_graphics_core::pixelcolor::PixelColor + Interpolate>
embedded_graphics_core::Drawable for DrawableView<T, C>
{
type Color = C;
type Output = ();
fn draw<D>(&self, target: &mut D) -> Result<Self::Output, D::Error>
where
D: embedded_graphics_core::draw_target::DrawTarget<Color = Self::Color>,
{
let mut embedded_target = crate::render_target::EmbeddedGraphicsRenderTarget::new(target);
self.render_tree
.render(&mut embedded_target, &self.default_color, Point::zero());
Ok(())
}
}