2020-04-25 13:26:24 +00:00
|
|
|
//! Frame container
|
2020-04-25 20:49:57 +00:00
|
|
|
|
2020-05-19 20:28:57 +00:00
|
|
|
use crate::{paint::*, *};
|
2020-04-25 13:26:24 +00:00
|
|
|
|
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-17 14:42:20 +00:00
|
|
|
// On each side
|
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,
|
2020-05-17 07:44:09 +00:00
|
|
|
fill_color: Some(style.background_fill_color),
|
2020-05-10 11:06:16 +00:00
|
|
|
outline: Some(Outline::new(1.0, color::WHITE)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 07:44:09 +00:00
|
|
|
pub fn menu_bar(_style: &Style) -> Self {
|
|
|
|
Self {
|
|
|
|
margin: Vec2::splat(1.0),
|
|
|
|
corner_radius: 0.0,
|
|
|
|
fill_color: None,
|
|
|
|
outline: Some(Outline::new(0.5, color::white(128))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 11:06:16 +00:00
|
|
|
pub fn menu(style: &Style) -> Self {
|
|
|
|
Self {
|
|
|
|
margin: Vec2::splat(1.0),
|
|
|
|
corner_radius: 2.0,
|
2020-05-17 07:44:09 +00:00
|
|
|
fill_color: Some(style.background_fill_color),
|
2020-05-10 11:06:16 +00:00
|
|
|
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,
|
2020-05-17 07:44:09 +00:00
|
|
|
fill_color: Some(style.background_fill_color),
|
2020-05-10 12:27:02 +00:00
|
|
|
outline: Some(Outline::new(1.0, color::white(128))),
|
|
|
|
}
|
|
|
|
}
|
2020-05-11 18:21:24 +00:00
|
|
|
|
|
|
|
pub fn fill_color(mut self, fill_color: Option<Color>) -> Self {
|
|
|
|
self.fill_color = fill_color;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn outline(mut self, outline: Option<Outline>) -> Self {
|
|
|
|
self.outline = outline;
|
|
|
|
self
|
|
|
|
}
|
2020-05-08 15:16:32 +00:00
|
|
|
}
|
2020-04-25 13:26:24 +00:00
|
|
|
|
|
|
|
impl Frame {
|
2020-05-19 21:57:48 +00:00
|
|
|
pub fn show<R>(self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> R {
|
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-12 16:21:09 +00:00
|
|
|
let outer_rect = ui.available();
|
2020-05-17 14:42:20 +00:00
|
|
|
let inner_rect = outer_rect.shrink2(margin);
|
2020-05-08 20:42:31 +00:00
|
|
|
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);
|
2020-05-19 21:57:48 +00:00
|
|
|
let ret = add_contents(&mut child_ui);
|
2020-04-25 13:26:24 +00:00
|
|
|
|
2020-05-12 16:21:09 +00:00
|
|
|
let outer_rect = Rect::from_min_max(outer_rect.min, child_ui.child_bounds().max + margin);
|
2020-04-25 13:26:24 +00:00
|
|
|
|
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-12 16:21:09 +00:00
|
|
|
ui.expand_to_include_child(outer_rect);
|
|
|
|
// TODO: move cursor in parent ui
|
2020-05-19 21:57:48 +00:00
|
|
|
|
|
|
|
ret
|
2020-04-25 13:26:24 +00:00
|
|
|
}
|
|
|
|
}
|