Draw separator between window title and its contents

This commit is contained in:
Emil Ernerfeldt 2020-04-19 23:51:38 +02:00
parent 388132ba93
commit 7f85b2623b
3 changed files with 41 additions and 13 deletions

View file

@ -169,6 +169,18 @@ impl Pos2 {
y: self.y, y: self.y,
} }
} }
pub fn floor(self) -> Self {
pos2(self.x.floor(), self.y.floor())
}
pub fn round(self) -> Self {
pos2(self.x.round(), self.y.round())
}
pub fn ceil(self) -> Self {
pos2(self.x.ceil(), self.y.ceil())
}
} }
impl std::ops::AddAssign<Vec2> for Pos2 { impl std::ops::AddAssign<Vec2> for Pos2 {

View file

@ -433,24 +433,39 @@ impl<'a> Widget for Slider<'a> {
pub struct Separator { pub struct Separator {
line_width: f32, line_width: f32,
width: f32, min_length: f32,
extra: f32,
color: Color,
} }
impl Separator { impl Separator {
pub fn new() -> Separator { pub fn new() -> Separator {
Separator { Separator {
line_width: 2.0, line_width: 2.0,
width: 6.0, min_length: 6.0,
extra: 0.0,
color: color::WHITE,
} }
} }
pub fn line_width(&mut self, line_width: f32) -> &mut Self { pub fn line_width(mut self, line_width: f32) -> Self {
self.line_width = line_width; self.line_width = line_width;
self self
} }
pub fn width(&mut self, width: f32) -> &mut Self { pub fn min_length(mut self, min_length: f32) -> Self {
self.width = width; self.min_length = min_length;
self
}
/// Draw this much longer on each side
pub fn extra(mut self, extra: f32) -> Self {
self.extra = extra;
self
}
pub fn color(mut self, color: Color) -> Self {
self.color = color;
self self
} }
} }
@ -460,21 +475,21 @@ impl Widget for Separator {
let available_space = region.available_space; let available_space = region.available_space;
let (points, interact) = match region.direction() { let (points, interact) = match region.direction() {
Direction::Horizontal => { Direction::Horizontal => {
let interact = region.reserve_space(vec2(self.width, available_space.y), None); let interact = region.reserve_space(vec2(self.min_length, available_space.y), None);
( (
vec![ vec![
pos2(interact.rect.center().x, interact.rect.min().y), pos2(interact.rect.center().x, interact.rect.min().y - self.extra),
pos2(interact.rect.center().x, interact.rect.max().y), pos2(interact.rect.center().x, interact.rect.max().y + self.extra),
], ],
interact, interact,
) )
} }
Direction::Vertical => { Direction::Vertical => {
let interact = region.reserve_space(vec2(available_space.x, self.width), None); let interact = region.reserve_space(vec2(available_space.x, self.min_length), None);
( (
vec![ vec![
pos2(interact.rect.min().x, interact.rect.center().y), pos2(interact.rect.min().x - self.extra, interact.rect.center().y),
pos2(interact.rect.max().x, interact.rect.center().y), pos2(interact.rect.max().x + self.extra, interact.rect.center().y),
], ],
interact, interact,
) )
@ -482,7 +497,7 @@ impl Widget for Separator {
}; };
region.add_paint_cmd(PaintCmd::Line { region.add_paint_cmd(PaintCmd::Line {
points, points,
color: color::WHITE, color: self.color,
width: self.line_width, width: self.line_width,
}); });
region.response(interact) region.response(interact)

View file

@ -1,6 +1,6 @@
use std::sync::Arc; use std::sync::Arc;
use crate::{layout::Direction, mesher::Path, widgets::Label, *}; use crate::{layout::Direction, mesher::Path, widgets::*, *};
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct WindowState { pub struct WindowState {
@ -88,6 +88,7 @@ impl Window {
// Show top bar: // Show top bar:
contents_region.add(Label::new(self.title).text_style(TextStyle::Heading)); contents_region.add(Label::new(self.title).text_style(TextStyle::Heading));
contents_region.add(Separator::new().line_width(1.0).extra(window_padding.x)); // TODO: nicer way to split window title from contents
add_contents(&mut contents_region); add_contents(&mut contents_region);