1use std::cell::RefCell;
5use std::rc::Rc;
6
7use crabshell_reactive::{Signal, untrack};
8
9use crate::access::AccessEvent;
10use crate::cx::{Anchor, Cx};
11use crate::input::{Handler, run_isolated};
12use crate::prop::{IntoProp, Prop};
13use crate::region::Region;
14use crate::style::Style;
15use crate::tree::{Tree, borrow};
16use crate::widgets::Widget;
17use crate::window::{Anchors, Hints, Output, Window};
18
19pub(crate) type CloseHandler = Rc<Handler<dyn Fn(&Cx)>>;
20
21pub struct FloatingWindow {
36 pub(crate) title: Option<Prop<String>>,
37 pub(crate) width: Option<Prop<u32>>,
38 pub(crate) height: Option<Prop<u32>>,
39 pub(crate) min_size: Option<Prop<(u32, u32)>>,
40 pub(crate) max_size: Option<Prop<(u32, u32)>>,
41 pub(crate) maximized: Option<Prop<bool>>,
42 pub(crate) fullscreen: Option<Prop<bool>>,
43 pub(crate) visible: Option<Prop<bool>>,
44 pub(crate) hints: Hints,
45 pub(crate) on_close: Option<CloseHandler>,
46 pub(crate) style: Option<Prop<Style>>,
47 pub(crate) child: Option<Box<dyn Widget>>,
48}
49
50impl Default for FloatingWindow {
51 fn default() -> FloatingWindow {
52 FloatingWindow::new()
53 }
54}
55
56impl FloatingWindow {
57 pub const DEFAULT_SIZE: (u32, u32) = (400, 300);
59
60 pub fn new() -> FloatingWindow {
62 FloatingWindow {
63 title: None,
64 width: None,
65 height: None,
66 min_size: None,
67 max_size: None,
68 maximized: None,
69 fullscreen: None,
70 visible: None,
71 hints: Hints::default(),
72 on_close: None,
73 style: None,
74 child: None,
75 }
76 }
77
78 pub fn title(mut self, title: impl IntoProp<String>) -> FloatingWindow {
80 self.title = Some(title.into_prop());
81 self
82 }
83
84 pub fn width(mut self, width: impl IntoProp<u32>) -> FloatingWindow {
87 self.width = Some(width.into_prop());
88 self
89 }
90
91 pub fn height(mut self, height: impl IntoProp<u32>) -> FloatingWindow {
93 self.height = Some(height.into_prop());
94 self
95 }
96
97 pub fn min_size(mut self, size: impl IntoProp<(u32, u32)>) -> FloatingWindow {
100 self.min_size = Some(size.into_prop());
101 self
102 }
103
104 pub fn max_size(mut self, size: impl IntoProp<(u32, u32)>) -> FloatingWindow {
107 self.max_size = Some(size.into_prop());
108 self
109 }
110
111 pub fn maximized(mut self, maximized: impl IntoProp<bool>) -> FloatingWindow {
113 self.maximized = Some(maximized.into_prop());
114 self
115 }
116
117 pub fn fullscreen(mut self, fullscreen: impl IntoProp<bool>) -> FloatingWindow {
119 self.fullscreen = Some(fullscreen.into_prop());
120 self
121 }
122
123 pub fn visible(mut self, visible: impl IntoProp<bool>) -> FloatingWindow {
126 self.visible = Some(visible.into_prop());
127 self
128 }
129
130 pub fn mask(mut self, mask: impl IntoProp<Option<Region>>) -> FloatingWindow {
135 self.hints.mask = Some(mask.into_prop());
136 self
137 }
138
139 pub fn blur(mut self, region: impl IntoProp<Option<Region>>) -> FloatingWindow {
142 self.hints.blur = Some(region.into_prop());
143 self
144 }
145
146 pub fn inhibit_idle(mut self, inhibit: impl IntoProp<bool>) -> FloatingWindow {
150 self.hints.inhibit_idle = Some(inhibit.into_prop());
151 self
152 }
153
154 pub fn inhibit_shortcuts(mut self, inhibit: impl IntoProp<bool>) -> FloatingWindow {
159 self.hints.inhibit_shortcuts = Some(inhibit.into_prop());
160 self
161 }
162
163 pub fn shortcuts_inhibited(mut self, signal: Signal<bool>) -> FloatingWindow {
167 self.hints.shortcuts_inhibited = Some(signal);
168 self
169 }
170
171 pub fn focus_grab(mut self, signal: Signal<bool>) -> FloatingWindow {
175 self.hints.focus_grab = Some(signal);
176 self
177 }
178
179 pub fn compositor_opacity(mut self, opacity: impl IntoProp<f32>) -> FloatingWindow {
184 self.hints.compositor_opacity = Some(opacity.into_prop());
185 self
186 }
187
188 pub fn visible_mask(mut self, region: impl IntoProp<Option<Region>>) -> FloatingWindow {
192 self.hints.visible_mask = Some(region.into_prop());
193 self
194 }
195
196 pub fn on_close(mut self, handler: impl Fn(&Cx) + 'static) -> FloatingWindow {
200 self.on_close = Some(Rc::new(Handler::new(handler)));
201 self
202 }
203
204 pub fn style(mut self, style: impl IntoProp<Style>) -> FloatingWindow {
207 self.style = Some(style.into_prop());
208 self
209 }
210
211 pub fn child(mut self, child: impl Widget) -> FloatingWindow {
213 self.child = Some(Box::new(child));
214 self
215 }
216}
217
218#[derive(Clone, Debug, PartialEq, Eq)]
220pub struct FloatingConfig {
221 pub title: String,
223 pub width: u32,
225 pub height: u32,
227 pub min_size: (u32, u32),
229 pub max_size: (u32, u32),
231 pub maximized: bool,
233 pub fullscreen: bool,
235 pub shown: bool,
237}
238
239#[derive(Clone, Copy, Debug, PartialEq, Eq)]
243pub enum WindowRequest {
244 Move,
246 Resize(Anchors),
248 Minimize,
250}
251
252pub(crate) struct FloatingState {
254 title: String,
255 width: u32,
256 height: u32,
257 min_size: (u32, u32),
258 max_size: (u32, u32),
259 maximized: bool,
260 fullscreen: bool,
261 visible: bool,
262 dismissed: bool,
264 on_close: Signal<Option<CloseHandler>>,
267 pub(crate) requests: Vec<WindowRequest>,
268}
269
270impl FloatingState {
271 pub(crate) fn shown(&self) -> bool {
273 self.visible && !self.dismissed
274 }
275
276 pub(crate) fn title(&self) -> &str {
277 &self.title
278 }
279}
280
281impl Window {
282 pub fn floating(cx: &Cx, mut window: FloatingWindow) -> Window {
285 let child = untrack(|| window.child.take().map(|child| child.into_element()));
286 let path: Rc<str> = "FloatingWindow".into();
287 let cx = cx
288 .child()
289 .named(path.to_string())
290 .at(Rc::new(Anchor::default()));
291 let owner = cx.owner();
292 let on_close = cx.signal(window.on_close.take());
293 let tree = Rc::new_cyclic(|this| {
294 RefCell::new(Tree::new(
295 this.clone(),
296 "FloatingWindow",
297 Output::new(""),
298 cx,
299 path,
300 ))
301 });
302 borrow(&tree).mount_floating(window, child, on_close);
303 Window { tree, owner }
304 }
305
306 pub fn floating_config(&self) -> Option<FloatingConfig> {
309 let tree = self.tree.borrow();
310 let floating = tree.floating.as_ref()?;
311 let (width, height) = FloatingWindow::DEFAULT_SIZE;
312 Some(FloatingConfig {
313 title: floating.title.clone(),
314 width: if floating.width == 0 {
315 width
316 } else {
317 floating.width
318 },
319 height: if floating.height == 0 {
320 height
321 } else {
322 floating.height
323 },
324 min_size: floating.min_size,
325 max_size: floating.max_size,
326 maximized: floating.maximized,
327 fullscreen: floating.fullscreen,
328 shown: floating.visible && !floating.dismissed,
329 })
330 }
331
332 pub fn request_close(&self) {
335 let call = {
336 let mut tree = borrow(&self.tree);
337 let root = tree.root;
338 let Some(floating) = tree.floating.as_mut() else {
339 return;
340 };
341 let handler = untrack(|| floating.on_close.try_with(Option::clone)).flatten();
342 let Some(handler) = handler else {
343 floating.dismissed = true;
344 tree.surface.changed = true;
345 tree.access_event(|_| AccessEvent::WindowsChanged);
346 return;
347 };
348 let Some(node) = tree.node(root) else {
349 return;
350 };
351 (node.cx.clone(), Rc::clone(&node.path), handler)
352 };
353 let (cx, path, handler) = call;
354 run_isolated(&path, "on_close", handler, |f| f(&cx));
355 }
356
357 pub fn take_requests(&self) -> Vec<WindowRequest> {
359 let mut tree = borrow(&self.tree);
360 tree
361 .floating
362 .as_mut()
363 .map(|floating| std::mem::take(&mut floating.requests))
364 .unwrap_or_default()
365 }
366}
367
368impl Cx {
369 pub fn start_move(&self) {
373 self.request(WindowRequest::Move);
374 }
375
376 pub fn start_resize(&self, edges: impl Into<Anchors>) {
380 self.request(WindowRequest::Resize(edges.into()));
381 }
382
383 pub fn minimize(&self) {
385 self.request(WindowRequest::Minimize);
386 }
387
388 fn request(&self, request: WindowRequest) {
389 let Some((tree, _)) = self.anchor.as_ref().and_then(|anchor| anchor.get()) else {
390 return tracing::warn!("{request:?} needs a widget's context in a floating window");
391 };
392 let mut tree = borrow(&tree);
393 match tree.floating.as_mut() {
394 Some(floating) => floating.requests.push(request),
395 None => tracing::warn!("{request:?} works only in a floating window"),
396 }
397 }
398}
399
400impl Tree {
401 fn mount_floating(
403 &mut self,
404 window: FloatingWindow,
405 child: Option<crate::widgets::Element>,
406 on_close: Signal<Option<CloseHandler>>,
407 ) {
408 let (width, height) = FloatingWindow::DEFAULT_SIZE;
409 self.floating = Some(FloatingState {
410 title: String::new(),
411 width,
412 height,
413 min_size: (0, 0),
414 max_size: (0, 0),
415 maximized: false,
416 fullscreen: false,
417 visible: true,
418 dismissed: false,
419 on_close,
420 requests: Vec::new(),
421 });
422 let root = self.root;
423 let FloatingWindow {
424 title,
425 width,
426 height,
427 min_size,
428 max_size,
429 maximized,
430 fullscreen,
431 visible,
432 hints,
433 on_close: _,
434 style,
435 child: _,
436 } = window;
437 self.bind_opt(root, title, "title", |tree, root, value| {
438 if tree.set_floating(|f| &mut f.title, value) {
439 tree.node_event(root, AccessEvent::NameChanged);
440 }
441 });
442 self.bind_opt(root, width, "width", |tree, _, value| {
443 tree.set_floating(|f| &mut f.width, value);
444 });
445 self.bind_opt(root, height, "height", |tree, _, value| {
446 tree.set_floating(|f| &mut f.height, value);
447 });
448 self.bind_opt(root, min_size, "min_size", |tree, _, value| {
449 tree.set_floating(|f| &mut f.min_size, value);
450 });
451 self.bind_opt(root, max_size, "max_size", |tree, _, value| {
452 tree.set_floating(|f| &mut f.max_size, value);
453 });
454 self.bind_opt(root, maximized, "maximized", |tree, _, value| {
455 tree.set_floating(|f| &mut f.maximized, value);
456 });
457 self.bind_opt(root, fullscreen, "fullscreen", |tree, _, value| {
458 tree.set_floating(|f| &mut f.fullscreen, value);
459 });
460 self.bind_opt(root, visible, "visible", |tree, _, value| {
461 let mut changed = !value && tree.set_floating(|f| &mut f.dismissed, false);
463 changed |= tree.set_floating(|f| &mut f.visible, value);
464 if changed {
465 tree.access_event(|_| AccessEvent::WindowsChanged);
466 }
467 });
468 self.bind_compositor_hints(hints);
469 self.mount_root(style, child);
470 self.surface.changed = true;
471 }
472
473 fn set_floating<T: PartialEq>(
476 &mut self,
477 field: impl FnOnce(&mut FloatingState) -> &mut T,
478 value: T,
479 ) -> bool {
480 let Some(floating) = self.floating.as_mut() else {
481 return false;
482 };
483 let slot = field(floating);
484 if *slot == value {
485 return false;
486 }
487 *slot = value;
488 self.surface.changed = true;
489 true
490 }
491}