From 9cbf3ae2eaf0bbbe7aa48425475e8e50654fb6a5 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Tue, 18 Mar 2025 21:25:44 +0100 Subject: [PATCH] fix crash with gtk3 when switching and saving notes --- application/application.c | 2 +- application/application.h | 6 ++++++ application/note.c | 2 ++ application/notebook.c | 7 ++++--- application/window.c | 4 ++++ ui/common/types.c | 4 +++- 6 files changed, 20 insertions(+), 5 deletions(-) diff --git a/application/application.c b/application/application.c index 745782b..c6ebc21 100644 --- a/application/application.c +++ b/application/application.c @@ -153,4 +153,4 @@ void action_note_delete(UiEvent *event, void *data) { .closebutton_label = "Cancel", .result = delete_result); free(msg.ptr); -} \ No newline at end of file +} diff --git a/application/application.h b/application/application.h index 00a06fc..6269728 100644 --- a/application/application.h +++ b/application/application.h @@ -117,6 +117,12 @@ struct NotebookModel { */ Note *current_note; + /* + * some actions might trigger unwanted selection events + * if this is true, selection events for the notes list are not handled + */ + UiBool disable_selection_events; + /* * The mempool used to allocate the current list of notes */ diff --git a/application/note.c b/application/note.c index 52f0b7e..b05b98a 100644 --- a/application/note.c +++ b/application/note.c @@ -129,8 +129,10 @@ void note_save(UiObject *obj, NotebookModel *notebook, Note *note) { if(note->note_id == 0) { // new note note_store_new_note_async(obj, note, NULL, NULL); + notebook->disable_selection_events = TRUE; ui_list_append(notebook->notes, note); ui_list_update(notebook->notes); + notebook->disable_selection_events = FALSE; } else { note_store_save_note_async(obj, note, NULL, NULL); } diff --git a/application/notebook.c b/application/notebook.c index b2ba831..85d76d5 100644 --- a/application/notebook.c +++ b/application/notebook.c @@ -151,15 +151,16 @@ void notebookmodel_attach_note(NotebookModel *model, Note *note) { void notebookmodel_detach_current_note(NotebookModel *model) { if(model->current_note) { + Note *current_note = model->current_note; // TODO: model->modified doesnt work yet, remove || 1 when it works - if(model->current_note->model->modified || 1) { + if(current_note->model->modified || 1) { note_save(model->window->obj, model, model->current_note); } // TODO: in theory ui_detach_document2 should save the state // of all vars, but it seems this doesn't work // without note_save, the content is not saved (internally) - if(model->current_note->model) { - ui_detach_document2(model->ctx, model->current_note->model); + if(current_note->model) { + ui_detach_document2(model->ctx, current_note->model); } model->current_note = NULL; } diff --git a/application/window.c b/application/window.c index a1da5c9..3d8ec1d 100644 --- a/application/window.c +++ b/application/window.c @@ -276,6 +276,10 @@ static int select_note(UiEvent *event) { return 0; // should not happen } + if(notebook->disable_selection_events) { + return 0; + } + if(sel->count != 1) { notebookmodel_detach_current_note(notebook); return 0; diff --git a/ui/common/types.c b/ui/common/types.c index dc8d5c3..ab04faa 100644 --- a/ui/common/types.c +++ b/ui/common/types.c @@ -162,7 +162,9 @@ void ui_list_clear(UiList *list) { UIEXPORT void ui_list_update(UiList *list) { if(list->update) { - list->update(list, 0); + ui_setop_enable(TRUE); + list->update(list, -1); + ui_setop_enable(FALSE); } } -- 2.43.5