From 0ce50051b07719a6ccb8b5cd99e46e0b66ca852b Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Thu, 28 May 2026 18:21:10 +0200 Subject: [PATCH] fix note save --- application/src/note.rs | 1 + application/src/notebook.rs | 6 ++++++ application/src/window.rs | 6 ++++++ ui/cocoa/Toolbar.m | 22 +++++++++++++++------- ui/cocoa/toolkit.m | 14 +++++++++----- ui/common/context.c | 3 +++ ui/common/types.c | 9 ++++++++- ui/common/wrapper.c | 2 ++ ui/gtk/text.c | 33 +++++++++++++++++++-------------- 9 files changed, 69 insertions(+), 27 deletions(-) diff --git a/application/src/note.rs b/application/src/note.rs index 14b7c64..4a9bd42 100644 --- a/application/src/note.rs +++ b/application/src/note.rs @@ -24,6 +24,7 @@ pub struct Note { #[ui_actions] impl Note { pub fn init_from_model(&mut self, model: &entity::note::Model) { + self.note_id = model.note_id; self.text.set(model.content.as_str()); let tab = match model.kind { diff --git a/application/src/notebook.rs b/application/src/notebook.rs index 2239b5a..eb1249d 100644 --- a/application/src/notebook.rs +++ b/application/src/notebook.rs @@ -93,4 +93,10 @@ pub fn note_getvalue<'a>(elm: &Note, col: i32, _row: i32) -> ListValue<'a> { 1 => ListValue::String(elm.lastmodified.to_string()), _ => ListValue::None } +} + +impl Drop for Notebook { + fn drop(&mut self) { + println!("Notebook dropped"); + } } \ No newline at end of file diff --git a/application/src/window.rs b/application/src/window.rs index ac5e96b..a3629ca 100644 --- a/application/src/window.rs +++ b/application/src/window.rs @@ -49,6 +49,12 @@ pub struct MainWindow { notebooks: UiSourceList } +impl Drop for MainWindow { + fn drop(&mut self) { + println!("MainWindow dropped!"); + } +} + #[ui_actions] impl MainWindow { pub fn new(app: &App) -> MainWindow { diff --git a/ui/cocoa/Toolbar.m b/ui/cocoa/Toolbar.m index cf03ced..8817b8e 100644 --- a/ui/cocoa/Toolbar.m +++ b/ui/cocoa/Toolbar.m @@ -243,14 +243,23 @@ NSToolbarItem* ui_nstoolbaritem_create_toggle(UiObject *obj, UiToolbarToggleItem } objc_setAssociatedObject(button, "eventdata", event, OBJC_ASSOCIATION_RETAIN); - NSSegmentedControl *seg; + NSSegmentedControl *seg = nil; if(!item->args.icon) { - NSArray *labels = @[[[NSString alloc] initWithUTF8String:item->args.label]]; - seg = [NSSegmentedControl segmentedControlWithLabels:labels trackingMode:NSSegmentSwitchTrackingSelectAny target:event action:@selector(handleEvent:)]; - button.view = seg; + if(item->args.label) { + NSArray *labels = @[[[NSString alloc] initWithUTF8String:item->args.label]]; + seg = [NSSegmentedControl segmentedControlWithLabels:labels trackingMode:NSSegmentSwitchTrackingSelectAny target:event action:@selector(handleEvent:)]; + button.view = seg; + } else { + NSLog(@"UI Error: toggle button has no icon or label"); + } } else { - NSArray *images = @[ui_cocoa_named_icon(item->args.icon)]; - seg = [NSSegmentedControl segmentedControlWithImages:images trackingMode:NSSegmentSwitchTrackingSelectAny target:event action:@selector(handleEvent:)]; + NSImage *image = ui_cocoa_named_icon(item->args.icon); + if(image != nil) { + NSArray *images = @[image]; + seg = [NSSegmentedControl segmentedControlWithImages:images trackingMode:NSSegmentSwitchTrackingSelectAny target:event action:@selector(handleEvent:)]; + } else { + NSLog(@"UI Error: icon %s not found", item->args.icon); + } } button.view = seg; @@ -265,7 +274,6 @@ NSToolbarItem* ui_nstoolbaritem_create_toggle(UiObject *obj, UiToolbarToggleItem } i->obj = (__bridge void*)seg; - printf("seg: %p\n", seg); i->get = ui_toolbar_seg_toggleitem_get; i->set = ui_toolbar_seg_toggleitem_set; } diff --git a/ui/cocoa/toolkit.m b/ui/cocoa/toolkit.m index 7168465..6eb503e 100644 --- a/ui/cocoa/toolkit.m +++ b/ui/cocoa/toolkit.m @@ -76,11 +76,6 @@ void ui_init(const char *appname, int argc, char **argv) { app_delegate = [[AppDelegate alloc] init]; - if(app.delegate != nil) { - printf("app delegate exists\n"); - } else { - printf("app delegate is null\n"); - } fflush(stdout); app.delegate = app_delegate; @@ -130,6 +125,9 @@ void ui_cocoa_onexit(void) { void ui_main(void) { NSApplicationMain(app_argc, app_argv); + //[NSApp finishLaunching]; + //[NSApp activateIgnoringOtherApps:YES]; + //[NSApp run]; if(exit_on_shutdown) { exit(0); } @@ -147,6 +145,12 @@ void ui_app_quit(void) { [[NSApplication sharedApplication] terminate:nil]; } +void ui_open_uri(const char *uri) { + NSString *urlString = [NSString stringWithUTF8String:uri]; + NSURL *url = [NSURL URLWithString:urlString]; + [[NSWorkspace sharedWorkspace] openURL:url]; +} + /* ------------------- Window Visibility functions ------------------- */ void ui_show(UiObject *obj) { diff --git a/ui/common/context.c b/ui/common/context.c index 19de77d..22ecd55 100644 --- a/ui/common/context.c +++ b/ui/common/context.c @@ -109,6 +109,7 @@ void uic_context_remove_destructor(UiContext *ctx, void *data) { void uic_context_prepare_close(UiContext *ctx) { cxListClear(ctx->states); cxListClear(ctx->state_widgets); + cxListClear(ctx->action_bindings); } void uic_context_destroy(UiContext *ctx, void *document) { @@ -134,6 +135,8 @@ void uic_context_destroy(UiContext *ctx, void *document) { h->destructor(h->data); } + uic_context_detach_all(ctx); + cxMempoolFree(ctx->mp); } diff --git a/ui/common/types.c b/ui/common/types.c index 0e62a7a..7105af5 100644 --- a/ui/common/types.c +++ b/ui/common/types.c @@ -776,6 +776,14 @@ void uic_string_unbind(UiString *s) { } void uic_text_unbind(UiText *t) { + t->obj = NULL; + if(t->data1 && t->datatype == UI_TEXT_TYPE_BUFFER) { + // the binding functions all work with t->data1, not t->obj + // and we don't want to NULL them, because they will be still + // functional even without a widget binding + return; + } + t->set = NULL; t->get = NULL; t->getsubstr = NULL; @@ -787,7 +795,6 @@ void uic_text_unbind(UiText *t) { t->setselection = NULL; t->length = NULL; t->remove = NULL; - t->obj = NULL; } void uic_range_unbind(UiRange *r) { diff --git a/ui/common/wrapper.c b/ui/common/wrapper.c index a6ef56a..7f1441d 100644 --- a/ui/common/wrapper.c +++ b/ui/common/wrapper.c @@ -52,6 +52,7 @@ void ui_object_set_onclose(UiObject *obj, ui_callback callback, void *userdata) static int obj_unref(void *ptr) { ui_object_unref(ptr); + return 0; } void ui_mainthread_object_unref(UiObject *obj) { @@ -62,6 +63,7 @@ void ui_mainthread_object_unref(UiObject *obj) { static int doc_unref(void *ptr) { ui_document_unref(ptr); + return 0; } void ui_mainthread_document_unref(void *doc) { diff --git a/ui/gtk/text.c b/ui/gtk/text.c index 3ae5318..4fc0eb1 100644 --- a/ui/gtk/text.c +++ b/ui/gtk/text.c @@ -406,7 +406,9 @@ int ui_textarea_position(UiText *text) { } void ui_textarea_showposition(UiText *text, int pos) { - ui_textarea_scroll_to(text->obj, pos); + if(text->obj) { + ui_textarea_scroll_to(text->obj, pos); + } } void ui_textarea_setselection(UiText *text, int begin, int end) { @@ -564,6 +566,7 @@ void ui_textbuf_delete( if(!value->data2) { value->data2 = ui_create_undomgr(); } + GtkTextBuffer *buf = value->data1; UiUndoMgr *mgr = value->data2; if(!mgr->event) { return; @@ -583,7 +586,7 @@ void ui_textbuf_delete( } } - char *text = gtk_text_buffer_get_text(value->obj, start, end, FALSE); + char *text = gtk_text_buffer_get_text(buf, start, end, FALSE); UiTextBufOp *op = malloc(sizeof(UiTextBufOp)); op->prev = NULL; @@ -659,6 +662,7 @@ int ui_check_insertstr(char *oldstr, int oldlen, char *newstr, int newlen) { } void ui_text_undo(UiText *value) { + GtkTextBuffer *buf = value->data1; UiUndoMgr *mgr = value->data2; if(mgr->cur) { @@ -668,17 +672,17 @@ void ui_text_undo(UiText *value) { case UI_TEXTBUF_INSERT: { GtkTextIter begin; GtkTextIter end; - gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start); - gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end); - gtk_text_buffer_delete(value->obj, &begin, &end); + gtk_text_buffer_get_iter_at_offset(buf, &begin, op->start); + gtk_text_buffer_get_iter_at_offset(buf, &end, op->end); + gtk_text_buffer_delete(buf, &begin, &end); break; } case UI_TEXTBUF_DELETE: { GtkTextIter begin; GtkTextIter end; - gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start); - gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end); - gtk_text_buffer_insert(value->obj, &begin, op->text, op->len); + gtk_text_buffer_get_iter_at_offset(buf, &begin, op->start); + gtk_text_buffer_get_iter_at_offset(buf, &end, op->end); + gtk_text_buffer_insert(buf, &begin, op->text, op->len); break; } } @@ -688,6 +692,7 @@ void ui_text_undo(UiText *value) { } void ui_text_redo(UiText *value) { + GtkTextBuffer *buf = value->data1; UiUndoMgr *mgr = value->data2; UiTextBufOp *elm = NULL; @@ -706,17 +711,17 @@ void ui_text_redo(UiText *value) { case UI_TEXTBUF_INSERT: { GtkTextIter begin; GtkTextIter end; - gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start); - gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end); - gtk_text_buffer_insert(value->obj, &begin, op->text, op->len); + gtk_text_buffer_get_iter_at_offset(buf, &begin, op->start); + gtk_text_buffer_get_iter_at_offset(buf, &end, op->end); + gtk_text_buffer_insert(buf, &begin, op->text, op->len); break; } case UI_TEXTBUF_DELETE: { GtkTextIter begin; GtkTextIter end; - gtk_text_buffer_get_iter_at_offset(value->obj, &begin, op->start); - gtk_text_buffer_get_iter_at_offset(value->obj, &end, op->end); - gtk_text_buffer_delete(value->obj, &begin, &end); + gtk_text_buffer_get_iter_at_offset(buf, &begin, op->start); + gtk_text_buffer_get_iter_at_offset(buf, &end, op->end); + gtk_text_buffer_delete(buf, &begin, &end); break; } } -- 2.47.3