Frame: more customizeable

This commit is contained in:
Emil Ernerfeldt 2020-05-10 13:06:16 +02:00
parent 2dd1c5ba78
commit f7291e4a0d
2 changed files with 34 additions and 16 deletions

View file

@ -4,20 +4,40 @@ use crate::*;
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct Frame { pub struct Frame {
pub margin: Option<Vec2>, pub margin: Vec2,
pub corner_radius: f32,
pub fill_color: Option<Color>,
pub outline: Option<Outline>,
} }
impl Frame { impl Frame {
pub fn margin(mut self, margin: Vec2) -> Self { pub fn window(style: &Style) -> Self {
self.margin = Some(margin); 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))),
}
} }
} }
impl Frame { impl Frame {
pub fn show(self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui)) { pub fn show(self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui)) {
let style = ui.style(); let Frame {
let margin = self.margin.unwrap_or_default(); margin,
corner_radius,
fill_color,
outline,
} = self;
let outer_pos = ui.cursor(); let outer_pos = ui.cursor();
let inner_rect = let inner_rect =
@ -32,14 +52,12 @@ impl Frame {
let outer_rect = Rect::from_min_size(outer_pos, margin + inner_size + margin); 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();
ui.insert_paint_cmd( ui.insert_paint_cmd(
where_to_put_background, where_to_put_background,
PaintCmd::Rect { PaintCmd::Rect {
corner_radius, corner_radius,
fill_color: Some(fill_color), fill_color,
outline: Some(Outline::new(1.0, color::WHITE)), outline,
rect: outer_rect, rect: outer_rect,
}, },
); );

View file

@ -8,7 +8,7 @@ use super::*;
pub struct Window { pub struct Window {
pub title_label: Label, pub title_label: Label,
pub floating: Floating, pub floating: Floating,
pub frame: Frame, pub frame: Option<Frame>,
pub resize: Resize, pub resize: Resize,
pub scroll: ScrollArea, pub scroll: ScrollArea,
} }
@ -24,7 +24,7 @@ impl Window {
Self { Self {
title_label, title_label,
floating, floating,
frame: Frame::default(), frame: None,
resize: Resize::default() resize: Resize::default()
.handle_offset(Vec2::splat(4.0)) .handle_offset(Vec2::splat(4.0))
.auto_shrink_width(true) .auto_shrink_width(true)
@ -85,15 +85,15 @@ impl Window {
} }
impl Window { impl Window {
pub fn show(self, ctx: &Arc<Context>, add_contents: impl FnOnce(&mut Ui)) { pub fn show(self, ctx: &Arc<Context>, add_contents: impl FnOnce(&mut Ui)) -> InteractInfo {
let Window { let Window {
title_label, title_label,
floating, floating,
mut frame, frame,
resize, resize,
scroll, scroll,
} = self; } = self;
frame.margin = Some(frame.margin.unwrap_or_else(|| ctx.style().window_padding)); let frame = frame.unwrap_or_else(|| Frame::window(&ctx.style()));
// TODO: easier way to compose these // TODO: easier way to compose these
floating.show(ctx, |ui| { floating.show(ctx, |ui| {
@ -101,7 +101,7 @@ impl Window {
resize.show(ui, |ui| { resize.show(ui, |ui| {
ui.add(title_label); ui.add(title_label);
ui.add(Separator::new().line_width(1.0)); // TODO: nicer way to split window title from contents ui.add(Separator::new().line_width(1.0)); // TODO: nicer way to split window title from contents
scroll.show(ui, |ui| add_contents(ui)) scroll.show(ui, add_contents)
}) })
}) })
}) })