use crate::{
environment::LayoutEnvironment,
layout::{LayoutDirection, ResolvedLayout},
primitives::{Dimensions, Point, ProposedDimensions},
transition::Opacity,
view::{ViewLayout, ViewMarker},
};
#[derive(Debug, Clone)]
pub struct Divider {
pub weight: u16,
}
impl Divider {
#[allow(missing_docs)]
#[must_use]
pub const fn new(weight: u16) -> Self {
Self { weight }
}
}
impl Default for Divider {
fn default() -> Self {
Self::new(1)
}
}
impl PartialEq for Divider {
fn eq(&self, other: &Self) -> bool {
self.weight == other.weight
}
}
impl ViewMarker for Divider {
type Renderables = crate::render::Rect;
type Transition = Opacity;
}
impl<Captures: ?Sized> ViewLayout<Captures> for Divider {
type State = ();
type Sublayout = Dimensions;
fn priority(&self) -> i8 {
i8::MAX
}
fn transition(&self) -> Self::Transition {
Opacity
}
fn build_state(&self, _captures: &mut Captures) -> Self::State {}
fn layout(
&self,
offer: &ProposedDimensions,
env: &impl LayoutEnvironment,
_captures: &mut Captures,
_state: &mut <Self as ViewLayout<Captures>>::State,
) -> ResolvedLayout<Self::Sublayout> {
let size = match env.layout_direction() {
LayoutDirection::Vertical => Dimensions {
width: offer.width.resolve_most_flexible(0, 1),
height: self.weight.into(),
},
LayoutDirection::Horizontal => Dimensions {
width: self.weight.into(),
height: offer.height.resolve_most_flexible(0, 1),
},
};
ResolvedLayout {
sublayouts: size,
resolved_size: size,
}
}
fn render_tree(
&self,
layout: &Self::Sublayout,
origin: Point,
_env: &impl LayoutEnvironment,
_captures: &mut Captures,
_state: &mut Self::State,
) -> Self::Renderables {
crate::render::Rect {
origin,
size: (*layout).into(),
}
}
}