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
use {Cursive, Printer};
use With;
use direction::{Direction, Orientation};
use event::{Callback, Event, EventResult, Key};
use std::rc::Rc;
use theme::ColorStyle;
use vec::Vec2;
use view::View;
pub struct SliderView {
orientation: Orientation,
on_change: Option<Rc<Fn(&mut Cursive, usize)>>,
on_enter: Option<Rc<Fn(&mut Cursive, usize)>>,
value: usize,
max_value: usize,
}
impl SliderView {
pub fn new(orientation: Orientation, max_value: usize) -> Self {
SliderView {
orientation: orientation,
value: 0,
max_value: max_value,
on_change: None,
on_enter: None,
}
}
pub fn vertical(max_value: usize) -> Self {
Self::new(Orientation::Vertical, max_value)
}
pub fn horizontal(max_value: usize) -> Self {
Self::new(Orientation::Horizontal, max_value)
}
pub fn set_value(&mut self, value: usize) -> EventResult {
self.value = value;
self.get_change_result()
}
pub fn value(self, value: usize) -> Self {
self.with(|s| { s.set_value(value); })
}
pub fn on_change<F>(mut self, callback: F) -> Self
where F: Fn(&mut Cursive, usize) + 'static
{
self.on_change = Some(Rc::new(callback));
self
}
pub fn on_enter<F>(mut self, callback: F) -> Self
where F: Fn(&mut Cursive, usize) + 'static
{
self.on_enter = Some(Rc::new(callback));
self
}
fn get_change_result(&self) -> EventResult {
EventResult::Consumed(self.on_change.clone().map(|cb| {
let value = self.value;
Callback::from_fn(move |s| { cb(s, value); })
}))
}
fn slide_plus(&mut self) -> EventResult {
if self.value + 1 < self.max_value {
self.value += 1;
self.get_change_result()
} else {
EventResult::Ignored
}
}
fn slide_minus(&mut self) -> EventResult {
if self.value > 0 {
self.value -= 1;
self.get_change_result()
} else {
EventResult::Ignored
}
}
}
impl View for SliderView {
fn draw(&self, printer: &Printer) {
match self.orientation {
Orientation::Vertical => {
printer.print_vline((0, 0), self.max_value, "|")
}
Orientation::Horizontal => {
printer.print_hline((0, 0), self.max_value, "-")
}
}
let color = if printer.focused {
ColorStyle::Highlight
} else {
ColorStyle::HighlightInactive
};
printer.with_color(color, |printer| {
printer.print(self.orientation.make_vec(self.value, 0), " ");
});
}
fn required_size(&mut self, _: Vec2) -> Vec2 {
self.orientation.make_vec(self.max_value, 1)
}
fn on_event(&mut self, event: Event) -> EventResult {
match event {
Event::Key(Key::Left) if self.orientation ==
Orientation::Horizontal => {
self.slide_minus()
}
Event::Key(Key::Right) if self.orientation ==
Orientation::Horizontal => {
self.slide_plus()
}
Event::Key(Key::Up) if self.orientation ==
Orientation::Vertical => self.slide_minus(),
Event::Key(Key::Down) if self.orientation ==
Orientation::Vertical => {
self.slide_plus()
}
Event::Key(Key::Enter) if self.on_enter.is_some() => {
let value = self.value;
let cb = self.on_enter.clone().unwrap();
EventResult::with_cb(move |s| { cb(s, value); })
}
_ => EventResult::Ignored,
}
}
fn take_focus(&mut self, _: Direction) -> bool {
true
}
}