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
// (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,
palette_chain: Option<crate::core::palette_chain::PaletteChainNode>,
}
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,
palette_chain: None,
}
}
/// 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;
}
fn set_palette_chain(&mut self, node: Option<crate::core::palette_chain::PaletteChainNode>) {
self.palette_chain = node;
}
fn get_palette_chain(&self) -> Option<&crate::core::palette_chain::PaletteChainNode> {
self.palette_chain.as_ref()
}
fn get_palette(&self) -> Option<crate::core::palette::Palette> {
use crate::core::palette::{Palette, palettes};
Some(Palette::from_slice(palettes::CP_CLUSTER))
}
}
// 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();
}
}
/// Builder for creating checkboxes with a fluent API.
///
/// # Examples
///
/// ```ignore
/// use turbo_vision::views::checkbox::CheckBoxBuilder;
/// use turbo_vision::core::geometry::Rect;
///
/// // Create an unchecked checkbox
/// let checkbox = CheckBoxBuilder::new()
/// .bounds(Rect::new(3, 5, 30, 6))
/// .label("Enable feature")
/// .build();
///
/// // Create a pre-checked checkbox
/// let checkbox = CheckBoxBuilder::new()
/// .bounds(Rect::new(3, 6, 30, 7))
/// .label("Auto-save")
/// .checked(true)
/// .build();
/// ```
pub struct CheckBoxBuilder {
bounds: Option<Rect>,
label: Option<String>,
checked: bool,
}
impl CheckBoxBuilder {
/// Creates a new CheckBoxBuilder with default values.
pub fn new() -> Self {
Self {
bounds: None,
label: None,
checked: false,
}
}
/// Sets the checkbox bounds (required).
#[must_use]
pub fn bounds(mut self, bounds: Rect) -> Self {
self.bounds = Some(bounds);
self
}
/// Sets the checkbox label (required).
#[must_use]
pub fn label(mut self, label: impl Into<String>) -> Self {
self.label = Some(label.into());
self
}
/// Sets whether the checkbox is initially checked (default: false).
#[must_use]
pub fn checked(mut self, checked: bool) -> Self {
self.checked = checked;
self
}
/// Builds the CheckBox.
///
/// # Panics
///
/// Panics if required fields (bounds, label) are not set.
pub fn build(self) -> CheckBox {
let bounds = self.bounds.expect("CheckBox bounds must be set");
let label = self.label.expect("CheckBox label must be set");
let mut checkbox = CheckBox::new(bounds, &label);
if self.checked {
checkbox.set_checked(true);
}
checkbox
}
/// Builds the CheckBox as a Box.
pub fn build_boxed(self) -> Box<CheckBox> {
Box::new(self.build())
}
}
impl Default for CheckBoxBuilder {
fn default() -> Self {
Self::new()
}
}
#[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());
}
#[test]
fn test_checkbox_builder() {
let checkbox = CheckBoxBuilder::new()
.bounds(Rect::new(3, 5, 30, 6))
.label("Test Feature")
.build();
assert_eq!(checkbox.label, "Test Feature");
assert!(!checkbox.is_checked());
}
#[test]
fn test_checkbox_builder_checked() {
let checkbox = CheckBoxBuilder::new()
.bounds(Rect::new(3, 5, 30, 6))
.label("Auto-save")
.checked(true)
.build();
assert!(checkbox.is_checked());
}
}