notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

toolbar.rs (3512B)


      1 use egui_nav::ReturnType;
      2 use notedeck::AppContext;
      3 
      4 use crate::{
      5     route::cleanup_popped_route,
      6     timeline::{kind::ListKind, TimelineKind},
      7     Damus, Route,
      8 };
      9 
     10 // TODO(kernelkind): should account for mutes
     11 #[profiling::function]
     12 pub fn unseen_notification(
     13     columns: &mut Damus,
     14     accounts: &notedeck::Accounts,
     15     active_col: usize,
     16 ) -> bool {
     17     let top = columns.columns(accounts).column(active_col).router().top();
     18     let current_pk = accounts.get_selected_account().keypair().pubkey;
     19 
     20     if let Route::Timeline(TimelineKind::Notifications(notif_pk)) = top {
     21         if notif_pk == current_pk {
     22             return false;
     23         }
     24     }
     25 
     26     let notif_kind = TimelineKind::Notifications(*current_pk);
     27     let Some(tl) = columns.timeline_cache.get_mut(&notif_kind) else {
     28         return false;
     29     };
     30 
     31     !tl.seen_latest_notes
     32 }
     33 
     34 /// When you click the toolbar button, these actions
     35 /// are returned
     36 #[derive(Debug, Eq, PartialEq)]
     37 pub enum ToolbarAction {
     38     Notifications,
     39     Search,
     40     Home,
     41 }
     42 
     43 impl ToolbarAction {
     44     pub fn process(&self, app: &mut Damus, ctx: &mut AppContext) {
     45         let cur_acc_pk = ctx.accounts.get_selected_account().key.pubkey;
     46         let route = match &self {
     47             ToolbarAction::Notifications => {
     48                 Route::timeline(TimelineKind::Notifications(cur_acc_pk))
     49             }
     50             ToolbarAction::Search => Route::Search,
     51             ToolbarAction::Home => {
     52                 Route::timeline(TimelineKind::List(ListKind::Contact(cur_acc_pk)))
     53             }
     54         };
     55 
     56         let Some(cols) = app.decks_cache.active_columns_mut(ctx.i18n, ctx.accounts) else {
     57             return;
     58         };
     59 
     60         let selection_result = cols.select_by_route(route);
     61 
     62         match selection_result {
     63             crate::column::SelectionResult::AlreadySelected(col_index) => {
     64                 // We're already on this toolbar view, so pop all routes to go to top
     65                 pop_to_root(app, ctx, col_index);
     66                 app.scroll_to_top();
     67             }
     68             crate::column::SelectionResult::NewSelection(_) => {
     69                 // we already selected this, so scroll to top
     70                 app.scroll_to_top();
     71             }
     72             crate::column::SelectionResult::Failed => {
     73                 // oh no, something went wrong
     74                 // TODO(jb55): handle tab selection failure
     75             }
     76         }
     77     }
     78 }
     79 
     80 /// Pop all routes in the column until we're back at depth 1 (the base route).
     81 /// This is used when clicking a toolbar button for a view we're already on
     82 /// to immediately return to the top level regardless of navigation depth.
     83 fn pop_to_root(app: &mut Damus, ctx: &mut AppContext, col_index: usize) {
     84     let Some(cols) = app.decks_cache.active_columns_mut(ctx.i18n, ctx.accounts) else {
     85         return;
     86     };
     87 
     88     let column = cols.column_mut(col_index);
     89 
     90     // Close any open sheets first
     91     if column.sheet_router.route().is_some() {
     92         column.sheet_router.go_back();
     93     }
     94 
     95     // Pop all routes except the base route
     96     while column.router().routes().len() > 1 {
     97         if let Some(popped) = column.router_mut().pop() {
     98             // Clean up resources for the popped route
     99             cleanup_popped_route(
    100                 &popped,
    101                 &mut app.timeline_cache,
    102                 &mut app.threads,
    103                 &mut app.view_state,
    104                 ctx.ndb,
    105                 ctx.pool,
    106                 ReturnType::Click,
    107                 col_index,
    108             );
    109         }
    110     }
    111 }