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 {
|
2020-05-10 11:06:16 +00:00
|
|
|
pub margin: Vec2,
|
|
|
|
pub corner_radius: f32,
|
|
|
|
pub fill_color: Option<Color>,
|
|
|
|
pub outline: Option<Outline>,
|
2020-05-08 15:16:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Frame {
|
2020-05-10 11:06:16 +00:00
|
|
|
pub fn window(style: &Style) -> Self {
|
|
|
|
Self {
|
|
|
|
margin: style.window_padding,
|
|
|
|
corner_radius: style.window.corner_radius,
|
|
|
|
fill_color: Some(style.background_fill_color()),
|
|
|
|
outline: Some(Outline::new(1.0, color::WHITE)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn menu(style: &Style) -> Self {
|
|
|
|
Self {
|
|
|
|
margin: Vec2::splat(1.0),
|
|
|
|
corner_radius: 2.0,
|
|
|
|
fill_color: Some(style.background_fill_color()),
|
|
|
|
outline: Some(Outline::new(1.0, color::white(128))),
|
|
|
|
}
|
2020-05-08 15:16:32 +00:00
|
|
|
}
|
2020-05-10 12:27:02 +00:00
|
|
|
|
|
|
|
pub fn popup(style: &Style) -> Self {
|
|
|
|
Self {
|
|
|
|
margin: style.window_padding,
|
|
|
|
corner_radius: 5.0,
|
|
|
|
fill_color: Some(style.background_fill_color()),
|
|
|
|
outline: Some(Outline::new(1.0, color::white(128))),
|
|
|
|
}
|
|
|
|
}
|
2020-05-08 15:16:32 +00:00
|
|
|
}
|
2020-04-25 13:26:24 +00:00
|
|
|
|
|
|
|
impl Frame {
|
2020-05-08 20:42:31 +00:00
|
|
|
pub fn show(self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui)) {
|
2020-05-10 11:06:16 +00:00
|
|
|
let Frame {
|
|
|
|
margin,
|
|
|
|
corner_radius,
|
|
|
|
fill_color,
|
|
|
|
outline,
|
|
|
|
} = self;
|
2020-04-25 13:26:24 +00:00
|
|
|
|
2020-05-08 20:42:31 +00:00
|
|
|
let outer_pos = ui.cursor();
|
2020-04-25 13:26:24 +00:00
|
|
|
let inner_rect =
|
2020-05-08 20:42:31 +00:00
|
|
|
Rect::from_min_size(outer_pos + margin, ui.available_space() - 2.0 * margin);
|
|
|
|
let where_to_put_background = ui.paint_list_len();
|
2020-04-25 13:26:24 +00:00
|
|
|
|
2020-05-08 20:42:31 +00:00
|
|
|
let mut child_ui = ui.child_ui(inner_rect);
|
|
|
|
add_contents(&mut child_ui);
|
2020-04-25 13:26:24 +00:00
|
|
|
|
2020-05-08 20:42:31 +00:00
|
|
|
let inner_size = child_ui.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);
|
|
|
|
|
2020-05-08 20:42:31 +00:00
|
|
|
ui.insert_paint_cmd(
|
2020-04-25 13:26:24 +00:00
|
|
|
where_to_put_background,
|
|
|
|
PaintCmd::Rect {
|
|
|
|
corner_radius,
|
2020-05-10 11:06:16 +00:00
|
|
|
fill_color,
|
|
|
|
outline,
|
2020-04-25 13:26:24 +00:00
|
|
|
rect: outer_rect,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-05-08 20:42:31 +00:00
|
|
|
ui.expand_to_include_child(child_ui.child_bounds().expand2(margin));
|
2020-05-05 06:15:20 +00:00
|
|
|
// TODO: move up cursor?
|
2020-04-25 13:26:24 +00:00
|
|
|
}
|
|
|
|
}
|