forked from tari-project/tari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontacts_tab.rs
399 lines (361 loc) · 15 KB
/
contacts_tab.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright 2022 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause
use tokio::runtime::Handle;
use tui::{
backend::Backend,
layout::{Constraint, Layout, Rect},
style::{Color, Modifier, Style},
text::{Span, Spans},
widgets::{Block, Borders, Clear, ListItem, Paragraph, Wrap},
Frame,
};
use unicode_width::UnicodeWidthStr;
use crate::{
ui::{
components::{Component, KeyHandled},
state::AppState,
ui_contact::UiContact,
widgets::{centered_rect_absolute, draw_dialog, MultiColumnList, WindowedListState},
MAX_WIDTH,
},
utils::formatting::display_compressed_string,
};
pub struct ContactsTab {
edit_contact_mode: ContactInputMode,
show_edit_contact: bool,
alias_field: String,
address_field: String,
error_message: Option<String>,
contacts_list_state: WindowedListState,
confirmation_dialog: Option<ConfirmationDialogType>,
}
impl ContactsTab {
pub fn new() -> Self {
Self {
edit_contact_mode: ContactInputMode::None,
show_edit_contact: false,
alias_field: String::new(),
address_field: String::new(),
error_message: None,
contacts_list_state: WindowedListState::new(),
confirmation_dialog: None,
}
}
fn draw_contacts<B>(&mut self, f: &mut Frame<B>, area: Rect, app_state: &AppState)
where B: Backend {
let block = Block::default().borders(Borders::ALL).title(Span::styled(
"Contacts",
Style::default().fg(Color::White).add_modifier(Modifier::BOLD),
));
f.render_widget(block, area);
let list_areas = Layout::default()
.constraints([Constraint::Length(1), Constraint::Min(42)].as_ref())
.margin(1)
.split(area);
let instructions = Paragraph::new(Spans::from(vec![
Span::raw("Use "),
Span::styled("Up↑/Down↓ Keys", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to select a contact, "),
Span::styled("E", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" or "),
Span::styled("Enter", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to (e)dit a contact, "),
Span::styled("D", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to (d)elete a contact and "),
Span::styled("N", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to create a (n)ew contact."),
]))
.wrap(Wrap { trim: true });
f.render_widget(instructions, list_areas[0]);
self.contacts_list_state.set_num_items(app_state.get_contacts().len());
let mut list_state = self
.contacts_list_state
.update_list_state((list_areas[1].height as usize).saturating_sub(3));
let window = self.contacts_list_state.get_start_end();
let windowed_view = app_state.get_contacts_slice(window.0, window.1);
let column_list = ContactsTab::create_column_view(windowed_view);
column_list.render(f, list_areas[1], &mut list_state);
}
// Helper function to create the column list to be rendered
pub fn create_column_view(windowed_view: &[UiContact]) -> MultiColumnList<Vec<ListItem>> {
let mut column0_items = Vec::new();
let mut column1_items = Vec::new();
let mut column2_items = Vec::new();
let mut column3_items = Vec::new();
let mut column4_items = Vec::new();
for c in windowed_view {
column0_items.push(ListItem::new(Span::raw(c.alias.clone())));
column1_items.push(ListItem::new(Span::raw(c.address.clone())));
column2_items.push(ListItem::new(Span::raw(display_compressed_string(
c.emoji_id.clone(),
3,
3,
))));
column3_items.push(ListItem::new(Span::raw(c.last_seen.clone())));
column4_items.push(ListItem::new(Span::raw(c.online_status.clone())));
}
let column_list = MultiColumnList::new()
.highlight_style(Style::default().add_modifier(Modifier::BOLD).fg(Color::Magenta))
.heading_style(Style::default().fg(Color::Magenta))
.max_width(MAX_WIDTH)
.add_column(Some("Alias"), Some(23), column0_items)
.add_column(None, Some(1), Vec::new())
.add_column(Some("Tari Address"), Some(66), column1_items)
.add_column(None, Some(1), Vec::new())
.add_column(Some("Emoji ID"), Some(14), column2_items)
.add_column(None, Some(1), Vec::new())
.add_column(Some("Last Seen"), Some(11), column3_items)
.add_column(None, Some(1), Vec::new())
.add_column(Some("Status"), Some(10), column4_items);
column_list
}
// casting here is okay as we only use it to draw widths
#[allow(clippy::cast_possible_truncation)]
fn draw_edit_contact<B>(&mut self, f: &mut Frame<B>, area: Rect, _app_state: &AppState)
where B: Backend {
let popup_area = centered_rect_absolute(120, 10, area);
f.render_widget(Clear, popup_area);
let block = Block::default().borders(Borders::ALL).title(Span::styled(
"Add/Edit Contact",
Style::default().fg(Color::White).add_modifier(Modifier::BOLD),
));
f.render_widget(block, popup_area);
let vert_chunks = Layout::default()
.constraints([Constraint::Length(2), Constraint::Length(3), Constraint::Length(3)].as_ref())
.margin(1)
.split(popup_area);
let instructions = Paragraph::new(Spans::from(vec![
Span::raw("Press "),
Span::styled("L", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to edit "),
Span::styled("Alias", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" field, "),
Span::styled("K", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to edit "),
Span::styled("Emoji ID", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" field, "),
Span::styled("Enter", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to save Contact."),
]))
.block(Block::default());
f.render_widget(instructions, vert_chunks[0]);
let alias_input = Paragraph::new(self.alias_field.as_ref())
.style(match self.edit_contact_mode {
ContactInputMode::Alias => Style::default().fg(Color::Magenta),
_ => Style::default(),
})
.block(Block::default().borders(Borders::ALL).title("A(l)ias:"));
f.render_widget(alias_input, vert_chunks[1]);
let tari_address_input = Paragraph::new(self.address_field.as_ref())
.style(match self.edit_contact_mode {
ContactInputMode::PubkeyEmojiId => Style::default().fg(Color::Magenta),
_ => Style::default(),
})
.block(Block::default().borders(Borders::ALL).title("Emoji Id:"));
f.render_widget(tari_address_input, vert_chunks[2]);
match self.edit_contact_mode {
ContactInputMode::None => (),
ContactInputMode::Alias => f.set_cursor(
// Put cursor past the end of the input text
vert_chunks[1].x + self.alias_field.width() as u16 + 1,
// Move one line down, from the border to the input line
vert_chunks[1].y + 1,
),
ContactInputMode::PubkeyEmojiId => f.set_cursor(
// Put cursor past the end of the input text
vert_chunks[2].x + self.address_field.width() as u16 + 1,
// Move one line down, from the border to the input line
vert_chunks[2].y + 1,
),
}
}
fn on_key_confirmation_dialog(&mut self, c: char, app_state: &mut AppState) -> KeyHandled {
if self.confirmation_dialog.is_some() {
if 'n' == c {
self.confirmation_dialog = None;
return KeyHandled::Handled;
} else if 'y' == c {
match self.confirmation_dialog {
None => (),
Some(ConfirmationDialogType::DeleteContact) => {
if 'y' == c {
if let Some(c) = self
.contacts_list_state
.selected()
.and_then(|i| app_state.get_contact(i))
.cloned()
{
if let Err(_e) = Handle::current().block_on(app_state.delete_contact(c.address)) {
self.error_message =
Some("Could not delete selected contact\nPress Enter to continue.".to_string());
}
}
self.confirmation_dialog = None;
return KeyHandled::Handled;
}
},
}
} else {
// dont care
}
}
KeyHandled::NotHandled
}
fn on_key_edit_contact(&mut self, c: char, app_state: &mut AppState) -> KeyHandled {
if self.show_edit_contact && self.edit_contact_mode != ContactInputMode::None {
match self.edit_contact_mode {
ContactInputMode::None => return KeyHandled::Handled,
ContactInputMode::Alias => match c {
'\n' | '\t' => {
self.edit_contact_mode = ContactInputMode::PubkeyEmojiId;
return KeyHandled::Handled;
},
c => {
self.alias_field.push(c);
return KeyHandled::Handled;
},
},
ContactInputMode::PubkeyEmojiId => match c {
'\n' => {
self.edit_contact_mode = ContactInputMode::None;
self.show_edit_contact = false;
if let Err(_e) = Handle::current()
.block_on(app_state.upsert_contact(self.alias_field.clone(), self.address_field.clone()))
{
self.error_message = Some(
"Invalid Tari address or Emoji ID provided\n Press Enter to continue.".to_string(),
);
}
self.alias_field = "".to_string();
self.address_field = "".to_string();
return KeyHandled::Handled;
},
c => {
self.address_field.push(c);
return KeyHandled::Handled;
},
},
}
}
KeyHandled::NotHandled
}
fn on_key_show_contacts(&mut self, c: char, _app_state: &mut AppState) -> KeyHandled {
match c {
'd' => {
if self.contacts_list_state.selected().is_none() {
return KeyHandled::NotHandled;
}
self.confirmation_dialog = Some(ConfirmationDialogType::DeleteContact);
return KeyHandled::Handled;
},
'n' => {
self.show_edit_contact = true;
self.edit_contact_mode = ContactInputMode::Alias;
return KeyHandled::Handled;
},
_ => (),
}
KeyHandled::NotHandled
}
}
impl<B: Backend> Component<B> for ContactsTab {
fn draw(&mut self, f: &mut Frame<B>, area: Rect, app_state: &AppState) {
self.draw_contacts(f, area, app_state);
if self.show_edit_contact {
self.draw_edit_contact(f, area, app_state);
}
if let Some(msg) = self.error_message.clone() {
draw_dialog(f, area, "Error!".to_string(), msg, Color::Red, 120, 9);
}
match self.confirmation_dialog {
None => (),
Some(ConfirmationDialogType::DeleteContact) => {
draw_dialog(
f,
area,
"Confirm Delete".to_string(),
"Are you sure you want to delete this contact?\n(Y)es / (N)o".to_string(),
Color::Red,
120,
9,
);
},
}
}
fn on_key(&mut self, app_state: &mut AppState, c: char) {
if self.error_message.is_some() {
if '\n' == c {
self.error_message = None;
}
return;
}
if self.on_key_confirmation_dialog(c, app_state) == KeyHandled::Handled {
return;
}
if self.on_key_edit_contact(c, app_state) == KeyHandled::Handled {
return;
}
if self.on_key_show_contacts(c, app_state) == KeyHandled::Handled {
return;
}
match c {
'e' | '\n' => {
if let Some(c) = self
.contacts_list_state
.selected()
.and_then(|i| app_state.get_contact(i))
{
self.address_field = c.address.clone();
self.alias_field = c.alias.clone();
self.show_edit_contact = true;
self.edit_contact_mode = ContactInputMode::Alias;
}
},
_ => {
self.show_edit_contact = false;
self.edit_contact_mode = ContactInputMode::Alias;
self.address_field = "".to_string();
},
}
}
fn on_up(&mut self, app_state: &mut AppState) {
self.contacts_list_state.set_num_items(app_state.get_contacts().len());
self.contacts_list_state.previous();
}
fn on_down(&mut self, app_state: &mut AppState) {
self.contacts_list_state.set_num_items(app_state.get_contacts().len());
self.contacts_list_state.next();
}
fn on_esc(&mut self, _: &mut AppState) {
if self.confirmation_dialog.is_some() {
return;
}
self.edit_contact_mode = ContactInputMode::None;
if self.show_edit_contact {
self.show_edit_contact = false;
} else {
self.contacts_list_state.select(None);
}
}
fn on_backspace(&mut self, _app_state: &mut AppState) {
match self.edit_contact_mode {
ContactInputMode::Alias => {
let _ = self.alias_field.pop();
},
ContactInputMode::PubkeyEmojiId => {
let _ = self.address_field.pop();
},
ContactInputMode::None => {},
}
}
}
#[derive(PartialEq, Debug)]
pub enum ContactInputMode {
None,
Alias,
PubkeyEmojiId,
}
#[derive(PartialEq, Debug)]
pub enum ConfirmationDialogType {
DeleteContact,
}