abletk/src/widget/column.rs

57 lines
1.6 KiB
Rust
Executable File

/*
* Copyright (C) 2022 Umut İnan Erdoğan <umutinanerdogan@pm.me>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
use crate::{layout::position::Position, widget::Widget};
use abletk_common::Renderer;
pub struct Column {
widgets: Vec<Box<dyn Widget>>,
}
impl Widget for Column {
fn draw(&self, renderer: &mut Renderer) {
self.widgets.iter().for_each(|widget| {
let pos = widget.position(renderer);
renderer.position_at(
renderer.position().0 + pos.x(),
renderer.position().1 + pos.y()
);
widget.draw(renderer);
renderer.position_at(
renderer.position().0,
renderer.position().1 + pos.height()
);
});
}
fn position(&self, renderer: &mut Renderer) -> Position {
let (width, height) = self.widgets.iter()
.map(|widget| widget.position(renderer))
.map(|pos| (pos.width(), pos.height()))
.fold((0, 0), |accum, item| (
if item.0 > accum.0 { item.0 } else { accum.0 },
accum.1 + item.1
));
Position::new(0, 0, width, height)
}
}
impl Column {
pub fn new() -> Self {
Self {
widgets: Vec::new(),
}
}
pub fn add<T: Widget + 'static>(mut self, widget: T) -> Self {
self.widgets.push(Box::new(widget));
self
}
}