-
Notifications
You must be signed in to change notification settings - Fork 15
/
app.rs
267 lines (223 loc) · 8.71 KB
/
app.rs
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
use anyhow::Result;
use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
prelude::{Constraint, Direction, Layout, Rect},
style::Style,
widgets::Block,
};
use tracing::warn;
use wiki_api::{languages::Language, Endpoint};
use tokio::sync::mpsc;
use crate::{
action::{Action, ActionPacket, ActionResult},
components::{
logger::LoggerComponent,
message_popup::MessagePopupComponent,
page_viewer::PageViewer,
search::SearchComponent,
search_bar::{SearchBarComponent, SEARCH_BAR_HEIGTH},
search_language_popup::SearchLanguageSelectionComponent,
Component,
},
config::Theme,
has_modifier,
page_loader::PageLoader,
terminal::Frame,
};
const CONTEXT_SEARCH: u8 = 0;
const CONTEXT_PAGE: u8 = 1;
#[derive(Default)]
pub struct AppComponent {
search: SearchComponent,
page: PageViewer,
logger: LoggerComponent,
search_bar: SearchBarComponent,
page_loader: Option<PageLoader>,
is_logger: bool,
popups: Vec<Box<dyn Component + Send>>,
theme: Theme,
context: u8,
prev_context: u8,
action_tx: Option<mpsc::UnboundedSender<Action>>,
}
impl AppComponent {
fn switch_context(&mut self, context: u8) {
self.prev_context = context;
std::mem::swap(&mut self.prev_context, &mut self.context);
}
fn show_page_language(&mut self) {
let selection_widget = self.page.get_page_language_selection_popup();
self.popups.push(Box::new(selection_widget));
}
}
impl Component for AppComponent {
fn init(&mut self, action_tx: mpsc::UnboundedSender<Action>, theme: Theme) -> Result<()> {
self.search.init(action_tx.clone(), theme.clone())?;
self.page.init(action_tx.clone(), theme.clone())?;
self.search_bar.init(action_tx.clone(), theme.clone())?;
self.theme = theme;
let endpoint = Endpoint::parse("https://en.wikipedia.org/w/api.php").unwrap();
let language = Language::English;
self.page_loader = Some(PageLoader::new(action_tx.clone()));
self.search.endpoint = Some(endpoint);
self.search.language = Some(language);
action_tx.send(Action::EnterSearchBar).unwrap();
self.action_tx = Some(action_tx);
Ok(())
}
fn handle_key_events(&mut self, key: KeyEvent) -> ActionResult {
// we need to always handle CTRL-C
if matches!(key.code, KeyCode::Char('c') if has_modifier!(key, Modifier::CONTROL)) {
return Action::Quit.into();
}
if self.search_bar.is_focussed {
return self.search_bar.handle_key_events(key);
}
if let Some(ref mut popup) = self.popups.last_mut() {
let result = popup.handle_key_events(key);
if result.is_consumed() {
return result;
}
}
let result = match self.context {
CONTEXT_SEARCH => self.search.handle_key_events(key),
CONTEXT_PAGE => self.page.handle_key_events(key),
_ => {
warn!("unknown context");
ActionResult::Ignored
}
};
if result.is_consumed() {
return result;
}
match key.code {
KeyCode::Char('q') => Action::Quit.into(),
KeyCode::Esc => Action::PopPopup.into(),
KeyCode::Char('l') => Action::ToggleShowLogger.into(),
KeyCode::Char('s') => Action::SwitchContextSearch.into(),
KeyCode::Char('p') => Action::SwitchContextPage.into(),
KeyCode::Char('j') => Action::ScrollDown(1).into(),
KeyCode::Char('k') => Action::ScrollUp(1).into(),
KeyCode::Char('g') | KeyCode::Home => Action::ScrollToTop.into(),
KeyCode::Char('G') | KeyCode::End => Action::ScrollToBottom.into(),
KeyCode::Char('d') if has_modifier!(key, Modifier::CONTROL) => {
Action::ScrollHalfDown.into()
}
KeyCode::Char('u') if has_modifier!(key, Modifier::CONTROL) => {
Action::ScrollHalfUp.into()
}
KeyCode::PageDown => Action::ScrollHalfDown.into(),
KeyCode::PageUp => Action::ScrollHalfUp.into(),
KeyCode::Char('h') => Action::UnselectScroll.into(),
KeyCode::Char('i') => Action::EnterSearchBar.into(),
KeyCode::F(2) => {
self.popups
.push(Box::new(SearchLanguageSelectionComponent::new(
self.theme.clone(),
)));
ActionResult::consumed()
}
_ => ActionResult::Ignored,
}
}
fn update(&mut self, action: Action) -> ActionResult {
// global actions
match action {
Action::PopPopup => {
self.popups.pop();
}
Action::ToggleShowLogger => self.is_logger = !self.is_logger,
Action::ShowPageLanguageSelection => self.show_page_language(),
Action::SwitchContextSearch => self.switch_context(CONTEXT_SEARCH),
Action::SwitchContextPage => self.switch_context(CONTEXT_PAGE),
Action::SwitchPreviousContext => self.switch_context(self.prev_context),
Action::EnterSearchBar => self.search_bar.is_focussed = true,
Action::ExitSearchBar => self.search_bar.is_focussed = false,
Action::ClearSearchBar => self.search_bar.clear(),
Action::SubmitSearchBar => {
return ActionPacket::default()
.action(Action::ExitSearchBar)
.action(Action::SwitchContextSearch)
.action(self.search_bar.submit())
.into()
}
Action::LoadSearchResult(title) => {
self.page_loader.as_ref().unwrap().load_search_result(title)
}
Action::LoadLink(link) => self.page_loader.as_ref().unwrap().load_link(link),
Action::LoadLangaugeLink(link) => {
self.page_loader.as_ref().unwrap().load_language_link(link)
}
Action::PopupMessage(title, content) => self.popups.push(Box::new(
MessagePopupComponent::new_raw(title, content, self.theme.clone()),
)),
Action::PopupError(error) => self.popups.push(Box::new(
MessagePopupComponent::new_error(error, self.theme.clone()),
)),
Action::PopupDialog(title, content, cb) => {
self.popups
.push(Box::new(MessagePopupComponent::new_confirmation(
title,
content,
*cb,
self.theme.clone(),
)))
}
_ => {
if let Some(ref mut popup) = self.popups.last_mut() {
let result = popup.update(action.clone());
if result.is_consumed() {
return result;
}
}
let result = match self.context {
CONTEXT_SEARCH => self.search.update(action.clone()),
CONTEXT_PAGE => self.page.update(action.clone()),
_ => {
warn!("unknown context");
return ActionResult::Ignored;
}
};
if result.is_consumed() {
return result;
}
}
};
ActionResult::consumed()
}
fn render(&mut self, f: &mut Frame<'_>, area: Rect) {
f.render_widget(
Block::default().style(Style::default().bg(self.theme.bg)),
area,
);
let (search_bar_area, area) = {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Min(SEARCH_BAR_HEIGTH),
Constraint::Percentage(100),
])
.split(area);
(chunks[0], chunks[1])
};
self.search_bar.render(f, search_bar_area);
let area = if self.is_logger {
let chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
.split(area);
self.logger.render(f, chunks[1]);
chunks[0]
} else {
area
};
match self.context {
CONTEXT_SEARCH => self.search.render(f, area),
CONTEXT_PAGE => self.page.render(f, area),
_ => warn!("unknown context"),
}
if let Some(ref mut popup) = self.popups.last_mut() {
popup.render(f, area);
}
}
}