2020-04-25 13:26:24 +00:00
|
|
|
//! Frame container
|
2020-04-25 20:49:57 +00:00
|
|
|
|
2020-04-25 13:26:24 +00:00
|
|
|
use crate::*;
|
|
|
|
|
2020-04-25 20:49:57 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2020-05-08 15:16:32 +00:00
|
|
|
pub struct Frame {
|
|
|
|
pub margin: Option<Vec2>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Frame {
|
|
|
|
pub fn margin(mut self, margin: Vec2) -> Self {
|
|
|
|
self.margin = Some(margin);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2020-04-25 13:26:24 +00:00
|
|
|
|
|
|
|
impl Frame {
|
|
|
|
pub fn show(self, region: &mut Region, add_contents: impl FnOnce(&mut Region)) {
|
|
|
|
let style = region.style();
|
2020-05-08 15:16:32 +00:00
|
|
|
let margin = self.margin.unwrap_or_default();
|
2020-04-25 13:26:24 +00:00
|
|
|
|
|
|
|
let outer_pos = region.cursor();
|
|
|
|
let inner_rect =
|
|
|
|
Rect::from_min_size(outer_pos + margin, region.available_space() - 2.0 * margin);
|
|
|
|
let where_to_put_background = region.paint_list_len();
|
|
|
|
|
|
|
|
let mut child_region = region.child_region(inner_rect);
|
|
|
|
add_contents(&mut child_region);
|
|
|
|
|
2020-04-25 13:46:50 +00:00
|
|
|
let inner_size = child_region.bounding_size();
|
2020-04-25 13:26:24 +00:00
|
|
|
let inner_size = inner_size.ceil(); // TODO: round to pixel
|
|
|
|
|
|
|
|
let outer_rect = Rect::from_min_size(outer_pos, margin + inner_size + margin);
|
|
|
|
|
|
|
|
let corner_radius = style.window.corner_radius;
|
|
|
|
let fill_color = style.background_fill_color();
|
|
|
|
region.insert_paint_cmd(
|
|
|
|
where_to_put_background,
|
|
|
|
PaintCmd::Rect {
|
|
|
|
corner_radius,
|
|
|
|
fill_color: Some(fill_color),
|
|
|
|
outline: Some(Outline::new(1.0, color::WHITE)),
|
|
|
|
rect: outer_rect,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-05-05 17:33:02 +00:00
|
|
|
region.expand_to_include_child(child_region.child_bounds().expand2(margin));
|
2020-05-05 06:15:20 +00:00
|
|
|
// TODO: move up cursor?
|
2020-04-25 13:26:24 +00:00
|
|
|
}
|
|
|
|
}
|