2025-10-23 11:25:02 -06:00
|
|
|
use crate::views::{Bounds, View};
|
|
|
|
|
|
|
|
|
|
pub struct VerticalLayout {
|
|
|
|
|
pub views: Vec<Box<dyn View>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl VerticalLayout {
|
|
|
|
|
pub fn new(views: Vec<Box<dyn View>>) -> Self {
|
|
|
|
|
Self { views }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl View for VerticalLayout {
|
2025-10-27 10:43:40 -06:00
|
|
|
fn draw(&mut self, renderer: &mut crate::render::Renderer, x: f32, y: f32, w: f32, h: f32) {
|
2025-10-23 11:25:02 -06:00
|
|
|
let mut cur_y = y;
|
|
|
|
|
|
2025-10-27 10:43:40 -06:00
|
|
|
for view in &mut self.views {
|
2025-10-23 11:25:02 -06:00
|
|
|
let (vx, vy) = view.bounds(w, h);
|
|
|
|
|
|
|
|
|
|
let vx = match vx {
|
|
|
|
|
super::Bounds::MatchParent => w,
|
|
|
|
|
super::Bounds::Pixels(x) => x,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let vy = match vy {
|
|
|
|
|
super::Bounds::MatchParent => h,
|
|
|
|
|
super::Bounds::Pixels(x) => x,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
view.draw(renderer, x, cur_y, w.min(vx), vy);
|
|
|
|
|
cur_y += vy;
|
|
|
|
|
if cur_y > h {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn bounds(&self, pw: f32, ph: f32) -> (Bounds, Bounds) {
|
|
|
|
|
let (mut maxx, mut totaly): (f32, f32) = (0., 0.);
|
|
|
|
|
for view in &self.views {
|
|
|
|
|
let (vx, vy) = view.bounds(pw, ph);
|
|
|
|
|
|
|
|
|
|
let vx = match vx {
|
|
|
|
|
super::Bounds::MatchParent => pw,
|
|
|
|
|
super::Bounds::Pixels(x) => x,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let vy = match vy {
|
|
|
|
|
super::Bounds::MatchParent => ph,
|
|
|
|
|
super::Bounds::Pixels(x) => x,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
maxx = maxx.max(vx);
|
|
|
|
|
totaly += vy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(Bounds::Pixels(maxx), Bounds::Pixels(totaly))
|
|
|
|
|
}
|
|
|
|
|
}
|