2020-03-23 02:19:05 +03:00
|
|
|
use crate::widgets::containers::{Container, Nothing};
|
2019-12-21 02:56:04 +03:00
|
|
|
use crate::{
|
2020-03-21 04:05:50 +03:00
|
|
|
Btn, Button, Checkbox, Choice, Color, Drawable, Dropdown, EventCtx, Filler, GeomBatch, GfxCtx,
|
2020-03-23 09:50:14 +03:00
|
|
|
HorizontalAlignment, JustDraw, Menu, MultiKey, RewriteColor, ScreenDims, ScreenPt,
|
|
|
|
ScreenRectangle, Slider, TextBox, VerticalAlignment, WidgetImpl,
|
2019-12-21 02:56:04 +03:00
|
|
|
};
|
2020-03-23 00:06:02 +03:00
|
|
|
use geom::{Distance, Polygon};
|
2020-03-22 04:51:07 +03:00
|
|
|
use std::collections::HashSet;
|
2019-12-21 02:56:04 +03:00
|
|
|
use stretch::geometry::{Rect, Size};
|
|
|
|
use stretch::node::{Node, Stretch};
|
2020-01-15 02:18:27 +03:00
|
|
|
use stretch::number::Number;
|
2020-01-15 03:46:45 +03:00
|
|
|
use stretch::style::{
|
|
|
|
AlignItems, Dimension, FlexDirection, FlexWrap, JustifyContent, PositionType, Style,
|
|
|
|
};
|
2019-12-21 02:56:04 +03:00
|
|
|
|
2020-03-21 09:41:07 +03:00
|
|
|
pub struct Widget {
|
2020-03-23 02:19:05 +03:00
|
|
|
// TODO pub just for Container. Just move that here?
|
|
|
|
pub(crate) widget: Box<dyn WidgetImpl>,
|
2020-03-25 20:45:17 +03:00
|
|
|
layout: LayoutStyle,
|
2020-03-23 02:35:20 +03:00
|
|
|
pub(crate) rect: ScreenRectangle,
|
2020-02-07 20:05:45 +03:00
|
|
|
bg: Option<Drawable>,
|
2020-02-21 23:19:24 +03:00
|
|
|
id: Option<String>,
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct LayoutStyle {
|
|
|
|
bg_color: Option<Color>,
|
2020-01-24 02:35:08 +03:00
|
|
|
outline: Option<(f64, Color)>,
|
2020-03-25 20:45:17 +03:00
|
|
|
style: Style,
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Layouting
|
|
|
|
// TODO Maybe I just want margin, not padding. And maybe more granular controls per side. And to
|
|
|
|
// apply margin to everything in a row or column.
|
|
|
|
// TODO Row and columns feel backwards when using them.
|
2020-03-21 09:41:07 +03:00
|
|
|
impl Widget {
|
|
|
|
pub fn centered(mut self) -> Widget {
|
2020-03-25 20:45:17 +03:00
|
|
|
self.layout.style.align_items = AlignItems::Center;
|
|
|
|
self.layout.style.justify_content = JustifyContent::SpaceAround;
|
2019-12-21 02:56:04 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn centered_horiz(self) -> Widget {
|
|
|
|
Widget::row(vec![self]).centered()
|
2020-01-29 23:16:18 +03:00
|
|
|
}
|
|
|
|
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn centered_vert(self) -> Widget {
|
|
|
|
Widget::col(vec![self]).centered()
|
2020-01-29 23:16:18 +03:00
|
|
|
}
|
|
|
|
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn centered_cross(mut self) -> Widget {
|
2020-03-25 20:45:17 +03:00
|
|
|
self.layout.style.align_items = AlignItems::Center;
|
2019-12-21 02:56:04 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn evenly_spaced(mut self) -> Widget {
|
2020-03-25 20:45:17 +03:00
|
|
|
self.layout.style.justify_content = JustifyContent::SpaceBetween;
|
2019-12-21 02:56:04 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-01-15 22:21:32 +03:00
|
|
|
// This one is really weird. percent_width should be LESS than the max_size_percent given to
|
|
|
|
// the overall Composite, otherwise weird things happen.
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn flex_wrap(mut self, ctx: &EventCtx, percent_width: usize) -> Widget {
|
2020-03-25 20:45:17 +03:00
|
|
|
self.layout.style.size = Size {
|
2020-01-15 22:21:32 +03:00
|
|
|
width: Dimension::Points(
|
|
|
|
(ctx.canvas.window_width * (percent_width as f64) / 100.0) as f32,
|
|
|
|
),
|
2019-12-23 22:05:24 +03:00
|
|
|
height: Dimension::Undefined,
|
2020-03-25 20:45:17 +03:00
|
|
|
};
|
|
|
|
self.layout.style.flex_wrap = FlexWrap::Wrap;
|
|
|
|
self.layout.style.justify_content = JustifyContent::SpaceAround;
|
2020-01-11 23:32:41 +03:00
|
|
|
self
|
|
|
|
}
|
2019-12-21 02:56:04 +03:00
|
|
|
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn bg(mut self, color: Color) -> Widget {
|
2020-03-25 20:45:17 +03:00
|
|
|
self.layout.bg_color = Some(color);
|
2019-12-21 02:56:04 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-01-24 02:35:08 +03:00
|
|
|
// Callers have to adjust padding too, probably
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn outline(mut self, thickness: f64, color: Color) -> Widget {
|
2020-03-25 20:45:17 +03:00
|
|
|
self.layout.outline = Some((thickness, color));
|
2020-01-24 02:35:08 +03:00
|
|
|
self
|
2020-01-20 01:18:38 +03:00
|
|
|
}
|
|
|
|
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn padding(mut self, pixels: usize) -> Widget {
|
2020-03-25 20:45:17 +03:00
|
|
|
self.layout.style.padding = Rect {
|
2019-12-21 02:56:04 +03:00
|
|
|
start: Dimension::Points(pixels as f32),
|
|
|
|
end: Dimension::Points(pixels as f32),
|
|
|
|
top: Dimension::Points(pixels as f32),
|
|
|
|
bottom: Dimension::Points(pixels as f32),
|
2020-03-25 20:45:17 +03:00
|
|
|
};
|
2019-12-21 02:56:04 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn margin(mut self, pixels: usize) -> Widget {
|
2020-03-25 20:45:17 +03:00
|
|
|
self.layout.style.margin = Rect {
|
2019-12-21 02:56:04 +03:00
|
|
|
start: Dimension::Points(pixels as f32),
|
|
|
|
end: Dimension::Points(pixels as f32),
|
|
|
|
top: Dimension::Points(pixels as f32),
|
|
|
|
bottom: Dimension::Points(pixels as f32),
|
2020-03-25 20:45:17 +03:00
|
|
|
};
|
2019-12-21 02:56:04 +03:00
|
|
|
self
|
|
|
|
}
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn margin_above(mut self, pixels: usize) -> Widget {
|
2020-03-25 20:45:17 +03:00
|
|
|
self.layout.style.margin = Rect {
|
2020-03-03 23:12:42 +03:00
|
|
|
start: Dimension::Undefined,
|
|
|
|
end: Dimension::Undefined,
|
|
|
|
top: Dimension::Points(pixels as f32),
|
|
|
|
bottom: Dimension::Undefined,
|
2020-03-25 20:45:17 +03:00
|
|
|
};
|
2020-03-03 23:12:42 +03:00
|
|
|
self
|
|
|
|
}
|
2020-01-11 22:54:42 +03:00
|
|
|
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn align_left(mut self) -> Widget {
|
2020-03-25 20:45:17 +03:00
|
|
|
self.layout.style.margin = Rect {
|
2020-02-04 03:13:40 +03:00
|
|
|
start: Dimension::Undefined,
|
|
|
|
end: Dimension::Auto,
|
|
|
|
top: Dimension::Undefined,
|
|
|
|
bottom: Dimension::Undefined,
|
2020-03-25 20:45:17 +03:00
|
|
|
};
|
2020-02-04 03:13:40 +03:00
|
|
|
self
|
|
|
|
}
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn align_right(mut self) -> Widget {
|
2020-03-25 20:45:17 +03:00
|
|
|
self.layout.style.margin = Rect {
|
2020-01-11 22:54:42 +03:00
|
|
|
start: Dimension::Auto,
|
|
|
|
end: Dimension::Undefined,
|
|
|
|
top: Dimension::Undefined,
|
|
|
|
bottom: Dimension::Undefined,
|
2020-03-25 20:45:17 +03:00
|
|
|
};
|
2020-01-11 22:54:42 +03:00
|
|
|
self
|
|
|
|
}
|
2020-02-04 03:13:40 +03:00
|
|
|
// This doesn't count against the entire container
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn align_vert_center(mut self) -> Widget {
|
2020-03-25 20:45:17 +03:00
|
|
|
self.layout.style.margin = Rect {
|
2020-02-04 03:13:40 +03:00
|
|
|
start: Dimension::Undefined,
|
|
|
|
end: Dimension::Undefined,
|
|
|
|
top: Dimension::Auto,
|
|
|
|
bottom: Dimension::Auto,
|
2020-03-25 20:45:17 +03:00
|
|
|
};
|
2020-02-04 03:13:40 +03:00
|
|
|
self
|
|
|
|
}
|
2020-01-15 03:46:45 +03:00
|
|
|
|
2020-03-21 09:41:07 +03:00
|
|
|
fn abs(mut self, x: f64, y: f64) -> Widget {
|
2020-03-25 20:45:17 +03:00
|
|
|
self.layout.style.position_type = PositionType::Absolute;
|
|
|
|
self.layout.style.position = Rect {
|
2020-01-15 03:46:45 +03:00
|
|
|
start: Dimension::Points(x as f32),
|
|
|
|
end: Dimension::Undefined,
|
|
|
|
top: Dimension::Points(y as f32),
|
|
|
|
bottom: Dimension::Undefined,
|
2020-03-25 20:45:17 +03:00
|
|
|
};
|
2020-01-15 03:46:45 +03:00
|
|
|
self
|
|
|
|
}
|
2020-02-21 23:19:24 +03:00
|
|
|
|
2020-03-22 20:48:11 +03:00
|
|
|
pub fn named<I: Into<String>>(mut self, id: I) -> Widget {
|
|
|
|
assert!(self.id.is_none());
|
|
|
|
self.id = Some(id.into());
|
2020-02-21 23:19:24 +03:00
|
|
|
self
|
|
|
|
}
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convenient?? constructors
|
2020-03-21 09:41:07 +03:00
|
|
|
impl Widget {
|
2020-03-23 06:59:44 +03:00
|
|
|
pub fn new(widget: Box<dyn WidgetImpl>) -> Widget {
|
2020-03-21 09:41:07 +03:00
|
|
|
Widget {
|
2019-12-21 02:56:04 +03:00
|
|
|
widget,
|
2020-03-25 20:45:17 +03:00
|
|
|
layout: LayoutStyle {
|
2019-12-21 02:56:04 +03:00
|
|
|
bg_color: None,
|
2020-01-24 02:35:08 +03:00
|
|
|
outline: None,
|
2020-03-25 20:45:17 +03:00
|
|
|
style: Style {
|
|
|
|
..Default::default()
|
|
|
|
},
|
2019-12-21 02:56:04 +03:00
|
|
|
},
|
|
|
|
rect: ScreenRectangle::placeholder(),
|
|
|
|
bg: None,
|
2020-02-21 23:19:24 +03:00
|
|
|
id: None,
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 09:50:14 +03:00
|
|
|
// TODO These are literally just convenient APIs to avoid importing JustDraw. Do we want this
|
|
|
|
// or not?
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn draw_batch(ctx: &EventCtx, batch: GeomBatch) -> Widget {
|
2020-02-07 10:08:17 +03:00
|
|
|
JustDraw::wrap(ctx, batch)
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn draw_svg(ctx: &EventCtx, filename: &str) -> Widget {
|
2020-02-07 10:08:17 +03:00
|
|
|
JustDraw::svg(ctx, filename)
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn draw_svg_transform(ctx: &EventCtx, filename: &str, rewrite: RewriteColor) -> Widget {
|
2020-02-07 10:08:17 +03:00
|
|
|
JustDraw::svg_transform(ctx, filename, rewrite)
|
2020-01-14 02:56:25 +03:00
|
|
|
}
|
2019-12-21 02:56:04 +03:00
|
|
|
|
2020-03-23 09:50:14 +03:00
|
|
|
// TODO Convenient constructors here, but shouldn't do it like this
|
2020-02-29 06:06:59 +03:00
|
|
|
pub fn checkbox(
|
|
|
|
ctx: &EventCtx,
|
|
|
|
label: &str,
|
|
|
|
hotkey: Option<MultiKey>,
|
|
|
|
enabled: bool,
|
2020-03-21 09:41:07 +03:00
|
|
|
) -> Widget {
|
|
|
|
Widget::custom_checkbox(
|
2020-03-18 21:45:20 +03:00
|
|
|
enabled,
|
2020-03-28 02:56:34 +03:00
|
|
|
Btn::text_fg(format!("[ ] {}", label)).build(ctx, label, hotkey.clone()),
|
|
|
|
Btn::text_fg(format!("[X] {}", label)).build(ctx, label, hotkey),
|
2020-03-18 21:45:20 +03:00
|
|
|
)
|
|
|
|
.outline(2.0, Color::WHITE)
|
|
|
|
.named(label)
|
|
|
|
}
|
|
|
|
// TODO Not typesafe! Gotta pass a button.
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn custom_checkbox(enabled: bool, false_btn: Widget, true_btn: Widget) -> Widget {
|
2020-03-23 02:19:05 +03:00
|
|
|
Widget::new(Box::new(Checkbox::new(
|
2020-03-18 21:45:20 +03:00
|
|
|
enabled,
|
2020-03-21 01:40:43 +03:00
|
|
|
false_btn.take_btn(),
|
|
|
|
true_btn.take_btn(),
|
2020-03-23 02:19:05 +03:00
|
|
|
)))
|
2020-03-05 02:55:44 +03:00
|
|
|
}
|
2020-02-29 06:06:59 +03:00
|
|
|
|
2020-03-23 09:50:14 +03:00
|
|
|
// TODO Likewise
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn text_entry(ctx: &EventCtx, prefilled: String, exclusive_focus: bool) -> Widget {
|
2020-03-05 02:33:28 +03:00
|
|
|
// TODO Hardcoded style, max chars
|
2020-03-23 02:19:05 +03:00
|
|
|
Widget::new(Box::new(TextBox::new(ctx, 50, prefilled, exclusive_focus)))
|
2020-03-05 02:33:28 +03:00
|
|
|
}
|
|
|
|
|
2020-03-23 09:50:14 +03:00
|
|
|
// TODO Likewise
|
2020-03-23 00:23:01 +03:00
|
|
|
pub fn dropdown<T: 'static + PartialEq + Clone>(
|
2020-03-06 02:00:42 +03:00
|
|
|
ctx: &EventCtx,
|
|
|
|
label: &str,
|
|
|
|
default_value: T,
|
|
|
|
choices: Vec<Choice<T>>,
|
2020-03-21 09:41:07 +03:00
|
|
|
) -> Widget {
|
2020-03-23 02:19:05 +03:00
|
|
|
Widget::new(Box::new(Dropdown::new(ctx, label, default_value, choices)))
|
|
|
|
.named(label)
|
|
|
|
.outline(2.0, Color::WHITE)
|
2020-03-06 02:00:42 +03:00
|
|
|
}
|
|
|
|
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn row(widgets: Vec<Widget>) -> Widget {
|
2020-03-23 02:19:05 +03:00
|
|
|
Widget::new(Box::new(Container::new(true, widgets)))
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn col(widgets: Vec<Widget>) -> Widget {
|
2020-03-23 02:19:05 +03:00
|
|
|
Widget::new(Box::new(Container::new(false, widgets)))
|
2020-03-05 03:13:17 +03:00
|
|
|
}
|
|
|
|
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn nothing() -> Widget {
|
2020-03-23 02:19:05 +03:00
|
|
|
Widget::new(Box::new(Nothing {}))
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internals
|
2020-03-21 09:41:07 +03:00
|
|
|
impl Widget {
|
2020-03-23 02:19:05 +03:00
|
|
|
pub(crate) fn draw(&self, g: &mut GfxCtx) {
|
2020-03-23 00:55:15 +03:00
|
|
|
// Don't draw these yet; clipping is still in effect.
|
|
|
|
if self.id == Some("horiz scrollbar".to_string())
|
|
|
|
|| self.id == Some("vert scrollbar".to_string())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-21 02:56:04 +03:00
|
|
|
if let Some(ref bg) = self.bg {
|
2020-02-07 20:05:45 +03:00
|
|
|
g.redraw_at(ScreenPt::new(self.rect.x1, self.rect.y1), bg);
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
|
2020-03-23 02:19:05 +03:00
|
|
|
self.widget.draw(g);
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate a flattened list of Nodes, matching the traversal order
|
2020-03-22 04:51:07 +03:00
|
|
|
fn get_flexbox(&self, parent: Node, stretch: &mut Stretch, nodes: &mut Vec<Node>) {
|
2020-03-23 02:19:05 +03:00
|
|
|
if let Some(container) = self.widget.downcast_ref::<Container>() {
|
2020-03-25 20:45:17 +03:00
|
|
|
let mut style = self.layout.style.clone();
|
|
|
|
style.flex_direction = if container.is_row {
|
|
|
|
FlexDirection::Row
|
|
|
|
} else {
|
|
|
|
FlexDirection::Column
|
2020-03-23 02:19:05 +03:00
|
|
|
};
|
|
|
|
let node = stretch.new_node(style, Vec::new()).unwrap();
|
|
|
|
nodes.push(node);
|
|
|
|
for widget in &container.members {
|
|
|
|
widget.get_flexbox(node, stretch, nodes);
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
2020-03-23 02:19:05 +03:00
|
|
|
stretch.add_child(parent, node).unwrap();
|
|
|
|
return;
|
|
|
|
} else {
|
2020-03-25 20:45:17 +03:00
|
|
|
let mut style = self.layout.style.clone();
|
|
|
|
style.size = Size {
|
|
|
|
width: Dimension::Points(self.widget.get_dims().width as f32),
|
|
|
|
height: Dimension::Points(self.widget.get_dims().height as f32),
|
2020-03-23 02:19:05 +03:00
|
|
|
};
|
|
|
|
let node = stretch.new_node(style, Vec::new()).unwrap();
|
|
|
|
stretch.add_child(parent, node).unwrap();
|
|
|
|
nodes.push(node);
|
|
|
|
}
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_flexbox(
|
|
|
|
&mut self,
|
|
|
|
stretch: &Stretch,
|
|
|
|
nodes: &mut Vec<Node>,
|
|
|
|
dx: f64,
|
|
|
|
dy: f64,
|
2020-01-15 02:47:35 +03:00
|
|
|
scroll_offset: (f64, f64),
|
2019-12-21 02:56:04 +03:00
|
|
|
ctx: &EventCtx,
|
2020-02-21 23:57:51 +03:00
|
|
|
recompute_layout: bool,
|
2019-12-21 02:56:04 +03:00
|
|
|
) {
|
|
|
|
let result = stretch.layout(nodes.pop().unwrap()).unwrap();
|
|
|
|
let x: f64 = result.location.x.into();
|
|
|
|
let y: f64 = result.location.y.into();
|
|
|
|
let width: f64 = result.size.width.into();
|
|
|
|
let height: f64 = result.size.height.into();
|
2020-03-23 00:06:02 +03:00
|
|
|
// Don't scroll the scrollbars
|
|
|
|
let top_left = if self.id == Some("horiz scrollbar".to_string())
|
|
|
|
|| self.id == Some("vert scrollbar".to_string())
|
|
|
|
{
|
|
|
|
ScreenPt::new(x, y)
|
|
|
|
} else {
|
|
|
|
ScreenPt::new(x + dx - scroll_offset.0, y + dy - scroll_offset.1)
|
2019-12-24 02:00:22 +03:00
|
|
|
};
|
|
|
|
self.rect = ScreenRectangle::top_left(top_left, ScreenDims::new(width, height));
|
2020-01-20 01:18:38 +03:00
|
|
|
|
|
|
|
// Assume widgets don't dynamically change, so we just upload the background once.
|
2020-02-21 23:57:51 +03:00
|
|
|
if (self.bg.is_none() || recompute_layout)
|
2020-03-25 20:45:17 +03:00
|
|
|
&& (self.layout.bg_color.is_some() || self.layout.outline.is_some())
|
2020-02-21 23:57:51 +03:00
|
|
|
{
|
2020-01-20 01:18:38 +03:00
|
|
|
let mut batch = GeomBatch::new();
|
2020-03-25 20:45:17 +03:00
|
|
|
if let Some(c) = self.layout.bg_color {
|
2020-01-20 23:36:27 +03:00
|
|
|
batch.push(c, Polygon::rounded_rectangle(width, height, 5.0));
|
2020-01-20 01:18:38 +03:00
|
|
|
}
|
2020-03-25 20:45:17 +03:00
|
|
|
if let Some((thickness, c)) = self.layout.outline {
|
2020-01-20 01:18:38 +03:00
|
|
|
batch.push(
|
|
|
|
c,
|
2020-01-20 23:36:27 +03:00
|
|
|
Polygon::rounded_rectangle(width, height, 5.0)
|
2020-01-24 02:35:08 +03:00
|
|
|
.to_outline(Distance::meters(thickness)),
|
2020-01-20 01:18:38 +03:00
|
|
|
);
|
2019-12-22 21:52:41 +03:00
|
|
|
}
|
2020-02-07 20:05:45 +03:00
|
|
|
self.bg = Some(ctx.upload(batch));
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
|
2020-03-23 02:19:05 +03:00
|
|
|
if let Some(container) = self.widget.downcast_mut::<Container>() {
|
|
|
|
// layout() doesn't return absolute position; it's relative to the container.
|
|
|
|
for widget in &mut container.members {
|
|
|
|
widget.apply_flexbox(
|
|
|
|
stretch,
|
|
|
|
nodes,
|
|
|
|
x + dx,
|
|
|
|
y + dy,
|
|
|
|
scroll_offset,
|
|
|
|
ctx,
|
|
|
|
recompute_layout,
|
|
|
|
);
|
2020-03-23 00:06:02 +03:00
|
|
|
}
|
2020-03-23 02:19:05 +03:00
|
|
|
} else {
|
|
|
|
self.widget.set_pos(top_left);
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 01:02:11 +03:00
|
|
|
fn get_all_click_actions(&self, actions: &mut HashSet<String>) {
|
2020-03-23 02:19:05 +03:00
|
|
|
if let Some(btn) = self.widget.downcast_ref::<Button>() {
|
|
|
|
if actions.contains(&btn.action) {
|
|
|
|
panic!(
|
|
|
|
"Two buttons in one Composite both use action {}",
|
|
|
|
btn.action
|
|
|
|
);
|
|
|
|
}
|
|
|
|
actions.insert(btn.action.clone());
|
|
|
|
} else if let Some(container) = self.widget.downcast_ref::<Container>() {
|
|
|
|
for w in &container.members {
|
|
|
|
w.get_all_click_actions(actions);
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 02:01:10 +03:00
|
|
|
|
2020-02-29 06:06:59 +03:00
|
|
|
pub fn is_btn(&self, name: &str) -> bool {
|
2020-03-23 02:19:05 +03:00
|
|
|
self.widget
|
|
|
|
.downcast_ref::<Button>()
|
|
|
|
.map(|btn| btn.action == name)
|
|
|
|
.unwrap_or(false)
|
2020-02-01 22:39:48 +03:00
|
|
|
}
|
|
|
|
|
2020-03-21 09:41:07 +03:00
|
|
|
fn find(&self, name: &str) -> Option<&Widget> {
|
2020-03-23 02:19:05 +03:00
|
|
|
if self.id == Some(name.to_string()) {
|
|
|
|
return Some(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(container) = self.widget.downcast_ref::<Container>() {
|
|
|
|
for widget in &container.members {
|
|
|
|
if let Some(w) = widget.find(name) {
|
|
|
|
return Some(w);
|
2020-01-22 02:01:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-23 02:35:20 +03:00
|
|
|
|
2020-03-23 02:19:05 +03:00
|
|
|
None
|
2020-01-22 02:01:10 +03:00
|
|
|
}
|
2020-03-21 09:41:07 +03:00
|
|
|
fn find_mut(&mut self, name: &str) -> Option<&mut Widget> {
|
2020-03-23 02:19:05 +03:00
|
|
|
if self.id == Some(name.to_string()) {
|
|
|
|
return Some(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(container) = self.widget.downcast_mut::<Container>() {
|
|
|
|
for widget in &mut container.members {
|
|
|
|
if let Some(w) = widget.find_mut(name) {
|
|
|
|
return Some(w);
|
2020-02-21 23:19:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-23 02:35:20 +03:00
|
|
|
|
2020-03-23 02:19:05 +03:00
|
|
|
None
|
2020-02-21 23:19:24 +03:00
|
|
|
}
|
2020-03-21 01:40:43 +03:00
|
|
|
|
|
|
|
pub(crate) fn take_btn(self) -> Button {
|
2020-03-23 02:19:05 +03:00
|
|
|
*self.widget.downcast::<Button>().ok().unwrap()
|
2020-03-21 01:40:43 +03:00
|
|
|
}
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
|
2020-02-04 03:13:40 +03:00
|
|
|
enum Dims {
|
|
|
|
MaxPercent(f64, f64),
|
|
|
|
ExactPercent(f64, f64),
|
|
|
|
}
|
|
|
|
|
2020-01-07 21:08:39 +03:00
|
|
|
pub struct CompositeBuilder {
|
2020-03-21 09:41:07 +03:00
|
|
|
top_level: Widget,
|
2020-01-15 21:33:08 +03:00
|
|
|
horiz: HorizontalAlignment,
|
|
|
|
vert: VerticalAlignment,
|
2020-02-04 03:13:40 +03:00
|
|
|
dims: Dims,
|
2020-03-28 01:02:11 +03:00
|
|
|
allow_duplicate_buttons: bool,
|
2020-01-07 21:08:39 +03:00
|
|
|
}
|
|
|
|
|
2019-12-21 02:56:04 +03:00
|
|
|
pub struct Composite {
|
2020-03-21 09:41:07 +03:00
|
|
|
top_level: Widget,
|
2020-01-15 21:33:08 +03:00
|
|
|
horiz: HorizontalAlignment,
|
|
|
|
vert: VerticalAlignment,
|
2020-02-04 03:13:40 +03:00
|
|
|
dims: Dims,
|
2020-01-15 21:33:08 +03:00
|
|
|
|
2020-01-15 02:47:35 +03:00
|
|
|
scrollable_x: bool,
|
|
|
|
scrollable_y: bool,
|
|
|
|
contents_dims: ScreenDims,
|
|
|
|
container_dims: ScreenDims,
|
2020-01-15 02:18:27 +03:00
|
|
|
clip_rect: Option<ScreenRectangle>,
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Outcome {
|
|
|
|
Clicked(String),
|
|
|
|
}
|
|
|
|
|
2019-12-22 21:26:48 +03:00
|
|
|
const SCROLL_SPEED: f64 = 5.0;
|
|
|
|
|
2019-12-21 02:56:04 +03:00
|
|
|
impl Composite {
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn new(top_level: Widget) -> CompositeBuilder {
|
2020-01-07 21:08:39 +03:00
|
|
|
CompositeBuilder {
|
2019-12-21 02:56:04 +03:00
|
|
|
top_level,
|
2020-01-15 21:33:08 +03:00
|
|
|
horiz: HorizontalAlignment::Center,
|
|
|
|
vert: VerticalAlignment::Center,
|
2020-02-04 03:13:40 +03:00
|
|
|
dims: Dims::MaxPercent(1.0, 1.0),
|
2020-03-28 01:02:11 +03:00
|
|
|
allow_duplicate_buttons: false,
|
2019-12-24 01:33:43 +03:00
|
|
|
}
|
2019-12-22 21:26:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-21 23:57:51 +03:00
|
|
|
fn recompute_layout(&mut self, ctx: &EventCtx, recompute_bg: bool) {
|
2019-12-21 02:56:04 +03:00
|
|
|
let mut stretch = Stretch::new();
|
|
|
|
let root = stretch
|
|
|
|
.new_node(
|
2020-01-11 23:59:27 +03:00
|
|
|
Style {
|
|
|
|
..Default::default()
|
2019-12-21 02:56:04 +03:00
|
|
|
},
|
|
|
|
Vec::new(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut nodes = vec![];
|
2020-03-22 04:51:07 +03:00
|
|
|
self.top_level.get_flexbox(root, &mut stretch, &mut nodes);
|
2019-12-21 02:56:04 +03:00
|
|
|
nodes.reverse();
|
|
|
|
|
2020-01-15 02:57:21 +03:00
|
|
|
// TODO Express more simply. Constraining this seems useless.
|
2020-01-15 02:18:27 +03:00
|
|
|
let container_size = Size {
|
2020-01-15 02:57:21 +03:00
|
|
|
width: Number::Undefined,
|
|
|
|
height: Number::Undefined,
|
2020-01-15 02:18:27 +03:00
|
|
|
};
|
|
|
|
stretch.compute_layout(root, container_size).unwrap();
|
2020-01-15 22:21:32 +03:00
|
|
|
|
|
|
|
// TODO I'm so confused why these 2 are acting differently. :(
|
|
|
|
let effective_dims = if self.scrollable_x || self.scrollable_y {
|
|
|
|
self.container_dims
|
|
|
|
} else {
|
|
|
|
let result = stretch.layout(root).unwrap();
|
|
|
|
ScreenDims::new(result.size.width.into(), result.size.height.into())
|
|
|
|
};
|
2020-03-02 23:02:25 +03:00
|
|
|
let top_left =
|
|
|
|
ctx.canvas
|
|
|
|
.align_window(&ctx.prerender.assets, effective_dims, self.horiz, self.vert);
|
2020-01-15 02:57:21 +03:00
|
|
|
let offset = self.scroll_offset();
|
2019-12-21 02:56:04 +03:00
|
|
|
self.top_level.apply_flexbox(
|
|
|
|
&stretch,
|
|
|
|
&mut nodes,
|
|
|
|
top_left.x,
|
2019-12-24 02:00:22 +03:00
|
|
|
top_left.y,
|
|
|
|
offset,
|
2019-12-21 02:56:04 +03:00
|
|
|
ctx,
|
2020-02-21 23:57:51 +03:00
|
|
|
recompute_bg,
|
2019-12-21 02:56:04 +03:00
|
|
|
);
|
|
|
|
assert!(nodes.is_empty());
|
|
|
|
}
|
|
|
|
|
2020-01-15 02:57:21 +03:00
|
|
|
fn scroll_offset(&self) -> (f64, f64) {
|
2020-01-15 02:47:35 +03:00
|
|
|
let x = if self.scrollable_x {
|
|
|
|
self.slider("horiz scrollbar").get_percent()
|
|
|
|
* (self.contents_dims.width - self.container_dims.width).max(0.0)
|
2019-12-24 02:00:22 +03:00
|
|
|
} else {
|
|
|
|
0.0
|
2020-01-15 02:47:35 +03:00
|
|
|
};
|
|
|
|
let y = if self.scrollable_y {
|
|
|
|
self.slider("vert scrollbar").get_percent()
|
|
|
|
* (self.contents_dims.height - self.container_dims.height).max(0.0)
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
|
|
|
(x, y)
|
2019-12-24 02:00:22 +03:00
|
|
|
}
|
|
|
|
|
2020-01-15 02:47:35 +03:00
|
|
|
fn set_scroll_offset(&mut self, ctx: &EventCtx, offset: (f64, f64)) {
|
|
|
|
let mut changed = false;
|
|
|
|
if self.scrollable_x {
|
|
|
|
changed = true;
|
|
|
|
let max = (self.contents_dims.width - self.container_dims.width).max(0.0);
|
|
|
|
if max == 0.0 {
|
2020-02-27 21:38:48 +03:00
|
|
|
self.slider_mut("horiz scrollbar").set_percent(ctx, 0.0);
|
2020-01-15 02:47:35 +03:00
|
|
|
} else {
|
2020-02-27 21:38:48 +03:00
|
|
|
self.slider_mut("horiz scrollbar")
|
2020-01-26 01:25:47 +03:00
|
|
|
.set_percent(ctx, abstutil::clamp(offset.0, 0.0, max) / max);
|
2020-01-15 02:47:35 +03:00
|
|
|
}
|
2020-01-10 21:28:13 +03:00
|
|
|
}
|
2020-01-15 02:47:35 +03:00
|
|
|
if self.scrollable_y {
|
|
|
|
changed = true;
|
|
|
|
let max = (self.contents_dims.height - self.container_dims.height).max(0.0);
|
|
|
|
if max == 0.0 {
|
2020-02-27 21:38:48 +03:00
|
|
|
self.slider_mut("vert scrollbar").set_percent(ctx, 0.0);
|
2020-01-15 02:47:35 +03:00
|
|
|
} else {
|
2020-02-27 21:38:48 +03:00
|
|
|
self.slider_mut("vert scrollbar")
|
2020-01-26 01:25:47 +03:00
|
|
|
.set_percent(ctx, abstutil::clamp(offset.1, 0.0, max) / max);
|
2020-01-15 02:47:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if changed {
|
2020-02-21 23:57:51 +03:00
|
|
|
self.recompute_layout(ctx, false);
|
2019-12-24 02:00:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-21 02:56:04 +03:00
|
|
|
pub fn event(&mut self, ctx: &mut EventCtx) -> Option<Outcome> {
|
2020-01-15 02:47:35 +03:00
|
|
|
if (self.scrollable_x || self.scrollable_y)
|
2020-01-18 01:11:07 +03:00
|
|
|
&& ctx
|
|
|
|
.canvas
|
|
|
|
.get_cursor_in_screen_space()
|
|
|
|
.map(|pt| self.top_level.rect.contains(pt))
|
|
|
|
.unwrap_or(false)
|
2019-12-22 21:26:48 +03:00
|
|
|
{
|
2020-01-15 02:47:35 +03:00
|
|
|
if let Some((dx, dy)) = ctx.input.get_mouse_scroll() {
|
|
|
|
let x_offset = if self.scrollable_x {
|
2020-01-26 01:25:47 +03:00
|
|
|
self.scroll_offset().0 + dx * SCROLL_SPEED
|
2020-01-15 02:47:35 +03:00
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
|
|
|
let y_offset = if self.scrollable_y {
|
2020-01-26 01:25:47 +03:00
|
|
|
self.scroll_offset().1 - dy * SCROLL_SPEED
|
2020-01-15 02:47:35 +03:00
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
|
|
|
self.set_scroll_offset(ctx, (x_offset, y_offset));
|
2019-12-22 21:26:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-21 02:56:04 +03:00
|
|
|
if ctx.input.is_window_resized() {
|
2020-02-21 23:57:51 +03:00
|
|
|
self.recompute_layout(ctx, false);
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
2019-12-22 21:26:48 +03:00
|
|
|
|
2020-01-15 02:57:21 +03:00
|
|
|
let before = self.scroll_offset();
|
2020-03-06 21:31:08 +03:00
|
|
|
let mut redo_layout = false;
|
2020-03-23 07:04:05 +03:00
|
|
|
let result = self.top_level.widget.event(ctx, &mut redo_layout);
|
2020-03-06 21:31:08 +03:00
|
|
|
if self.scroll_offset() != before || redo_layout {
|
|
|
|
self.recompute_layout(ctx, true);
|
2019-12-24 02:00:22 +03:00
|
|
|
}
|
|
|
|
result
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn draw(&self, g: &mut GfxCtx) {
|
2020-01-18 00:58:33 +03:00
|
|
|
if let Some(ref rect) = self.clip_rect {
|
|
|
|
g.enable_clipping(rect.clone());
|
|
|
|
g.canvas.mark_covered_area(rect.clone());
|
|
|
|
} else {
|
|
|
|
g.canvas.mark_covered_area(self.top_level.rect.clone());
|
2020-01-15 02:18:27 +03:00
|
|
|
}
|
2020-02-04 03:13:40 +03:00
|
|
|
|
|
|
|
// Debugging
|
|
|
|
if false {
|
|
|
|
g.fork_screenspace();
|
|
|
|
g.draw_polygon(Color::RED.alpha(0.5), &self.top_level.rect.to_polygon());
|
|
|
|
|
2020-03-02 23:02:25 +03:00
|
|
|
let top_left = g.canvas.align_window(
|
|
|
|
&g.prerender.assets,
|
|
|
|
self.container_dims,
|
|
|
|
self.horiz,
|
|
|
|
self.vert,
|
|
|
|
);
|
2020-02-04 03:13:40 +03:00
|
|
|
g.draw_polygon(
|
|
|
|
Color::BLUE.alpha(0.5),
|
|
|
|
&Polygon::rectangle(self.container_dims.width, self.container_dims.height)
|
|
|
|
.translate(top_left.x, top_left.y),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
g.unfork();
|
|
|
|
|
2020-03-22 04:51:07 +03:00
|
|
|
self.top_level.draw(g);
|
2020-01-15 02:47:35 +03:00
|
|
|
if self.scrollable_x || self.scrollable_y {
|
2020-01-15 02:18:27 +03:00
|
|
|
g.disable_clipping();
|
2020-01-15 03:50:26 +03:00
|
|
|
|
|
|
|
// Draw the scrollbars after clipping is disabled, because they actually live just
|
|
|
|
// outside the rectangle.
|
|
|
|
if self.scrollable_x {
|
2020-03-22 04:41:01 +03:00
|
|
|
self.slider("horiz scrollbar").draw(g);
|
2020-01-15 03:50:26 +03:00
|
|
|
}
|
|
|
|
if self.scrollable_y {
|
2020-03-22 04:41:01 +03:00
|
|
|
self.slider("vert scrollbar").draw(g);
|
2020-01-15 03:50:26 +03:00
|
|
|
}
|
2020-01-15 02:18:27 +03:00
|
|
|
}
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_all_click_actions(&self) -> HashSet<String> {
|
|
|
|
let mut actions = HashSet::new();
|
|
|
|
self.top_level.get_all_click_actions(&mut actions);
|
|
|
|
actions
|
|
|
|
}
|
|
|
|
|
2020-01-15 02:57:21 +03:00
|
|
|
pub fn preserve_scroll(&self) -> (f64, f64) {
|
|
|
|
self.scroll_offset()
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
|
|
|
|
2020-01-15 02:47:35 +03:00
|
|
|
pub fn restore_scroll(&mut self, ctx: &EventCtx, offset: (f64, f64)) {
|
|
|
|
self.set_scroll_offset(ctx, offset);
|
2019-12-24 01:33:43 +03:00
|
|
|
}
|
|
|
|
|
2020-01-30 02:59:56 +03:00
|
|
|
pub fn scroll_to_member(&mut self, ctx: &EventCtx, name: String) {
|
2020-02-29 06:06:59 +03:00
|
|
|
if let Some(w) = self.top_level.find(&name) {
|
|
|
|
let y1 = w.rect.y1;
|
2020-01-30 02:59:56 +03:00
|
|
|
self.set_scroll_offset(ctx, (0.0, y1));
|
|
|
|
} else {
|
|
|
|
panic!("Can't scroll_to_member of unknown {}", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 02:35:20 +03:00
|
|
|
pub fn has_widget(&self, name: &str) -> bool {
|
|
|
|
self.top_level.find(name).is_some()
|
2019-12-24 01:33:43 +03:00
|
|
|
}
|
2020-03-23 02:35:20 +03:00
|
|
|
|
|
|
|
pub fn slider(&self, name: &str) -> &Slider {
|
|
|
|
self.find(name)
|
2020-03-22 03:42:17 +03:00
|
|
|
}
|
2020-02-27 21:38:48 +03:00
|
|
|
pub fn slider_mut(&mut self, name: &str) -> &mut Slider {
|
2020-03-23 02:35:20 +03:00
|
|
|
self.find_mut(name)
|
2019-12-24 01:33:43 +03:00
|
|
|
}
|
2019-12-25 23:01:51 +03:00
|
|
|
|
2020-03-23 09:50:14 +03:00
|
|
|
pub fn menu<T: 'static + Clone>(&self, name: &str) -> &Menu<T> {
|
2020-03-23 02:35:20 +03:00
|
|
|
self.find(name)
|
2020-01-02 20:35:41 +03:00
|
|
|
}
|
|
|
|
|
2020-02-29 06:06:59 +03:00
|
|
|
pub fn is_checked(&self, name: &str) -> bool {
|
2020-03-23 02:35:20 +03:00
|
|
|
self.find::<Checkbox>(name).enabled
|
2020-02-29 06:06:59 +03:00
|
|
|
}
|
|
|
|
|
2020-03-05 02:33:28 +03:00
|
|
|
pub fn text_box(&self, name: &str) -> String {
|
2020-03-23 02:35:20 +03:00
|
|
|
self.find::<TextBox>(name).get_line()
|
2020-03-05 02:33:28 +03:00
|
|
|
}
|
|
|
|
|
2020-03-31 22:31:40 +03:00
|
|
|
pub fn dropdown_value<T: 'static + PartialEq + Clone>(&self, name: &str) -> T {
|
2020-03-23 02:35:20 +03:00
|
|
|
self.find::<Dropdown<T>>(name).current_value()
|
2020-03-06 02:00:42 +03:00
|
|
|
}
|
|
|
|
|
2019-12-25 23:01:51 +03:00
|
|
|
pub fn filler_rect(&self, name: &str) -> ScreenRectangle {
|
2020-03-23 02:35:20 +03:00
|
|
|
if let Some(w) = self.top_level.find(name) {
|
|
|
|
if w.widget.is::<Filler>() {
|
|
|
|
return w.rect.clone();
|
|
|
|
}
|
2020-03-22 04:51:07 +03:00
|
|
|
}
|
2020-03-23 02:35:20 +03:00
|
|
|
panic!("{} isn't a filler", name);
|
2019-12-25 23:01:51 +03:00
|
|
|
}
|
2020-01-22 02:01:10 +03:00
|
|
|
|
2020-03-23 02:35:20 +03:00
|
|
|
pub fn find<T: WidgetImpl>(&self, name: &str) -> &T {
|
2020-02-29 06:06:59 +03:00
|
|
|
if let Some(w) = self.top_level.find(name) {
|
2020-03-23 02:35:20 +03:00
|
|
|
if let Some(x) = w.widget.downcast_ref::<T>() {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
panic!("Found widget {}, but wrong type", name);
|
|
|
|
}
|
2020-02-29 06:06:59 +03:00
|
|
|
} else {
|
|
|
|
panic!("Can't find widget {}", name);
|
|
|
|
}
|
|
|
|
}
|
2020-03-23 02:35:20 +03:00
|
|
|
pub fn find_mut<T: WidgetImpl>(&mut self, name: &str) -> &mut T {
|
2020-02-29 06:06:59 +03:00
|
|
|
if let Some(w) = self.top_level.find_mut(name) {
|
2020-03-23 02:35:20 +03:00
|
|
|
if let Some(x) = w.widget.downcast_mut::<T>() {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
panic!("Found widget {}, but wrong type", name);
|
|
|
|
}
|
2020-01-22 02:01:10 +03:00
|
|
|
} else {
|
2020-02-29 06:06:59 +03:00
|
|
|
panic!("Can't find widget {}", name);
|
2020-01-22 02:01:10 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-29 06:06:59 +03:00
|
|
|
|
|
|
|
pub fn rect_of(&self, name: &str) -> &ScreenRectangle {
|
2020-03-23 02:35:20 +03:00
|
|
|
&self.top_level.find(name).unwrap().rect
|
2020-02-29 06:06:59 +03:00
|
|
|
}
|
2020-02-05 00:25:53 +03:00
|
|
|
// TODO Deprecate
|
|
|
|
pub fn center_of(&self, name: &str) -> ScreenPt {
|
|
|
|
self.rect_of(name).center()
|
|
|
|
}
|
2020-01-22 02:01:10 +03:00
|
|
|
pub fn center_of_panel(&self) -> ScreenPt {
|
|
|
|
self.top_level.rect.center()
|
|
|
|
}
|
2020-01-31 01:38:26 +03:00
|
|
|
|
|
|
|
pub fn align_above(&mut self, ctx: &mut EventCtx, other: &Composite) {
|
2020-01-31 02:33:27 +03:00
|
|
|
// Small padding
|
2020-02-05 00:25:53 +03:00
|
|
|
self.vert = VerticalAlignment::Above(other.top_level.rect.y1 - 5.0);
|
2020-02-21 23:57:51 +03:00
|
|
|
self.recompute_layout(ctx, false);
|
2020-01-31 01:38:26 +03:00
|
|
|
}
|
2020-02-21 23:19:24 +03:00
|
|
|
|
2020-03-21 09:41:07 +03:00
|
|
|
pub fn replace(&mut self, ctx: &mut EventCtx, id: &str, new: Widget) {
|
2020-03-23 02:35:20 +03:00
|
|
|
*self.top_level.find_mut(id).unwrap() = new;
|
2020-02-21 23:57:51 +03:00
|
|
|
self.recompute_layout(ctx, true);
|
2020-02-21 23:19:24 +03:00
|
|
|
}
|
2020-02-23 22:37:09 +03:00
|
|
|
|
|
|
|
pub fn clicked_outside(&self, ctx: &mut EventCtx) -> bool {
|
|
|
|
// TODO No great way to populate OSD from here with "click to cancel"
|
|
|
|
!self.top_level.rect.contains(ctx.canvas.get_cursor()) && ctx.normal_left_click()
|
|
|
|
}
|
2019-12-21 02:56:04 +03:00
|
|
|
}
|
2020-01-07 21:08:39 +03:00
|
|
|
|
|
|
|
impl CompositeBuilder {
|
|
|
|
pub fn build(self, ctx: &mut EventCtx) -> Composite {
|
|
|
|
let mut c = Composite {
|
|
|
|
top_level: self.top_level,
|
2020-01-15 02:18:27 +03:00
|
|
|
|
2020-01-15 21:33:08 +03:00
|
|
|
horiz: self.horiz,
|
|
|
|
vert: self.vert,
|
2020-02-04 03:13:40 +03:00
|
|
|
dims: self.dims,
|
2020-01-15 21:33:08 +03:00
|
|
|
|
2020-01-15 02:47:35 +03:00
|
|
|
scrollable_x: false,
|
|
|
|
scrollable_y: false,
|
|
|
|
contents_dims: ScreenDims::new(0.0, 0.0),
|
|
|
|
container_dims: ScreenDims::new(0.0, 0.0),
|
2020-01-15 02:18:27 +03:00
|
|
|
clip_rect: None,
|
2020-01-07 21:08:39 +03:00
|
|
|
};
|
2020-02-04 03:13:40 +03:00
|
|
|
if let Dims::ExactPercent(w, h) = c.dims {
|
2020-03-25 20:45:17 +03:00
|
|
|
c.top_level.layout.style.size = Size {
|
2020-02-04 03:13:40 +03:00
|
|
|
width: Dimension::Points((w * ctx.canvas.window_width) as f32),
|
|
|
|
height: Dimension::Points((h * ctx.canvas.window_height) as f32),
|
2020-03-25 20:45:17 +03:00
|
|
|
};
|
2020-02-04 03:13:40 +03:00
|
|
|
}
|
2020-02-21 23:57:51 +03:00
|
|
|
c.recompute_layout(ctx, false);
|
2020-01-15 02:18:27 +03:00
|
|
|
|
2020-01-15 02:47:35 +03:00
|
|
|
c.contents_dims = ScreenDims::new(c.top_level.rect.width(), c.top_level.rect.height());
|
2020-02-04 03:13:40 +03:00
|
|
|
c.container_dims = match c.dims {
|
|
|
|
Dims::MaxPercent(w, h) => ScreenDims::new(
|
|
|
|
c.contents_dims.width.min(w * ctx.canvas.window_width),
|
|
|
|
c.contents_dims.height.min(h * ctx.canvas.window_height),
|
|
|
|
),
|
|
|
|
Dims::ExactPercent(w, h) => {
|
|
|
|
ScreenDims::new(w * ctx.canvas.window_width, h * ctx.canvas.window_height)
|
|
|
|
}
|
|
|
|
};
|
2020-01-15 02:18:27 +03:00
|
|
|
|
2020-01-15 21:19:21 +03:00
|
|
|
// If the panel fits without a scrollbar, don't add one.
|
2020-03-02 23:02:25 +03:00
|
|
|
let top_left =
|
|
|
|
ctx.canvas
|
|
|
|
.align_window(&ctx.prerender.assets, c.container_dims, c.horiz, c.vert);
|
2020-01-15 02:47:35 +03:00
|
|
|
if c.contents_dims.width > c.container_dims.width {
|
|
|
|
c.scrollable_x = true;
|
2020-03-22 04:41:01 +03:00
|
|
|
c.top_level = Widget::col(vec![
|
|
|
|
c.top_level,
|
2020-03-23 09:50:14 +03:00
|
|
|
Slider::horizontal(
|
2020-01-15 23:07:25 +03:00
|
|
|
ctx,
|
|
|
|
c.container_dims.width,
|
|
|
|
c.container_dims.width * (c.container_dims.width / c.contents_dims.width),
|
2020-03-23 09:50:14 +03:00
|
|
|
0.0,
|
|
|
|
)
|
2020-03-22 04:41:01 +03:00
|
|
|
.named("horiz scrollbar")
|
|
|
|
.abs(top_left.x, top_left.y + c.container_dims.height),
|
2020-01-15 03:46:45 +03:00
|
|
|
]);
|
2020-01-15 02:47:35 +03:00
|
|
|
}
|
|
|
|
if c.contents_dims.height > c.container_dims.height {
|
|
|
|
c.scrollable_y = true;
|
2020-03-22 04:41:01 +03:00
|
|
|
c.top_level = Widget::row(vec![
|
|
|
|
c.top_level,
|
2020-03-23 09:50:14 +03:00
|
|
|
Slider::vertical(
|
2020-01-15 23:07:25 +03:00
|
|
|
ctx,
|
|
|
|
c.container_dims.height,
|
|
|
|
c.container_dims.height * (c.container_dims.height / c.contents_dims.height),
|
2020-03-23 09:50:14 +03:00
|
|
|
0.0,
|
|
|
|
)
|
2020-03-22 04:41:01 +03:00
|
|
|
.named("vert scrollbar")
|
|
|
|
.abs(top_left.x + c.container_dims.width, top_left.y),
|
2020-01-15 03:46:45 +03:00
|
|
|
]);
|
2020-01-15 02:47:35 +03:00
|
|
|
}
|
|
|
|
if c.scrollable_x || c.scrollable_y {
|
2020-02-21 23:57:51 +03:00
|
|
|
c.recompute_layout(ctx, false);
|
2020-01-15 03:50:26 +03:00
|
|
|
c.clip_rect = Some(ScreenRectangle::top_left(top_left, c.container_dims));
|
2020-01-07 21:08:39 +03:00
|
|
|
}
|
2020-01-15 02:47:35 +03:00
|
|
|
|
2020-03-28 01:02:11 +03:00
|
|
|
if !self.allow_duplicate_buttons {
|
|
|
|
// Just trigger error if a button is double-defined
|
|
|
|
c.get_all_click_actions();
|
|
|
|
}
|
2020-01-24 04:11:29 +03:00
|
|
|
ctx.no_op_event(true, |ctx| assert!(c.event(ctx).is_none()));
|
2020-01-07 21:08:39 +03:00
|
|
|
c
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn aligned(
|
|
|
|
mut self,
|
|
|
|
horiz: HorizontalAlignment,
|
|
|
|
vert: VerticalAlignment,
|
|
|
|
) -> CompositeBuilder {
|
2020-01-15 21:33:08 +03:00
|
|
|
self.horiz = horiz;
|
|
|
|
self.vert = vert;
|
2020-01-07 21:08:39 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-01-15 21:33:08 +03:00
|
|
|
pub fn max_size_percent(mut self, pct_width: usize, pct_height: usize) -> CompositeBuilder {
|
|
|
|
if pct_width == 100 && pct_height == 100 {
|
|
|
|
panic!("By default, Composites are capped at 100% of the screen. This is redundant.");
|
|
|
|
}
|
2020-02-04 03:13:40 +03:00
|
|
|
self.dims = Dims::MaxPercent((pct_width as f64) / 100.0, (pct_height as f64) / 100.0);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exact_size_percent(mut self, pct_width: usize, pct_height: usize) -> CompositeBuilder {
|
|
|
|
self.dims = Dims::ExactPercent((pct_width as f64) / 100.0, (pct_height as f64) / 100.0);
|
2020-01-11 23:32:41 +03:00
|
|
|
self
|
|
|
|
}
|
2020-03-28 01:02:11 +03:00
|
|
|
|
|
|
|
pub fn allow_duplicate_buttons(mut self) -> CompositeBuilder {
|
|
|
|
self.allow_duplicate_buttons = true;
|
|
|
|
self
|
|
|
|
}
|
2020-01-07 21:08:39 +03:00
|
|
|
}
|