2020-04-17 12:26:36 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-04-17 21:44:14 +00:00
|
|
|
use crate::{layout::Direction, widgets::Label, *};
|
2020-04-17 12:26:36 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct WindowState {
|
|
|
|
/// Last known pos/size
|
|
|
|
pub rect: Rect,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Window {
|
|
|
|
/// The title of the window and by default the source of its identity.
|
|
|
|
title: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Window {
|
|
|
|
pub fn new<S: Into<String>>(title: S) -> Self {
|
|
|
|
Self {
|
|
|
|
title: title.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 13:33:52 +00:00
|
|
|
pub fn show<F>(self, ctx: &Arc<Context>, add_contents: F)
|
2020-04-17 12:26:36 +00:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Region),
|
|
|
|
{
|
2020-04-17 21:44:14 +00:00
|
|
|
let id = Id::new(&self.title);
|
2020-04-17 12:26:36 +00:00
|
|
|
|
2020-04-17 21:30:01 +00:00
|
|
|
let mut state = ctx.memory.lock().get_or_create_window(
|
2020-04-17 12:26:36 +00:00
|
|
|
id,
|
|
|
|
Rect::from_min_size(
|
2020-04-18 23:05:49 +00:00
|
|
|
pos2(400.0, 200.0), // TODO
|
2020-04-17 12:26:36 +00:00
|
|
|
vec2(200.0, 200.0), // TODO
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
let layer = Layer::Window(id);
|
2020-04-17 21:30:01 +00:00
|
|
|
let where_to_put_background = ctx.graphics.lock().layer(layer).len();
|
2020-04-17 12:26:36 +00:00
|
|
|
|
2020-04-17 13:33:52 +00:00
|
|
|
let style = ctx.style();
|
2020-04-17 12:26:36 +00:00
|
|
|
let window_padding = style.window_padding;
|
|
|
|
|
|
|
|
let mut contents_region = Region {
|
2020-04-17 13:33:52 +00:00
|
|
|
ctx: ctx.clone(),
|
2020-04-17 21:22:28 +00:00
|
|
|
layer,
|
2020-04-17 12:26:36 +00:00
|
|
|
style,
|
2020-04-17 21:44:14 +00:00
|
|
|
id,
|
2020-04-17 12:26:36 +00:00
|
|
|
dir: Direction::Vertical,
|
|
|
|
align: Align::Min,
|
|
|
|
cursor: state.rect.min() + window_padding,
|
|
|
|
bounding_size: vec2(0.0, 0.0),
|
2020-04-17 13:33:52 +00:00
|
|
|
available_space: vec2(ctx.input.screen_size.x.min(350.0), std::f32::INFINITY), // TODO: window.width
|
2020-04-17 12:26:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Show top bar:
|
|
|
|
contents_region.add(Label::new(self.title).text_style(TextStyle::Heading));
|
|
|
|
|
|
|
|
add_contents(&mut contents_region);
|
|
|
|
|
|
|
|
// Now insert window background:
|
|
|
|
|
|
|
|
// TODO: handle the last item_spacing in a nicer way
|
|
|
|
let inner_size = contents_region.bounding_size - style.item_spacing;
|
|
|
|
let outer_size = inner_size + 2.0 * window_padding;
|
|
|
|
|
|
|
|
state.rect = Rect::from_min_size(state.rect.min(), outer_size);
|
|
|
|
|
2020-04-17 21:30:01 +00:00
|
|
|
let mut graphics = ctx.graphics.lock();
|
|
|
|
graphics.layer(layer).insert(
|
2020-04-17 12:26:36 +00:00
|
|
|
where_to_put_background,
|
|
|
|
PaintCmd::Rect {
|
|
|
|
corner_radius: 5.0,
|
|
|
|
fill_color: Some(style.background_fill_color()),
|
|
|
|
outline: Some(Outline {
|
|
|
|
color: color::WHITE,
|
|
|
|
width: 1.0,
|
|
|
|
}),
|
|
|
|
rect: state.rect,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-04-17 13:33:52 +00:00
|
|
|
let interact = ctx.interact(layer, state.rect, Some(id));
|
2020-04-17 12:26:36 +00:00
|
|
|
if interact.active {
|
2020-04-17 13:33:52 +00:00
|
|
|
state.rect = state.rect.translate(ctx.input().mouse_move);
|
2020-04-17 12:26:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 21:30:01 +00:00
|
|
|
let mut memory = ctx.memory.lock();
|
2020-04-17 12:26:36 +00:00
|
|
|
if interact.active || interact.clicked {
|
|
|
|
memory.move_window_to_top(id);
|
|
|
|
}
|
|
|
|
memory.set_window_state(id, state);
|
|
|
|
}
|
|
|
|
}
|