use crate::ItemsAlignment;
use super::implz::Platform;
use uniui_gui::{
prelude::*,
AlignmentHorizontal,
AlignmentVertical,
Application,
Color,
DataProcessor,
NativeWidget,
Orientation,
Widget,
WidgetGenerator,
Wrap,
};
pub struct LinearLayout {
childs: Vec<Box<dyn Widget>>,
orientation: Orientation,
wrap: Property<Wrap>,
alignment_vertical: Property<AlignmentVertical>,
alignment_horizontal: Property<AlignmentHorizontal>,
items_alignment: Property<ItemsAlignment>,
platform: Platform,
}
impl LinearLayout {
pub fn new(orientation: Orientation) -> LinearLayout {
return LinearLayout {
childs: Vec::new(),
orientation,
wrap: Property::new(Wrap::NoWrap),
alignment_horizontal: Property::new(AlignmentHorizontal::Stretch),
alignment_vertical: Property::new(AlignmentVertical::Stretch),
items_alignment: Property::new(ItemsAlignment::Stretch),
platform: Platform::new(),
};
}
pub fn push_widget<W>(
&mut self,
widget: W,
) where
W: Sized + Widget + 'static,
{
self.childs.push(Box::new(widget));
}
pub fn slot_alignment_horizontal(&self) -> &dyn Slot<AlignmentHorizontal> {
return self.alignment_horizontal.slot();
}
pub fn slot_alignment_vertical(&self) -> &dyn Slot<AlignmentVertical> {
return self.alignment_vertical.slot();
}
pub fn slot_set_items_alignment(&self) -> &dyn Slot<ItemsAlignment> {
return self.items_alignment.slot();
}
pub fn slot_set_wrap(&self) -> &dyn Slot<Wrap> {
return self.wrap.slot();
}
#[doc(hidden)]
pub fn set_background(
&mut self,
color: &Color,
) {
self.platform.set_background(color);
}
#[doc(hidden)]
pub fn set_alignment_horizontal(
&mut self,
alignment: AlignmentHorizontal,
) {
self.slot_alignment_horizontal().exec_for(alignment);
}
#[doc(hidden)]
pub fn set_alignment_vertical(
&mut self,
alignment: AlignmentVertical,
) {
self.slot_alignment_vertical().exec_for(alignment);
}
}
impl Widget for LinearLayout {
fn to_native(
&mut self,
widget_generator: &mut dyn WidgetGenerator,
) -> Result<NativeWidget, ()> {
self.items_alignment.try_update();
self.wrap.try_update();
self.alignment_horizontal.try_update();
self.alignment_vertical.try_update();
return self.platform.to_native(
widget_generator,
&mut self.childs,
&self.orientation,
self.wrap.as_ref(),
self.alignment_horizontal.as_ref(),
self.alignment_vertical.as_ref(),
self.items_alignment.as_ref(),
);
}
fn draw(&mut self) {
self.childs.iter_mut().for_each(|child| child.as_mut().draw());
}
}
impl DataProcessor for LinearLayout {
fn process_data(
&mut self,
msec_since_app_epoch: i64,
app: &mut dyn Application,
) {
self.childs
.iter_mut()
.for_each(|child| child.as_mut().process_data(msec_since_app_epoch, app));
if self.items_alignment.try_update() {
self.platform.update_alignment(self.items_alignment.as_ref());
}
if self.wrap.try_update() {
self.platform.update_wrap(self.wrap.as_ref());
}
if self.alignment_horizontal.try_update() {
self.platform.update_alignment_horizontal(self.alignment_horizontal.as_ref());
}
if self.alignment_vertical.try_update() {
self.platform.update_alignment_vertical(self.alignment_vertical.as_ref());
}
}
}