Position Menu layer based on available space

This commit is contained in:
Héctor Ramón Jiménez 2020-04-18 20:44:14 +02:00
parent e29feef8ba
commit f655d9b967
3 changed files with 27 additions and 11 deletions

View File

@ -60,10 +60,6 @@ impl Sandbox for Example {
.push(Text::new("Which is your favorite language?"))
.push(combo_box);
if self.selected_language == Language::Javascript {
content = content.push(Text::new("You are wrong!"));
}
content = content
.push(button)
.push(Space::with_height(Length::Units(800)));

View File

@ -1,6 +1,7 @@
use crate::{
container, layout, mouse, scrollable, Clipboard, Container, Element, Event,
Hasher, Layer, Layout, Length, Point, Rectangle, Scrollable, Size, Widget,
Hasher, Layer, Layout, Length, Point, Rectangle, Scrollable, Size, Vector,
Widget,
};
use std::borrow::Cow;
@ -8,6 +9,7 @@ pub struct Menu<'a, Message, Renderer: self::Renderer> {
container: Container<'a, Message, Renderer>,
is_open: &'a mut bool,
width: u16,
target_height: f32,
}
#[derive(Default)]
@ -38,6 +40,7 @@ where
options: impl Into<Cow<'a, [T]>>,
on_selected: Box<dyn Fn(T) -> Message>,
width: u16,
target_height: f32,
text_size: u16,
padding: u16,
) -> Self
@ -60,6 +63,7 @@ where
container,
is_open: &mut state.is_open,
width,
target_height,
}
}
}
@ -75,15 +79,29 @@ where
bounds: Size,
position: Point,
) -> layout::Node {
let space_below = bounds.height - (position.y + self.target_height);
let space_above = position.y;
let limits = layout::Limits::new(
Size::ZERO,
Size::new(bounds.width - position.x, bounds.height - position.y),
Size::new(
bounds.width - position.x,
if space_below > space_above {
space_below
} else {
space_above
},
),
)
.width(Length::Units(self.width));
let mut node = self.container.layout(renderer, &limits);
node.move_to(position);
node.move_to(if space_below > space_above {
position + Vector::new(0.0, self.target_height)
} else {
position - Vector::new(0.0, node.size().height)
});
node
}

View File

@ -1,7 +1,7 @@
use crate::{
layer::{self, menu},
layout, mouse, scrollable, text, Clipboard, Element, Event, Hasher, Layout,
Length, Overlay, Point, Rectangle, Size, Vector, Widget,
Length, Overlay, Point, Rectangle, Size, Widget,
};
use std::borrow::Cow;
@ -210,14 +210,16 @@ where
if is_open {
if let Some(Internal { menu, on_selected }) = self.internal.take() {
let bounds = layout.bounds();
Some(Overlay::new(
layout.position()
+ Vector::new(0.0, layout.bounds().height),
layout.position(),
Box::new(layer::Menu::new(
menu,
self.options.clone(),
on_selected,
layout.bounds().width.round() as u16,
bounds.width.round() as u16,
bounds.height,
self.text_size.unwrap_or(20),
self.padding,
)),