# HG changeset patch
# User Olaf Wintermann <olaf.wintermann@gmail.com>
# Date 1730194634 -3600
# Node ID 004b5dd01f6a50c8d3b215244b15e173ef3de2f5
# Parent  eaec7c62ce41563c3e2fb19f262dcb97fa89aaab
clear context states before closing a window

diff -r eaec7c62ce41 -r 004b5dd01f6a ui/common/context.c
--- a/ui/common/context.c	Tue Oct 29 10:28:52 2024 +0100
+++ b/ui/common/context.c	Tue Oct 29 10:37:14 2024 +0100
@@ -81,6 +81,11 @@
     return ctx->parent ? uic_root_context(ctx->parent) : ctx;
 }
 
+void uic_context_prepare_close(UiContext *ctx) {
+    cxListClear(ctx->groups);
+    cxListClear(ctx->group_widgets);
+}
+
 void uic_context_attach_document(UiContext *ctx, void *document) {
     cxListAdd(ctx->documents, document);
     ctx->document = document;
diff -r eaec7c62ce41 -r 004b5dd01f6a ui/common/context.h
--- a/ui/common/context.h	Tue Oct 29 10:28:52 2024 +0100
+++ b/ui/common/context.h	Tue Oct 29 10:37:14 2024 +0100
@@ -115,6 +115,8 @@
 void uic_context_set_document(UiContext *ctx, void *document); // deprecated
 void uic_context_detach_document(UiContext *ctx); // deprecated
 
+void uic_context_prepare_close(UiContext *ctx);
+
 void uic_context_attach_document(UiContext *ctx, void *document);
 void uic_context_detach_document2(UiContext *ctx, void *document);
 void uic_context_detach_all(UiContext *ctx);
diff -r eaec7c62ce41 -r 004b5dd01f6a ui/gtk/toolkit.c
--- a/ui/gtk/toolkit.c	Tue Oct 29 10:28:52 2024 +0100
+++ b/ui/gtk/toolkit.c	Tue Oct 29 10:37:14 2024 +0100
@@ -166,6 +166,7 @@
 }
 
 void ui_close(UiObject *obj) {
+    uic_context_prepare_close(obj->ctx);
 #if GTK_CHECK_VERSION(4, 0, 0)
     gtk_window_close(GTK_WINDOW(obj->widget));
 #else
diff -r eaec7c62ce41 -r 004b5dd01f6a ui/gtk/window.c
--- a/ui/gtk/window.c	Tue Oct 29 10:28:52 2024 +0100
+++ b/ui/gtk/window.c	Tue Oct 29 10:37:14 2024 +0100
@@ -71,6 +71,11 @@
 #endif
 }
 
+static gboolean close_request(GtkWindow* self, UiContext *ctx) {
+    uic_context_prepare_close(ctx);
+    return FALSE;
+}
+
 static UiObject* create_window(const char *title, void *window_data, UiBool simple) {
     CxMempool *mp = cxBasicMempoolCreate(256);
     UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObject)); 
@@ -114,6 +119,11 @@
             "destroy",
             G_CALLBACK(ui_exit_event),
             obj);
+    g_signal_connect(
+            obj->widget,
+            "close-request",
+            G_CALLBACK(close_request),
+            obj->ctx);
     
     GtkWidget *vbox = ui_gtk_vbox_new(0);
 #ifdef UI_LIBADWAITA