diff options
author | 2020-04-18 20:44:14 +0200 | |
---|---|---|
committer | 2020-07-08 11:06:39 +0200 | |
commit | f655d9b9674fe5a705e26b8797231d93a117395b (patch) | |
tree | 5ac67a08c5b986e629f09e7c0e20393065d91b82 /native | |
parent | e29feef8ba4f95f286039fcc1ca2e53bfe5019c5 (diff) | |
download | iced-f655d9b9674fe5a705e26b8797231d93a117395b.tar.gz iced-f655d9b9674fe5a705e26b8797231d93a117395b.tar.bz2 iced-f655d9b9674fe5a705e26b8797231d93a117395b.zip |
Position `Menu` layer based on available space
Diffstat (limited to 'native')
-rw-r--r-- | native/src/layer/menu.rs | 24 | ||||
-rw-r--r-- | native/src/widget/combo_box.rs | 10 |
2 files changed, 27 insertions, 7 deletions
diff --git a/native/src/layer/menu.rs b/native/src/layer/menu.rs index 9e26767b..05026a54 100644 --- a/native/src/layer/menu.rs +++ b/native/src/layer/menu.rs @@ -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 } diff --git a/native/src/widget/combo_box.rs b/native/src/widget/combo_box.rs index 1b04a9a8..0f249282 100644 --- a/native/src/widget/combo_box.rs +++ b/native/src/widget/combo_box.rs @@ -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, )), |