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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
use crossbeam::{Receiver, Sender};
use cursive::direction::Direction;
use cursive::event::{AnyCb, Event, EventResult};
use cursive::view::{Selector, View};
use cursive::{Printer, Rect, Vec2};
use log::debug;
use std::collections::HashMap;
use std::hash::Hash;
mod bar;
mod panel;
use bar::{Bar, TabBar};
pub use panel::TabPanel;
pub struct TabView<K: Hash> {
current_id: Option<K>,
map: HashMap<K, Box<dyn View + Send>>,
key_order: Vec<K>,
bar_rx: Option<Receiver<K>>,
active_key_tx: Option<Sender<K>>,
}
impl<K: Hash + Eq + Copy + 'static> TabView<K> {
pub fn new() -> Self {
Self {
current_id: None,
map: HashMap::new(),
key_order: Vec::new(),
bar_rx: None,
active_key_tx: None,
}
}
pub fn active_tab(&self) -> Option<K> {
self.current_id
}
pub fn set_active_tab(&mut self, id: K) -> Result<(), ()> {
if self.map.contains_key(&id) {
if let Some(sender) = &self.active_key_tx {
match sender.send(id) {
Ok(_) => {}
Err(e) => debug!(
"error occured while trying to send new active key to sender: {}",
e
),
}
}
self.current_id = Some(id);
Ok(())
} else {
Err(())
}
}
pub fn with_active_tab(mut self, id: K) -> Result<Self, ()> {
self.set_active_tab(id)?;
Ok(self)
}
pub fn add_tab<T: View + Send>(&mut self, id: K, view: T) {
self.map.insert(id, Box::new(view));
self.current_id = Some(id);
self.key_order.push(id);
}
pub fn with_tab<T: View + Send>(mut self, id: K, view: T) -> Self {
self.add_tab(id, view);
self
}
pub fn remove_tab(&mut self, id: K) -> Result<(), ()> {
if let Some(_) = self.map.remove(&id) {
if let Some(key) = &self.current_id {
if *key == id {
self.current_id = None;
}
}
self.key_order = self
.key_order
.iter()
.filter_map(|key| if id == *key { None } else { Some(*key) })
.collect();
Ok(())
} else {
Err(())
}
}
pub fn tab_order(&self) -> Vec<K> {
self.key_order.clone()
}
fn index_key(cur_key: &K, key_order: &Vec<K>) -> usize {
let mut count: usize = 0;
for key in key_order {
if *key != *cur_key {
count += 1;
} else {
break;
}
}
count
}
pub fn next(&mut self) {
if let Some(cur_key) = self.current_id {
self.set_active_tab(
self.key_order
[(Self::index_key(&cur_key, &self.key_order) + 1) % self.key_order.len()],
)
.expect("Key content changed during operation, this should not happen");
}
}
pub fn prev(&mut self) {
if let Some(cur_key) = self.current_id {
self.set_active_tab(
self.key_order[(self.key_order.len() + Self::index_key(&cur_key, &self.key_order)
- 1)
% self.key_order.len()],
)
.expect("Key content changed during operation, this should not happen");
}
}
}
impl<K: Hash + Eq + Copy + 'static> View for TabView<K> {
fn draw(&self, printer: &Printer) {
if let Some(key) = &self.current_id {
if let Some(view) = self.map.get(&key) {
view.draw(printer);
}
}
}
fn layout(&mut self, size: Vec2) {
if let Some(key) = &self.current_id {
if let Some(view) = self.map.get_mut(&key) {
view.layout(size);
}
}
}
fn required_size(&mut self, req: Vec2) -> Vec2 {
if let Some(rx) = &self.bar_rx {
if let Ok(evt) = rx.try_recv() {
match self.set_active_tab(evt) {
Ok(_) => {}
Err(err) => debug!("could not accept tab bar event: {:?}", err),
}
}
}
if let Some(key) = &self.current_id {
if let Some(view) = self.map.get_mut(&key) {
view.required_size(req)
} else {
(1, 1).into()
}
} else {
(1, 1).into()
}
}
fn on_event(&mut self, evt: Event) -> EventResult {
if let Some(key) = &self.current_id {
if let Some(view) = self.map.get_mut(&key) {
view.on_event(evt)
} else {
EventResult::Ignored
}
} else {
EventResult::Ignored
}
}
fn take_focus(&mut self, src: Direction) -> bool {
if let Some(key) = &self.current_id {
if let Some(view) = self.map.get_mut(&key) {
view.take_focus(src)
} else {
false
}
} else {
false
}
}
fn call_on_any<'a>(&mut self, slt: &Selector, cb: AnyCb<'a>) {
if let Some(key) = &self.current_id {
if let Some(view) = self.map.get_mut(&key) {
view.call_on_any(slt, cb);
}
}
}
fn focus_view(&mut self, slt: &Selector) -> Result<(), ()> {
if let Some(key) = &self.current_id {
if let Some(view) = self.map.get_mut(&key) {
view.focus_view(slt)
} else {
Err(())
}
} else {
Err(())
}
}
fn needs_relayout(&self) -> bool {
true
}
fn important_area(&self, size: Vec2) -> Rect {
if let Some(key) = &self.current_id {
if let Some(view) = self.map.get(&key) {
view.important_area(size)
} else {
Rect::from((1, 1))
}
} else {
Rect::from((1, 1))
}
}
}
#[cfg(test)]
mod test {
use super::TabView;
use cursive::views::DummyView;
#[test]
fn smoke() {
let _ = TabView::<i32>::new();
}
#[test]
fn insert() {
let mut tabs = TabView::<i32>::new().with_tab(0, DummyView);
tabs.add_tab(1, DummyView);
}
#[test]
fn switch() {
let mut tabs = TabView::<i32>::new();
tabs.add_tab(0, DummyView);
tabs.add_tab(1, DummyView);
assert_eq!(tabs.active_tab().expect("Id not correct"), 1);
tabs.set_active_tab(0).expect("Id not taken");
assert_eq!(tabs.active_tab().expect("Id not correct"), 0);
}
#[test]
fn remove() {
let mut tabs = TabView::<i32>::new();
tabs.add_tab(0, DummyView);
tabs.add_tab(1, DummyView);
assert_eq!(tabs.remove_tab(1), Ok(()));
assert!(tabs.active_tab().is_none());
}
}