From: Olaf Wintermann Date: Tue, 16 Jun 2026 15:40:36 +0000 (+0200) Subject: disable the new note button when the current note is new and empty X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=HEAD;p=note.git disable the new note button when the current note is new and empty --- diff --git a/application/src/note.rs b/application/src/note.rs index 28352f0..51aa015 100644 --- a/application/src/note.rs +++ b/application/src/note.rs @@ -101,6 +101,7 @@ impl Note { n.doc = d.doc_ref(); if n.id.is_new() { n.init_new(); + d.ctx.suppress_state(AppStates::NoteEnableNew as i32); } else { n.init_from_model(model); n.load_content(d); @@ -264,7 +265,7 @@ impl Note { let proxy = doc.doc_proxy(); self.backend.save_note(self.id.clone(), note, Some(notecontent), |result|{ - proxy.call_mainthread(move |_doc, note|{ + proxy.call_mainthread(move |doc, note|{ match result { SaveNoteResult::Ok((notemodel, content_id)) => { note.id = NoteId::Id(notemodel.note_id); @@ -273,6 +274,8 @@ impl Note { println!("Note saved: note_id: {}, content_id: {}, version: {}", notemodel.note_id, content_id, note.version); note.backend.backend.lockmgr.update_note_version(notemodel.note_id, note.version); + // make sure the "new note" button is always enabled within this note + doc.ctx.unsuppress_state(AppStates::NoteEnableNew as i32); }, SaveNoteResult::VersionConflict => { println!("Failed to save note: version conflict"); @@ -316,11 +319,25 @@ impl Note { #[action] pub fn note_text_changed(&mut self, event: &ActionEvent) { + let Some(doc) = self.doc.get_doc() else { + return; + }; + if event.set { // this event was triggered by UiText.set return; } self.modified = true; + + let text = self.text.get(); + if self.id.is_new() { + if text.is_empty() { + doc.ctx.suppress_state(AppStates::NoteEnableNew as i32); + } else { + doc.ctx.unsuppress_state(AppStates::NoteEnableNew as i32); + } + } + if !event.set && self.extract_title { self.update_title(self.text.get().as_str(), true); } diff --git a/application/src/window.rs b/application/src/window.rs index 8d89892..7206615 100644 --- a/application/src/window.rs +++ b/application/src/window.rs @@ -190,6 +190,8 @@ pub fn create_window(app: &App, ctx: &AppContext) -> UiObjectstate_widgets = cxLinkedListCreate(mp->allocator, sizeof(UiStateWidget)); ctx->states = cxArrayListCreate(mp->allocator, sizeof(int), 32); cxSetCompareFunc(ctx->states, cx_cmp_int); + ctx->suppressed_states = cxArrayListCreate(mp->allocator, sizeof(int), 16); + cxSetCompareFunc(ctx->suppressed_states, cx_cmp_int); ctx->actions = cxHashMapCreate(ctx->allocator, sizeof(UiAction), 8); ctx->action_bindings = cxArrayListCreate(ctx->allocator, sizeof(UiActionBinding), 0); @@ -682,15 +684,39 @@ void ui_unset_state(UiContext *ctx, int state) { uic_check_state_widgets(ui_context_toplevel_parent(ctx)); } +void ui_suppress_state(UiContext *ctx, int state) { + if(!cxListIndexValid(ctx->suppressed_states, cxListFind(ctx->suppressed_states, &state))) { + cxListAdd(ctx->suppressed_states, &state); + } + + // enable/disable group widgets + uic_check_state_widgets(ui_context_toplevel_parent(ctx)); +} + +void ui_unsuppress_state(UiContext *ctx, int state) { + int i = cxListFind(ctx->suppressed_states, &state); + if(i != -1) { + cxListRemove(ctx->suppressed_states, i); + } + + // enable/disable group widgets + uic_check_state_widgets(ui_context_toplevel_parent(ctx)); +} + typedef struct StatesList { CX_ARRAY(int, states); + CX_ARRAY(int, suppressed); } StatesList; static void add_ctx_states(UiContext *ctx, StatesList *states) { size_t nstates = cxListSize(ctx->states); - int *ctx_states = cxListAt(ctx->states, 0);; + int *ctx_states = cxListAt(ctx->states, 0); + + size_t nsuppressed = cxListSize(ctx->suppressed_states); + int *ctx_suppressed_states = cxListAt(ctx->suppressed_states, 0); cx_array_add_array(states->states, ctx_states, nstates); + cx_array_add_array(states->suppressed, ctx_suppressed_states, nsuppressed); CxIterator i = cxListIterator(ctx->documents); cx_foreach(void *, doc, i) { @@ -702,7 +728,28 @@ static void add_ctx_states(UiContext *ctx, StatesList *states) { int* ui_active_states(UiContext *ctx, int *nstates) { StatesList states; cx_array_init(states.states, 32); + cx_array_init(states.suppressed, 8); add_ctx_states(ctx, &states); + + // remove suppressed states + for(size_t i=0;istate_widgets); cx_foreach(UiStateWidget *, gw, i) { char *check = calloc(1, gw->numstates); - for(int i=0;inumstates;k++) { - if(groups[i] == gw->states[k]) { + if(states[i] == gw->states[k]) { check[k] = 1; } } @@ -738,7 +785,7 @@ void uic_check_state_widgets(UiContext *ctx) { gw->enable(gw->widget, enable); } - free(groups); + free(states); } void ui_widget_set_states(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, ...) { diff --git a/ui/common/context.h b/ui/common/context.h index 45a9dda..f742d38 100644 --- a/ui/common/context.h +++ b/ui/common/context.h @@ -75,6 +75,7 @@ struct UiContext { CxMap *vars; CxList *states; // int list + CxList *suppressed_states; // int list CxList *state_widgets; // UiGroupWidget list CxMap *actions; // key: action name (string), value: UiAction diff --git a/ui/ui/toolkit.h b/ui/ui/toolkit.h index 848f4fe..0537d58 100644 --- a/ui/ui/toolkit.h +++ b/ui/ui/toolkit.h @@ -624,6 +624,8 @@ UIEXPORT void ui_call_action_on(UiContext *ctx, const char *action); UIEXPORT void ui_set_state(UiContext *ctx, int state); UIEXPORT void ui_unset_state(UiContext *ctx, int state); +UIEXPORT void ui_suppress_state(UiContext *ctx, int state); +UIEXPORT void ui_unsuppress_state(UiContext *ctx, int state); UIEXPORT int* ui_active_states(UiContext *ctx, int *nstates); UIEXPORT void* ui_allocator(UiContext *ctx);