prototype flexbox support via stretch. it unbreaks the challenge picker

screen, to start. :)
This commit is contained in:
Dustin Carlino 2019-11-24 07:21:30 -08:00
parent e3ddd5b3db
commit b1bc7de2e8
3 changed files with 53 additions and 2 deletions

View File

@ -21,4 +21,5 @@ ordered-float = "1.0.1"
serde = "1.0.98"
serde_derive = "1.0.98"
simsearch = "0.1.4"
stretch = "0.3.2"
textwrap = "0.11"

View File

@ -48,3 +48,53 @@ pub fn stack_vertically(
top_left.y += dims.height;
}
}
// TODO This is just a first experiment...
pub fn flexbox(ctx: &EventCtx, widgets: Vec<&mut dyn Widget>) {
assert!(!widgets.is_empty());
use stretch::geometry::Size;
use stretch::node::{Node, Stretch};
use stretch::style::{Dimension, FlexWrap, Style};
let mut stretch = Stretch::new();
let widget_nodes: Vec<Node> = widgets
.iter()
.map(|w| {
let dims = w.get_dims();
stretch
.new_node(
Style {
size: Size {
width: Dimension::Points(dims.width as f32),
height: Dimension::Points(dims.height as f32),
},
..Default::default()
},
vec![],
)
.unwrap()
})
.collect();
let root = stretch
.new_node(
Style {
size: Size {
width: Dimension::Points(ctx.canvas.window_width as f32),
height: Dimension::Points(ctx.canvas.window_height as f32),
},
flex_wrap: FlexWrap::Wrap,
..Default::default()
},
widget_nodes.clone(),
)
.unwrap();
stretch.compute_layout(root, Size::undefined()).unwrap();
for (node, widget) in widget_nodes.into_iter().zip(widgets) {
let top_left = stretch.layout(node).unwrap().location;
widget.set_pos(ScreenPt::new(top_left.x.into(), top_left.y.into()), 0.0);
}
}

View File

@ -57,8 +57,8 @@ impl ManagedGUIState {
impl State for ManagedGUIState {
fn event(&mut self, ctx: &mut EventCtx, ui: &mut UI) -> Transition {
layout::stack_vertically(
layout::ContainerOrientation::Centered,
// TODO If this ever gets slow, only run if window size has changed.
layout::flexbox(
ctx,
self.buttons
.iter_mut()