use crate::{
Nc, NcAlign, NcError, NcPlane, NcPlaneOptions, NcPlaneOptionsBuilder,
NcResizeCb, NcResult,
};
#[derive(Debug, Default)]
pub struct NcPlaneBuilder<'nc, 'parent> {
nc: Option<&'nc mut Nc>,
parent: Option<&'parent mut NcPlane>,
options: NcPlaneOptionsBuilder,
}
impl<'nc, 'parent> NcPlaneBuilder<'nc, 'parent> {
pub fn options(mut self, options: &NcPlaneOptions) -> Self {
self.options = options.into();
self
}
pub fn get_options(&self) -> NcPlaneOptions {
self.options.clone().into()
}
pub fn parent(mut self, plane: &'parent mut NcPlane) -> Self {
self.parent = Some(plane);
self.nc = None;
self
}
pub fn no_parent(mut self) -> Self {
self.parent = None;
self
}
pub fn nc(mut self, nc: &'nc mut Nc) -> Self {
self.nc = Some(nc);
self.parent = None;
self
}
pub fn y(mut self, y: i32) -> Self {
self.options.y = y;
self.options.flags &= !NcPlaneOptions::VERALIGNED;
self
}
pub fn x(mut self, x: i32) -> Self {
self.options.x = x;
self.options.flags &= !NcPlaneOptions::HORALIGNED;
self
}
pub fn yx(mut self, y: i32, x: i32) -> Self {
self.options.y = y;
self.options.x = x;
self.options.flags &= !NcPlaneOptions::VERALIGNED;
self.options.flags &= !NcPlaneOptions::HORALIGNED;
self
}
pub fn valign(mut self, valign: impl Into<NcAlign>) -> Self {
self.options.y = valign.into() as i32;
self.options.flags |= NcPlaneOptions::VERALIGNED;
self
}
pub fn halign(mut self, halign: impl Into<NcAlign>) -> Self {
self.options.y = halign.into() as i32;
self.options.flags |= NcPlaneOptions::VERALIGNED;
self
}
pub fn align(mut self, halign: impl Into<NcAlign>) -> Self {
self.options.y = halign.into() as i32;
self.options.flags |= NcPlaneOptions::VERALIGNED;
self
}
pub fn rows(mut self, rows: u32) -> Self {
self.options.rows = rows;
self.options.flags &= !NcPlaneOptions::MARGINALIZED;
self
}
pub fn cols(mut self, cols: u32) -> Self {
self.options.cols = cols;
self.options.flags &= !NcPlaneOptions::MARGINALIZED;
self
}
pub fn rows_cols(mut self, rows: u32, cols: u32) -> Self {
self.options.rows = rows;
self.options.cols = cols;
self.options.flags &= !NcPlaneOptions::MARGINALIZED;
self
}
pub fn margins(mut self, bottom: u32, right: u32) -> Self {
self.options.margin_b = bottom;
self.options.margin_r = right;
self.options.flags &= !NcPlaneOptions::MARGINALIZED;
self
}
pub fn fixed(mut self, fixed: bool) -> Self {
if fixed {
self.options.flags |= NcPlaneOptions::FIXED;
} else {
self.options.flags &= !NcPlaneOptions::FIXED;
}
self
}
pub fn resizecb(mut self, callback: Option<NcResizeCb>) -> Self {
self.options.resizecb = callback;
self
}
pub fn build(self) -> NcResult<&'parent mut NcPlane> {
if let Some(plane) = self.parent {
if self.nc.is_none() {
NcPlane::new_child(plane, &self.options.into())
} else {
Err(NcError::new_msg(
"NcPlaneBuilder.build() error: Both `parent` and `nc` fields are set.",
))
}
} else if let Some(nc) = self.nc {
NcPlane::new_pile(nc, &self.options.into())
} else {
Err(NcError::new_msg(
"NcPlaneBuilder.build() error: Neither `parent` or `nc` fields are set.",
))
}
}
}