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
// (C) 2025 - Enzo Lombardi
//! Cluster - base trait and state management for grouped button controls.
// Cluster - Base trait and state for button group controls
//
// Matches Borland: TCluster (cluster.h, tcluster.cc)
//
// This module provides the foundational infrastructure for button group controls:
// - ClusterState: Shared state (selection, items, group management)
// - Cluster trait: Common behavior with default implementations
//
// Architecture: Hybrid trait + helper struct approach (same as ListViewer/MenuViewer)
//
// Borland inheritance:
// TView → TCluster → TCheckBoxes, TRadioButtons
//
// Rust composition:
// View trait + Cluster trait → CheckBox, RadioButton (embed ClusterState)
use crate::core::event::{Event, EventType};
use crate::core::palette::Attr;
use super::view::View;
/// State management for cluster (button group) components
///
/// Matches Borland: TCluster fields
///
/// This struct holds the common state for all button group controls.
/// Components embed this and expose it via the Cluster trait.
#[derive(Clone, Debug)]
pub struct ClusterState {
/// Current selection value
/// For CheckBox: 0 = unchecked, 1 = checked
/// For RadioButton: index of selected button in group
pub value: u32,
/// Group ID for radio button groups
/// Radio buttons with same group_id are mutually exclusive
pub group_id: u16,
/// Whether to enable keyboard selection with space
pub enable_keyboard: bool,
}
impl ClusterState {
/// Create a new cluster state
pub fn new() -> Self {
Self {
value: 0,
group_id: 0,
enable_keyboard: true,
}
}
/// Create with a specific group ID (for radio buttons)
pub fn with_group(group_id: u16) -> Self {
Self {
value: 0,
group_id,
enable_keyboard: true,
}
}
/// Check if a specific item is selected
pub fn is_selected(&self, item_value: u32) -> bool {
self.value == item_value
}
/// Set the selection value
pub fn set_value(&mut self, value: u32) {
self.value = value;
}
/// Toggle selection (for checkboxes)
pub fn toggle(&mut self) {
self.value = if self.value == 0 { 1 } else { 0 };
}
}
impl Default for ClusterState {
fn default() -> Self {
Self::new()
}
}
/// Trait for button group (cluster) components
///
/// Matches Borland: TCluster virtual methods
///
/// This trait provides the common interface for all button group controls.
/// Components implement this trait and embed ClusterState for shared logic.
pub trait Cluster: View {
/// Get the cluster state (read-only)
fn cluster_state(&self) -> &ClusterState;
/// Get the cluster state (mutable)
fn cluster_state_mut(&mut self) -> &mut ClusterState;
/// Get the label text for display
fn get_label(&self) -> &str;
/// Get the marker string for this control
///
/// Examples:
/// - CheckBox unchecked: "[ ] "
/// - CheckBox checked: "[X] "
/// - RadioButton unselected: "( ) "
/// - RadioButton selected: "(•) "
fn get_marker(&self) -> &str;
/// Get the current selection value
fn get_value(&self) -> u32 {
self.cluster_state().value
}
/// Set the selection value
fn set_value(&mut self, value: u32) {
self.cluster_state_mut().set_value(value);
}
/// Check if currently selected/checked
fn is_selected(&self) -> bool {
self.cluster_state().value != 0
}
/// Toggle selection (for checkboxes)
fn toggle(&mut self) {
self.cluster_state_mut().toggle();
}
/// Get the group ID
fn group_id(&self) -> u16 {
self.cluster_state().group_id
}
/// Get colors based on focus state
///
/// Returns (normal_color, hotkey_color)
fn get_colors(&self) -> (Attr, Attr) {
use crate::core::palette::{Attr, TvColor};
if self.is_focused() {
(
Attr::new(TvColor::Yellow, TvColor::Blue),
Attr::new(TvColor::LightRed, TvColor::Blue),
)
} else {
(
Attr::new(TvColor::Black, TvColor::LightGray),
Attr::new(TvColor::Red, TvColor::LightGray),
)
}
}
/// Handle standard cluster events
///
/// Matches Borland: TCluster::handleEvent() keyboard logic
/// Returns true if event was handled
fn handle_cluster_event(&mut self, event: &mut Event) -> bool {
if event.what == EventType::Keyboard && self.is_focused() {
if self.cluster_state().enable_keyboard {
// Space key toggles/selects
if event.key_code == ' ' as u16 {
self.on_space_pressed();
event.clear();
return true;
}
}
}
false
}
/// Called when space key is pressed
///
/// Default: toggle for checkboxes, select for radio buttons
/// Subclasses can override for custom behavior
fn on_space_pressed(&mut self) {
// Default behavior: toggle
self.toggle();
}
/// Draw the cluster control with marker and label
///
/// Provides common drawing logic for all cluster controls
fn draw_cluster(&self, terminal: &mut crate::terminal::Terminal) {
use crate::core::draw::DrawBuffer;
use crate::views::view::write_line_to_terminal;
let bounds = self.bounds();
let width = bounds.width() as usize;
let mut buffer = DrawBuffer::new(width);
let (color, hotkey_color) = self.get_colors();
// Draw marker (checkbox/radio button)
let marker = self.get_marker();
buffer.move_str(0, marker, color);
// Draw label with hotkey support
let label = self.get_label();
buffer.move_str_with_shortcut(marker.len(), label, color, hotkey_color);
write_line_to_terminal(terminal, bounds.a.x, bounds.a.y, &buffer);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cluster_state_creation() {
let state = ClusterState::new();
assert_eq!(state.value, 0);
assert_eq!(state.group_id, 0);
assert!(state.enable_keyboard);
}
#[test]
fn test_cluster_state_with_group() {
let state = ClusterState::with_group(5);
assert_eq!(state.value, 0);
assert_eq!(state.group_id, 5);
}
#[test]
fn test_cluster_state_selection() {
let mut state = ClusterState::new();
assert!(!state.is_selected(1));
state.set_value(1);
assert!(state.is_selected(1));
assert!(!state.is_selected(2));
}
#[test]
fn test_cluster_state_toggle() {
let mut state = ClusterState::new();
assert_eq!(state.value, 0);
state.toggle();
assert_eq!(state.value, 1);
state.toggle();
assert_eq!(state.value, 0);
}
}