Files
IntroToWebAuthoring/src/views/vertical_layout.rs
T

105 lines
2.7 KiB
Rust
Raw Normal View History

2025-10-31 12:55:51 -06:00
use std::collections::HashMap;
2025-10-23 11:25:02 -06:00
2025-10-31 12:55:51 -06:00
use crate::{
parser::Tag,
2025-11-10 11:08:23 -07:00
render::Renderer,
2025-10-31 12:55:51 -06:00
views::{Bounds, View, box_view::BoxView},
};
#[derive(Debug)]
2025-10-23 11:25:02 -06:00
pub struct VerticalLayout {
2025-10-31 12:55:51 -06:00
pub views: Vec<BoxView>,
2025-10-29 14:18:23 -06:00
pub bounds: (Option<Bounds>, Option<Bounds>),
2025-10-23 11:25:02 -06:00
}
impl VerticalLayout {
2025-10-31 12:55:51 -06:00
pub fn new(views: Vec<BoxView>) -> Self {
2025-10-29 14:18:23 -06:00
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-11-10 11:08:23 -07:00
fn resize(&mut self, renderer: &mut Renderer, x: f32, y: f32, w: f32, h: f32) {}
2025-10-31 12:55:51 -06:00
fn from_tag(attributes: &HashMap<String, String>, children: &Vec<Tag>) -> Box<dyn View>
2025-10-30 14:44:41 -06:00
where
Self: Sized,
{
2025-11-10 11:39:02 -07:00
let mut parsed_children = Vec::new();
for child in children {
let child = child.parse();
if let Ok(b) = child.as_any().downcast::<BoxView>() {
parsed_children.push(*b);
} else {
panic!("Constraint Layout must contain only BoxView!")
}
}
Box::new(Self::new(parsed_children))
2025-10-31 12:55:51 -06:00
// Box::new(Self {
// views: children.iter().map(|tag| tag.parse()).collect(),
// bounds: (None, None),
// })
}
fn as_any(self: Box<Self>) -> Box<dyn std::any::Any> {
self
2025-10-30 14:44:41 -06:00
}
2025-10-29 14:18:23 -06:00
}
2025-10-31 12:55:51 -06:00
// impl LayoutView for VerticalLayout {
// 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);
2025-10-23 11:25:02 -06:00
2025-10-31 12:55:51 -06:00
// let vx = match vx {
// super::Bounds::MatchParent => pw,
// super::Bounds::Pixels(x) => x,
// };
2025-10-23 11:25:02 -06:00
2025-10-31 12:55:51 -06:00
// let vy = match vy {
// super::Bounds::MatchParent => ph,
// super::Bounds::Pixels(x) => x,
// };
2025-10-23 11:25:02 -06:00
2025-10-31 12:55:51 -06:00
// maxx = maxx.max(vx);
// totaly += vy;
// }
2025-10-23 11:25:02 -06:00
2025-10-31 12:55:51 -06:00
// (Bounds::Pixels(maxx), Bounds::Pixels(totaly))
// }
// }