-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathnav.rs
572 lines (514 loc) · 19.1 KB
/
nav.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
use crate::{
accounts::render_accounts_route,
actionbar::NoteAction,
app::{get_active_columns_mut, get_decks_mut},
column::ColumnsAction,
deck_state::DeckState,
decks::{Deck, DecksAction, DecksCache},
profile::{ProfileAction, SaveProfileChanges},
profile_state::ProfileState,
relay_pool_manager::RelayPoolManager,
route::Route,
timeline::{route::render_timeline_route, TimelineCache},
ui::{
self,
add_column::render_add_column_routes,
column::NavTitle,
configure_deck::ConfigureDeckView,
edit_deck::{EditDeckResponse, EditDeckView},
note::{contents::NoteContext, PostAction, PostType},
profile::EditProfileView,
search::{FocusState, SearchView},
support::SupportView,
RelayView, View,
},
Damus,
};
use egui_nav::{Nav, NavAction, NavResponse, NavUiType};
use nostrdb::Transaction;
use notedeck::{AccountsAction, AppContext};
use tracing::error;
#[allow(clippy::enum_variant_names)]
pub enum RenderNavAction {
Back,
RemoveColumn,
PostAction(PostAction),
NoteAction(NoteAction),
ProfileAction(ProfileAction),
SwitchingAction(SwitchingAction),
}
pub enum SwitchingAction {
Accounts(AccountsAction),
Columns(ColumnsAction),
Decks(crate::decks::DecksAction),
}
impl SwitchingAction {
/// process the action, and return whether switching occured
pub fn process(
&self,
timeline_cache: &mut TimelineCache,
decks_cache: &mut DecksCache,
ctx: &mut AppContext<'_>,
) -> bool {
match &self {
SwitchingAction::Accounts(account_action) => match account_action {
AccountsAction::Switch(switch_action) => {
ctx.accounts.select_account(switch_action.switch_to);
// pop nav after switch
if let Some(src) = switch_action.source {
get_active_columns_mut(ctx.accounts, decks_cache)
.column_mut(src)
.router_mut()
.go_back();
}
}
AccountsAction::Remove(index) => ctx.accounts.remove_account(*index),
},
SwitchingAction::Columns(columns_action) => match *columns_action {
ColumnsAction::Remove(index) => {
let kinds_to_pop =
get_active_columns_mut(ctx.accounts, decks_cache).delete_column(index);
for kind in &kinds_to_pop {
if let Err(err) = timeline_cache.pop(kind, ctx.ndb, ctx.pool) {
error!("error popping timeline: {err}");
}
}
}
ColumnsAction::Switch(from, to) => {
get_active_columns_mut(ctx.accounts, decks_cache).move_col(from, to);
}
},
SwitchingAction::Decks(decks_action) => match *decks_action {
DecksAction::Switch(index) => {
get_decks_mut(ctx.accounts, decks_cache).set_active(index)
}
DecksAction::Removing(index) => {
get_decks_mut(ctx.accounts, decks_cache).remove_deck(index)
}
},
}
true
}
}
impl From<PostAction> for RenderNavAction {
fn from(post_action: PostAction) -> Self {
Self::PostAction(post_action)
}
}
impl From<NoteAction> for RenderNavAction {
fn from(note_action: NoteAction) -> RenderNavAction {
Self::NoteAction(note_action)
}
}
pub type NotedeckNavResponse = NavResponse<Option<RenderNavAction>>;
pub struct RenderNavResponse {
column: usize,
response: NotedeckNavResponse,
}
impl RenderNavResponse {
#[allow(private_interfaces)]
pub fn new(column: usize, response: NotedeckNavResponse) -> Self {
RenderNavResponse { column, response }
}
#[must_use = "Make sure to save columns if result is true"]
pub fn process_render_nav_response(&self, app: &mut Damus, ctx: &mut AppContext<'_>) -> bool {
let mut switching_occured: bool = false;
let col = self.column;
if let Some(action) = self
.response
.response
.as_ref()
.or(self.response.title_response.as_ref())
{
// start returning when we're finished posting
match action {
RenderNavAction::Back => {
app.columns_mut(ctx.accounts)
.column_mut(col)
.router_mut()
.go_back();
}
RenderNavAction::RemoveColumn => {
let kinds_to_pop = app.columns_mut(ctx.accounts).delete_column(col);
for kind in &kinds_to_pop {
if let Err(err) = app.timeline_cache.pop(kind, ctx.ndb, ctx.pool) {
error!("error popping timeline: {err}");
}
}
switching_occured = true;
}
RenderNavAction::PostAction(post_action) => {
let txn = Transaction::new(ctx.ndb).expect("txn");
let _ = post_action.execute(ctx.ndb, &txn, ctx.pool, &mut app.drafts);
get_active_columns_mut(ctx.accounts, &mut app.decks_cache)
.column_mut(col)
.router_mut()
.go_back();
}
RenderNavAction::NoteAction(note_action) => {
let txn = Transaction::new(ctx.ndb).expect("txn");
note_action.execute_and_process_result(
ctx.ndb,
get_active_columns_mut(ctx.accounts, &mut app.decks_cache),
col,
&mut app.timeline_cache,
ctx.note_cache,
ctx.pool,
&txn,
ctx.unknown_ids,
);
}
RenderNavAction::SwitchingAction(switching_action) => {
switching_occured = switching_action.process(
&mut app.timeline_cache,
&mut app.decks_cache,
ctx,
);
}
RenderNavAction::ProfileAction(profile_action) => {
profile_action.process(
&mut app.view_state.pubkey_to_profile_state,
ctx.ndb,
ctx.pool,
get_active_columns_mut(ctx.accounts, &mut app.decks_cache)
.column_mut(col)
.router_mut(),
);
}
}
}
if let Some(action) = self.response.action {
match action {
NavAction::Returned => {
let r = app
.columns_mut(ctx.accounts)
.column_mut(col)
.router_mut()
.pop();
if let Some(Route::Timeline(kind)) = &r {
if let Err(err) = app.timeline_cache.pop(kind, ctx.ndb, ctx.pool) {
error!("popping timeline had an error: {err} for {:?}", kind);
}
};
switching_occured = true;
}
NavAction::Navigated => {
let cur_router = app.columns_mut(ctx.accounts).column_mut(col).router_mut();
cur_router.navigating = false;
if cur_router.is_replacing() {
cur_router.remove_previous_routes();
}
switching_occured = true;
}
NavAction::Dragging => {}
NavAction::Returning => {}
NavAction::Resetting => {}
NavAction::Navigating => {}
}
}
switching_occured
}
}
fn render_nav_body(
ui: &mut egui::Ui,
app: &mut Damus,
ctx: &mut AppContext<'_>,
top: &Route,
depth: usize,
col: usize,
inner_rect: egui::Rect,
) -> Option<RenderNavAction> {
let mut note_context = NoteContext {
ndb: ctx.ndb,
img_cache: ctx.img_cache,
note_cache: ctx.note_cache,
};
match top {
Route::Timeline(kind) => render_timeline_route(
ctx.unknown_ids,
&mut app.timeline_cache,
ctx.accounts,
kind,
col,
app.note_options,
depth,
ui,
&mut note_context,
),
Route::Accounts(amr) => {
let mut action = render_accounts_route(
ui,
ctx.ndb,
col,
ctx.img_cache,
ctx.accounts,
&mut app.decks_cache,
&mut app.view_state.login,
*amr,
);
let txn = Transaction::new(ctx.ndb).expect("txn");
action.process_action(ctx.unknown_ids, ctx.ndb, &txn);
action
.accounts_action
.map(|f| RenderNavAction::SwitchingAction(SwitchingAction::Accounts(f)))
}
Route::Relays => {
let manager = RelayPoolManager::new(ctx.pool);
RelayView::new(ctx.accounts, manager, &mut app.view_state.id_string_map).ui(ui);
None
}
Route::Reply(id) => {
let txn = if let Ok(txn) = Transaction::new(ctx.ndb) {
txn
} else {
ui.label("Reply to unknown note");
return None;
};
let note = if let Ok(note) = ctx.ndb.get_note_by_id(&txn, id.bytes()) {
note
} else {
ui.label("Reply to unknown note");
return None;
};
let id = egui::Id::new(("post", col, note.key().unwrap()));
let poster = ctx.accounts.selected_or_first_nsec()?;
let action = {
let draft = app.drafts.reply_mut(note.id());
let response = egui::ScrollArea::vertical()
.show(ui, |ui| {
ui::PostReplyView::new(
&mut note_context,
poster,
draft,
¬e,
inner_rect,
app.note_options,
)
.id_source(id)
.show(ui)
})
.inner;
if let Some(selection) = response.context_selection {
selection.process(ui, ¬e);
}
response.action
};
action.map(Into::into)
}
Route::Quote(id) => {
let txn = Transaction::new(ctx.ndb).expect("txn");
let note = if let Ok(note) = ctx.ndb.get_note_by_id(&txn, id.bytes()) {
note
} else {
ui.label("Quote of unknown note");
return None;
};
let id = egui::Id::new(("post", col, note.key().unwrap()));
let poster = ctx.accounts.selected_or_first_nsec()?;
let draft = app.drafts.quote_mut(note.id());
let response = egui::ScrollArea::vertical()
.show(ui, |ui| {
crate::ui::note::QuoteRepostView::new(
&mut note_context,
poster,
draft,
¬e,
inner_rect,
app.note_options,
)
.id_source(id)
.show(ui)
})
.inner;
if let Some(selection) = response.context_selection {
selection.process(ui, ¬e);
}
response.action.map(Into::into)
}
Route::ComposeNote => {
let kp = ctx.accounts.get_selected_account()?.to_full()?;
let draft = app.drafts.compose_mut();
let txn = Transaction::new(ctx.ndb).expect("txn");
let post_response = ui::PostView::new(
&mut note_context,
draft,
PostType::New,
kp,
inner_rect,
app.note_options,
)
.ui(&txn, ui);
post_response.action.map(Into::into)
}
Route::AddColumn(route) => {
render_add_column_routes(ui, app, ctx, col, route);
None
}
Route::Support => {
SupportView::new(&mut app.support).show(ui);
None
}
Route::Search => {
let id = ui.id().with(("search", depth, col));
let navigating = app
.columns_mut(ctx.accounts)
.column(col)
.router()
.navigating;
let search_buffer = app.view_state.searches.entry(id).or_default();
let txn = Transaction::new(ctx.ndb).expect("txn");
if navigating {
search_buffer.focus_state = FocusState::Navigating
} else if search_buffer.focus_state == FocusState::Navigating {
// we're not navigating but our last search buffer state
// says we were navigating. This means that navigating has
// stopped. Let's make sure to focus the input field
search_buffer.focus_state = FocusState::ShouldRequestFocus;
}
SearchView::new(
&txn,
&ctx.accounts.mutefun(),
app.note_options,
search_buffer,
&mut note_context,
)
.show(ui)
.map(RenderNavAction::NoteAction)
}
Route::NewDeck => {
let id = ui.id().with("new-deck");
let new_deck_state = app.view_state.id_to_deck_state.entry(id).or_default();
let mut resp = None;
if let Some(config_resp) = ConfigureDeckView::new(new_deck_state).ui(ui) {
if let Some(cur_acc) = ctx.accounts.get_selected_account() {
app.decks_cache.add_deck(
cur_acc.pubkey,
Deck::new(config_resp.icon, config_resp.name),
);
// set new deck as active
let cur_index = get_decks_mut(ctx.accounts, &mut app.decks_cache)
.decks()
.len()
- 1;
resp = Some(RenderNavAction::SwitchingAction(SwitchingAction::Decks(
DecksAction::Switch(cur_index),
)));
}
new_deck_state.clear();
get_active_columns_mut(ctx.accounts, &mut app.decks_cache)
.get_first_router()
.go_back();
}
resp
}
Route::EditDeck(index) => {
let mut action = None;
let cur_deck = get_decks_mut(ctx.accounts, &mut app.decks_cache)
.decks_mut()
.get_mut(*index)
.expect("index wasn't valid");
let id = ui.id().with((
"edit-deck",
ctx.accounts.get_selected_account().map(|k| k.pubkey),
index,
));
let deck_state = app
.view_state
.id_to_deck_state
.entry(id)
.or_insert_with(|| DeckState::from_deck(cur_deck));
if let Some(resp) = EditDeckView::new(deck_state).ui(ui) {
match resp {
EditDeckResponse::Edit(configure_deck_response) => {
cur_deck.edit(configure_deck_response);
}
EditDeckResponse::Delete => {
action = Some(RenderNavAction::SwitchingAction(SwitchingAction::Decks(
DecksAction::Removing(*index),
)));
}
}
get_active_columns_mut(ctx.accounts, &mut app.decks_cache)
.get_first_router()
.go_back();
}
action
}
Route::EditProfile(pubkey) => {
let mut action = None;
if let Some(kp) = ctx.accounts.get_full(pubkey.bytes()) {
let state = app
.view_state
.pubkey_to_profile_state
.entry(*kp.pubkey)
.or_insert_with(|| {
let txn = Transaction::new(ctx.ndb).expect("txn");
if let Ok(record) = ctx.ndb.get_profile_by_pubkey(&txn, kp.pubkey.bytes()) {
ProfileState::from_profile(&record)
} else {
ProfileState::default()
}
});
if EditProfileView::new(state, ctx.img_cache).ui(ui) {
if let Some(taken_state) =
app.view_state.pubkey_to_profile_state.remove(kp.pubkey)
{
action = Some(RenderNavAction::ProfileAction(ProfileAction::SaveChanges(
SaveProfileChanges::new(kp.to_full(), taken_state),
)))
}
}
} else {
error!("Pubkey in EditProfile route did not have an nsec attached in Accounts");
}
action
}
}
}
#[must_use = "RenderNavResponse must be handled by calling .process_render_nav_response(..)"]
pub fn render_nav(
col: usize,
inner_rect: egui::Rect,
app: &mut Damus,
ctx: &mut AppContext<'_>,
ui: &mut egui::Ui,
) -> RenderNavResponse {
let nav_response = Nav::new(
&app.columns(ctx.accounts)
.column(col)
.router()
.routes()
.clone(),
)
.navigating(
app.columns_mut(ctx.accounts)
.column_mut(col)
.router_mut()
.navigating,
)
.returning(
app.columns_mut(ctx.accounts)
.column_mut(col)
.router_mut()
.returning,
)
.id_source(egui::Id::new(("nav", col)))
.show_mut(ui, |ui, render_type, nav| match render_type {
NavUiType::Title => NavTitle::new(
ctx.ndb,
ctx.img_cache,
get_active_columns_mut(ctx.accounts, &mut app.decks_cache),
nav.routes(),
col,
)
.show(ui),
NavUiType::Body => {
if let Some(top) = nav.routes().last() {
render_nav_body(ui, app, ctx, top, nav.routes().len(), col, inner_rect)
} else {
None
}
}
});
RenderNavResponse::new(col, nav_response)
}