use waft_embedder::{Rect, Size, TextDirection};
use waft_painting::{EdgeInsetsGeometry, ShapeBorderRef};
use waft_widgets::{IntoWidget, WidgetRef};
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PanePreferredSize {
Fixed(f64),
Proportion(f64),
}
impl PanePreferredSize {
pub(crate) fn resolve(self, available: f64) -> f64 {
match self {
PanePreferredSize::Fixed(size) => size,
PanePreferredSize::Proportion(proportion) => available * proportion,
}
}
}
#[derive(Clone, Debug)]
pub struct Pane {
pub child: WidgetRef,
pub preferred_width: Option<PanePreferredSize>,
pub preferred_height: Option<PanePreferredSize>,
pub margins: EdgeInsetsGeometry,
pub shape: Option<ShapeBorderRef>,
}
impl Pane {
#[cfg_attr(debug_assertions, track_caller)]
pub fn new<K>(child: impl IntoWidget<K>) -> Pane {
Pane {
child: child.into_widget(),
preferred_width: None,
preferred_height: None,
margins: EdgeInsetsGeometry::ZERO,
shape: None,
}
}
pub fn preferred_width(mut self, preferred_width: PanePreferredSize) -> Pane {
self.preferred_width = Some(preferred_width);
self
}
pub fn preferred_height(mut self, preferred_height: PanePreferredSize) -> Pane {
self.preferred_height = Some(preferred_height);
self
}
pub fn margins(mut self, margins: impl Into<EdgeInsetsGeometry>) -> Pane {
let margins = margins.into();
self.margins = margins;
self
}
pub fn shape(mut self, shape: impl Into<ShapeBorderRef>) -> Pane {
self.shape = Some(shape.into());
self
}
pub(crate) fn within_margins(
&self,
bounds: Rect,
scaffold: Size,
text_direction: TextDirection,
) -> Rect {
let margins = self.margins.resolve(Some(text_direction));
Rect::from_ltrb(
bounds.left.max(margins.left),
bounds.top.max(margins.top),
bounds.right.min(scaffold.width() - margins.right),
bounds.bottom.min(scaffold.height() - margins.bottom),
)
}
pub(crate) fn width_for(&self, scaffold: Size, default_width: f64) -> f64 {
self.preferred_width
.map(|size| size.resolve(scaffold.width()))
.unwrap_or(default_width)
}
pub(crate) fn height_for(&self, scaffold: Size, default_height: f64) -> f64 {
self.preferred_height
.map(|size| size.resolve(scaffold.height()))
.unwrap_or(default_height)
}
}
impl From<WidgetRef> for Pane {
fn from(child: WidgetRef) -> Pane {
Pane::new(child)
}
}