liora-components 0.1.8

Enterprise-style native GPUI component library for Liora applications.
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
//! Scrollbar module.
//!
//! This public module implements the Liora custom scrollbar wrappers for GPUI virtual scroll areas. It keeps the reusable
//! component logic inside `liora-components` rather than Gallery or Docs so
//! downstream GPUI applications can compose the same behavior with their own
//! app state, assets, and release policy.
//!
//! ## Usage model
//!
//! Components in this module render native GPUI element trees. Stateless builder
//! values can be constructed inline, while controls with focus, selection,
//! popup, drag, or editing state should be stored as `gpui::Entity<T>` fields in
//! the parent view so state survives GPUI render passes.
//!
//! ## Design contract
//!
//! The implementation should use Liora theme tokens from `liora-core` and
//! `liora-theme`, keep accessibility-oriented keyboard/pointer behavior close to
//! the component, and avoid app-specific Gallery/Docs resources in this SDK
//! crate.

use gpui::{
    AnyElement, App, Bounds, Context, DispatchPhase, Element, GlobalElementId, Hitbox,
    HitboxBehavior, InspectorElementId, IntoElement, LayoutId, ListState, MouseButton,
    MouseDownEvent, MouseMoveEvent, MouseUpEvent, PaintQuad, Pixels, Point, Render, ScrollHandle,
    Size, Style, Window, div, point, prelude::*, px, relative, size,
};
use liora_core::Config;
use std::cell::Cell;

thread_local! {
    static VIRTUAL_SCROLLBAR_GRAB_OFFSET: Cell<Option<Pixels>> = const { Cell::new(None) };
    static SCROLLBAR_GRAB_OFFSET: Cell<Option<Pixels>> = const { Cell::new(None) };
}

const SCROLLBAR_THUMB_WIDTH: Pixels = px(4.0);
const SCROLLBAR_THUMB_HOVER_WIDTH: Pixels = px(8.0);
const SCROLLBAR_HIT_WIDTH: Pixels = px(14.0);
const SCROLLBAR_MIN_THUMB_HEIGHT: Pixels = px(24.0);

/// Fluent native GPUI component for rendering Liora scrollbar.
pub struct Scrollbar {
    scroll_handle: ScrollHandle,
    render_content: Box<dyn Fn(&mut Window, &mut App) -> AnyElement + 'static>,
    height: Option<Pixels>,
}

impl Scrollbar {
    /// Creates `Scrollbar` initialized from the supplied render content.
    pub fn new<F, E>(_cx: &mut Context<Self>, render_content: F) -> Self
    where
        F: Fn(&mut Window, &mut App) -> E + 'static,
        E: IntoElement,
    {
        Self {
            scroll_handle: ScrollHandle::new(),
            render_content: Box::new(move |window, cx| {
                render_content(window, cx).into_any_element()
            }),
            height: None,
        }
    }

    /// Sets the component height token used during GPUI layout.
    pub fn height(mut self, h: impl Into<Pixels>) -> Self {
        self.height = Some(h.into());
        self
    }
}

/// Paints and drives a scrollbar for GPUI's virtual [`ListState`].
///
/// This lets Liora Docs use GPUI's native virtual list for visible-area rendering
/// while still bootstrapping the visual scrollbar from Liora's component layer.
pub struct VirtualScrollbar {
    list_state: ListState,
}

impl VirtualScrollbar {
    /// Creates `VirtualScrollbar` initialized from the supplied list state.
    pub fn new(list_state: ListState) -> Self {
        Self { list_state }
    }
}

impl IntoElement for VirtualScrollbar {
    type Element = Self;
    fn into_element(self) -> Self::Element {
        self
    }
}

/// Fluent native GPUI component for rendering Liora virtual scrollbar prepaint.
pub struct VirtualScrollbarPrepaint {
    thumb_bounds: Option<Bounds<Pixels>>,
    hover_bounds: Bounds<Pixels>,
    hitbox: Hitbox,
    active: bool,
    dragging: bool,
}

#[derive(Clone, Copy)]
struct ThumbMetrics {
    bounds: Bounds<Pixels>,
    max_offset: Pixels,
    track_height: Pixels,
}

impl Element for VirtualScrollbar {
    type RequestLayoutState = ();
    type PrepaintState = VirtualScrollbarPrepaint;

    fn id(&self) -> Option<gpui::ElementId> {
        None
    }

    fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
        None
    }

    fn request_layout(
        &mut self,
        _id: Option<&GlobalElementId>,
        _id2: Option<&InspectorElementId>,
        window: &mut Window,
        cx: &mut App,
    ) -> (LayoutId, Self::RequestLayoutState) {
        let mut style = Style::default();
        style.position = gpui::Position::Absolute;
        style.size.width = relative(1.0).into();
        style.size.height = relative(1.0).into();
        (window.request_layout(style, [], cx), ())
    }

    fn prepaint(
        &mut self,
        _id: Option<&GlobalElementId>,
        _id2: Option<&InspectorElementId>,
        bounds: Bounds<Pixels>,
        _request_layout: &mut Self::RequestLayoutState,
        window: &mut Window,
        _cx: &mut App,
    ) -> Self::PrepaintState {
        let metrics = virtual_thumb_metrics(&self.list_state, SCROLLBAR_THUMB_WIDTH);
        let thumb = metrics.map(|metrics| metrics.bounds);
        let hitbox_bounds = thumb.map(expand_scrollbar_hitbox).unwrap_or(Bounds {
            origin: point(bounds.right() - SCROLLBAR_HIT_WIDTH, bounds.top()),
            size: Size {
                width: SCROLLBAR_HIT_WIDTH,
                height: bounds.size.height,
            },
        });
        let hitbox = window.insert_hitbox(hitbox_bounds, HitboxBehavior::Normal);
        let dragging = virtual_scrollbar_grab_offset().is_some();
        let active = hitbox.is_hovered(window)
            || dragging
            || hitbox_bounds.contains(&window.mouse_position());
        let thumb_bounds = thumb.map(|thumb| {
            let target_width = if active {
                SCROLLBAR_THUMB_HOVER_WIDTH
            } else {
                SCROLLBAR_THUMB_WIDTH
            };
            scrollbar_thumb_bounds_for_width(thumb, target_width)
        });
        VirtualScrollbarPrepaint {
            thumb_bounds,
            hover_bounds: hitbox_bounds,
            hitbox,
            active,
            dragging,
        }
    }

    fn paint(
        &mut self,
        _id: Option<&GlobalElementId>,
        _id2: Option<&InspectorElementId>,
        _bounds: Bounds<Pixels>,
        _request_layout: &mut Self::RequestLayoutState,
        prepaint: &mut Self::PrepaintState,
        window: &mut Window,
        cx: &mut App,
    ) {
        let Some(thumb_bounds) = prepaint.thumb_bounds else {
            return;
        };

        let thumb_color = cx
            .global::<Config>()
            .theme
            .neutral
            .border
            .opacity(if prepaint.dragging { 1.0 } else { 0.8 });
        window.paint_quad(PaintQuad {
            bounds: thumb_bounds,
            corner_radii: gpui::Corners::all(thumb_bounds.size.width / 2.0),
            background: thumb_color.into(),
            border_widths: gpui::Edges::all(px(0.0)),
            border_color: gpui::transparent_black(),
            border_style: gpui::BorderStyle::Solid,
        });

        let was_active = prepaint.active;
        let hover_bounds = prepaint.hover_bounds;
        let current_view = window.current_view();
        window.on_mouse_event(move |_: &MouseMoveEvent, phase, window, cx| {
            if phase == DispatchPhase::Capture {
                let active = virtual_scrollbar_grab_offset().is_some()
                    || hover_bounds.contains(&window.mouse_position());
                if active != was_active {
                    cx.notify(current_view);
                    window.refresh();
                }
            }
        });

        let list_state = self.list_state.clone();
        let hitbox = prepaint.hitbox.clone();
        let hover_bounds = prepaint.hover_bounds;
        let raw_thumb_bounds = thumb_bounds;
        window.on_mouse_event(move |event: &MouseDownEvent, phase, window, cx| {
            if phase == DispatchPhase::Capture
                && event.button == MouseButton::Left
                && (hitbox.is_hovered(window) || hover_bounds.contains(&event.position))
            {
                let grab_offset = if raw_thumb_bounds.contains(&event.position) {
                    event.position.y - raw_thumb_bounds.top()
                } else {
                    raw_thumb_bounds.size.height / 2.0
                };
                set_virtual_scrollbar_grab_offset(Some(grab_offset));
                list_state.scrollbar_drag_started();
                set_virtual_scrollbar_position(&list_state, event.position, grab_offset);

                cx.stop_propagation();
                window.refresh();
            }
        });

        let list_state = self.list_state.clone();
        window.on_mouse_event(move |event: &MouseMoveEvent, phase, window, cx| {
            if phase == DispatchPhase::Capture {
                let Some(grab_offset) = virtual_scrollbar_grab_offset() else {
                    return;
                };
                set_virtual_scrollbar_position(&list_state, event.position, grab_offset);
                cx.stop_propagation();
                window.refresh();
            }
        });

        let list_state = self.list_state.clone();
        window.on_mouse_event(move |event: &MouseUpEvent, phase, window, cx| {
            if phase == DispatchPhase::Capture
                && event.button == MouseButton::Left
                && virtual_scrollbar_grab_offset().is_some()
            {
                list_state.scrollbar_drag_ended();
                set_virtual_scrollbar_grab_offset(None);
                cx.stop_propagation();
                window.refresh();
            }
        });
    }
}

fn set_virtual_scrollbar_position(
    list_state: &ListState,
    position: Point<Pixels>,
    grab_offset: Pixels,
) {
    let viewport = list_state.viewport_bounds();
    let max_offset = list_state.max_offset_for_scrollbar();
    if max_offset.height <= px(0.0) || viewport.size.height <= px(0.0) {
        return;
    }

    let content_height = viewport.size.height + max_offset.height;
    let thumb_height = (viewport.size.height * (viewport.size.height / content_height))
        .max(SCROLLBAR_MIN_THUMB_HEIGHT)
        .min(viewport.size.height);
    let track_height = (viewport.size.height - thumb_height).max(px(1.0));
    let y = (position.y - viewport.top() - grab_offset).clamp(px(0.0), track_height);
    let content_offset = y / track_height * max_offset.height;
    list_state.set_offset_from_scrollbar(point(px(0.0), content_offset));
}

fn virtual_thumb_metrics(list_state: &ListState, width: Pixels) -> Option<ThumbMetrics> {
    let viewport = list_state.viewport_bounds();
    let max_offset = list_state.max_offset_for_scrollbar();
    let offset = list_state.scroll_px_offset_for_scrollbar();
    let viewport_h = viewport.size.height;
    let content_h = viewport_h + max_offset.height;

    if content_h <= viewport_h || content_h <= px(0.0) || viewport_h <= px(0.0) {
        return None;
    }

    let ratio = viewport_h / content_h;
    let thumb_h = (viewport_h * ratio)
        .max(SCROLLBAR_MIN_THUMB_HEIGHT)
        .min(viewport_h);
    let scroll_ratio = if max_offset.height > px(0.0) {
        -offset.y / max_offset.height
    } else {
        0.0
    }
    .clamp(0.0, 1.0);
    let thumb_top = (viewport_h - thumb_h) * scroll_ratio;

    let bounds = Bounds {
        origin: point(
            viewport.right() - width - px(2.0),
            viewport.top() + thumb_top,
        ),
        size: size(width, thumb_h),
    };

    Some(ThumbMetrics {
        bounds,
        max_offset: max_offset.height,
        track_height: viewport_h - thumb_h,
    })
}

fn scrollbar_thumb_bounds_for_width(target: Bounds<Pixels>, width: Pixels) -> Bounds<Pixels> {
    Bounds {
        origin: point(target.right() - width, target.top()),
        size: size(width, target.size.height),
    }
}

fn expand_scrollbar_hitbox(thumb: Bounds<Pixels>) -> Bounds<Pixels> {
    Bounds {
        origin: point(
            thumb.right() - SCROLLBAR_HIT_WIDTH - px(2.0),
            thumb.top() - px(4.0),
        ),
        size: size(SCROLLBAR_HIT_WIDTH + px(2.0), thumb.size.height + px(8.0)),
    }
}

fn virtual_scrollbar_grab_offset() -> Option<Pixels> {
    VIRTUAL_SCROLLBAR_GRAB_OFFSET.with(Cell::get)
}

fn set_virtual_scrollbar_grab_offset(offset: Option<Pixels>) {
    VIRTUAL_SCROLLBAR_GRAB_OFFSET.with(|state| state.set(offset));
}

fn scrollbar_grab_offset() -> Option<Pixels> {
    SCROLLBAR_GRAB_OFFSET.with(Cell::get)
}

fn set_scrollbar_grab_offset(offset: Option<Pixels>) {
    SCROLLBAR_GRAB_OFFSET.with(|state| state.set(offset));
}

fn scroll_handle_thumb_metrics(
    scroll_handle: &ScrollHandle,
    width: Pixels,
) -> Option<ThumbMetrics> {
    let viewport_bounds = scroll_handle.bounds();
    let max_offset = scroll_handle.max_offset();
    let offset = scroll_handle.offset();

    let viewport_h = viewport_bounds.size.height;
    let content_h = viewport_h + max_offset.height;

    if content_h <= viewport_h || content_h <= px(0.0) || viewport_h <= px(0.0) {
        return None;
    }

    let ratio = viewport_h / content_h;
    let thumb_h = (viewport_h * ratio)
        .max(SCROLLBAR_MIN_THUMB_HEIGHT)
        .min(viewport_h);
    let scroll_ratio = if max_offset.height > px(0.0) {
        -offset.y / max_offset.height
    } else {
        0.0
    }
    .clamp(0.0, 1.0);
    let thumb_top = (viewport_h - thumb_h) * scroll_ratio;
    let bounds = Bounds {
        origin: point(
            viewport_bounds.right() - width - px(2.0),
            viewport_bounds.top() + thumb_top,
        ),
        size: size(width, thumb_h),
    };

    Some(ThumbMetrics {
        bounds,
        max_offset: max_offset.height,
        track_height: viewport_h - thumb_h,
    })
}

fn set_scroll_handle_position(
    scroll_handle: &ScrollHandle,
    position: Point<Pixels>,
    grab_offset: Pixels,
) {
    let Some(metrics) = scroll_handle_thumb_metrics(scroll_handle, SCROLLBAR_THUMB_WIDTH) else {
        return;
    };
    if metrics.max_offset <= px(0.0) || metrics.track_height <= px(0.0) {
        return;
    }

    let viewport = scroll_handle.bounds();
    let y = (position.y - viewport.top() - grab_offset).clamp(px(0.0), metrics.track_height);
    let content_offset = y / metrics.track_height * metrics.max_offset;
    scroll_handle.set_offset(point(px(0.0), -content_offset));
}

impl Render for Scrollbar {
    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let scroll_handle = self.scroll_handle.clone();
        let content = (self.render_content)(_window, cx);

        let mut container = div().flex().flex_col().overflow_hidden();
        if let Some(h) = self.height {
            container = container.h(h);
        } else {
            container = container.h_full();
        }

        container
            .relative()
            .child(
                div()
                    .flex_1()
                    .id("scroll-viewport")
                    .overflow_y_scroll()
                    .track_scroll(&scroll_handle)
                    .on_scroll_wheel(cx.listener(|_, _, _, cx| {
                        cx.notify();
                    }))
                    .child(content),
            )
            .child(ScrollbarThumb {
                scroll_handle: self.scroll_handle.clone(),
            })
    }
}

struct ScrollbarThumb {
    scroll_handle: ScrollHandle,
}

struct ScrollbarThumbPrepaint {
    thumb_bounds: Option<Bounds<Pixels>>,
    hover_bounds: Bounds<Pixels>,
    hitbox: Hitbox,
    active: bool,
    dragging: bool,
}

impl IntoElement for ScrollbarThumb {
    type Element = Self;
    fn into_element(self) -> Self::Element {
        self
    }
}

impl gpui::Element for ScrollbarThumb {
    type RequestLayoutState = ();
    type PrepaintState = ScrollbarThumbPrepaint;

    fn id(&self) -> Option<gpui::ElementId> {
        None
    }

    fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
        None
    }

    fn request_layout(
        &mut self,
        _id: Option<&gpui::GlobalElementId>,
        _id2: Option<&gpui::InspectorElementId>,
        window: &mut Window,
        cx: &mut App,
    ) -> (gpui::LayoutId, Self::RequestLayoutState) {
        let mut style = gpui::Style::default();
        style.position = gpui::Position::Absolute;
        style.size.width = gpui::relative(1.0).into();
        style.size.height = gpui::relative(1.0).into();
        (window.request_layout(style, [], cx), ())
    }

    fn prepaint(
        &mut self,
        _id: Option<&gpui::GlobalElementId>,
        _id2: Option<&gpui::InspectorElementId>,
        bounds: gpui::Bounds<Pixels>,
        _request_layout: &mut Self::RequestLayoutState,
        window: &mut Window,
        _cx: &mut App,
    ) -> Self::PrepaintState {
        let metrics = scroll_handle_thumb_metrics(&self.scroll_handle, SCROLLBAR_THUMB_WIDTH);
        let thumb = metrics.map(|metrics| metrics.bounds);
        let hover_bounds = thumb.map(expand_scrollbar_hitbox).unwrap_or(Bounds {
            origin: point(bounds.right() - SCROLLBAR_HIT_WIDTH, bounds.top()),
            size: Size {
                width: SCROLLBAR_HIT_WIDTH,
                height: bounds.size.height,
            },
        });
        let hitbox = window.insert_hitbox(hover_bounds, HitboxBehavior::Normal);
        let dragging = scrollbar_grab_offset().is_some();
        let active = hitbox.is_hovered(window)
            || dragging
            || hover_bounds.contains(&window.mouse_position());
        let thumb_bounds = thumb.map(|thumb| {
            let target_width = if active {
                SCROLLBAR_THUMB_HOVER_WIDTH
            } else {
                SCROLLBAR_THUMB_WIDTH
            };
            scrollbar_thumb_bounds_for_width(thumb, target_width)
        });

        ScrollbarThumbPrepaint {
            thumb_bounds,
            hover_bounds,
            hitbox,
            active,
            dragging,
        }
    }

    fn paint(
        &mut self,
        _id: Option<&gpui::GlobalElementId>,
        _id2: Option<&gpui::InspectorElementId>,
        _bounds: gpui::Bounds<Pixels>,
        _request_layout: &mut Self::RequestLayoutState,
        prepaint: &mut Self::PrepaintState,
        window: &mut Window,
        cx: &mut App,
    ) -> () {
        let Some(thumb_bounds) = prepaint.thumb_bounds else {
            return;
        };

        let thumb_color = cx
            .global::<Config>()
            .theme
            .neutral
            .border
            .opacity(if prepaint.dragging { 1.0 } else { 0.8 });

        let was_active = prepaint.active;
        let hover_bounds = prepaint.hover_bounds;
        let current_view = window.current_view();
        window.on_mouse_event(move |_: &MouseMoveEvent, phase, window, cx| {
            if phase == DispatchPhase::Capture {
                let active = scrollbar_grab_offset().is_some()
                    || hover_bounds.contains(&window.mouse_position());
                if active != was_active {
                    cx.notify(current_view);
                    window.refresh();
                }
            }
        });

        let scroll_handle = self.scroll_handle.clone();
        let hitbox = prepaint.hitbox.clone();
        let hover_bounds = prepaint.hover_bounds;
        let raw_thumb_bounds = thumb_bounds;
        window.on_mouse_event(move |event: &MouseDownEvent, phase, window, cx| {
            if phase == DispatchPhase::Capture
                && event.button == MouseButton::Left
                && (hitbox.is_hovered(window) || hover_bounds.contains(&event.position))
            {
                let grab_offset = if raw_thumb_bounds.contains(&event.position) {
                    event.position.y - raw_thumb_bounds.top()
                } else {
                    raw_thumb_bounds.size.height / 2.0
                };
                set_scrollbar_grab_offset(Some(grab_offset));
                set_scroll_handle_position(&scroll_handle, event.position, grab_offset);

                cx.stop_propagation();
                window.refresh();
            }
        });

        let scroll_handle = self.scroll_handle.clone();
        window.on_mouse_event(move |event: &MouseMoveEvent, phase, window, cx| {
            if phase == DispatchPhase::Capture {
                let Some(grab_offset) = scrollbar_grab_offset() else {
                    return;
                };
                set_scroll_handle_position(&scroll_handle, event.position, grab_offset);
                cx.stop_propagation();
                window.refresh();
            }
        });

        window.on_mouse_event(move |event: &MouseUpEvent, phase, window, cx| {
            if phase == DispatchPhase::Capture
                && event.button == MouseButton::Left
                && scrollbar_grab_offset().is_some()
            {
                set_scrollbar_grab_offset(None);
                cx.stop_propagation();
                window.refresh();
            }
        });

        window.paint_quad(gpui::PaintQuad {
            bounds: thumb_bounds,
            corner_radii: gpui::Corners::all(thumb_bounds.size.width / 2.0),
            background: thumb_color.into(),
            border_widths: gpui::Edges::all(gpui::px(0.0)),
            border_color: gpui::hsla(0.0, 0.0, 0.0, 0.0),
            border_style: gpui::BorderStyle::Solid,
        });
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn virtual_scrollbar_bootstraps_gpui_list_state_scrolling() {
        let source = include_str!("scrollbar.rs");

        assert!(source.contains("pub struct VirtualScrollbar"));
        assert!(source.contains("ListState"));
        assert!(source.contains("scroll_px_offset_for_scrollbar"));
        assert!(source.contains("max_offset_for_scrollbar"));
        assert!(source.contains("set_offset_from_scrollbar"));
        assert!(source.contains("scrollbar_drag_started"));
        assert!(source.contains("scrollbar_drag_ended"));
        assert!(source.contains("virtual_scrollbar_grab_offset"));
    }

    #[test]
    fn scrollbars_expand_on_hover_and_drag_without_smoothing() {
        let source = include_str!("scrollbar.rs")
            .split("#[cfg(test)]")
            .next()
            .expect("production source should precede tests");

        assert!(source.contains("SCROLLBAR_THUMB_HOVER_WIDTH"));
        assert!(source.contains("SCROLLBAR_HIT_WIDTH"));
        assert!(source.contains("scrollbar_thumb_bounds_for_width"));
        assert!(source.contains("set_scrollbar_grab_offset"));
        assert!(source.contains("set_virtual_scrollbar_grab_offset"));
        assert!(source.contains("set_scroll_handle_position"));
        assert!(source.contains("set_virtual_scrollbar_position"));
        assert!(source.contains("hover_bounds.contains(&window.mouse_position())"));
        assert!(source.contains("cx.notify(current_view)"));
        assert!(!source.contains("lerp_pixels"));
        assert!(!source.contains("request_animation_frame"));
    }
}