Skip to main content

mitsuami_widgets/
popover.rs

1//! A popover: content the platform shows by a container, over the window.
2
3use mitsuami_core::{
4    Children, Display, Element, ElementBuilder, FlexDirection, Length, NodeId, Prop, Ui, UiEvent, View, WidgetKind,
5};
6use mitsuami_reactive::{IntoValue, Signal};
7
8/// The platform's popover (an `NSPopover`, a `gtk::Popover`, a XAML
9/// `Flyout`, a `QQC2.Popup`), shown by its parent while `shown` is true.
10/// Put it in the container it points at, after what that shows; it takes
11/// no room there. Its children are laid out as a column's at their natural
12/// size, and the platform places it by its parent, where it fits. The user
13/// dismisses it as the platform lets them (a click outside, Escape), which
14/// sets `shown` back to false.
15///
16/// ```ignore
17/// let open = signal(false);
18/// Row::new().children((
19///     Button::new("Contains in").on_click(move || open.set(true)),
20///     Popover::new(open).padding(Spacing::Md).child(branches),
21/// ))
22/// ```
23pub struct Popover(Element);
24widget!(Popover);
25
26impl Popover {
27    pub fn new(shown: Signal<bool>) -> Popover {
28        let mut element = Element::new(WidgetKind::Popover);
29        element.prop(shown.into_value(), Prop::PopoverShown);
30        element.on(move |event| {
31            if let UiEvent::PopoverShownChanged(on) = event {
32                shown.set(*on);
33            }
34        });
35        Popover(element).style(|s| {
36            s.display = Display::Flex;
37            s.flex_direction = FlexDirection::Column;
38        })
39    }
40
41    /// Space between its children.
42    pub fn gap(mut self, gap: impl IntoValue<Length>) -> Popover {
43        self.0.style_prop(gap.into_value(), |s, v| {
44            s.row_gap = v;
45            s.column_gap = v;
46        });
47        self
48    }
49
50    pub fn children(mut self, children: impl Children) -> Popover {
51        self.0.add_children(children);
52        self
53    }
54
55    pub fn child(self, child: impl View) -> Popover {
56        self.children(child)
57    }
58
59    /// `<Popover shown={open}>…</Popover>`
60    #[doc(hidden)]
61    pub fn __tag(shown: Signal<bool>) -> Popover {
62        Popover::new(shown)
63    }
64
65    #[doc(hidden)]
66    pub fn __children<C: Children>(self, children: impl FnOnce() -> C) -> Popover {
67        self.children(children())
68    }
69}