Skip to content

Commit

Permalink
fix crash when removing non-last columns
Browse files Browse the repository at this point in the history
Fixes: #445
  • Loading branch information
jb55 committed Nov 18, 2024
1 parent a678e64 commit de8029d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
16 changes: 9 additions & 7 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,8 +729,10 @@ fn render_damus_mobile(ctx: &egui::Context, app: &mut Damus) {
//let routes = app.timelines[0].routes.clone();

main_panel(&ctx.style(), ui::is_narrow(ctx)).show(ctx, |ui| {
if !app.columns.columns().is_empty() && nav::render_nav(0, app, ui) {
storage::save_columns(&app.path, app.columns.as_serializable_columns());
if !app.columns.columns().is_empty() {
if let Some(r) = nav::render_nav(0, app, ui) {
r.process_nav_response(&app.path, &mut app.columns)
}
}
});
}
Expand Down Expand Up @@ -807,12 +809,12 @@ fn timelines_view(ui: &mut egui::Ui, sizes: Size, app: &mut Damus) {
);
});

let mut columns_changed = false;
let mut nav_resp: Option<nav::RenderNavResponse> = None;
for col_index in 0..app.columns.num_columns() {
strip.cell(|ui| {
let rect = ui.available_rect_before_wrap();
if nav::render_nav(col_index, app, ui) {
columns_changed = true;
if let Some(r) = nav::render_nav(col_index, app, ui) {
nav_resp = Some(r);
}

// vertical line
Expand All @@ -826,8 +828,8 @@ fn timelines_view(ui: &mut egui::Ui, sizes: Size, app: &mut Damus) {
//strip.cell(|ui| timeline::timeline_view(ui, app, timeline_ind));
}

if columns_changed {
storage::save_columns(&app.path, app.columns.as_serializable_columns());
if let Some(r) = nav_resp {
r.process_nav_response(&app.path, &mut app.columns);
}
});
}
Expand Down
35 changes: 28 additions & 7 deletions src/nav.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::{
account_manager::render_accounts_route,
app_style::{get_font_size, NotedeckTextStyle},
column::Columns,
fonts::NamedFontFamily,
notes_holder::NotesHolder,
profile::Profile,
relay_pool_manager::RelayPoolManager,
route::Route,
storage::{self, DataPath},
thread::Thread,
timeline::{
route::{render_profile_route, render_timeline_route, AfterRouteExecution, TimelineRoute},
Expand All @@ -27,8 +29,28 @@ use egui_nav::{Nav, NavAction, TitleBarResponse};
use nostrdb::{Ndb, Transaction};
use tracing::{error, info};

pub fn render_nav(col: usize, app: &mut Damus, ui: &mut egui::Ui) -> bool {
let mut col_changed = false;
pub enum RenderNavResponse {
ColumnChanged,
RemoveColumn(usize),
}

impl RenderNavResponse {
pub fn process_nav_response(&self, path: &DataPath, columns: &mut Columns) {
match self {
RenderNavResponse::ColumnChanged => {
storage::save_columns(path, columns.as_serializable_columns());
}

RenderNavResponse::RemoveColumn(col) => {
columns.delete_column(*col);
storage::save_columns(path, columns.as_serializable_columns());
}
}
}
}

pub fn render_nav(col: usize, app: &mut Damus, ui: &mut egui::Ui) -> Option<RenderNavResponse> {
let mut resp: Option<RenderNavResponse> = None;
let col_id = app.columns.get_column_id_at_index(col);
// TODO(jb55): clean up this router_mut mess by using Router<R> in egui-nav directly
let routes = app
Expand Down Expand Up @@ -193,14 +215,14 @@ pub fn render_nav(col: usize, app: &mut Damus, ui: &mut egui::Ui) -> bool {
pubkey.bytes(),
);
}
col_changed = true;
resp = Some(RenderNavResponse::ColumnChanged)
} else if let Some(NavAction::Navigated) = nav_response.action {
let cur_router = app.columns_mut().column_mut(col).router_mut();
cur_router.navigating = false;
if cur_router.is_replacing() {
cur_router.remove_previous_routes();
}
col_changed = true;
resp = Some(RenderNavResponse::ColumnChanged)
}

if let Some(title_response) = nav_response.title_response {
Expand All @@ -210,13 +232,12 @@ pub fn render_nav(col: usize, app: &mut Damus, ui: &mut egui::Ui) -> bool {
if let Some(timeline) = tl {
unsubscribe_timeline(app.ndb(), timeline);
}
app.columns_mut().delete_column(col);
col_changed = true;
resp = Some(RenderNavResponse::RemoveColumn(col))
}
}
}

col_changed
resp
}

fn unsubscribe_timeline(ndb: &Ndb, timeline: &Timeline) {
Expand Down

0 comments on commit de8029d

Please sign in to comment.