remember app window size (GTK)

Sat, 13 Sep 2025 19:53:50 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 13 Sep 2025 19:53:50 +0200
changeset 763
258ec399fbe6
parent 762
9452eccfc0f0
child 764
5ff4b9c0cf96

remember app window size (GTK)

ui/common/properties.c file | annotate | diff | comparison | revisions
ui/gtk/window.c file | annotate | diff | comparison | revisions
--- a/ui/common/properties.c	Sat Sep 13 19:52:18 2025 +0200
+++ b/ui/common/properties.c	Sat Sep 13 19:53:50 2025 +0200
@@ -204,9 +204,9 @@
 
 void ui_set_property(const char *name, const char *value) {
     if(value) {
-        cxMapRemove(application_properties, name);
+        cxMapPut(application_properties, name, strdup(value));
     } else {
-        cxMapPut(application_properties, name, strdup(value));
+        cxMapRemove(application_properties, name);
     }
 }
 
--- a/ui/gtk/window.c	Sat Sep 13 19:52:18 2025 +0200
+++ b/ui/gtk/window.c	Sat Sep 13 19:53:50 2025 +0200
@@ -77,6 +77,36 @@
 }
 
 static gboolean ui_window_close_request(UiObject *obj) {
+    if(obj->widget) {
+        void *appwindow = g_object_get_data(G_OBJECT(obj->widget), "ui.appwindow");
+        if(appwindow) {
+            int width = 0;
+            int height = 0;
+#if GTK_CHECK_VERSION(4, 10, 0)
+            graphene_rect_t bounds;
+            if(gtk_widget_compute_bounds(obj->widget, obj->widget, &bounds)) {
+                width = bounds.size.width;
+                height = bounds.size.height;
+            }
+#elif GTK_CHECK_VERSION(4, 0, 0)
+            GtkAllocation alloc;
+            gtk_widget_get_allocation(GTK_WIDGET(obj->widget), &alloc);
+            width = alloc.width;
+            height = alloc.height;
+#else
+            gtk_window_get_size(GTK_WINDOW(obj->widget), &width, &height);
+#endif
+            if(width > 0 && height > 0) {
+                char width_str[32];
+                char height_str[32];
+                snprintf(width_str, 32, "%d", width);
+                snprintf(height_str, 32, "%d", height);
+                ui_set_property("ui.window.width", width_str);
+                ui_set_property("ui.window.height", height_str);
+            }
+        }
+    }
+    
     uic_context_prepare_close(obj->ctx);
     obj->ref--;
     if(obj->ref > 0) {
@@ -122,6 +152,10 @@
         gtk_window_set_title(GTK_WINDOW(obj->widget), title);
     }
     
+    if(!simple) {
+        g_object_set_data(G_OBJECT(obj->widget), "ui.appwindow", obj);
+    }
+    
     const char *width = ui_get_property("ui.window.width");
     const char *height = ui_get_property("ui.window.height");
     if(width && height) {

mercurial