agg-gui 0.1.1

Immediate-mode Rust GUI library with AGG rendering, Y-up layout, widgets, text, SVG, and native/WASM adapters
Documentation
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
use super::*;

impl Widget for Window {
    fn type_name(&self) -> &'static str {
        "Window"
    }
    /// External identity for z-order persistence, inspector lookup, etc.
    fn id(&self) -> Option<&str> {
        Some(&self.title)
    }

    fn is_visible(&self) -> bool {
        self.requested_visible() || self.fade_out_active.get()
    }

    /// A collapsed window paints only its title bar — nothing inside the
    /// content area is visible, so no child can legitimately request a
    /// repaint.  Closing (`is_visible` false) also short-circuits, matching
    /// the default trait impl.  Without these overrides a cursor blink or
    /// hover tween inside a collapsed/closed window would keep the host
    /// loop awake despite being invisible.
    fn needs_draw(&self) -> bool {
        if !self.is_visible() || self.collapsed {
            return false;
        }
        self.children().iter().any(|c| c.needs_draw())
    }

    fn next_draw_deadline(&self) -> Option<web_time::Instant> {
        if !self.is_visible() || self.collapsed {
            return None;
        }
        let mut best: Option<web_time::Instant> = None;
        for c in self.children() {
            if let Some(t) = c.next_draw_deadline() {
                best = Some(match best {
                    Some(b) if b <= t => b,
                    _ => t,
                });
            }
        }
        best
    }

    fn bounds(&self) -> Rect {
        self.bounds
    }

    fn margin(&self) -> Insets {
        self.base.margin
    }
    fn h_anchor(&self) -> HAnchor {
        self.base.h_anchor
    }
    fn v_anchor(&self) -> VAnchor {
        self.base.v_anchor
    }
    fn min_size(&self) -> Size {
        self.base.min_size
    }
    fn max_size(&self) -> Size {
        self.base.max_size
    }

    fn properties(&self) -> Vec<(&'static str, String)> {
        vec![
            (
                "backbuffer_kind",
                if self.use_gl_backbuffer {
                    "GlFbo".to_string()
                } else {
                    "None".to_string()
                },
            ),
            ("backbuffer_dirty", self.backbuffer.dirty.to_string()),
            (
                "backbuffer_repaints",
                self.backbuffer.repaint_count.to_string(),
            ),
            (
                "backbuffer_composites",
                self.backbuffer.composite_count.to_string(),
            ),
            (
                "backbuffer_size",
                format!("{}x{}", self.backbuffer.width, self.backbuffer.height),
            ),
        ]
    }

    /// Pop this window to the top of the parent `Stack` when the
    /// false→true visibility edge fires (see `layout`).
    fn take_raise_request(&mut self) -> bool {
        let pending = self.raise_request.get();
        self.raise_request.set(false);
        pending
    }

    fn set_bounds(&mut self, b: Rect) {
        if let Some(ref cell) = self.reset_to {
            if let Some(new_b) = cell.get() {
                self.bounds = new_b;
                self.pre_collapse_h = new_b.height;
                self.collapsed = false;
                cell.set(None);
                return;
            }
        }
        if self.bounds.width == 0.0 || self.bounds.height == 0.0 {
            self.bounds = b;
            self.pre_collapse_h = b.height;
        }
    }

    fn children(&self) -> &[Box<dyn Widget>] {
        &self.children
    }
    fn children_mut(&mut self) -> &mut Vec<Box<dyn Widget>> {
        &mut self.children
    }

    fn backbuffer_spec(&mut self) -> BackbufferSpec {
        if !self.use_gl_backbuffer {
            return BackbufferSpec::none();
        }
        if !self.is_visible() {
            let alpha = self.visibility_anim.value();
            if self.requested_visible() || alpha <= 0.001 {
                return BackbufferSpec::none();
            }
        }

        let requested_visible = self.requested_visible();
        self.visibility_anim
            .set_target(if requested_visible { 1.0 } else { 0.0 });
        let alpha = self.visibility_anim.tick();
        if !requested_visible && alpha > 0.001 {
            self.fade_out_active.set(true);
        }
        if !requested_visible && alpha <= 0.001 {
            self.fade_out_active.set(false);
        }

        let (outset_left, outset_bottom, outset_right, outset_top) = Self::layer_outsets();
        BackbufferSpec {
            kind: BackbufferKind::GlFbo,
            cached: true,
            alpha,
            outsets: Insets {
                left: outset_left,
                right: outset_right,
                top: outset_top,
                bottom: outset_bottom,
            },
            rounded_clip: Some(CORNER_R),
        }
    }

    fn backbuffer_state_mut(&mut self) -> Option<&mut BackbufferState> {
        Some(&mut self.backbuffer)
    }

    /// Clip child painting to the content area (below the title bar).
    /// When collapsed bounds.height == TITLE_H so the content rect has zero height,
    /// preventing any child from drawing outside the visible title-bar strip.
    fn clip_children_rect(&self) -> Option<(f64, f64, f64, f64)> {
        if !self.is_visible() {
            return None;
        }
        let w = self.bounds.width;
        let content_h = (self.bounds.height - TITLE_H).max(0.0);
        // Clip to content area: y=0 (bottom) up to content_h, full width.
        Some((0.0, 0.0, w, content_h))
    }

    fn hit_test(&self, local_pos: Point) -> bool {
        if !self.requested_visible() {
            return false;
        }
        if self.drag_mode != DragMode::None {
            return true;
        }
        let b = self.bounds();
        local_pos.x >= 0.0
            && local_pos.x <= b.width
            && local_pos.y >= 0.0
            && local_pos.y <= b.height
    }

    fn claims_pointer_exclusively(&self, local_pos: Point) -> bool {
        self.requested_visible()
            && (self.drag_mode != DragMode::None || self.resize_dir(local_pos).is_some())
    }

    fn layout(&mut self, available: Size) -> Size {
        // Rising-edge visibility detection → request parent raise.  The
        // sidebar toggles `visible_cell`; we observe the transition here
        // and set `raise_request`, which the parent `Stack` drains on its
        // next layout (one-frame delay, invisible to the user).
        let now_visible = self.requested_visible();
        if now_visible && !self.last_visible.get() {
            self.raise_request.set(true);
            if let Some(cb) = self.on_raised.as_mut() {
                cb(&self.title);
            }
            // Un-maximize on reopen.  Clicking a sidebar checkbox is "open
            // this window for use" — the user expects the window to come
            // up at its normal size, not still stretched to fill the canvas
            // from the last session's maximise.  Restore `pre_maximize_bounds`
            // which `toggle_maximize` saved when the user maximised.
            if self.maximized {
                self.bounds = self.pre_maximize_bounds;
                self.maximized = false;
            }
            self.fit_fully_to_canvas(available);
        }
        if now_visible {
            self.fade_out_active.set(false);
            self.visibility_anim.set_target(1.0);
        } else {
            self.visibility_anim.set_target(0.0);
            if self.visibility_anim.tick() <= 0.001 {
                self.fade_out_active.set(false);
            }
        }
        self.last_visible.set(now_visible);

        if !self.is_visible() {
            return Size::new(self.bounds.width, self.bounds.height);
        }

        if self.maximized && available.width > 0.0 && available.height > 0.0 {
            self.bounds = snap(Rect::new(0.0, 0.0, available.width, available.height));
            self.pre_collapse_h = self.bounds.height;
        }

        // Auto-size: measure the child's preferred size, then adopt it as the
        // new window size (pinning the top edge — Y-up → adjust `bounds.y` so
        // the title bar stays put when the height changes).  Skip while
        // collapsed: the user toggled a fixed TITLE_H height.
        //
        // We cap the measurement request by `child.max_size()` when finite
        // (otherwise by the canvas size): flex containers return their given
        // `available.width` rather than an intrinsic natural width, so without
        // a cap we'd produce an infinite/canvas-wide window.  Callers wanting
        // a content-fitted window set `with_max_size(Size::new(w, f64::MAX))`
        // on their root widget.
        if self.auto_size && !self.collapsed && !self.maximized {
            if let Some(child) = self.children.first_mut() {
                let max_sz = child.max_size();
                // `Size::MAX` uses `f64::MAX / 2.0` as its sentinel so
                // widgets can add-without-overflow (see `geometry.rs`).
                // That value is *technically* finite, so a plain
                // `.is_finite()` check wrongly treats it as a real cap
                // and cascades an ~`f64::MAX/2` width down to wrapped
                // Labels, whose bounds then blow up LCD-backbuffer
                // allocators to hundreds of GB.  Guard with a sane
                // threshold: anything ≥ `CAP_SENTINEL` means "no cap,
                // fall back to viewport-provided bounds".
                const CAP_SENTINEL: f64 = 1.0e18;
                // WIDTH is PINNED to the current bounds.width (seeded
                // by `with_bounds` and preserved across frames).
                // Why: wrapping Labels inside the content claim their
                // full available width — if we pass the viewport
                // width here, the window grows to the canvas on the
                // first frame and never shrinks back.  egui's
                // equivalent is `default_width`, which also pins.
                let cap_w = self.bounds.width.max(MIN_W);
                let cap_h = if max_sz.height.is_finite() && max_sz.height < CAP_SENTINEL {
                    max_sz.height
                } else {
                    available.height.max(MIN_H)
                };
                let pref = child.layout(Size::new(cap_w, cap_h));
                // Auto-size follows content in BOTH directions — so
                // the window can also shrink back down when the
                // inner Resize (or any other sizing widget) narrows.
                // Lower bound: `MIN_W`.  Upper bound: the parent-
                // provided `available.width` (main_area / canvas).
                // Matches egui where auto_sized tracks content size
                // symmetrically.
                let new_w = pref.width.max(MIN_W).min(available.width.max(MIN_W));
                let new_h = (pref.height + TITLE_H).min(cap_h + TITLE_H).max(MIN_H);
                let top = self.bounds.y + self.bounds.height;
                self.bounds.width = new_w;
                self.bounds.height = new_h;
                self.bounds.y = top - new_h;
                self.pre_collapse_h = new_h;
            }
        }

        // ── Tight-fit pre-pass ───────────────────────────────────
        //
        // When `with_tight_content_fit(true)` is set (and we're not
        // already in the auto_size block above, which handles both
        // axes), ask the content tree what minimum height it needs
        // at our current width and SNAP `bounds.height` to that.
        //
        // Uses `Widget::measure_min_height` rather than `layout` so
        // the result is independent of flex distribution — a
        // flex-fill widget like `TextArea` reports its true wrapped-
        // content height through `measure_min_height` even though
        // its `layout` returns the full slot.  This is what makes
        // egui's "no scroll, no clip, no whitespace" contract work
        // for windows whose content includes a flex-fill child.
        if self.tight_content_fit && !self.auto_size && !self.collapsed && !self.maximized {
            if let Some(child) = self.children.first() {
                let needed = child.measure_min_height(self.bounds.width);
                let new_h = (needed + TITLE_H).max(MIN_H);
                let top = self.bounds.y + self.bounds.height;
                self.bounds.height = new_h;
                self.bounds.y = top - new_h;
                self.last_content_natural_h.set(needed);
            }
        }

        // When collapsed, bounds.height == TITLE_H (set during toggle).
        let content_h = (self.bounds.height - TITLE_H).max(0.0);

        if let Some(child) = self.children.first_mut() {
            if !self.collapsed {
                let desired = child.layout(Size::new(self.bounds.width, content_h));
                let child_h = if child.v_anchor().is_stretch() {
                    content_h
                } else {
                    desired.height.clamp(
                        child.min_size().height,
                        child.max_size().height.min(content_h),
                    )
                };
                let child_y = if child.v_anchor().contains(VAnchor::BOTTOM) {
                    0.0
                } else if child.v_anchor().contains(VAnchor::CENTER) {
                    ((content_h - child_h) * 0.5).max(0.0)
                } else {
                    (content_h - child_h).max(0.0)
                };
                if (child_h - content_h).abs() > f64::EPSILON {
                    child.layout(Size::new(self.bounds.width, child_h));
                }
                child.set_bounds(Rect::new(0.0, child_y, self.bounds.width, child_h));
            }
            // When collapsed the child keeps its last bounds but is not visible
            // because hit_test returns false for the content area.
        }

        // Cache the child's required height via `measure_min_height`
        // so `apply_resize` and the tight-fit floor see a current
        // value EVEN when the content's `layout` returns the slot
        // size (the flex-fill case).  `Widget::measure_min_height`
        // walks the content tree and returns the actual content
        // requirement at the supplied width.
        if (self.tight_content_fit || self.floor_content_height) && !self.collapsed {
            if let Some(child) = self.children.first() {
                self.last_content_natural_h
                    .set(child.measure_min_height(self.bounds.width));
            }
        }

        // Position the title-bar strip at the top of the window and
        // give it a layout pass so the title label knows its size.
        let tb_y = self.bounds.height - TITLE_H;
        self.title_bar
            .set_bounds(Rect::new(0.0, tb_y, self.bounds.width, TITLE_H));
        self.title_bar.layout(Size::new(self.bounds.width, TITLE_H));

        // Record the canvas size — used by drag / resize / collapse clamp
        // paths that fire on USER ACTION.  We deliberately do NOT clamp
        // passively at layout time: platforms fire a Resized event with a
        // transient smaller size during fullscreen/maximize EXIT (Windows
        // notably), and if we clamped on shrink the auto-save would persist
        // those transient clamped bounds — the "all windows pushed down to
        // the same Y on next startup" bug.  Clamping only on user actions
        // (dragging a window, resize-handle, collapse toggle) keeps saved
        // state pinned to what the user actually chose.
        //
        // If a later OS shrink genuinely leaves a window's title bar out of
        // reach, the user can drag it back, use "Organize windows" to
        // retile, or a dedicated "reset positions" command.
        self.canvas_size = available;
        if let Some(ref cell) = self.position_cell {
            // When maximised, persist the UNDERLYING pre-maximise bounds,
            // not the stretched-to-canvas ones.  The maximized flag itself is
            // persisted separately so reloads restore the interaction state
            // without losing the user's last normal-size bounds.
            let save_bounds = if self.maximized {
                self.pre_maximize_bounds
            } else {
                self.bounds
            };
            cell.set(save_bounds);
        }
        if let Some(ref cell) = self.maximized_cell {
            cell.set(self.maximized);
        }

        Size::new(self.bounds.width, self.bounds.height)
    }

    fn paint(&mut self, ctx: &mut dyn DrawCtx) {
        if !self.is_visible() {
            return;
        }

        let v = ctx.visuals();
        let w = self.bounds.width;
        // bounds.height == TITLE_H when collapsed (adjusted on toggle).
        let h = self.bounds.height;

        // Drop shadow — stacked rounded rects approximating a Gaussian blur.
        // Outer layers inflate outward and fade with a (1−t)² falloff; drawn
        // outside-in so the denser core overlays the softer halo.
        let base = v.window_shadow;
        for i in (0..SHADOW_STEPS).rev() {
            let t = i as f64 / SHADOW_STEPS as f64;
            let infl = t * SHADOW_BLUR;
            let falloff = (1.0 - t).powi(2) as f32;
            let alpha = base.a * falloff / SHADOW_STEPS as f32 * 6.0;
            ctx.set_fill_color(Color::rgba(base.r, base.g, base.b, alpha));
            ctx.begin_path();
            ctx.rounded_rect(
                SHADOW_DX - infl,
                -SHADOW_DY - infl,
                w + 2.0 * infl,
                h + 2.0 * infl,
                CORNER_R + infl,
            );
            ctx.fill();
        }

        self.foreground_layer_active.set(false);
        if ctx.supports_compositing_layers() {
            ctx.push_layer(w, h);
            self.foreground_layer_active.set(true);
        }

        // Window body. Expanded windows leave the top strip to `WindowTitleBar`
        // so the top corner alpha comes from one shape, not overlapping fills.
        let content_h = (h - TITLE_H).max(0.0);
        if content_h > 0.0 {
            ctx.set_fill_color(v.window_fill);
            ctx.begin_path();
            ctx.rounded_rect(0.0, 0.0, w, content_h, CORNER_R);
            ctx.rect(
                0.0,
                (content_h - CORNER_R).max(0.0),
                w,
                CORNER_R.min(content_h),
            );
            ctx.fill();
        }

        ctx.set_layer_rounded_clip(0.0, 0.0, w, h, CORNER_R);

        // Sync the title-bar sub-widget's display state for this frame
        // and paint it.  Positioning was done in `layout`; we just need
        // to hand it the per-frame interaction snapshot and dispatch
        // through `paint_subtree` so the ancestor-chain stack gets the
        // WindowTitleBar entry (background_color = window_title_fill).
        {
            let mut st = self.title_state.borrow_mut();
            st.bar_color = if self.drag_mode == DragMode::Move {
                v.window_title_fill_drag
            } else {
                v.window_title_fill
            };
            st.title_color = v.window_title_text;
            st.collapsed = self.collapsed;
            st.maximized = self.maximized;
            st.close_hovered = self.close_hovered;
            st.maximize_hovered = self.maximize_hovered;
        }
        let tb_bounds = self.title_bar.bounds();
        ctx.save();
        ctx.translate(tb_bounds.x, tb_bounds.y);
        paint_subtree(&mut self.title_bar, ctx);
        ctx.restore();

        // Outer border — on top of the title bar so the rounded corners
        // cleanly frame both body and title region.
        ctx.set_fill_color(v.window_fill); // restore default fill — stroke follows
        ctx.set_stroke_color(v.window_stroke);
        ctx.set_line_width(1.0);
        ctx.begin_path();
        ctx.rounded_rect(0.5, 0.5, (w - 1.0).max(0.0), (h - 1.0).max(0.0), CORNER_R);
        ctx.stroke();
    }

    // paint_overlay: draws the resize handle dots + edge highlights on top of content.
    fn paint_overlay(&mut self, ctx: &mut dyn DrawCtx) {
        if !self.is_visible() || self.collapsed {
            return;
        }
        // Skip all resize-related chrome when the window can't be resized,
        // so an auto-sized or `.resizable(false)` window doesn't look
        // deceptively interactive.
        if !self.resizable || self.auto_size {
            return;
        }
        let v = ctx.visuals();
        let w = self.bounds.width;
        let h = self.bounds.height;

        // ── SE corner drag grip (3 diagonal lines, egui-style) ───────────────
        // Only shown when both axes are resizable; for uni-axis resizable
        // windows the SE grip would suggest a capability that isn't there.
        if self.resizable_h && self.resizable_v {
            let is_se_active = matches!(self.drag_mode, DragMode::Resize(ResizeDir::SE));
            let is_se_hover = self.hover_dir == Some(ResizeDir::SE);
            let grip_color = if is_se_active {
                v.window_resize_active
            } else if is_se_hover {
                v.window_resize_hover
            } else {
                v.window_stroke
            };
            ctx.set_stroke_color(grip_color);
            ctx.set_line_width(1.5);
            let m = 3.0_f64; // margin from corner edge
            for i in 1..=3_i32 {
                let off = i as f64 * 4.0 + m;
                ctx.begin_path();
                ctx.move_to(w - off, m);
                ctx.line_to(w - m, off);
                ctx.stroke();
            }
        }

        // ── Resize edge / corner highlight ────────────────────────────────────
        // Determine the highlighted direction and whether it is actively dragging.
        let (highlight, is_active) = match self.drag_mode {
            DragMode::Resize(d) => (Some(d), true),
            DragMode::Move => (None, false), // no edge highlight while moving
            DragMode::None => (self.hover_dir, false),
        };
        let dir = match highlight {
            Some(d) => d,
            None => return,
        };

        let color = if is_active {
            v.window_resize_active
        } else {
            v.window_resize_hover
        };
        ctx.set_stroke_color(color);
        ctx.set_line_width(2.0);

        // Which edges to highlight (derived from direction).
        let (top, bottom, left, right) = match dir {
            ResizeDir::N => (true, false, false, false),
            ResizeDir::S => (false, true, false, false),
            ResizeDir::E => (false, false, false, true),
            ResizeDir::W => (false, false, true, false),
            ResizeDir::NE => (true, false, false, true),
            ResizeDir::NW => (true, false, true, false),
            ResizeDir::SE => (false, true, false, true),
            ResizeDir::SW => (false, true, true, false),
        };

        // Segments run between the rounded-corner tangent points.
        let cr = CORNER_R;
        if top {
            ctx.begin_path();
            ctx.move_to(cr, h);
            ctx.line_to(w - cr, h);
            ctx.stroke();
        }
        if bottom {
            ctx.begin_path();
            ctx.move_to(cr, 0.0);
            ctx.line_to(w - cr, 0.0);
            ctx.stroke();
        }
        if left {
            ctx.begin_path();
            ctx.move_to(0.0, cr);
            ctx.line_to(0.0, h - cr);
            ctx.stroke();
        }
        if right {
            ctx.begin_path();
            ctx.move_to(w, cr);
            ctx.line_to(w, h - cr);
            ctx.stroke();
        }
    }

    fn finish_paint(&mut self, ctx: &mut dyn DrawCtx) {
        if self.foreground_layer_active.replace(false) {
            ctx.pop_layer();
        }
    }

    fn on_event(&mut self, event: &Event) -> EventResult {
        if !self.requested_visible() {
            return EventResult::Ignored;
        }

        match event {
            Event::MouseMove { pos } => {
                let was_close = self.close_hovered;
                let was_max = self.maximize_hovered;
                let was_dir = self.hover_dir;
                self.close_hovered = self.in_close_button(*pos);
                self.maximize_hovered = self.in_maximize_button(*pos);

                match self.drag_mode {
                    DragMode::Move => {
                        let world = Point::new(pos.x + self.bounds.x, pos.y + self.bounds.y);
                        let dx = world.x - self.drag_start_world.x;
                        let dy = world.y - self.drag_start_world.y;
                        self.bounds.x = (self.drag_start_bounds.x + dx).round();
                        self.bounds.y = (self.drag_start_bounds.y + dy).round();
                        self.clamp_to_canvas();
                        self.hover_dir = None;
                        set_cursor_icon(CursorIcon::Grabbing);
                        crate::animation::request_draw_without_invalidation();
                        return EventResult::Ignored;
                    }
                    DragMode::Resize(dir) => {
                        let world = Point::new(pos.x + self.bounds.x, pos.y + self.bounds.y);
                        self.apply_resize(world);
                        set_cursor_icon(resize_cursor(dir));
                        crate::animation::request_draw();
                        return EventResult::Consumed;
                    }
                    DragMode::None => {
                        // Track which edge/corner the cursor is hovering over so
                        // paint_overlay can draw the appropriate highlight.
                        self.hover_dir = self.resize_dir(*pos);
                        if let Some(dir) = self.hover_dir {
                            set_cursor_icon(resize_cursor(dir));
                        }
                    }
                }
                if was_close != self.close_hovered
                    || was_max != self.maximize_hovered
                    || was_dir != self.hover_dir
                {
                    crate::animation::request_draw();
                }
                EventResult::Ignored
            }

            Event::MouseDown { button, pos, .. }
                if matches!(*button, MouseButton::Left | MouseButton::Middle) =>
            {
                let is_left_click = *button == MouseButton::Left;
                // Press-to-raise — any direct press that reaches this Window
                // (hit-test routed it here in reverse paint order, so we
                // ARE the topmost widget under the cursor in the stack
                // sense) requests a raise.  Classic window-manager
                // behaviour: clicking anywhere on a window pops it to the
                // top of the z-order.  Consumed by `Stack::layout` on the
                // next frame via `take_raise_request`; one-frame visual
                // delay is invisible in practice.
                self.raise_request.set(true);
                // Z-order changes are visible; repaint.
                crate::animation::request_draw();
                if let Some(cb) = self.on_raised.as_mut() {
                    cb(&self.title);
                }

                // Close button — highest priority.
                if is_left_click && self.in_close_button(*pos) {
                    self.visible = false;
                    self.visibility_anim.set_target(0.0);
                    if let Some(ref cell) = self.visible_cell {
                        cell.set(false);
                    }
                    if let Some(cb) = self.on_close.as_mut() {
                        cb();
                    }
                    crate::animation::request_draw();
                    return EventResult::Consumed;
                }

                // Maximize / Restore button.
                if is_left_click && self.in_maximize_button(*pos) {
                    self.toggle_maximize();
                    crate::animation::request_draw();
                    return EventResult::Consumed;
                }

                // Collapse / expand chevron.
                if is_left_click && self.in_chevron_button(*pos) {
                    self.toggle_collapse();
                    // Null out the double-click timer so clicking the
                    // chevron then quickly clicking the bar doesn't
                    // trigger a maximize toggle.
                    self.last_title_click = None;
                    crate::animation::request_draw();
                    return EventResult::Consumed;
                }

                // Resize edge — check before title bar to handle corner overlap.
                if let Some(dir) = self.resize_dir(*pos) {
                    // Only start resize if not in the close button area and not a pure title bar drag.
                    // The N edge overlaps the title bar — prefer resize over drag from the top N px.
                    let world = Point::new(pos.x + self.bounds.x, pos.y + self.bounds.y);
                    self.drag_mode = DragMode::Resize(dir);
                    self.drag_start_world = world;
                    self.drag_start_bounds = self.bounds;
                    return EventResult::Consumed;
                }

                // Title bar drag + double-click maximize.
                if self.in_title_bar(*pos) {
                    // Double-click detection.
                    let is_double = if is_left_click {
                        let now = Instant::now();
                        self.last_title_click
                            .map(|t| now.duration_since(t).as_millis() < DBL_CLICK_MS)
                            .unwrap_or(false)
                    } else {
                        false
                    };

                    if is_double {
                        // Windows convention: double-click title bar toggles
                        // maximize / restore.  Collapse/expand lives on the
                        // chevron button to the left.
                        self.toggle_maximize();
                        self.last_title_click = None;
                        crate::animation::request_draw();
                    } else {
                        if is_left_click {
                            self.last_title_click = Some(Instant::now());
                        }
                        let world = Point::new(pos.x + self.bounds.x, pos.y + self.bounds.y);
                        self.drag_mode = DragMode::Move;
                        self.drag_start_world = world;
                        self.drag_start_bounds = self.bounds;
                    }
                    return EventResult::Consumed;
                }

                // Click on content area: consume so it doesn't fall through.
                if is_left_click && !self.collapsed {
                    EventResult::Consumed
                } else {
                    EventResult::Ignored
                }
            }

            Event::MouseUp {
                button: MouseButton::Left | MouseButton::Middle,
                ..
            } => {
                let was_dragging = self.drag_mode != DragMode::None;
                self.drag_mode = DragMode::None;
                if was_dragging {
                    crate::animation::request_draw();
                    EventResult::Consumed
                } else {
                    EventResult::Ignored
                }
            }

            _ => EventResult::Ignored,
        }
    }
}