blinc_layout 0.5.1

Blinc layout engine - Flexbox layout powered by Taffy
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
//! Ready-to-use Button widget
//!
//! A button with built-in hover, press, and disabled states using FSM-driven
//! state management via `Stateful<ButtonState>`. This provides efficient
//! incremental prop updates without tree rebuilds.
//!
//! # Example
//!
//! ```ignore
//! // Simple text button - state persists across rebuilds
//! let btn_state = ctx.use_state_for("my_button", ButtonState::Idle);
//! button(btn_state, "Click me")
//!     .on_click(|_| println!("Clicked!"))
//!     .bg_color(Color::RED)
//!     .hover_color(Color::GREEN)
//!
//! // Button with custom child content
//! let save_btn_state = ctx.use_state_for("save_btn", ButtonState::Idle);
//! button_with(save_btn_state, |_state| {
//!     div().flex_row().gap(8.0)
//!         .child(svg_icon("save"))
//!         .child(text("Save"))
//! })
//! .on_click(|_| save())
//! ```

use std::sync::{Arc, Mutex};

use blinc_core::reactive::SignalId;
use blinc_core::Color;
use blinc_theme::{ColorToken, ThemeState};

use crate::css_parser::{active_stylesheet, ElementState};
use crate::div::{div, Div, ElementBuilder};
use crate::element::RenderProps;
use crate::stateful::{ButtonState, SharedState, Stateful};
use crate::text::text;
use crate::tree::{LayoutNodeId, LayoutTree};

/// Button visual states (re-exported from stateful)
pub use crate::stateful::ButtonState as ButtonVisualState;

/// Button-specific configuration (colors)
#[derive(Clone)]
pub struct ButtonConfig {
    pub label: Option<String>,
    pub text_color: Color,
    pub text_size: f32,
    pub bg_color: Color,
    pub hover_color: Color,
    pub pressed_color: Color,
    pub disabled_color: Color,
    pub disabled: bool,
    /// CSS class names for stylesheet matching
    pub css_classes: Vec<String>,
}

impl Default for ButtonConfig {
    fn default() -> Self {
        let theme = ThemeState::get();
        Self {
            label: None,
            text_color: theme.color(ColorToken::TextInverse),
            text_size: 16.0,
            bg_color: theme.color(ColorToken::Primary),
            hover_color: theme.color(ColorToken::PrimaryHover),
            pressed_color: theme.color(ColorToken::PrimaryActive),
            disabled_color: theme.color(ColorToken::InputBgDisabled),
            disabled: false,
            css_classes: Vec::new(),
        }
    }
}

type ClickHandler = Arc<dyn Fn(&crate::event_handler::EventContext) + Send + Sync>;
type StateCallback = Arc<dyn Fn(ButtonState, &mut Div) + Send + Sync>;

/// Button widget - wraps `Stateful<ButtonState>`
///
/// Buttons can have custom content via `button_with()` or use the simple
/// `button("Label")` constructor for text-only buttons.
pub struct Button {
    inner: Stateful<ButtonState>,
    config: Arc<Mutex<ButtonConfig>>,
    click_handler: Option<ClickHandler>,
    custom_state_callback: Option<StateCallback>,
    extra_deps: Vec<SignalId>,
}

impl Button {
    /// Create a button with a text label and externally-managed state
    ///
    /// The state handle should be created via `ctx.use_state_for()` for persistence
    /// across rebuilds. The label text color can be customized with `.text_color()`.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let btn_state = ctx.use_state_for("my_button", ButtonState::Idle);
    /// Button::new(btn_state, "Click me")
    ///     .on_click(|_| println!("Clicked!"))
    /// ```
    pub fn new(state: SharedState<ButtonState>, label: impl Into<String>) -> Self {
        let config = Arc::new(Mutex::new(ButtonConfig {
            label: Some(label.into()),
            ..Default::default()
        }));

        // Create the inner Stateful - we'll apply bg color dynamically in build()
        // Don't use on_state callback for content since config changes after construction
        let inner = Stateful::with_shared_state(state);

        Self {
            inner,
            config,
            click_handler: None,
            custom_state_callback: None,
            extra_deps: Vec::new(),
        }
    }

    /// Create a button with custom content and externally-managed state
    ///
    /// The state handle should be created via `ctx.use_state_for()` for persistence
    /// across rebuilds. The content builder receives the current button state, allowing
    /// state-dependent content rendering (e.g., different icons for pressed state).
    ///
    /// Note: When using custom content, `text_color()` has no effect.
    /// Style your content directly within the content builder.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let btn_state = ctx.use_state_for("icon_btn", ButtonState::Idle);
    /// Button::with_content(btn_state, |state| {
    ///     div().child(text("Click me").color(Color::WHITE))
    /// })
    /// .on_click(|_| println!("Clicked!"))
    /// ```
    pub fn with_content<F>(state: SharedState<ButtonState>, content_builder: F) -> Self
    where
        F: Fn(ButtonState) -> Div + Send + Sync + 'static,
    {
        let config = Arc::new(Mutex::new(ButtonConfig::default()));

        // Store content builder in config for use in build()
        // We use a custom_state_callback to hold the content builder
        let content_builder = Arc::new(content_builder);

        // Create the inner Stateful
        let inner = Stateful::with_shared_state(state);

        Self {
            inner,
            config,
            click_handler: None,
            custom_state_callback: Some(Arc::new({
                let content_builder = Arc::clone(&content_builder);
                move |state, container: &mut Div| {
                    let content = content_builder(state);
                    container.merge(div().child(content));
                }
            })),
            extra_deps: Vec::new(),
        }
    }

    /// Access the shared config (allows external content builders to read CSS-resolved values)
    pub fn config_arc(&self) -> Arc<Mutex<ButtonConfig>> {
        Arc::clone(&self.config)
    }

    // Button-specific methods
    pub fn bg_color(self, color: impl Into<Color>) -> Self {
        self.config.lock().unwrap().bg_color = color.into();
        self
    }

    pub fn hover_color(self, color: impl Into<Color>) -> Self {
        self.config.lock().unwrap().hover_color = color.into();
        self
    }

    pub fn pressed_color(self, color: impl Into<Color>) -> Self {
        self.config.lock().unwrap().pressed_color = color.into();
        self
    }

    /// Set text color for simple text buttons created with `button("Label")`
    ///
    /// Note: This has no effect on buttons created with `button_with()`.
    /// For custom content buttons, style the text directly in your content builder.
    pub fn text_color(self, color: impl Into<Color>) -> Self {
        self.config.lock().unwrap().text_color = color.into();
        self
    }

    /// Set text size for simple text buttons created with `button("Label")`
    ///
    /// Note: This has no effect on buttons created with `button_with()`.
    pub fn text_size(self, size: f32) -> Self {
        self.config.lock().unwrap().text_size = size;
        self
    }

    pub fn disabled(self, disabled: bool) -> Self {
        self.config.lock().unwrap().disabled = disabled;
        // If disabling, also update the state
        if disabled {
            self.inner.set_state(ButtonState::Disabled);
        }
        self
    }

    pub fn on_click<F>(mut self, handler: F) -> Self
    where
        F: Fn(&crate::event_handler::EventContext) + Send + Sync + 'static,
    {
        // Register click handler on the inner Stateful
        let handler = Arc::new(handler);
        let handler_clone = Arc::clone(&handler);
        self.inner = self.inner.on_click(move |ctx| handler_clone(ctx));
        self.click_handler = Some(handler);
        self
    }

    pub fn on_state<F>(mut self, callback: F) -> Self
    where
        F: Fn(ButtonState, &mut Div) + Send + Sync + 'static,
    {
        self.custom_state_callback = Some(Arc::new(callback));
        self
    }

    pub fn deps(mut self, signal_ids: &[SignalId]) -> Self {
        self.extra_deps = signal_ids.to_vec();
        self.inner = self.inner.deps(&self.extra_deps);
        self
    }

    // Forward ALL Stateful layout methods to inner

    pub fn px(mut self, v: f32) -> Self {
        self.inner = self.inner.px(v);
        self
    }

    pub fn py(mut self, v: f32) -> Self {
        self.inner = self.inner.py(v);
        self
    }

    pub fn p(mut self, v: f32) -> Self {
        self.inner = self.inner.p(v);
        self
    }

    pub fn pt(mut self, v: f32) -> Self {
        self.inner = self.inner.pt(v);
        self
    }

    pub fn pb(mut self, v: f32) -> Self {
        self.inner = self.inner.pb(v);
        self
    }

    pub fn pl(mut self, v: f32) -> Self {
        self.inner = self.inner.pl(v);
        self
    }

    pub fn pr(mut self, v: f32) -> Self {
        self.inner = self.inner.pr(v);
        self
    }

    pub fn rounded(mut self, v: f32) -> Self {
        self.inner = self.inner.rounded(v);
        self
    }

    pub fn border(mut self, width: f32, color: Color) -> Self {
        self.inner = self.inner.border(width, color);
        self
    }

    pub fn border_color(mut self, color: Color) -> Self {
        self.inner = self.inner.border_color(color);
        self
    }

    pub fn border_width(mut self, width: f32) -> Self {
        self.inner = self.inner.border_width(width);
        self
    }

    pub fn w(mut self, v: f32) -> Self {
        self.inner = self.inner.w(v);
        self
    }

    pub fn h(mut self, v: f32) -> Self {
        self.inner = self.inner.h(v);
        self
    }

    pub fn w_full(mut self) -> Self {
        self.inner = self.inner.w_full();
        self
    }

    pub fn h_full(mut self) -> Self {
        self.inner = self.inner.h_full();
        self
    }

    pub fn w_fit(mut self) -> Self {
        self.inner = self.inner.w_fit();
        self
    }

    pub fn h_fit(mut self) -> Self {
        self.inner = self.inner.h_fit();
        self
    }

    pub fn mt(mut self, v: f32) -> Self {
        self.inner = self.inner.mt(v);
        self
    }

    pub fn mb(mut self, v: f32) -> Self {
        self.inner = self.inner.mb(v);
        self
    }

    pub fn ml(mut self, v: f32) -> Self {
        self.inner = self.inner.ml(v);
        self
    }

    pub fn mr(mut self, v: f32) -> Self {
        self.inner = self.inner.mr(v);
        self
    }

    pub fn mx(mut self, v: f32) -> Self {
        self.inner = self.inner.mx(v);
        self
    }

    pub fn my(mut self, v: f32) -> Self {
        self.inner = self.inner.my(v);
        self
    }

    pub fn m(mut self, v: f32) -> Self {
        self.inner = self.inner.m(v);
        self
    }

    pub fn gap(mut self, v: f32) -> Self {
        self.inner = self.inner.gap(v);
        self
    }

    pub fn items_center(mut self) -> Self {
        self.inner = self.inner.items_center();
        self
    }

    pub fn items_start(mut self) -> Self {
        self.inner = self.inner.items_start();
        self
    }

    pub fn items_end(mut self) -> Self {
        self.inner = self.inner.items_end();
        self
    }

    pub fn justify_center(mut self) -> Self {
        self.inner = self.inner.justify_center();
        self
    }

    pub fn justify_start(mut self) -> Self {
        self.inner = self.inner.justify_start();
        self
    }

    pub fn justify_end(mut self) -> Self {
        self.inner = self.inner.justify_end();
        self
    }

    pub fn justify_between(mut self) -> Self {
        self.inner = self.inner.justify_between();
        self
    }

    pub fn flex_row(mut self) -> Self {
        self.inner = self.inner.flex_row();
        self
    }

    pub fn flex_col(mut self) -> Self {
        self.inner = self.inner.flex_col();
        self
    }

    pub fn flex_grow(mut self) -> Self {
        self.inner = self.inner.flex_grow();
        self
    }

    pub fn flex_shrink(mut self) -> Self {
        self.inner = self.inner.flex_shrink();
        self
    }

    pub fn flex_shrink_0(mut self) -> Self {
        self.inner = self.inner.flex_shrink_0();
        self
    }

    pub fn shadow_sm(mut self) -> Self {
        self.inner = self.inner.shadow_sm();
        self
    }

    pub fn shadow_md(mut self) -> Self {
        self.inner = self.inner.shadow_md();
        self
    }

    pub fn shadow_lg(mut self) -> Self {
        self.inner = self.inner.shadow_lg();
        self
    }

    pub fn shadow_xl(mut self) -> Self {
        self.inner = self.inner.shadow_xl();
        self
    }

    pub fn opacity(mut self, v: f32) -> Self {
        self.inner = self.inner.opacity(v);
        self
    }

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

    /// Add a CSS class for selector matching
    pub fn class(mut self, class: &str) -> Self {
        self.config
            .lock()
            .unwrap()
            .css_classes
            .push(class.to_string());
        self.inner = self.inner.class(class);
        self
    }
}

/// Create a button with a text label and context-managed state
///
/// The state handle should be created via `ctx.use_state_for()` for persistence
/// across rebuilds. This is the most common button constructor. For buttons with
/// custom content (icons, multiple elements, etc.), use `button_with()`.
///
/// # Example
/// ```ignore
/// let btn_state = ctx.use_state_for("save_btn", ButtonState::Idle);
/// button(btn_state, "Save")
///     .on_click(|_| save_data())
///     .bg_color(Color::GREEN)
/// ```
pub fn button(state: SharedState<ButtonState>, label: impl Into<String>) -> Button {
    Button::new(state, label)
        .px(12.0)
        .py(6.0)
        .rounded(8.0)
        .items_center()
        .justify_center()
}

/// Create a button with custom content and context-managed state
///
/// The state handle should be created via `ctx.use_state_for()` for persistence
/// across rebuilds. The content builder receives the current button state, allowing
/// state-dependent content (e.g., different icons for pressed state).
///
/// # Example
/// ```ignore
/// // Icon button
/// let trash_btn = ctx.use_state_for("trash_btn", ButtonState::Idle);
/// button_with(trash_btn, |_state| {
///     div().child(svg_icon("trash"))
/// })
/// .on_click(|_| delete_item())
///
/// // Button with icon and text
/// let save_btn = ctx.use_state_for("save_btn", ButtonState::Idle);
/// button_with(save_btn, |_state| {
///     div().flex_row().gap(8.0)
///         .child(svg_icon("save"))
///         .child(text("Save").color(Color::WHITE))
/// })
/// .on_click(|_| save())
///
/// // State-aware button (e.g., loading spinner when pressed)
/// let submit_btn = ctx.use_state_for("submit_btn", ButtonState::Idle);
/// button_with(submit_btn, |state| {
///     if matches!(state, ButtonState::Pressed) {
///         div().child(spinner())
///     } else {
///         div().child(text("Submit").color(Color::WHITE))
///     }
/// })
/// ```
pub fn button_with<F>(state: SharedState<ButtonState>, content_builder: F) -> Button
where
    F: Fn(ButtonState) -> Div + Send + Sync + 'static,
{
    Button::with_content(state, content_builder)
        .px(12.0)
        .py(6.0)
        .rounded(8.0)
        .items_center()
        .justify_center()
}

impl ElementBuilder for Button {
    fn build(&self, tree: &mut LayoutTree) -> LayoutNodeId {
        tracing::debug!("Button::build called");
        // Capture current config values for the on_state callback
        let config_for_state = Arc::clone(&self.config);
        let custom_callback = self.custom_state_callback.clone();
        // Capture element ID for CSS override resolution
        let css_element_id = self.inner.element_id().map(|s| s.to_string());

        // Ensure state transition handlers (hover, press) are registered
        // This is needed because Button bypasses Stateful::on_state() and sets
        // the callback directly, so register_state_handlers() is never called.
        self.inner.ensure_state_handlers_registered();

        // Register on_state callback with current config
        // This will be applied by Stateful::build() when it sees needs_visual_update
        {
            let shared_state = self.inner.shared_state();
            let mut shared = shared_state.lock().unwrap();
            shared.state_callback =
                Some(Arc::new(move |state: &ButtonState, container: &mut Div| {
                    tracing::debug!("Button on_state callback fired, state={:?}", state);
                    let mut cfg = config_for_state.lock().unwrap();

                    // Apply CSS overrides if stylesheet is active
                    apply_css_overrides_button(
                        &mut cfg,
                        css_element_id.as_deref(),
                        state,
                        container,
                    );

                    let bg = match state {
                        ButtonState::Idle => cfg.bg_color,
                        ButtonState::Hovered => cfg.hover_color,
                        ButtonState::Pressed => cfg.pressed_color,
                        ButtonState::Disabled => cfg.disabled_color,
                    };

                    // Apply background color and content
                    let mut update = div().bg(bg);

                    // Add content based on whether we have custom content or label
                    if let Some(ref callback) = custom_callback {
                        // Drop config lock before calling custom callback to avoid
                        // deadlock if the callback reads the config (e.g. for text_color)
                        drop(cfg);
                        callback(*state, &mut update);
                    } else if let Some(ref label) = cfg.label {
                        tracing::debug!("Button adding label child: {}", label);
                        update =
                            update.child(text(label).size(cfg.text_size).color(cfg.text_color));
                        drop(cfg);
                    } else {
                        drop(cfg);
                    }

                    let update_children = update.children.len();
                    tracing::debug!(
                        "Button update div has {} children before merge",
                        update_children
                    );
                    container.merge(update);
                    let container_children = container.children.len();
                    tracing::debug!(
                        "Button container has {} children after merge",
                        container_children
                    );
                }));
            shared.base_render_props = Some(self.inner.inner_render_props());
            shared.base_style = self.inner.inner_layout_style();
            shared.needs_visual_update = true;
        }

        // Build the inner Stateful - it will apply the callback we just set
        self.inner.build(tree)
    }

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

    fn children_builders(&self) -> &[Box<dyn ElementBuilder>] {
        // Delegate to the inner Stateful which has the cached children
        self.inner.children_builders()
    }

    fn element_type_id(&self) -> crate::div::ElementTypeId {
        crate::div::ElementTypeId::Div
    }

    fn semantic_type_name(&self) -> Option<&'static str> {
        Some("button")
    }

    fn element_id(&self) -> Option<&str> {
        self.inner.element_id()
    }

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

    fn event_handlers(&self) -> Option<&crate::event_handler::EventHandlers> {
        // Delegate to the inner Stateful which has the cached event handlers
        self.inner.event_handlers()
    }

    fn layout_style(&self) -> Option<&taffy::Style> {
        self.inner.layout_style()
    }
}

/// Apply CSS overrides from active stylesheet to button config
fn apply_css_overrides_button(
    cfg: &mut ButtonConfig,
    css_element_id: Option<&str>,
    state: &ButtonState,
    container: &mut Div,
) {
    let stylesheet = match active_stylesheet() {
        Some(s) => s,
        None => return,
    };

    // Helper to apply a single ElementStyle to the button config + container
    let apply = |cfg: &mut ButtonConfig,
                 container: &mut Div,
                 style: &crate::element_style::ElementStyle,
                 is_state_specific: bool| {
        if let Some(blinc_core::Brush::Solid(c)) = style.background.as_ref() {
            if is_state_specific {
                match state {
                    ButtonState::Idle => cfg.bg_color = *c,
                    ButtonState::Hovered => cfg.hover_color = *c,
                    ButtonState::Pressed => cfg.pressed_color = *c,
                    ButtonState::Disabled => cfg.disabled_color = *c,
                }
            } else {
                cfg.bg_color = *c;
            }
        }
        if let Some(c) = style.text_color {
            cfg.text_color = c;
        }
        if let Some(fs) = style.font_size {
            cfg.text_size = fs;
        }
        // Corner radius — NOT applied here. Border-radius is handled by the
        // renderer's apply_stylesheet_base_styles(), which correctly evaluates
        // hierarchical selectors (e.g. `.sidebar .cn-button--secondary`).
        // Applying it here from simple class styles would overwrite higher-specificity
        // hierarchical CSS on every state change.
        //
        // Border
        if let Some(bw) = style.border_width {
            if let Some(bc) = style.border_color {
                container.set_border(bw, bc);
            }
        } else if let Some(bc) = style.border_color {
            // border-color only (keep existing width)
            container.border_color = Some(bc);
        }
    };

    let element_state = match state {
        ButtonState::Hovered => Some(ElementState::Hover),
        ButtonState::Pressed => Some(ElementState::Active),
        ButtonState::Disabled => Some(ElementState::Disabled),
        ButtonState::Idle => None,
    };

    // 1. Resolve by class (lowest priority)
    let classes = cfg.css_classes.clone();
    for class in &classes {
        if let Some(base) = stylesheet.get_class(class) {
            apply(cfg, container, base, false);
        }
        if let Some(s) = element_state {
            if let Some(state_style) = stylesheet.get_class_with_state(class, s) {
                apply(cfg, container, state_style, true);
            }
        }
    }

    // 2. Resolve by element ID (higher priority)
    if let Some(id) = css_element_id {
        if let Some(base) = stylesheet.get(id) {
            apply(cfg, container, base, false);
        }
        if let Some(s) = element_state {
            if let Some(state_style) = stylesheet.get_with_state(id, s) {
                apply(cfg, container, state_style, true);
            }
        }
    }
}