Files
IntroToWebAuthoring/src/views/vertical_layout.rs
T

70 lines
1.8 KiB
Rust
Raw Normal View History

2025-10-29 14:18:23 -06:00
use crate::views::{Bounds, LayoutView, View};
2025-10-23 11:25:02 -06:00
pub struct VerticalLayout {
2025-10-29 14:18:23 -06:00
pub views: Vec<Box<dyn LayoutView>>,
pub bounds: (Option<Bounds>, Option<Bounds>),
2025-10-23 11:25:02 -06:00
}
impl VerticalLayout {
2025-10-29 14:18:23 -06:00
pub fn new(views: Vec<Box<dyn LayoutView>>) -> Self {
Self {
views,
bounds: (None, None),
}
2025-10-23 11:25:02 -06:00
}
2025-10-29 21:10:51 -06:00
pub fn set_bounds(&mut self, width: Option<Bounds>, height: Option<Bounds>) {
self.bounds = (width, height);
}
2025-10-23 11:25:02 -06:00
}
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;
}
}
}
2025-10-29 21:10:51 -06:00
fn resize(&mut self, x: f32, y: f32, w: f32, h: f32) {}
2025-10-29 14:18:23 -06:00
}
impl LayoutView for VerticalLayout {
2025-10-23 11:25:02 -06:00
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))
}
}