From 57069ff7c0d0e6918d107f724c169148f54c6ef6 Mon Sep 17 00:00:00 2001 From: kernelkind Date: Tue, 1 Oct 2024 12:51:14 -0400 Subject: [PATCH] push column picker immediately to new column instead of pushing to temporary column first Signed-off-by: kernelkind --- src/app.rs | 27 ++++----------------------- src/column.rs | 16 ++++++++++++++++ src/nav.rs | 34 ++++++++++++++++++++-------------- src/route.rs | 24 ++++++++++++++++++++++++ src/timeline/route.rs | 2 -- src/ui/side_panel.rs | 23 ++++++++++++++++------- 6 files changed, 80 insertions(+), 46 deletions(-) diff --git a/src/app.rs b/src/app.rs index c14b6779..0753a961 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,7 +3,7 @@ use crate::{ app_creation::setup_cc, app_style::user_requested_visuals_change, args::Args, - column::{Column, Columns}, + column::Columns, draft::Drafts, error::{Error, FilterError}, filter::{self, FilterState}, @@ -13,7 +13,6 @@ use crate::{ nav, note::NoteRef, notecache::{CachedNote, NoteCache}, - route::Route, subscriptions::{SubKind, Subscriptions}, thread::Threads, timeline::{Timeline, TimelineId, TimelineKind, ViewFilter}, @@ -699,11 +698,7 @@ impl Damus { let debug = parsed_args.debug; if columns.columns().is_empty() { - let filter = Filter::from_json(include_str!("../queries/timeline.json")).unwrap(); - columns.add_timeline(Timeline::new( - TimelineKind::Generic, - FilterState::ready(vec![filter]), - )) + columns.new_column_picker(); } Self { @@ -770,7 +765,7 @@ impl Damus { } } - pub fn add_new_timeline(&mut self, timeline_id: TimelineId) { + pub fn subscribe_new_timeline(&mut self, timeline_id: TimelineId) { self.state = DamusState::NewTimelineSub(timeline_id); } @@ -993,22 +988,8 @@ fn timelines_view(ui: &mut egui::Ui, sizes: Size, app: &mut Damus, columns: usiz ) .show(ui); - let router = if let Some(router) = app - .columns - .columns_mut() - .get_mut(0) - .map(|c: &mut Column| c.router_mut()) - { - router - } else { - // TODO(jb55): Maybe we should have an empty column route? - let columns = app.columns.columns_mut(); - columns.push(Column::new(vec![Route::accounts()])); - columns[0].router_mut() - }; - if side_panel.response.clicked() { - DesktopSidePanel::perform_action(router, side_panel.action); + DesktopSidePanel::perform_action(app.columns_mut(), side_panel.action); } // vertical sidebar line diff --git a/src/column.rs b/src/column.rs index 16e4d5b0..bfc7a831 100644 --- a/src/column.rs +++ b/src/column.rs @@ -47,6 +47,22 @@ impl Columns { self.columns.push(Column::new(routes)) } + pub fn add_timeline_to_column(&mut self, col: usize, timeline: Timeline) -> bool { + if let Some(column) = self.columns.get_mut(col) { + column + .router_mut() + .route_to_replaced(Route::timeline(timeline.id)); + self.timelines.push(timeline); + true + } else { + false + } + } + + pub fn new_column_picker(&mut self) { + self.columns.push(Column::new(vec![Route::AddColumn])); + } + pub fn columns_mut(&mut self) -> &mut Vec { &mut self.columns } diff --git a/src/nav.rs b/src/nav.rs index 57e94044..0702efb5 100644 --- a/src/nav.rs +++ b/src/nav.rs @@ -78,9 +78,21 @@ pub fn render_nav(col: usize, app: &mut Damus, ui: &mut egui::Ui) { None } - Route::AddColumn => AddColumnView::new(&app.ndb, app.accounts.get_selected_account()) - .ui(ui) - .map(AfterRouteExecution::AddColumn), + Route::AddColumn => { + let resp = AddColumnView::new(&app.ndb, app.accounts.get_selected_account()).ui(ui); + + if let Some(resp) = resp { + match resp { + AddColumnResponse::Timeline(timeline) => { + let id = timeline.id; + if app.columns_mut().add_timeline_to_column(col, timeline) { + app.subscribe_new_timeline(id); + } + } + }; + } + None + } }); if let Some(after_route_execution) = nav_response.inner { @@ -95,16 +107,6 @@ pub fn render_nav(col: usize, app: &mut Damus, ui: &mut egui::Ui) { } } } - - AfterRouteExecution::AddColumn(add_column_resp) => { - match add_column_resp { - AddColumnResponse::Timeline(timeline) => { - app.add_new_timeline(timeline.id); - app.columns_mut().add_timeline(timeline); - } - }; - app.columns_mut().column_mut(col).router_mut().go_back(); - } } } @@ -120,6 +122,10 @@ pub fn render_nav(col: usize, app: &mut Damus, ui: &mut egui::Ui) { ); } } else if let Some(NavAction::Navigated) = nav_response.action { - app.columns_mut().column_mut(col).router_mut().navigating = false; + let cur_router = app.columns_mut().column_mut(col).router_mut(); + cur_router.navigating = false; + if cur_router.is_replacing() { + cur_router.remove_previous_route(); + } } } diff --git a/src/route.rs b/src/route.rs index 2f46923f..22828abe 100644 --- a/src/route.rs +++ b/src/route.rs @@ -61,6 +61,7 @@ pub struct Router { routes: Vec, pub returning: bool, pub navigating: bool, + replacing: bool, } impl Router { @@ -70,10 +71,12 @@ impl Router { } let returning = false; let navigating = false; + let replacing = false; Router { routes, returning, navigating, + replacing, } } @@ -82,6 +85,13 @@ impl Router { self.routes.push(route); } + // Route to R. Then when it is successfully placed, should call `remove_previous_route` + pub fn route_to_replaced(&mut self, route: R) { + self.navigating = true; + self.replacing = true; + self.routes.push(route); + } + /// Go back, start the returning process pub fn go_back(&mut self) -> Option { if self.returning || self.routes.len() == 1 { @@ -100,6 +110,20 @@ impl Router { self.routes.pop() } + pub fn remove_previous_route(&mut self) -> Option { + let num_routes = self.routes.len(); + if num_routes <= 1 { + return None; + } + self.returning = false; + self.replacing = false; + Some(self.routes.remove(num_routes - 2)) + } + + pub fn is_replacing(&self) -> bool { + self.replacing + } + pub fn top(&self) -> &R { self.routes.last().expect("routes can't be empty") } diff --git a/src/timeline/route.rs b/src/timeline/route.rs index 1125b0de..71b9774f 100644 --- a/src/timeline/route.rs +++ b/src/timeline/route.rs @@ -8,7 +8,6 @@ use crate::{ timeline::TimelineId, ui::{ self, - add_column::AddColumnResponse, note::{ post::{PostAction, PostResponse}, QuoteRepostView, @@ -29,7 +28,6 @@ pub enum TimelineRoute { pub enum AfterRouteExecution { Post(PostResponse), - AddColumn(AddColumnResponse), } impl AfterRouteExecution { diff --git a/src/ui/side_panel.rs b/src/ui/side_panel.rs index b964059a..b2d10972 100644 --- a/src/ui/side_panel.rs +++ b/src/ui/side_panel.rs @@ -4,7 +4,7 @@ use tracing::info; use crate::{ account_manager::AccountsRoute, colors, - column::Column, + column::{Column, Columns}, imgcache::ImageCache, route::{Route, Router}, user_account::UserAccount, @@ -162,7 +162,8 @@ impl<'a> DesktopSidePanel<'a> { helper.take_animation_response() } - pub fn perform_action(router: &mut Router, action: SidePanelAction) { + pub fn perform_action(columns: &mut Columns, action: SidePanelAction) { + let router = get_first_router(columns); match action { SidePanelAction::Panel => {} // TODO SidePanelAction::Account => { @@ -189,7 +190,7 @@ impl<'a> DesktopSidePanel<'a> { if router.routes().iter().any(|&r| r == Route::AddColumn) { router.go_back(); } else { - router.route_to(Route::AddColumn); + columns.new_column_picker(); } } SidePanelAction::ComposeNote => { @@ -233,6 +234,17 @@ fn settings_button(dark_mode: bool) -> impl Widget { } } +fn get_first_router(columns: &mut Columns) -> &mut Router { + if columns.columns().is_empty() { + columns.new_column_picker(); + } + columns + .columns_mut() + .get_mut(0) + .expect("There should be at least one column") + .router_mut() +} + fn add_column_button(dark_mode: bool) -> impl Widget { let _ = dark_mode; move |ui: &mut egui::Ui| { @@ -391,10 +403,7 @@ mod preview { ); let response = panel.show(ui); - DesktopSidePanel::perform_action( - self.app.columns.columns_mut()[0].router_mut(), - response.action, - ); + DesktopSidePanel::perform_action(&mut self.app.columns, response.action); }); }); }