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
use crate::{
direction::Direction,
event::{Event, EventResult, Key},
view::{View, ViewWrapper},
With,
};
pub struct CircularFocus<T: View> {
view: T,
wrap_tab: bool,
wrap_up_down: bool,
wrap_left_right: bool,
}
impl<T: View> CircularFocus<T> {
pub fn new(view: T) -> Self {
CircularFocus {
view,
wrap_tab: false,
wrap_left_right: false,
wrap_up_down: false,
}
}
pub fn wraps_tab(&self) -> bool {
self.wrap_tab
}
pub fn wraps_arrows(&self) -> bool {
self.wrap_left_right && self.wrap_up_down
}
pub fn wraps_left_right(&self) -> bool {
self.wrap_left_right
}
pub fn wraps_up_down(&self) -> bool {
self.wrap_up_down
}
#[must_use]
pub fn wrap_arrows(self) -> Self {
self.with_wrap_arrows(true)
}
#[must_use]
pub fn wrap_up_down(self) -> Self {
self.with_wrap_up_down(true)
}
#[must_use]
pub fn wrap_left_right(self) -> Self {
self.with_wrap_left_right(true)
}
#[must_use]
pub fn with_wrap_tab(self, wrap_tab: bool) -> Self {
self.with(|s| s.set_wrap_tab(wrap_tab))
}
#[must_use]
pub fn wrap_tab(self) -> Self {
self.with_wrap_tab(true)
}
#[must_use]
pub fn with_wrap_left_right(self, wrap_left_right: bool) -> Self {
self.with(|s| s.set_wrap_left_right(wrap_left_right))
}
#[must_use]
pub fn with_wrap_up_down(self, wrap_up_down: bool) -> Self {
self.with(|s| s.set_wrap_up_down(wrap_up_down))
}
#[must_use]
pub fn with_wrap_arrows(self, wrap_arrows: bool) -> Self {
self.with(|s| s.set_wrap_arrows(wrap_arrows))
}
pub fn set_wrap_tab(&mut self, wrap_tab: bool) {
self.wrap_tab = wrap_tab;
}
pub fn set_wrap_arrows(&mut self, wrap_arrows: bool) {
self.wrap_left_right = wrap_arrows;
self.wrap_up_down = wrap_arrows;
}
pub fn set_wrap_up_down(&mut self, wrap_up_down: bool) {
self.wrap_up_down = wrap_up_down;
}
pub fn set_wrap_left_right(&mut self, wrap_left_right: bool) {
self.wrap_left_right = wrap_left_right;
}
inner_getters!(self.view: T);
}
impl<T: View> ViewWrapper for CircularFocus<T> {
wrap_impl!(self.view: T);
fn wrap_on_event(&mut self, event: Event) -> EventResult {
match (self.view.on_event(event.clone()), event) {
(EventResult::Ignored, Event::Key(Key::Tab)) if self.wrap_tab => {
self.view
.take_focus(Direction::front())
.unwrap_or(EventResult::Ignored)
}
(EventResult::Ignored, Event::Shift(Key::Tab))
if self.wrap_tab =>
{
self.view
.take_focus(Direction::back())
.unwrap_or(EventResult::Ignored)
}
(EventResult::Ignored, Event::Key(Key::Right))
if self.wrap_left_right =>
{
self.view
.take_focus(Direction::left())
.unwrap_or(EventResult::Ignored)
}
(EventResult::Ignored, Event::Key(Key::Left))
if self.wrap_left_right =>
{
self.view
.take_focus(Direction::right())
.unwrap_or(EventResult::Ignored)
}
(EventResult::Ignored, Event::Key(Key::Up))
if self.wrap_up_down =>
{
self.view
.take_focus(Direction::down())
.unwrap_or(EventResult::Ignored)
}
(EventResult::Ignored, Event::Key(Key::Down))
if self.wrap_up_down =>
{
self.view
.take_focus(Direction::up())
.unwrap_or(EventResult::Ignored)
}
(other, _) => other,
}
}
}