bevy_ui_builders/checkbox/
types.rs

1//! Checkbox component types and styles
2
3use bevy::prelude::*;
4
5/// Marker component for checkbox entities
6#[derive(Component, Debug, Clone, Copy)]
7pub struct Checkbox;
8
9/// State of a checkbox (checked or unchecked)
10#[derive(Component, Debug, Clone, Copy, PartialEq, Eq)]
11pub enum CheckboxState {
12    /// Checkbox is unchecked
13    Unchecked,
14    /// Checkbox is checked
15    Checked,
16}
17
18impl Default for CheckboxState {
19    fn default() -> Self {
20        Self::Unchecked
21    }
22}
23
24impl CheckboxState {
25    /// Toggle the checkbox state
26    pub fn toggle(&mut self) {
27        *self = match self {
28            Self::Unchecked => Self::Checked,
29            Self::Checked => Self::Unchecked,
30        };
31    }
32
33    /// Check if the checkbox is checked
34    pub fn is_checked(&self) -> bool {
35        matches!(self, Self::Checked)
36    }
37}
38
39/// Visual style variants for checkboxes
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub enum CheckboxStyle {
42    /// Default gray style
43    Default,
44    /// Primary blue style (recommended for most use cases)
45    Primary,
46    /// Success green style
47    Success,
48    /// Danger red style
49    Danger,
50}
51
52impl Default for CheckboxStyle {
53    fn default() -> Self {
54        Self::Primary
55    }
56}
57
58impl CheckboxStyle {
59    /// Get the background color for the checked state
60    pub fn checked_color(&self) -> Color {
61        match self {
62            Self::Default => crate::styles::colors::SECONDARY,
63            Self::Primary => crate::styles::colors::PRIMARY,
64            Self::Success => crate::styles::colors::SUCCESS,
65            Self::Danger => crate::styles::colors::DANGER,
66        }
67    }
68
69    /// Get the background color for the unchecked state
70    pub fn unchecked_color(&self) -> Color {
71        crate::styles::colors::BACKGROUND_SECONDARY
72    }
73
74    /// Get the border color
75    pub fn border_color(&self) -> Color {
76        crate::styles::colors::BORDER_DEFAULT
77    }
78}
79
80/// Component that stores the visual style of a checkbox
81#[derive(Component, Debug, Clone, Copy)]
82pub struct CheckboxStyleComponent(pub CheckboxStyle);
83
84/// Marker component for the checkmark icon inside a checkbox
85#[derive(Component, Debug, Clone, Copy)]
86pub struct CheckboxCheckmark;