]> uap-core.de Git - note.git/commitdiff
fix crash when switching notebooks
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 23 Feb 2025 16:08:33 +0000 (17:08 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 23 Feb 2025 16:08:33 +0000 (17:08 +0100)
application/notebook.c
ui/common/context.c

index 734c3bacb626a95780c824d61e20c113da747013..cd5c636dcb8cf93b9855e750c44b23358a48ca0f 100644 (file)
@@ -85,9 +85,12 @@ void notebookmodel_reload(UiObject *obj, NotebookModel *model) {
 
 
 void notebookmodel_attach_note(NotebookModel *model, Note *note) {
-    if(model->current_note == note) {
-        return; // NOOP
-    }
+    // TODO: enable this optimization when possible
+    //       currently, a reattaching a notebook will still have current_note
+    //       but the UI binding will not be enabled
+    //if(model->current_note == note) {
+    //    return; // NOOP
+    //}
     
     if(note && !note->model) {
         NoteModel *notemodel = notemodel_create(model->current_notes_pool->allocator);
@@ -95,7 +98,7 @@ void notebookmodel_attach_note(NotebookModel *model, Note *note) {
     }
     
     notebookmodel_detach_current_note(model);
-    ui_attach_document(model->window->obj->ctx, note->model);
+    ui_attach_document(model->ctx, note->model);
     // TODO: this is only a workaround and should be removed when
     //       sub-document groups are supported
     ui_set_group(model->window->obj->ctx, APP_STATE_NOTE_SELECTED);
@@ -108,7 +111,7 @@ void notebookmodel_attach_note(NotebookModel *model, Note *note) {
 
 void notebookmodel_detach_current_note(NotebookModel *model) {
     if(model->current_note && model->current_note->model) {
-        ui_detach_document2(model->window->obj->ctx, model->current_note->model);
+        ui_detach_document2(model->ctx, model->current_note->model);
         model->current_note = NULL;
     }
 }
index f7dfdd363107e876f80b396f2e8f0f4485fec032..825ac9b67051e3e01d5df81d18a52ab5e32173f9 100644 (file)
@@ -92,6 +92,7 @@ void uic_context_attach_document(UiContext *ctx, void *document) {
     ctx->document = document;
     
     UiContext *doc_ctx = ui_document_context(document);
+    doc_ctx->parent = ctx;
     
     // check if any parent context has an unbound variable with the same name
     // as any document variable
@@ -111,7 +112,7 @@ void uic_context_attach_document(UiContext *ctx, void *document) {
             }
         }
         
-        var_ctx = ctx->parent;
+        var_ctx = var_ctx->parent;
     }
 }
 
@@ -119,7 +120,7 @@ static void uic_context_unbind_vars(UiContext *ctx) {
     CxMapIterator mi = cxMapIterator(ctx->vars);
     cx_foreach(CxMapEntry*, entry, mi) {
         UiVar *var = entry->value;
-        if(var->from && var->from_ctx) {
+        if(var->from && var->from_ctx && var->from_ctx != ctx) {
             uic_save_var2(var);
             uic_copy_binding(var, var->from, FALSE);
             cxMapPut(var->from_ctx->vars_unbound, *entry->key, var->from);
@@ -148,6 +149,7 @@ void uic_context_detach_document2(UiContext *ctx, void *document) {
     
     UiContext *docctx = ui_document_context(document);
     uic_context_unbind_vars(docctx); // unbind all doc/subdoc vars from the parent
+    docctx->parent = NULL;
 }
 
 void uic_context_detach_all(UiContext *ctx) {