1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//! Popups: [`PopupWindow`], the description of one, and how a widget opens
//! and closes it with [`Cx::open_popup`] and [`Cx::close_popup`].
use std::cell::RefCell;
use std::panic::{self, AssertUnwindSafe};
use std::rc::Rc;
use crabshell_reactive::untrack;
use crate::cx::{Anchor, Cx};
use crate::geometry::{Rect, Size};
use crate::input::panic_message;
use crate::prop::{IntoProp, Prop};
use crate::region::Region;
use crate::style::Style;
use crate::tree::{PopupState, Tree, borrow};
use crate::widgets::Widget;
use crate::window::{Anchors, Hints, Window};
/// A popup: a window next to the widget that opened it, sized to fit its
/// one child. Open one with [`Cx::open_popup`].
///
/// ```
/// use crabshell_ui::{Button, PopupWindow, Text};
///
/// let button = Button::new(Text::new("open")).on_click(|cx| {
/// cx.open_popup(|_cx| PopupWindow::new().child(Text::new("hello")))
/// });
/// ```
#[derive(Default)]
pub struct PopupWindow {
style: Option<Prop<Style>>,
child: Option<Box<dyn Widget>>,
edges: Option<Prop<Anchors>>,
gravity: Option<Prop<Anchors>>,
adjustment: Option<Prop<Adjust>>,
hints: Hints,
}
/// How a popup may move or shrink to stay on its output, combined with
/// `|`, as `xdg_positioner`'s constraint adjustments. The compositor
/// flips first, then slides, then resizes.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Adjust(u8);
impl Adjust {
/// Never moved: it may leave the output.
pub const NONE: Adjust = Adjust(0);
/// Slid sideways.
pub const SLIDE_X: Adjust = Adjust(1);
/// Slid up or down.
pub const SLIDE_Y: Adjust = Adjust(2);
/// Flipped to the other side.
pub const FLIP_X: Adjust = Adjust(4);
/// Flipped to above or below.
pub const FLIP_Y: Adjust = Adjust(8);
/// Made narrower, to the part on the output. It is laid out at the
/// width the compositor gives, and what doesn't fit is cut off.
pub const RESIZE_X: Adjust = Adjust(16);
/// Made shorter, to the part on the output, as `RESIZE_X`.
pub const RESIZE_Y: Adjust = Adjust(32);
/// Slid either way.
pub const SLIDE: Adjust = Adjust(1 | 2);
/// Flipped either way.
pub const FLIP: Adjust = Adjust(4 | 8);
/// Resized either way.
pub const RESIZE: Adjust = Adjust(16 | 32);
/// Any of them: flipped, then slid, then resized.
pub const ALL: Adjust = Adjust(63);
/// Whether every adjustment in `other` is allowed.
pub const fn contains(self, other: Adjust) -> bool {
self.0 & other.0 == other.0
}
}
impl std::ops::BitOr for Adjust {
type Output = Adjust;
fn bitor(self, other: Adjust) -> Adjust {
Adjust(self.0 | other.0)
}
}
/// Where a popup goes next to the widget that opened it. Unset parts
/// keep the defaults: below the widget from a horizontal bar, beside it
/// from a vertical bar or another popup, flipped or slid to stay on the
/// output.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Placement {
/// The edge or corner of the widget's rectangle the popup hangs from.
pub edges: Option<Anchors>,
/// Which way the popup extends from there.
pub gravity: Option<Anchors>,
/// How it may move to stay on the output.
pub adjustment: Option<Adjust>,
}
impl PopupWindow {
/// An empty popup.
pub fn new() -> PopupWindow {
PopupWindow::default()
}
/// Sets the popup's style. Its color and font are inherited by every
/// [`Text`](crate::Text) in it that doesn't set its own.
pub fn style(mut self, style: impl IntoProp<Style>) -> PopupWindow {
self.style = Some(style.into_prop());
self
}
/// Sets the popup's one child. The popup is as large as the child wants
/// to be, plus the style's padding.
pub fn child(mut self, child: impl Widget) -> PopupWindow {
self.child = Some(Box::new(child));
self
}
/// Sets the edge or corner of the widget the popup hangs from, such as
/// `Edge::Bottom | Edge::Left`; opposite edges cancel. Read when it
/// opens, as the compositor places a popup once.
pub fn edges(mut self, edges: impl IntoProp<Anchors>) -> PopupWindow {
self.edges = Some(edges.into_prop());
self
}
/// Sets which way the popup extends from its edges, such as
/// `Edge::Bottom | Edge::Right`. Read when it opens.
pub fn gravity(mut self, gravity: impl IntoProp<Anchors>) -> PopupWindow {
self.gravity = Some(gravity.into_prop());
self
}
/// Sets how the popup may move or shrink to stay on its output. The
/// default flips and slides it. Read when it opens.
pub fn adjustment(mut self, adjustment: impl IntoProp<Adjust>) -> PopupWindow {
self.adjustment = Some(adjustment.into_prop());
self
}
/// Sets the part of the popup that takes pointer input, as
/// [`PanelWindow::mask`](crate::PanelWindow::mask) does, and `None`, the
/// default, is the whole popup. A popup a click opened holds the
/// pointer, so a click outside its mask reaches the shell's own window
/// below, but over another program it closes the popup, as any click
/// outside does.
pub fn mask(mut self, mask: impl IntoProp<Option<Region>>) -> PopupWindow {
self.hints.mask = Some(mask.into_prop());
self
}
/// Sets the opacity the compositor shows the whole popup at, blur
/// included, as
/// [`PanelWindow::compositor_opacity`](crate::PanelWindow::compositor_opacity)
/// does.
pub fn compositor_opacity(mut self, opacity: impl IntoProp<f32>) -> PopupWindow {
self.hints.compositor_opacity = Some(opacity.into_prop());
self
}
/// Tells the compositor which part of the popup holds anything to see,
/// as [`PanelWindow::visible_mask`](crate::PanelWindow::visible_mask)
/// does.
pub fn visible_mask(mut self, region: impl IntoProp<Option<Region>>) -> PopupWindow {
self.hints.visible_mask = Some(region.into_prop());
self
}
}
impl Cx {
/// Opens a popup next to the widget this context belongs to, such as the
/// button whose handler got it. `build` describes the popup and gets its
/// context: what it creates there goes when the popup does.
///
/// The popup belongs to the widget, so it closes when the widget goes,
/// as well as on [`close_popup`](Self::close_popup) and when the user
/// clicks outside it. If `build` panics, that is logged with the
/// widget's path and nothing opens. A context of no widget, such as the
/// one `per_output` gives, logs that and opens nothing.
pub fn open_popup(&self, build: impl FnOnce(&Cx) -> PopupWindow) {
let Some((anchor, (tree, node))) = self
.anchor
.clone()
.and_then(|anchor| anchor.get().map(|found| (anchor, found)))
else {
return tracing::warn!("open_popup needs a widget's context, such as a handler's");
};
let opener = {
let tree = tree
.try_borrow()
.expect("invariant: a window's tree is not borrowed while user code runs");
tree
.node(node)
.map(|node| node.path.clone())
.zip(Some(tree.output.clone()))
};
let Some((opener, output)) = opener else {
return;
};
let path: Rc<str> = format!("{opener}/PopupWindow").into();
let root = Rc::new(Anchor::default());
let cx = self.child().named(path.to_string()).at(Rc::clone(&root));
// Untracked, so an effect that opens a popup doesn't rerun, and open
// another, when something `build` read changes.
let built = panic::catch_unwind(AssertUnwindSafe(|| {
untrack(|| {
let popup = build(&cx);
let child = popup.child.map(|child| child.into_element());
(
popup.style,
child,
(popup.edges, popup.gravity, popup.adjustment, popup.hints),
)
})
}));
let (style, child, placement) = match built {
Ok(built) => built,
Err(payload) => {
tracing::error!(
"{path} panicked while it was built, so it doesn't open: {}",
panic_message(payload.as_ref())
);
cx.owner().dispose();
return;
}
};
let owner = cx.owner();
let alive = cx.signal(());
let popup = Rc::new_cyclic(|this| {
let mut tree = Tree::new(this.clone(), "PopupWindow", output, cx, path);
tree.popup = Some(PopupState {
opener: anchor,
close: false,
alive,
placement: Placement::default(),
wanted: Size::default(),
limit: None,
});
RefCell::new(tree)
});
{
let mut tree = borrow(&popup);
tree.mount_root(style, child);
let (edges, gravity, adjustment, hints) = placement;
let root = tree.root;
tree.bind_opt(root, edges, "edges", |tree, _, value| {
if let Some(popup) = tree.popup.as_mut() {
popup.placement.edges = Some(value);
}
});
tree.bind_opt(root, gravity, "gravity", |tree, _, value| {
if let Some(popup) = tree.popup.as_mut() {
popup.placement.gravity = Some(value);
}
});
tree.bind_opt(root, adjustment, "adjustment", |tree, _, value| {
if let Some(popup) = tree.popup.as_mut() {
popup.placement.adjustment = Some(value);
}
});
tree.bind_compositor_hints(hints);
}
borrow(&tree).popups.push(Window { tree: popup, owner });
}
/// Closes the popup this context's widget is in, with every popup it
/// opened and every popup that opened it, as a menu does once one of
/// its entries is clicked. Outside a popup it logs that and does
/// nothing.
pub fn close_popup(&self) {
if !self.close_popups_if_any() {
tracing::warn!("close_popup was called outside a popup");
}
}
/// [`close_popup`](Self::close_popup), saying whether there was a popup
/// to close. It marks this popup and each popup that opened it, one
/// tree borrowed at a time; the popups they opened close with them.
pub(crate) fn close_popups_if_any(&self) -> bool {
let mut next = self
.anchor
.as_ref()
.and_then(|anchor| anchor.get())
.map(|(tree, _)| tree);
let mut closed = false;
while let Some(tree) = next.take() {
let mut tree = borrow(&tree);
let Some(popup) = tree.popup.as_mut() else {
break;
};
popup.close = true;
closed = true;
next = popup.opener.get().map(|(tree, _)| tree);
}
closed
}
}
impl Window {
/// Takes the popups this window's widgets opened since the last call.
pub fn take_popups(&self) -> Vec<Window> {
std::mem::take(&mut borrow(&self.tree).popups)
}
/// For a popup, the rectangle of the widget that opened it, at its
/// window's last layout, in that window's logical pixels. The platform
/// places the popup next to it. `None` for a panel, once the widget is
/// gone, or before its first layout.
pub fn anchor(&self) -> Option<Rect> {
let opener = Rc::clone(&self.tree.borrow().popup.as_ref()?.opener);
let (tree, node) = opener.get()?;
let tree = tree.borrow();
tree.node(node)?.rect
}
/// Where this popup goes, as its config set it; the defaults for a
/// panel.
pub fn placement(&self) -> Placement {
let tree = self.tree.borrow();
tree
.popup
.as_ref()
.map(|popup| popup.placement)
.unwrap_or_default()
}
/// For a popup, the size the compositor shrank it to, so it fits on
/// its output, or `None` once it takes the size it wants. It is laid
/// out and drawn no larger, its content cut off. Other windows ignore
/// it.
pub fn limit_size(&self, limit: Option<Size>) {
let mut tree = borrow(&self.tree);
let Some(popup) = tree.popup.as_mut() else {
return;
};
if popup.limit != limit {
popup.limit = limit;
tree.needs_layout = true;
}
}
/// The size the window asks the compositor for: a popup's content size
/// at its last layout, before any limit; another window's size.
pub fn wanted_size(&self) -> Size {
let tree = self.tree.borrow();
tree.popup.as_ref().map_or(tree.size, |popup| popup.wanted)
}
/// Whether this window is a popup.
pub fn is_popup(&self) -> bool {
self.tree.borrow().popup.is_some()
}
/// Whether this popup should close: [`Cx::close_popup`] asked, or the
/// widget that opened it is gone. Always `false` for a panel.
pub fn wants_close(&self) -> bool {
let tree = self.tree.borrow();
tree
.popup
.as_ref()
.is_some_and(|popup| popup.close || popup.alive.try_with(|_| ()).is_none())
}
}