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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
//     https://www.apache.org/licenses/LICENSE-2.0

//! Window widgets

use crate::cast::Cast;
use crate::decorations::{Border, Decorations, TitleBar};
use crate::dir::Directional;
use crate::event::{ConfigCx, Event, EventCx, IsUsed, ResizeDirection, Scroll, Unused, Used};
use crate::geom::{Coord, Offset, Rect, Size};
use crate::layout::{self, AxisInfo, SizeRules};
use crate::theme::{DrawCx, FrameStyle, SizeCx};
use crate::{Action, Events, Icon, Id, Layout, LayoutExt, Widget};
use kas_macros::impl_scope;
use smallvec::SmallVec;
use std::num::NonZeroU32;

/// Identifier for a window or pop-up
///
/// Identifiers should always be unique.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct WindowId(NonZeroU32);

impl WindowId {
    /// Construct a [`WindowId`]
    ///
    /// Only for use by the graphics/platform backend!
    #[allow(unused)]
    pub(crate) fn new(n: NonZeroU32) -> WindowId {
        WindowId(n)
    }

    pub(crate) fn get(self) -> u32 {
        self.0.get()
    }
}

/// Commands supported by the [`Window`]
///
/// This may be sent as a message from any widget in the window.
#[derive(Clone, Debug)]
pub enum WindowCommand {
    /// Change the window's title
    SetTitle(String),
    /// Change the window's icon
    SetIcon(Option<Icon>),
}

impl_scope! {
    /// The window widget
    ///
    /// This widget is the root of any UI tree used as a window. It manages
    /// window decorations.
    ///
    /// To change window properties at run-time, send a [`WindowCommand`] from a
    /// child widget.
    #[widget]
    pub struct Window<Data: 'static> {
        core: widget_core!(),
        icon: Option<Icon>, // initial icon, if any
        decorations: Decorations,
        restrictions: (bool, bool),
        drag_anywhere: bool,
        transparent: bool,
        #[widget]
        inner: Box<dyn Widget<Data = Data>>,
        #[widget(&())]
        title_bar: TitleBar,
        #[widget(&())] b_w: Border,
        #[widget(&())] b_e: Border,
        #[widget(&())] b_n: Border,
        #[widget(&())] b_s: Border,
        #[widget(&())] b_nw: Border,
        #[widget(&())] b_ne: Border,
        #[widget(&())] b_sw: Border,
        #[widget(&())] b_se: Border,
        bar_h: i32,
        dec_offset: Offset,
        dec_size: Size,
        popups: SmallVec<[(WindowId, kas::PopupDescriptor, Offset); 16]>,
    }

    impl Layout for Self {
        fn size_rules(&mut self, sizer: SizeCx, axis: AxisInfo) -> SizeRules {
            let mut inner = self.inner.size_rules(sizer.re(), axis);

            self.bar_h = 0;
            if matches!(self.decorations, Decorations::Toolkit) {
                let bar = self.title_bar.size_rules(sizer.re(), axis);
                if axis.is_horizontal() {
                    inner.max_with(bar);
                } else {
                    inner.append(bar);
                    self.bar_h = bar.min_size();
                }
            }

            // These methods don't return anything useful, but we are required to call them:
            let _ = self.b_w.size_rules(sizer.re(), axis);
            let _ = self.b_e.size_rules(sizer.re(), axis);
            let _ = self.b_n.size_rules(sizer.re(), axis);
            let _ = self.b_s.size_rules(sizer.re(), axis);
            let _ = self.b_nw.size_rules(sizer.re(), axis);
            let _ = self.b_ne.size_rules(sizer.re(), axis);
            let _ = self.b_se.size_rules(sizer.re(), axis);
            let _ = self.b_sw.size_rules(sizer.re(), axis);

            if matches!(self.decorations, Decorations::Border | Decorations::Toolkit) {
                let frame = sizer.frame(FrameStyle::Window, axis);
                let (rules, offset, size) = frame.surround(inner);
                self.dec_offset.set_component(axis, offset);
                self.dec_size.set_component(axis, size);
                rules
            } else {
                inner
            }
        }

        fn set_rect(&mut self, cx: &mut ConfigCx, rect: Rect) {
            self.core.rect = rect;
            // Calculate position and size for nw, ne, and inner portions:
            let s_nw: Size = self.dec_offset.cast();
            let s_se = self.dec_size - s_nw;
            let mut s_in = rect.size - self.dec_size;
            let p_nw = rect.pos;
            let mut p_in = p_nw + self.dec_offset;
            let p_se = p_in + s_in;

            self.b_w.set_rect(cx, Rect::new(Coord(p_nw.0, p_in.1), Size(s_nw.0, s_in.1)));
            self.b_e.set_rect(cx, Rect::new(Coord(p_se.0, p_in.1), Size(s_se.0, s_in.1)));
            self.b_n.set_rect(cx, Rect::new(Coord(p_in.0, p_nw.1), Size(s_in.0, s_nw.1)));
            self.b_s.set_rect(cx, Rect::new(Coord(p_in.0, p_se.1), Size(s_in.0, s_se.1)));
            self.b_nw.set_rect(cx, Rect::new(p_nw, s_nw));
            self.b_ne.set_rect(cx, Rect::new(Coord(p_se.0, p_nw.1), Size(s_se.0, s_nw.1)));
            self.b_se.set_rect(cx, Rect::new(p_se, s_se));
            self.b_sw.set_rect(cx, Rect::new(Coord(p_nw.0, p_se.1), Size(s_nw.0, s_se.1)));

            if self.bar_h > 0 {
                let bar_size = Size(s_in.0, self.bar_h);
                self.title_bar.set_rect(cx, Rect::new(p_in, bar_size));
                p_in.1 += self.bar_h;
                s_in -= Size(0, self.bar_h);
            }
            self.inner.set_rect(cx, Rect::new(p_in, s_in));
        }

        fn find_id(&mut self, _: Coord) -> Option<Id> {
            unimplemented!()
        }

        fn draw(&mut self, _: DrawCx) {
            unimplemented!()
        }
    }

    impl Self {
        pub(crate) fn find_id(&mut self, data: &Data, coord: Coord) -> Option<Id> {
            if !self.core.rect.contains(coord) {
                return None;
            }
            for (_, popup, translation) in self.popups.iter_mut().rev() {
                if let Some(Some(id)) = self.inner.as_node(data).find_node(&popup.id, |mut node| node.find_id(coord + *translation)) {
                    return Some(id);
                }
            }
            if self.bar_h > 0 {
                if let Some(id) = self.title_bar.find_id(coord) {
                    return Some(id);
                }
            }
            self.inner.find_id(coord)
                .or_else(|| self.b_w.find_id(coord))
                .or_else(|| self.b_e.find_id(coord))
                .or_else(|| self.b_n.find_id(coord))
                .or_else(|| self.b_s.find_id(coord))
                .or_else(|| self.b_nw.find_id(coord))
                .or_else(|| self.b_ne.find_id(coord))
                .or_else(|| self.b_sw.find_id(coord))
                .or_else(|| self.b_se.find_id(coord))
                .or_else(|| Some(self.id()))
        }

        #[cfg(winit)]
        pub(crate) fn draw(&mut self, data: &Data, mut draw: DrawCx) {
            if self.dec_size != Size::ZERO {
                draw.frame(self.core.rect, FrameStyle::Window, Default::default());
                if self.bar_h > 0 {
                    draw.recurse(&mut self.title_bar);
                }
            }
            draw.recurse(&mut self.inner);
            for (_, popup, translation) in &self.popups {
                self.inner.as_node(data).find_node(&popup.id, |mut node| {
                    let clip_rect = node.rect() - *translation;
                    draw.with_overlay(clip_rect, *translation, |draw| {
                        node._draw(draw);
                    });
                });
            }
        }
    }

    impl Events for Self {
        type Data = Data;

        fn configure(&mut self, cx: &mut ConfigCx) {
            if cx.platform().is_wayland() && self.decorations == Decorations::Server {
                // Wayland's base protocol does not support server-side decorations
                // TODO: Wayland has extensions for this; server-side is still
                // usually preferred where supported (e.g. KDE).
                self.decorations = Decorations::Toolkit;
            }
        }

        fn handle_event(&mut self, cx: &mut EventCx, _: &Self::Data, event: Event) -> IsUsed {
            match event {
                Event::PressStart { .. } if self.drag_anywhere => {
                    cx.drag_window();
                    Used
                }
                _ => Unused,
            }
        }

        fn handle_messages(&mut self, cx: &mut EventCx, _: &Self::Data) {
            if let Some(cmd) = cx.try_pop() {
                match cmd {
                    WindowCommand::SetTitle(title) => {
                        cx.action(self.id(), self.title_bar.set_title(title));
                        #[cfg(winit)]
                        if self.decorations == Decorations::Server {
                            if let Some(w) = cx.winit_window() {
                                w.set_title(self.title());
                            }
                        }
                    }
                    WindowCommand::SetIcon(icon) => {
                        #[cfg(winit)]
                        if self.decorations == Decorations::Server {
                            if let Some(w) = cx.winit_window() {
                                w.set_window_icon(icon);
                                return; // do not set self.icon
                            }
                        }
                        self.icon = icon;
                    }
                }
            }
        }

        fn handle_scroll(&mut self, cx: &mut EventCx, data: &Data, _: Scroll) {
            // Something was scrolled; update pop-up translations
            self.resize_popups(&mut cx.config_cx(), data);
        }
    }

    impl std::fmt::Debug for Self {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.debug_struct("Window")
                .field("core", &self.core)
                .field("title", &self.title_bar.title())
                .finish()
        }
    }
}

impl<Data: 'static> Window<Data> {
    /// Construct a window with a `W: Widget` and a title
    pub fn new(ui: impl Widget<Data = Data> + 'static, title: impl ToString) -> Self {
        Self::new_boxed(Box::new(ui), title)
    }

    /// Construct a window from a boxed `ui` widget and a `title`
    pub fn new_boxed(ui: Box<dyn Widget<Data = Data>>, title: impl ToString) -> Self {
        Window {
            core: Default::default(),
            icon: None,
            decorations: Decorations::Server,
            restrictions: (true, false),
            drag_anywhere: true,
            transparent: false,
            inner: ui,
            title_bar: TitleBar::new(title),
            b_w: Border::new(ResizeDirection::West),
            b_e: Border::new(ResizeDirection::East),
            b_n: Border::new(ResizeDirection::North),
            b_s: Border::new(ResizeDirection::South),
            b_nw: Border::new(ResizeDirection::NorthWest),
            b_ne: Border::new(ResizeDirection::NorthEast),
            b_sw: Border::new(ResizeDirection::SouthWest),
            b_se: Border::new(ResizeDirection::SouthEast),
            bar_h: 0,
            dec_offset: Default::default(),
            dec_size: Default::default(),
            popups: Default::default(),
        }
    }

    /// Get the window's title
    pub fn title(&self) -> &str {
        self.title_bar.title()
    }

    /// Get the window's icon, if any
    pub(crate) fn icon(&mut self) -> Option<Icon> {
        self.icon.clone()
    }

    /// Set the window's icon (inline)
    ///
    /// Default: `None`
    pub fn with_icon(mut self, icon: impl Into<Option<Icon>>) -> Self {
        self.icon = icon.into();
        self
    }

    /// Get the preference for window decorations
    pub fn decorations(&self) -> crate::Decorations {
        self.decorations
    }

    /// Set the preference for window decorations
    ///
    /// "Windowing" platforms (i.e. not mobile or web) usually include a
    /// title-bar, icons and potentially side borders. These are known as
    /// **decorations**.
    ///
    /// This controls the *preferred* type of decorations. The resulting
    /// behaviour is platform-dependent.
    ///
    /// Default: [`Decorations::Server`].
    pub fn with_decorations(mut self, decorations: Decorations) -> Self {
        self.decorations = decorations;
        self
    }

    /// Get window resizing restrictions: `(restrict_min, restrict_max)`
    pub fn restrictions(&self) -> (bool, bool) {
        self.restrictions
    }

    /// Whether to limit the maximum size of a window
    ///
    /// All widgets' size rules allow calculation of two sizes: the minimum
    /// size and the ideal size. Windows are initially sized to the ideal size.
    ///
    /// If `restrict_min`, the window may not be sized below the minimum size.
    /// Default value: `true`.
    ///
    /// If `restrict_max`, the window may not be sized above the ideal size.
    /// Default value: `false`.
    pub fn with_restrictions(mut self, restrict_min: bool, restrict_max: bool) -> Self {
        self.restrictions = (restrict_min, restrict_max);
        let resizable = !restrict_min || !restrict_max;
        self.b_w.set_resizable(resizable);
        self.b_e.set_resizable(resizable);
        self.b_n.set_resizable(resizable);
        self.b_s.set_resizable(resizable);
        self.b_nw.set_resizable(resizable);
        self.b_ne.set_resizable(resizable);
        self.b_se.set_resizable(resizable);
        self.b_sw.set_resizable(resizable);
        self
    }

    /// Get "drag anywhere" state
    pub fn drag_anywhere(&self) -> bool {
        self.drag_anywhere
    }

    /// Whether to allow dragging the window from the background
    ///
    /// If true, then any unhandled click+drag in the window may be used to
    /// drag the window on supported platforms. Default value: `true`.
    pub fn with_drag_anywhere(mut self, drag_anywhere: bool) -> Self {
        self.drag_anywhere = drag_anywhere;
        self
    }

    /// Get whether this window should use transparent rendering
    pub fn transparent(&self) -> bool {
        self.transparent
    }

    /// Whether the window supports transparency
    ///
    /// If true, painting with `alpha < 1.0` makes the background visible.
    /// Additionally, window draw targets are cleared to transparent. This does
    /// not stop theme elements from drawing a solid background.
    ///
    /// Note: results may vary by platform. Current output does *not* use
    /// pre-multiplied alpha which *some* platforms expect, thus pixels with
    /// partial transparency may have incorrect appearance.
    ///
    /// Default: `false`.
    pub fn with_transparent(mut self, transparent: bool) -> Self {
        self.transparent = transparent;
        self
    }

    /// Add a pop-up as a layer in the current window
    ///
    /// Each [`crate::Popup`] is assigned a [`WindowId`]; both are passed.
    pub(crate) fn add_popup(
        &mut self,
        cx: &mut EventCx,
        data: &Data,
        id: WindowId,
        popup: kas::PopupDescriptor,
    ) {
        let index = self.popups.len();
        self.popups.push((id, popup, Offset::ZERO));
        self.resize_popup(&mut cx.config_cx(), data, index);
        cx.action(Id::ROOT, Action::REDRAW);
    }

    /// Trigger closure of a pop-up
    ///
    /// If the given `id` refers to a pop-up, it should be closed.
    pub(crate) fn remove_popup(&mut self, cx: &mut EventCx, id: WindowId) {
        for i in 0..self.popups.len() {
            if id == self.popups[i].0 {
                self.popups.remove(i);
                cx.action(Id::ROOT, Action::REGION_MOVED);
                return;
            }
        }
    }

    /// Resize popups
    ///
    /// This is called immediately after [`Layout::set_rect`] to resize
    /// existing pop-ups.
    pub(crate) fn resize_popups(&mut self, cx: &mut ConfigCx, data: &Data) {
        for i in 0..self.popups.len() {
            self.resize_popup(cx, data, i);
        }
    }
}

// Search for a widget by `id`. On success, return that widget's [`Rect`] and
// the translation of its children.
fn find_rect(widget: &dyn Layout, id: Id, mut translation: Offset) -> Option<(Rect, Offset)> {
    let mut widget = widget;
    loop {
        if widget.eq_id(&id) {
            if widget.translation() != Offset::ZERO {
                // Unvalidated: does this cause issues with the parent's event handlers?
                log::warn!(
                    "Parent of pop-up {} has non-zero translation",
                    widget.identify()
                );
            }

            let rect = widget.rect();
            return Some((rect, translation));
        } else if let Some(child) = widget
            .find_child_index(&id)
            .and_then(|i| widget.get_child(i))
        {
            translation += widget.translation();
            widget = child;
            continue;
        } else {
            return None;
        }
    }
}

impl<Data: 'static> Window<Data> {
    fn resize_popup(&mut self, cx: &mut ConfigCx, data: &Data, index: usize) {
        // Notation: p=point/coord, s=size, m=margin
        // r=window/root rect, c=anchor rect
        let r = self.core.rect;
        let (_, ref mut popup, ref mut translation) = self.popups[index];

        let is_reversed = popup.direction.is_reversed();
        let place_in = |rp, rs: i32, cp: i32, cs: i32, ideal, m: (u16, u16)| -> (i32, i32) {
            let m: (i32, i32) = (m.0.into(), m.1.into());
            let before: i32 = cp - (rp + m.1);
            let before = before.max(0);
            let after = (rs - (cs + before + m.0)).max(0);
            if after >= ideal {
                if is_reversed && before >= ideal {
                    (cp - ideal - m.1, ideal)
                } else {
                    (cp + cs + m.0, ideal)
                }
            } else if before >= ideal {
                (cp - ideal - m.1, ideal)
            } else if before > after {
                (rp, before)
            } else {
                (cp + cs + m.0, after)
            }
        };
        let place_out = |rp, rs, cp: i32, cs, ideal: i32| -> (i32, i32) {
            let pos = cp.min(rp + rs - ideal).max(rp);
            let size = ideal.max(cs).min(rs);
            (pos, size)
        };

        let (c, t) = find_rect(self.inner.as_layout(), popup.parent.clone(), Offset::ZERO).unwrap();
        *translation = t;
        let r = r + t; // work in translated coordinate space
        let result = self.inner.as_node(data).find_node(&popup.id, |mut node| {
            let mut cache = layout::SolveCache::find_constraints(node.re(), cx.size_cx());
            let ideal = cache.ideal(false);
            let m = cache.margins();

            let rect = if popup.direction.is_horizontal() {
                let (x, w) = place_in(r.pos.0, r.size.0, c.pos.0, c.size.0, ideal.0, m.horiz);
                let (y, h) = place_out(r.pos.1, r.size.1, c.pos.1, c.size.1, ideal.1);
                Rect::new(Coord(x, y), Size::new(w, h))
            } else {
                let (x, w) = place_out(r.pos.0, r.size.0, c.pos.0, c.size.0, ideal.0);
                let (y, h) = place_in(r.pos.1, r.size.1, c.pos.1, c.size.1, ideal.1, m.vert);
                Rect::new(Coord(x, y), Size::new(w, h))
            };

            cache.apply_rect(node.re(), cx, rect, false);
            cache.print_widget_heirarchy(node.as_layout());
        });

        // Event handlers expect that the popup's rect is now assigned.
        // If we were to try recovering we should remove the popup.
        assert!(result.is_some());
    }
}