blinc_cn 0.5.1

Blinc Component Library - shadcn-style themed components built on blinc_layout primitives
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
//! Scroll Area component - styled scrollable container with customizable scrollbar
//!
//! A themed scroll container that wraps `blinc_layout::scroll()` with the built-in
//! scrollbar. Supports various scrollbar visibility modes and auto-dismiss.
//!
//! # Example
//!
//! ```ignore
//! use blinc_cn::prelude::*;
//!
//! // Basic scroll area with auto-dismissing scrollbar
//! cn::scroll_area()
//!     .h(400.0)
//!     .child(
//!         div().flex_col().gap(8.0)
//!             .child(text("Item 1"))
//!             .child(text("Item 2"))
//!             // ... many items
//!     )
//!
//! // Always show scrollbar
//! cn::scroll_area()
//!     .scrollbar(ScrollbarVisibility::Always)
//!     .h(300.0)
//!     .child(content)
//!
//! // Horizontal scroll
//! cn::scroll_area()
//!     .horizontal()
//!     .w(400.0)
//!     .child(wide_content)
//!
//! // Custom scrollbar styling
//! cn::scroll_area()
//!     .scrollbar_width(8.0)
//!     .scrollbar_color(Color::GRAY)
//!     .h(400.0)
//!     .child(content)
//! ```

use std::cell::{OnceCell, RefCell};

use blinc_core::Color;
use blinc_layout::element::RenderProps;
use blinc_layout::prelude::*;
use blinc_layout::tree::{LayoutNodeId, LayoutTree};
use blinc_layout::widgets::scroll::{
    scroll, Scroll, ScrollDirection, ScrollbarSize,
    ScrollbarVisibility as LayoutScrollbarVisibility,
};
use blinc_theme::{ColorToken, ThemeState};

/// Scrollbar visibility modes
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ScrollbarVisibility {
    /// Always show scrollbar (like classic Windows style)
    Always,
    /// Show scrollbar only when hovering over the scroll area
    Hover,
    /// Show when scrolling, auto-dismiss after inactivity (like macOS)
    #[default]
    Auto,
    /// Never show scrollbar (content still scrollable)
    Never,
}

impl ScrollbarVisibility {
    /// Convert to the layout crate's ScrollbarVisibility
    fn to_layout(self) -> LayoutScrollbarVisibility {
        match self {
            ScrollbarVisibility::Always => LayoutScrollbarVisibility::Always,
            ScrollbarVisibility::Hover => LayoutScrollbarVisibility::Hover,
            ScrollbarVisibility::Auto => LayoutScrollbarVisibility::Auto,
            ScrollbarVisibility::Never => LayoutScrollbarVisibility::Never,
        }
    }
}

/// Scroll area size presets
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ScrollAreaSize {
    /// Small scrollbar (4px width)
    Small,
    /// Medium scrollbar (6px width)
    #[default]
    Medium,
    /// Large scrollbar (10px width)
    Large,
}

impl ScrollAreaSize {
    fn scrollbar_width(&self) -> f32 {
        match self {
            ScrollAreaSize::Small => 4.0,
            ScrollAreaSize::Medium => 6.0,
            ScrollAreaSize::Large => 10.0,
        }
    }

    /// Convert to the layout crate's ScrollbarSize
    fn to_layout(self) -> ScrollbarSize {
        match self {
            ScrollAreaSize::Small => ScrollbarSize::Thin,
            ScrollAreaSize::Medium => ScrollbarSize::Normal,
            ScrollAreaSize::Large => ScrollbarSize::Wide,
        }
    }
}

/// Configuration for scroll area
struct ScrollAreaConfig {
    /// Scrollbar visibility mode
    visibility: ScrollbarVisibility,
    /// Scroll direction
    direction: ScrollDirection,
    /// Custom scrollbar width (overrides size preset)
    scrollbar_width: Option<f32>,
    /// Scrollbar size preset
    size: ScrollAreaSize,
    /// Thumb color (uses theme if None)
    thumb_color: Option<Color>,
    /// Track color (uses theme if None)
    track_color: Option<Color>,
    /// Viewport dimensions
    width: Option<f32>,
    height: Option<f32>,
    /// Enable bounce physics
    bounce: bool,
    /// Content builder
    content: Option<Div>,
    /// Corner radius
    rounded: Option<f32>,
    /// Background color
    bg: Option<Color>,
}

impl Default for ScrollAreaConfig {
    fn default() -> Self {
        Self {
            visibility: ScrollbarVisibility::default(),
            direction: ScrollDirection::Vertical,
            scrollbar_width: None,
            size: ScrollAreaSize::default(),
            thumb_color: None,
            track_color: None,
            width: None,
            height: None,
            bounce: true,
            content: None,
            rounded: None,
            bg: None,
        }
    }
}

/// Built scroll area using the underlying scroll widget
struct BuiltScrollArea {
    inner: Scroll,
}

impl BuiltScrollArea {
    fn from_config(config: ScrollAreaConfig) -> Self {
        let theme = ThemeState::get();

        // Get theme-based colors if not specified
        let thumb_color = config
            .thumb_color
            .unwrap_or_else(|| theme.color(ColorToken::Border).with_alpha(0.5));
        let track_color = config
            .track_color
            .unwrap_or_else(|| theme.color(ColorToken::Surface).with_alpha(0.1));

        // Get viewport dimensions
        let viewport_width = config.width.unwrap_or(300.0);
        let viewport_height = config.height.unwrap_or(400.0);

        // Build the scroll widget with built-in scrollbar
        let mut scroll_widget = scroll()
            .w(viewport_width)
            .h(viewport_height)
            .direction(config.direction)
            .bounce(config.bounce)
            .scrollbar_visibility(config.visibility.to_layout())
            .scrollbar_thumb_color(thumb_color.r, thumb_color.g, thumb_color.b, thumb_color.a)
            .scrollbar_track_color(track_color.r, track_color.g, track_color.b, track_color.a);

        // Apply scrollbar width
        if let Some(width) = config.scrollbar_width {
            scroll_widget = scroll_widget.scrollbar_width(width);
        } else {
            scroll_widget = scroll_widget.scrollbar_size(config.size.to_layout());
        }

        // Apply corner radius
        if let Some(radius) = config.rounded {
            scroll_widget = scroll_widget.rounded(radius);
        }

        // Apply background color
        if let Some(bg) = config.bg {
            scroll_widget = scroll_widget.bg(bg);
        }

        // Add content if provided
        if let Some(content) = config.content {
            scroll_widget = scroll_widget.child(content);
        }

        Self {
            inner: scroll_widget,
        }
    }
}

/// Scroll Area component with customizable scrollbar
pub struct ScrollArea {
    inner: Div,
}

impl ScrollArea {
    /// Add a CSS class for selector matching
    pub fn class(mut self, name: impl Into<String>) -> Self {
        self.inner = self.inner.class(name);
        self
    }

    /// Set the element ID for CSS selector matching
    pub fn id(mut self, id: &str) -> Self {
        self.inner = self.inner.id(id);
        self
    }
}

impl ElementBuilder for ScrollArea {
    fn build(&self, tree: &mut LayoutTree) -> LayoutNodeId {
        self.inner.build(tree)
    }

    fn render_props(&self) -> RenderProps {
        self.inner.render_props()
    }

    fn children_builders(&self) -> &[Box<dyn ElementBuilder>] {
        self.inner.children_builders()
    }

    fn event_handlers(&self) -> Option<&blinc_layout::event_handler::EventHandlers> {
        ElementBuilder::event_handlers(&self.inner)
    }

    fn element_classes(&self) -> &[String] {
        self.inner.element_classes()
    }
}

/// Builder for scroll area
pub struct ScrollAreaBuilder {
    config: RefCell<ScrollAreaConfig>,
    built: OnceCell<ScrollArea>,
}

impl ScrollAreaBuilder {
    /// Create a new scroll area builder
    #[track_caller]
    pub fn new() -> Self {
        Self {
            config: RefCell::new(ScrollAreaConfig::default()),
            built: OnceCell::new(),
        }
    }

    fn get_or_build(&self) -> &ScrollArea {
        self.built.get_or_init(|| {
            // Take ownership of config, replacing with default
            let config = self.config.take();
            let built = BuiltScrollArea::from_config(config);
            ScrollArea {
                inner: div().class("cn-scroll-area").child(built.inner),
            }
        })
    }

    /// Set scrollbar visibility mode
    pub fn scrollbar(self, visibility: ScrollbarVisibility) -> Self {
        self.config.borrow_mut().visibility = visibility;
        self
    }

    /// Set scroll direction
    pub fn direction(self, direction: ScrollDirection) -> Self {
        self.config.borrow_mut().direction = direction;
        self
    }

    /// Set to vertical scrolling (default)
    pub fn vertical(self) -> Self {
        self.config.borrow_mut().direction = ScrollDirection::Vertical;
        self
    }

    /// Set to horizontal scrolling
    pub fn horizontal(self) -> Self {
        self.config.borrow_mut().direction = ScrollDirection::Horizontal;
        self
    }

    /// Set to scroll in both directions
    pub fn both_directions(self) -> Self {
        self.config.borrow_mut().direction = ScrollDirection::Both;
        self
    }

    /// Set scrollbar size preset
    pub fn size(self, size: ScrollAreaSize) -> Self {
        self.config.borrow_mut().size = size;
        self
    }

    /// Set custom scrollbar width
    pub fn scrollbar_width(self, width: f32) -> Self {
        self.config.borrow_mut().scrollbar_width = Some(width);
        self
    }

    /// Set scrollbar thumb color
    pub fn thumb_color(self, color: impl Into<Color>) -> Self {
        self.config.borrow_mut().thumb_color = Some(color.into());
        self
    }

    /// Set scrollbar track color
    pub fn track_color(self, color: impl Into<Color>) -> Self {
        self.config.borrow_mut().track_color = Some(color.into());
        self
    }

    /// Set viewport width
    pub fn w(self, width: f32) -> Self {
        self.config.borrow_mut().width = Some(width);
        self
    }

    /// Set viewport height
    pub fn h(self, height: f32) -> Self {
        self.config.borrow_mut().height = Some(height);
        self
    }

    /// Enable or disable bounce physics
    pub fn bounce(self, enabled: bool) -> Self {
        self.config.borrow_mut().bounce = enabled;
        self
    }

    /// Disable bounce physics
    pub fn no_bounce(self) -> Self {
        self.config.borrow_mut().bounce = false;
        self
    }

    /// Set corner radius
    pub fn rounded(self, radius: f32) -> Self {
        self.config.borrow_mut().rounded = Some(radius);
        self
    }

    /// Set background color
    pub fn bg(self, color: impl Into<Color>) -> Self {
        self.config.borrow_mut().bg = Some(color.into());
        self
    }

    /// Set the scrollable content
    pub fn child(self, content: Div) -> Self {
        self.config.borrow_mut().content = Some(content);
        self
    }

    /// Build the final ScrollArea component
    pub fn build_final(self) -> ScrollArea {
        let config = self.config.into_inner();
        let built = BuiltScrollArea::from_config(config);
        ScrollArea {
            inner: div().class("cn-scroll-area").child(built.inner),
        }
    }
}

impl Default for ScrollAreaBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl ElementBuilder for ScrollAreaBuilder {
    fn build(&self, tree: &mut LayoutTree) -> LayoutNodeId {
        self.get_or_build().build(tree)
    }

    fn render_props(&self) -> RenderProps {
        self.get_or_build().render_props()
    }

    fn children_builders(&self) -> &[Box<dyn ElementBuilder>] {
        self.get_or_build().children_builders()
    }

    fn event_handlers(&self) -> Option<&blinc_layout::event_handler::EventHandlers> {
        self.get_or_build().event_handlers()
    }

    fn element_classes(&self) -> &[String] {
        self.get_or_build().element_classes()
    }
}

/// Create a new scroll area with customizable scrollbar
///
/// # Example
///
/// ```ignore
/// use blinc_cn::prelude::*;
///
/// // Basic usage with auto-dismiss scrollbar
/// cn::scroll_area()
///     .h(400.0)
///     .child(long_content)
///
/// // Always show scrollbar
/// cn::scroll_area()
///     .scrollbar(ScrollbarVisibility::Always)
///     .h(300.0)
///     .child(content)
///
/// // Horizontal scroll
/// cn::scroll_area()
///     .horizontal()
///     .w(400.0)
///     .child(wide_content)
/// ```
#[track_caller]
pub fn scroll_area() -> ScrollAreaBuilder {
    ScrollAreaBuilder::new()
}

#[cfg(test)]
mod tests {
    use super::*;
    use blinc_theme::ThemeState;

    fn init_theme() {
        let _ = ThemeState::try_get().unwrap_or_else(|| {
            ThemeState::init_default();
            ThemeState::get()
        });
    }

    #[test]
    fn test_scrollbar_width_presets() {
        assert_eq!(ScrollAreaSize::Small.scrollbar_width(), 4.0);
        assert_eq!(ScrollAreaSize::Medium.scrollbar_width(), 6.0);
        assert_eq!(ScrollAreaSize::Large.scrollbar_width(), 10.0);
    }

    #[test]
    fn test_scroll_area_builder_config() {
        init_theme();

        let builder = scroll_area()
            .scrollbar(ScrollbarVisibility::Always)
            .size(ScrollAreaSize::Large)
            .h(500.0)
            .w(300.0);

        let config = builder.config.borrow();
        assert_eq!(config.visibility, ScrollbarVisibility::Always);
        assert_eq!(config.size, ScrollAreaSize::Large);
        assert_eq!(config.height, Some(500.0));
        assert_eq!(config.width, Some(300.0));
    }
}