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
use {Printer, With};
use direction::Direction;
use event::{Event, EventResult, Key, MouseButton, MouseEvent};
use std::cell::RefCell;
use std::rc::Rc;
use theme::ColorStyle;
use vec::Vec2;
use view::View;
struct SharedState<T> {
selection: usize,
values: Vec<Rc<T>>,
}
impl<T> SharedState<T> {
pub fn selection(&self) -> Rc<T> {
Rc::clone(&self.values[self.selection])
}
}
#[derive(Clone)]
pub struct RadioGroup<T> {
state: Rc<RefCell<SharedState<T>>>,
}
impl<T> Default for RadioGroup<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> RadioGroup<T> {
pub fn new() -> Self {
RadioGroup {
state: Rc::new(RefCell::new(SharedState {
selection: 0,
values: Vec::new(),
})),
}
}
pub fn button<S: Into<String>>(
&mut self, value: T, label: S
) -> RadioButton<T> {
let count = self.state.borrow().values.len();
self.state.borrow_mut().values.push(Rc::new(value));
RadioButton::new(Rc::clone(&self.state), count, label.into())
}
pub fn selected_id(&self) -> usize {
self.state.borrow().selection
}
pub fn selection(&self) -> Rc<T> {
self.state.borrow().selection()
}
}
impl RadioGroup<String> {
pub fn button_str<S: Into<String>>(
&mut self, text: S
) -> RadioButton<String> {
let text = text.into();
self.button(text.clone(), text)
}
}
pub struct RadioButton<T> {
state: Rc<RefCell<SharedState<T>>>,
id: usize,
enabled: bool,
label: String,
}
impl<T> RadioButton<T> {
impl_enabled!(self.enabled);
fn new(
state: Rc<RefCell<SharedState<T>>>, id: usize, label: String
) -> Self {
RadioButton {
state: state,
id: id,
enabled: true,
label: label,
}
}
pub fn is_selected(&self) -> bool {
self.state.borrow().selection == self.id
}
pub fn select(&mut self) {
self.state.borrow_mut().selection = self.id;
}
pub fn selected(self) -> Self {
self.with(Self::select)
}
fn draw_internal(&self, printer: &Printer) {
printer.print((0, 0), "( )");
if self.is_selected() {
printer.print((1, 0), "X");
}
if !self.label.is_empty() {
printer.print((3, 0), " ");
printer.print((4, 0), &self.label);
}
}
fn req_size(&self) -> Vec2 {
if self.label.is_empty() {
Vec2::new(3, 1)
} else {
Vec2::new(3 + 1 + self.label.len(), 1)
}
}
}
impl<T> View for RadioButton<T> {
fn required_size(&mut self, _: Vec2) -> Vec2 {
self.req_size()
}
fn take_focus(&mut self, _: Direction) -> bool {
self.enabled
}
fn draw(&self, printer: &Printer) {
if self.enabled {
printer.with_selection(
printer.focused,
|printer| self.draw_internal(printer),
);
} else {
printer.with_color(
ColorStyle::Secondary,
|printer| self.draw_internal(printer),
);
}
}
fn on_event(&mut self, event: Event) -> EventResult {
match event {
Event::Key(Key::Enter) | Event::Char(' ') => {
self.select();
EventResult::Consumed(None)
}
Event::Mouse {
event: MouseEvent::Release(MouseButton::Left),
position,
offset,
} if position.fits_in_rect(offset, self.req_size()) =>
{
self.select();
EventResult::Consumed(None)
}
_ => EventResult::Ignored,
}
}
}