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
// (C) 2025 - Enzo Lombardi
//! CheckBox view - boolean selection control with checkable items.
// CheckBox - Boolean selection control
//
// Matches Borland: TCheckBoxes (extends TCluster)
//
// A checkbox control displays a box with a label. The box can be checked or unchecked
// by clicking on it or pressing Space when focused.
//
// Visual appearance:
// [ ] Unchecked option
// [X] Checked option
//
// Architecture: Uses Cluster trait for shared button group behavior
//
// Usage:
// let checkbox = CheckBox::new(
// Rect::new(3, 5, 20, 6),
// "Enable feature",
// );
use crate::core::event::Event;
use crate::core::geometry::Rect;
use crate::core::state::StateFlags;
use crate::terminal::Terminal;
use super::view::View;
use super::cluster::{Cluster, ClusterState};
/// CheckBox - A boolean selection control with a label
///
/// Now implements Cluster trait for standard button group behavior.
/// Matches Borland: TCheckBoxes (extends TCluster)
#[derive(Debug)]
pub struct CheckBox {
bounds: Rect,
label: String,
cluster_state: ClusterState,
state: StateFlags,
}
impl CheckBox {
/// Create a new checkbox with the given bounds and label
pub fn new(bounds: Rect, label: &str) -> Self {
CheckBox {
bounds,
label: label.to_string(),
cluster_state: ClusterState::new(),
state: 0,
}
}
/// Set the checked state
pub fn set_checked(&mut self, checked: bool) {
self.cluster_state.set_value(if checked { 1 } else { 0 });
}
/// Get the checked state
pub fn is_checked(&self) -> bool {
self.cluster_state.value != 0
}
/// Toggle the checked state
pub fn toggle(&mut self) {
self.cluster_state.toggle();
}
}
impl View for CheckBox {
fn bounds(&self) -> Rect {
self.bounds
}
fn set_bounds(&mut self, bounds: Rect) {
self.bounds = bounds;
}
fn handle_event(&mut self, event: &mut Event) {
// Use Cluster trait's standard event handling
self.handle_cluster_event(event);
}
fn draw(&mut self, terminal: &mut Terminal) {
// Use Cluster trait's standard drawing
self.draw_cluster(terminal);
}
fn can_focus(&self) -> bool {
true
}
fn state(&self) -> StateFlags {
self.state
}
fn set_state(&mut self, state: StateFlags) {
self.state = state;
}
}
// Implement Cluster trait
impl Cluster for CheckBox {
fn cluster_state(&self) -> &ClusterState {
&self.cluster_state
}
fn cluster_state_mut(&mut self) -> &mut ClusterState {
&mut self.cluster_state
}
fn get_label(&self) -> &str {
&self.label
}
fn get_marker(&self) -> &str {
if self.is_checked() {
"[X] "
} else {
"[ ] "
}
}
/// Checkboxes toggle on space (default behavior)
fn on_space_pressed(&mut self) {
self.toggle();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_checkbox_creation() {
let checkbox = CheckBox::new(Rect::new(0, 0, 20, 1), "Test option");
assert!(!checkbox.is_checked());
assert_eq!(checkbox.label, "Test option");
}
#[test]
fn test_checkbox_toggle() {
let mut checkbox = CheckBox::new(Rect::new(0, 0, 20, 1), "Test");
assert!(!checkbox.is_checked());
checkbox.toggle();
assert!(checkbox.is_checked());
checkbox.toggle();
assert!(!checkbox.is_checked());
}
#[test]
fn test_checkbox_set_checked() {
let mut checkbox = CheckBox::new(Rect::new(0, 0, 20, 1), "Test");
checkbox.set_checked(true);
assert!(checkbox.is_checked());
checkbox.set_checked(false);
assert!(!checkbox.is_checked());
}
}