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
extern crate std;
use std::convert::TryFrom;
extern crate tui;
use tui::buffer::Buffer;
use tui::layout::{Alignment, Rect};
use tui::style::{Modifier, Style};
use tui::text::{Span, Spans, Text};
use tui::widgets::{Block, Borders, Paragraph, Widget};
use crate::userinterface::widgets::BORDERS;
pub struct SelectorPopupWidget<'a> {
keyboard_instructions: Text<'a>,
value: Vec<String>,
cursor: usize,
}
impl<'a> SelectorPopupWidget<'a> {
pub fn new(keyboard_instructions: Text<'a>, value: Vec<String>,
cursor: usize) -> SelectorPopupWidget<'a> {
SelectorPopupWidget {
keyboard_instructions,
value,
cursor,
}
}
}
impl<'a> Widget for SelectorPopupWidget<'a> {
fn render(self, area: Rect, buf: &mut Buffer) {
let (left, top, width, height) = (area.left(), area.top(),
area.right() - area.left(), area.bottom() - area.top());
let kb_height =
u16::try_from(self.keyboard_instructions.height()).unwrap();
let value_area = Rect::new(left, top + 4 + kb_height, width,
u16::try_from(self.value.len()).unwrap());
let selector = self.value.iter().enumerate().map(|(index, key)| {
if index == self.cursor {
Spans::from(vec![Span::styled(format!(" ▸ {} ◂ ", key),
Style::default().add_modifier(Modifier::REVERSED))])
} else {
Spans::from(vec![Span::raw(key)])
}
}).collect::<Vec<Spans>>();
Paragraph::new(selector).alignment(Alignment::Center)
.render(value_area, buf);
let keyboard_instructions_area =
Rect::new(left + 1, top + 2, width - BORDERS, height - BORDERS);
Paragraph::new(self.keyboard_instructions).alignment(Alignment::Center)
.render(keyboard_instructions_area, buf);
let window_block = Block::default().borders(Borders::ALL);
window_block.render(area, buf);
}
}