Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom position in popover #218

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions src/widget/popover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use iced_core::mouse;
use iced_core::overlay;
use iced_core::renderer;
use iced_core::widget::{Operation, OperationOutputWrapper, Tree};
use iced_core::{Clipboard, Element, Layout, Length, Point, Rectangle, Shell, Size, Widget};
use iced_core::{
Clipboard, Element, Layout, Length, Point, Rectangle, Shell, Size, Vector, Widget,
};
use std::cell::RefCell;

pub use iced_style::container::{Appearance, StyleSheet};
Expand All @@ -25,19 +27,26 @@ pub struct Popover<'a, Message, Renderer> {
content: Element<'a, Message, Renderer>,
// XXX Avoid refcell; improve iced overlay API?
popup: RefCell<Element<'a, Message, Renderer>>,
position: Option<Point>,
}

impl<'a, Message, Renderer> Popover<'a, Message, Renderer> {
fn new(
pub fn new(
content: impl Into<Element<'a, Message, Renderer>>,
popup: impl Into<Element<'a, Message, Renderer>>,
) -> Self {
Self {
content: content.into(),
popup: RefCell::new(popup.into()),
position: None,
}
}

pub fn position(mut self, position: Point) -> Self {
self.position = Some(position);
self
}

// TODO More options for positioning similar to GdkPopup, xdg_popup
}

Expand Down Expand Up @@ -145,16 +154,28 @@ where
layout: Layout<'_>,
_renderer: &Renderer,
) -> Option<overlay::Element<'b, Message, Renderer>> {
// Set position to center of bottom edge
let bounds = layout.bounds();
let position = Point::new(bounds.x + bounds.width / 2.0, bounds.y + bounds.height);
let (position, centered) = match self.position {
Some(relative) => (
bounds.position() + Vector::new(relative.x, relative.y),
false,
),
None => {
// Set position to center of bottom edge
(
Point::new(bounds.x + bounds.width / 2.0, bounds.y + bounds.height),
true,
)
}
};

// XXX needed to use RefCell to get &mut for popup element
Some(overlay::Element::new(
position,
Box::new(Overlay {
tree: &mut tree.children[1],
content: &self.popup,
centered,
}),
))
}
Expand All @@ -171,9 +192,10 @@ where
}
}

struct Overlay<'a, 'b, Message, Renderer> {
pub struct Overlay<'a, 'b, Message, Renderer> {
tree: &'a mut Tree,
content: &'a RefCell<Element<'b, Message, Renderer>>,
centered: bool,
}

impl<'a, 'b, Message, Renderer> overlay::Overlay<Message, Renderer>
Expand All @@ -182,13 +204,13 @@ where
Renderer: iced_core::Renderer,
{
fn layout(&self, renderer: &Renderer, bounds: Size, mut position: Point) -> layout::Node {
// Position is set to the center bottom of the lower widget

let limits = layout::Limits::new(Size::UNIT, bounds);
let mut node = self.content.borrow().as_widget().layout(renderer, &limits);

let width = node.size().width;
position.x = (position.x - width / 2.0).clamp(0.0, bounds.width - width);
if self.centered {
// Position is set to the center bottom of the lower widget
let width = node.size().width;
position.x = (position.x - width / 2.0).clamp(0.0, bounds.width - width);
}
node.move_to(position);

node
Expand Down
Loading