Browse Source

Draw separator between window title and its contents

clip_rect
Emil Ernerfeldt 5 years ago
parent
commit
7f85b2623b
  1. 12
      emigui/src/math.rs
  2. 39
      emigui/src/widgets.rs
  3. 3
      emigui/src/window.rs

12
emigui/src/math.rs

@ -169,6 +169,18 @@ impl Pos2 {
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 {

39
emigui/src/widgets.rs

@ -433,24 +433,39 @@ impl<'a> Widget for Slider<'a> {
pub struct Separator {
line_width: f32,
width: f32,
min_length: f32,
extra: f32,
color: Color,
}
impl Separator {
pub fn new() -> Separator {
Separator {
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
}
pub fn width(&mut self, width: f32) -> &mut Self {
self.width = width;
pub fn min_length(mut self, min_length: f32) -> Self {
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
}
}
@ -460,21 +475,21 @@ impl Widget for Separator {
let available_space = region.available_space;
let (points, interact) = match region.direction() {
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![
pos2(interact.rect.center().x, interact.rect.min().y),
pos2(interact.rect.center().x, interact.rect.max().y),
pos2(interact.rect.center().x, interact.rect.min().y - self.extra),
pos2(interact.rect.center().x, interact.rect.max().y + self.extra),
],
interact,
)
}
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![
pos2(interact.rect.min().x, interact.rect.center().y),
pos2(interact.rect.max().x, interact.rect.center().y),
pos2(interact.rect.min().x - self.extra, interact.rect.center().y),
pos2(interact.rect.max().x + self.extra, interact.rect.center().y),
],
interact,
)
@ -482,7 +497,7 @@ impl Widget for Separator {
};
region.add_paint_cmd(PaintCmd::Line {
points,
color: color::WHITE,
color: self.color,
width: self.line_width,
});
region.response(interact)

3
emigui/src/window.rs

@ -1,6 +1,6 @@
use std::sync::Arc;
use crate::{layout::Direction, mesher::Path, widgets::Label, *};
use crate::{layout::Direction, mesher::Path, widgets::*, *};
#[derive(Clone, Copy, Debug)]
pub struct WindowState {
@ -88,6 +88,7 @@ impl Window {
// Show top bar:
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);

Loading…
Cancel
Save