rust_widgets 2.5.2

Pure Rust cross-platform native GUI library with hardware-adaptive rendering, 180 widgets, touch/gesture support, i18n, and SVG-pipeline-accurate output
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
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT

//! ModalBottomSheet widget — Material-style modal bottom sheet with drag-to-dismiss.
//!
//! Displays a semi-transparent overlay behind a rounded top sheet containing a
//! drag handle, title, and optional content. Supports show/hide, drag-to-dismiss,
//! and overlay-click-to-dismiss. Emits a `dismissed` signal when closed.

use crate::core::{Color, Font, HorizontalAlignment, Point, Rect, Size};
use crate::event::{Event, EventHandler};
use crate::render::RenderContext;
use crate::signal::GenericSignal;
use crate::widget::capability::coercion::expect_bool;
use crate::widget::capability::properties_trait::{base_property_get, base_property_set};
use crate::widget::capability::types::{CapabilityAccessError, CapabilityValue};
use crate::widget::capability::WidgetProperties;
use crate::widget::{BaseWidget, Draw, Widget, WidgetKind};
use crate::{impl_widget_property_hooks, property_names_of};

/// Material-style modal bottom sheet widget.
///
/// When visible, a semi-transparent overlay covers the widget geometry and a
/// rounded sheet appears at the bottom with a drag handle, title, and optional
/// child content. The user can tap the overlay or drag downward to dismiss.
pub struct ModalBottomSheet {
    base: BaseWidget,
    title: String,
    content: Option<Box<dyn Widget>>,
    is_visible: bool,
    drag_offset: f32,
    is_dragging: bool,
    /// Pointer `y` where the current drag began, in the same space `Event` carries.
    ///
    /// Kept so `drag_to` can turn an absolute position into the delta `drag_offset`
    /// accumulates from. `None` outside a drag.
    drag_origin_y: Option<i32>,
    /// Emitted when the sheet is dismissed by user interaction.
    pub dismissed: GenericSignal,
}

impl ModalBottomSheet {
    /// Creates a new ModalBottomSheet widget.
    pub fn new(geometry: Rect) -> Self {
        Self {
            base: BaseWidget::new(WidgetKind::ModalBottomSheet, geometry, "ModalBottomSheet"),
            title: String::new(),
            content: None,
            is_visible: false,
            drag_offset: 0.0,
            is_dragging: false,
            drag_origin_y: None,
            dismissed: GenericSignal::new(),
        }
    }

    /// Shows the bottom sheet.
    pub fn show(&mut self) {
        if !self.is_visible {
            self.is_visible = true;
            self.drag_offset = 0.0;
            self.is_dragging = false;
            self.base.request_redraw();
        }
    }

    /// Hides the bottom sheet without emitting the dismissed signal.
    pub fn hide(&mut self) {
        if self.is_visible {
            self.is_visible = false;
            self.drag_offset = 0.0;
            self.is_dragging = false;
            self.base.request_redraw();
        }
    }

    /// Returns whether the sheet is currently visible.
    pub fn is_visible(&self) -> bool {
        self.is_visible
    }

    /// Reports the sheet's own visibility flag.
    ///
    /// Deliberately distinct from [`Widget::is_visible`], which this widget
    /// overrides to return the same flag. Keeping the two entry points separate
    /// lets the property contract call the inherent one unambiguously.
    pub fn is_sheet_visible(&self) -> bool {
        self.is_visible
    }

    /// Sets the sheet title text.
    pub fn set_title(&mut self, title: impl Into<String>) {
        self.title = title.into();
        self.base.request_redraw();
    }

    /// Returns the sheet title.
    pub fn title(&self) -> &str {
        &self.title
    }

    /// Sets the child content widget displayed in the sheet body.
    pub fn set_content(&mut self, widget: Box<dyn Widget>) {
        self.content = Some(widget);
        self.base.request_redraw();
    }

    /// Returns a reference to the child content, if any.
    pub fn content(&self) -> Option<&dyn Widget> {
        self.content.as_deref()
    }

    /// Returns a mutable reference to the child content, if any.
    pub fn content_mut(&mut self) -> Option<&mut dyn Widget> {
        self.content.as_deref_mut()
    }

    /// Returns the current drag offset.
    pub fn drag_offset(&self) -> f32 {
        self.drag_offset
    }

    /// Returns whether the user is currently dragging the sheet.
    pub fn is_dragging(&self) -> bool {
        self.is_dragging
    }

    /// Called when the user starts dragging the sheet.
    pub fn start_drag(&mut self) {
        if self.is_visible {
            self.is_dragging = true;
            self.base.request_redraw();
        }
    }

    /// Called to update the drag offset.
    /// Positive values indicate dragging downward.
    pub fn update_drag(&mut self, delta: f32) {
        if self.is_dragging {
            self.drag_offset = (self.drag_offset + delta).max(0.0);
            self.base.request_redraw();
        }
    }

    /// Tracks a pointer position during a drag, relative to where the press began.
    ///
    /// This is what the `MouseMove` event arm uses. `update_drag` takes a *delta*,
    /// but an event carries an absolute position, so the press origin has to be
    /// remembered to derive it. Without that the `MouseMove` arm had nothing to
    /// pass, `drag_offset` stayed `0.0`, and `end_drag` could never cross the
    /// one-third threshold — so drag-to-dismiss was unreachable through the event
    /// API even though the methods that implement it were correct and tested.
    ///
    /// The offset is recomputed from the origin rather than accumulated, so moving the
    /// pointer back up restores the original position exactly.
    pub fn drag_to(&mut self, y: i32) {
        let Some(origin_y) = self.drag_origin_y else { return };
        if !self.is_dragging {
            return;
        }
        self.drag_offset = (y - origin_y).max(0) as f32;
        self.base.request_redraw();
    }

    /// Ends a drag, dismissing the sheet when it was pulled far enough.
    ///
    /// Dismisses the sheet if drag offset exceeds one third of the sheet height.
    pub fn end_drag(&mut self) {
        if self.is_dragging {
            self.is_dragging = false;
            self.drag_origin_y = None;
            let sheet_height = self.compute_sheet_height();
            if self.drag_offset > sheet_height as f32 / 3.0 {
                self.is_visible = false;
                self.dismissed.emit();
            }
            self.drag_offset = 0.0;
            self.base.request_redraw();
        }
    }

    /// Cancels a drag without dismissing, restoring the sheet's resting position.
    ///
    /// Used when the pointer leaves the sheet mid-drag: a release outside the
    /// control is never delivered, so without this the sheet would stay in a
    /// half-dragged state with `is_dragging` set.
    pub fn cancel_drag(&mut self) {
        if self.is_dragging {
            self.is_dragging = false;
            self.drag_origin_y = None;
            self.drag_offset = 0.0;
            self.base.request_redraw();
        }
    }

    /// Computes the approximate sheet panel height based on geometry and content.
    fn compute_sheet_height(&self) -> u32 {
        let rect = self.geometry();
        let title_height: u32 = 40;
        let handle_area: u32 = 24;
        let min_sheet = 120;
        let max_sheet = rect.height.saturating_sub(40);
        (title_height + handle_area + 80).min(max_sheet).max(min_sheet)
    }
}

impl Widget for ModalBottomSheet {
    fn base(&self) -> &BaseWidget {
        &self.base
    }
    fn base_mut(&mut self) -> &mut BaseWidget {
        &mut self.base
    }

    fn size_hint(&self) -> Size {
        crate::core::Size::new(300, 200)
    }
    impl_draw_bridge!();
    impl_widget_property_hooks!();
}

/// `ModalBottomSheet`'s property contract.
///
/// Read/write semantics are carried over unchanged from the centralised
/// `access_read_dialog.in.rs` / `access_write_dialog.in.rs` dispatch, so callers see
/// the same coercions and the same errors as before. `visible` is served by the
/// inherent accessor rather than the base fallthrough, so a modal sheet's own
/// show/hide state is what the name reports.
impl WidgetProperties for ModalBottomSheet {
    fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
        match name {
            "visible" => Ok(CapabilityValue::Bool(self.is_sheet_visible())),
            _ => base_property_get(self, name),
        }
    }

    fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
        match name {
            "visible" => {
                if expect_bool(value)? {
                    self.show();
                } else {
                    self.hide();
                }
                Ok(())
            }
            _ => base_property_set(self, name, value),
        }
    }

    fn property_names(&self) -> &'static [&'static str] {
        property_names_of!["visible", BASE_PROPERTY_NAMES]
    }

    /// Runs one of the commands `modal_bottom_sheet` publishes.
    ///
    /// `show` and `dismiss` are payload-free and map onto the widget's real
    /// methods. `dismiss` takes the same path every other close route takes —
    /// [`ModalBottomSheet::hide`], which the property route's `visible = false`
    /// and the overlay click also use — so the sheet ends up with no more than one
    /// way to become hidden. `set_visible` carries the boolean the property route
    /// already accepts, so a bare invocation is reported as needing one rather than
    /// being called unknown.
    fn command(&mut self, name: &str) -> Result<(), CapabilityAccessError> {
        match name {
            "show" => {
                self.show();
                Ok(())
            }
            "dismiss" => {
                self.hide();
                Ok(())
            }
            "set_visible" => Err(CapabilityAccessError::OutOfRange),
            _ => Err(CapabilityAccessError::UnknownCommand),
        }
    }
}

impl Draw for ModalBottomSheet {
    fn draw(&mut self, context: &mut RenderContext) {
        let rect = self.geometry();
        if rect.width == 0 || rect.height == 0 {
            return;
        }

        // Chrome colours resolve explicit style first, then the theme's resolved style for
        // this control, and only then a literal. Every colour below used to be a literal —
        // and the whole sheet used to be skipped unless it was already showing — so the
        // census reported `ink = 0` *and* no response to a light/dark switch.
        //
        // The theme reads take and release the global manager's lock internally, so no
        // guard is held across the draw (the mutex is not re-entrant).
        let style = self.base.style().clone();
        let theme = crate::style::resolved_theme_style("modal_bottom_sheet");
        // `modal_bottom_sheet` is absent from `WidgetRole::for_kind_name`'s table, so it
        // classifies as `Surface` and resolves to `theme.colors.background` — the window's
        // own fill. A pane painted in that colour would be byte-identical to the dark scrim
        // over the frame behind it, so a resolved surface equal to the window fill is
        // re-derived a visible step away from it, the same distinction
        // `Colors::input_background` draws for a field.
        let window_fill = {
            let manager = crate::style::theme_manager();
            manager.current_theme().map(|active| active.colors.background).unwrap_or(Color::WHITE)
        };
        let ink = style
            .text_color
            .or_else(|| theme.as_ref().and_then(|t| t.text_color))
            .unwrap_or(Color::rgb(40, 40, 40));
        let sheet_fill = match style
            .background_color
            .or_else(|| theme.as_ref().and_then(|t| t.background_color))
        {
            Some(resolved) if resolved != window_fill => resolved,
            _ => window_fill.blend(&ink, 0.08),
        };
        let border = style
            .border_color
            .or_else(|| theme.as_ref().and_then(|t| t.border_color))
            .filter(|resolved| *resolved != sheet_fill)
            .unwrap_or_else(|| sheet_fill.blend(&ink, 0.35));

        let sheet_height = self.compute_sheet_height();
        let drag_offset_px = self.drag_offset as i32;

        // The scrim is drawn in both states. It used to be part of the visible-only body, so
        // a freshly constructed sheet painted nothing at all; keeping it unconditional gives
        // the control a rendered extent at rest while leaving the sheet itself the opaque
        // element of the open state.
        let scrim = ink.blend(&sheet_fill, 0.55);
        let overlay_rect = Rect::new(rect.x, rect.y, rect.width, rect.height);
        context.fill_rect(overlay_rect, scrim);

        // A hidden sheet paints only the scrim; the panel, its handle and its title belong to
        // the open state alone.
        if !self.is_visible {
            return;
        }

        // 2. Sheet panel at the bottom, shifted by drag offset
        let sheet_y = rect.y + rect.height as i32 - sheet_height as i32 + drag_offset_px;
        let sheet_rect_panel = Rect::new(rect.x, sheet_y, rect.width, sheet_height);
        let corner_radius: u32 = 16;

        context.fill_rounded_rect(sheet_rect_panel, corner_radius, sheet_fill);
        context.draw_rounded_rect_stroke(sheet_rect_panel, corner_radius, border, 1);

        // 3. Drag handle
        let handle_width: u32 = 36;
        let handle_height: u32 = 5;
        let handle_x = rect.x + (rect.width as i32 - handle_width as i32) / 2;
        let handle_y = sheet_y + 10;
        let handle_rect = Rect::new(handle_x, handle_y, handle_width, handle_height);
        context.fill_rounded_rect(handle_rect, handle_height / 2, ink.blend(&sheet_fill, 0.35));

        // 4. Title
        let title_y = handle_y + handle_height as i32 + 12;
        let title_font = Font::simple("sans-serif", 16.0);
        let title_metrics = context.measure_text(&self.title, &title_font);
        if !self.title.is_empty() {
            let title_x = rect.x + (rect.width as i32 - title_metrics.width as i32) / 2;
            context.draw_text(
                Point::new(title_x.max(rect.x), title_y + title_metrics.ascent as i32),
                &self.title,
                &title_font,
                ink,
                HorizontalAlignment::Left,
            );
        }

        // 5. Content area (child widget rendering is delegated)
        let content_top = title_y + title_metrics.height as i32 + 8;
        let content_bottom = rect.y + rect.height as i32 + drag_offset_px;
        let content_available = (content_bottom - content_top) as u32;

        if content_available > 20 {
            let content_rect = Rect::new(
                rect.x + 8,
                content_top,
                rect.width.saturating_sub(16),
                content_available,
            );
            // The content well is one step away from the sheet it sits in, so the two read as
            // separate regions in either appearance.
            context.fill_rect(content_rect, sheet_fill.blend(&ink, 0.04));
        }
    }
}

impl EventHandler for ModalBottomSheet {
    fn handle_event(&mut self, event: &Event) {
        if !self.base.is_enabled() || !self.is_visible {
            return;
        }

        match event {
            Event::MousePress { pos, button } => {
                if *button == 1 {
                    let rect = self.geometry();
                    let sheet_height = self.compute_sheet_height();
                    let drag_offset_px = self.drag_offset as i32;
                    let sheet_y =
                        rect.y + rect.height as i32 - sheet_height as i32 + drag_offset_px;

                    // Check if click is in the sheet area (including drag handle area)
                    let in_sheet = pos.y >= sheet_y;

                    if in_sheet {
                        // Start a drag, recording where the pointer went down so
                        // `drag_to` can measure from it.
                        self.start_drag();
                        self.drag_origin_y = Some(pos.y);
                    } else {
                        // Click on overlay — dismiss
                        self.is_visible = false;
                        self.dismissed.emit();
                        self.base.request_redraw();
                    }
                }
            }
            Event::MouseMove { pos } => {
                // Driven through `drag_to`, which needs the press origin to turn an
                // absolute pointer position into the offset `end_drag` tests. The arm
                // used to be empty with a comment saying the origin "would be tracked",
                // so `drag_offset` never left `0.0` and the one-third threshold in
                // `end_drag` was unreachable from a real event sequence.
                self.drag_to(pos.y);
            }
            Event::MouseRelease { pos: _, button } => {
                if *button == 1 && self.is_dragging {
                    self.end_drag();
                }
            }
            // A release outside the sheet is never delivered (the runtime's hit-test
            // answers `None` outside every control), so the drag is cancelled here
            // rather than left half-finished.
            Event::MouseLeave { .. } if self.is_dragging => {
                self.cancel_drag();
            }
            _ => {
                self.base.handle_event(event);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::Point;
    use crate::widget::svg::render_to_svg;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::Arc;

    fn make_sheet() -> ModalBottomSheet {
        ModalBottomSheet::new(Rect::new(0, 0, 400, 600))
    }

    #[test]
    fn modal_bottom_sheet_default_state() {
        let sheet = make_sheet();
        assert!(!sheet.is_visible());
        assert_eq!(sheet.title(), "");
        assert_eq!(sheet.kind(), WidgetKind::ModalBottomSheet);
    }

    #[test]
    fn modal_bottom_sheet_show_and_hide() {
        let mut sheet = make_sheet();
        assert!(!sheet.is_visible());

        sheet.show();
        assert!(sheet.is_visible());

        sheet.hide();
        assert!(!sheet.is_visible());
    }

    #[test]
    fn modal_bottom_sheet_dismiss_signal_on_overlay_click() {
        let mut sheet = make_sheet();
        sheet.show();
        assert!(sheet.is_visible());

        let dismissed = Arc::new(AtomicBool::new(false));
        sheet.dismissed.connect({
            let d = Arc::clone(&dismissed);
            move || {
                d.store(true, Ordering::SeqCst);
            }
        });

        // Click above the sheet area (overlay region)
        // Sheet height is computed, but geometry is 600 tall so overlay is top portion
        sheet.handle_event(&Event::MousePress { pos: Point::new(200, 50), button: 1 });
        assert!(!sheet.is_visible());
        assert!(dismissed.load(Ordering::SeqCst));
    }

    #[test]
    fn modal_bottom_sheet_set_title() {
        let mut sheet = make_sheet();
        assert_eq!(sheet.title(), "");
        sheet.set_title("Options");
        assert_eq!(sheet.title(), "Options");
    }

    #[test]
    fn modal_bottom_sheet_drag_end_dismisses() {
        let mut sheet = make_sheet();
        sheet.show();
        assert!(sheet.is_visible());

        let dismissed = Arc::new(AtomicBool::new(false));
        sheet.dismissed.connect({
            let d = Arc::clone(&dismissed);
            move || {
                d.store(true, Ordering::SeqCst);
            }
        });

        // Start drag, push it past threshold, then release
        sheet.start_drag();
        assert!(sheet.is_dragging());

        // Push past 1/3 of sheet height (sheet is at least 120, so > 40)
        sheet.update_drag(80.0);
        assert_eq!(sheet.drag_offset(), 80.0);

        sheet.end_drag();
        assert!(!sheet.is_visible());
        assert!(dismissed.load(Ordering::SeqCst));
    }

    /// The same dismissal, driven through real `Event`s rather than the methods.
    ///
    /// # Why this test exists
    ///
    /// The test above calls `start_drag`/`update_drag`/`end_drag` directly, so it
    /// passed even while the `MouseMove` event arm was an empty body with a comment
    /// saying the origin "would be tracked". Through the event API `drag_offset`
    /// therefore never left `0.0`, the one-third threshold in `end_drag` was
    /// unreachable, and a user dragging the sheet down got nothing — with every unit
    /// test green.
    #[test]
    fn a_real_drag_gesture_dismisses_the_sheet() {
        let mut sheet = make_sheet();
        sheet.show();
        let rect = sheet.geometry();

        let dismissed = Arc::new(AtomicBool::new(false));
        sheet.dismissed.connect({
            let d = Arc::clone(&dismissed);
            move || {
                d.store(true, Ordering::SeqCst);
            }
        });

        // Press inside the sheet panel, near its bottom edge, as a user would.
        let press_y = rect.y + rect.height as i32 - 20;
        sheet.handle_event(&Event::MousePress { pos: Point::new(rect.x + 50, press_y), button: 1 });
        assert!(sheet.is_dragging(), "a press inside the sheet must start a drag");

        // Drag downward well past a third of the sheet height.
        for step in 1..=8 {
            sheet.handle_event(&Event::MouseMove {
                pos: Point::new(rect.x + 50, press_y + step * 20),
            });
        }
        assert!(
            sheet.drag_offset() > 40.0,
            "dragging down must accumulate an offset, got {}",
            sheet.drag_offset()
        );

        sheet.handle_event(&Event::MouseRelease {
            pos: Point::new(rect.x + 50, press_y + 160),
            button: 1,
        });
        assert!(!sheet.is_visible(), "a long downward drag must dismiss the sheet");
        assert!(dismissed.load(Ordering::SeqCst));
    }

    /// A short drag through real events must not dismiss, and must reset the offset.
    #[test]
    fn a_real_short_drag_leaves_the_sheet_visible_and_resets_the_offset() {
        let mut sheet = make_sheet();
        sheet.show();
        let rect = sheet.geometry();
        let press_y = rect.y + rect.height as i32 - 20;

        sheet.handle_event(&Event::MousePress { pos: Point::new(rect.x + 50, press_y), button: 1 });
        sheet.handle_event(&Event::MouseMove { pos: Point::new(rect.x + 50, press_y + 10) });
        sheet.handle_event(&Event::MouseRelease {
            pos: Point::new(rect.x + 50, press_y + 10),
            button: 1,
        });

        assert!(sheet.is_visible(), "a 10px drag must not dismiss");
        assert_eq!(sheet.drag_offset(), 0.0, "the offset must reset after the drag ends");
        assert!(!sheet.is_dragging());
    }

    /// A drag whose pointer leaves the sheet is cancelled, not left half-finished.
    #[test]
    fn a_drag_that_leaves_the_sheet_is_cancelled() {
        let mut sheet = make_sheet();
        sheet.show();
        let rect = sheet.geometry();
        let press_y = rect.y + rect.height as i32 - 20;

        sheet.handle_event(&Event::MousePress { pos: Point::new(rect.x + 50, press_y), button: 1 });
        sheet.handle_event(&Event::MouseMove { pos: Point::new(rect.x + 50, press_y + 60) });
        assert!(sheet.drag_offset() > 0.0);

        sheet.handle_event(&Event::MouseLeave { pos: Point::new(0, 0) });
        assert!(!sheet.is_dragging(), "leaving the sheet must end the drag");
        assert_eq!(sheet.drag_offset(), 0.0, "the sheet must return to its resting position");
        assert!(sheet.is_visible(), "leaving is not a dismissal");
    }

    #[test]
    fn modal_bottom_sheet_drag_small_offset_no_dismiss() {
        let mut sheet = make_sheet();
        sheet.show();

        sheet.start_drag();
        sheet.update_drag(20.0);
        sheet.end_drag();

        // Small drag should not dismiss
        assert!(sheet.is_visible());
    }

    #[test]
    fn modal_bottom_sheet_svg_output_visible() {
        let mut sheet = make_sheet();
        sheet.set_title("Example");
        sheet.show();
        let svg = render_to_svg(&mut sheet);
        assert!(svg.starts_with("<svg"));
        assert!(svg.ends_with("</svg>"));
    }

    #[test]
    fn modal_bottom_sheet_svg_output_hidden() {
        let mut sheet = make_sheet();
        let svg = render_to_svg(&mut sheet);
        assert!(svg.starts_with("<svg"));
        assert!(svg.ends_with("</svg>"));
    }
}