add API for setting the window splitview and implement position autosave

Fri, 03 Oct 2025 11:16:24 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 03 Oct 2025 11:16:24 +0200
changeset 790
67da3f795226
parent 789
d70799b3333e
child 791
c83b9acfa316

add API for setting the window splitview and implement position autosave

ui/gtk/window.c file | annotate | diff | comparison | revisions
ui/ui/window.h file | annotate | diff | comparison | revisions
--- a/ui/gtk/window.c	Fri Oct 03 10:17:31 2025 +0200
+++ b/ui/gtk/window.c	Fri Oct 03 11:16:24 2025 +0200
@@ -49,6 +49,9 @@
 static int window_default_width = 650;
 static int window_default_height = 550;
 
+static int splitview_window_default_pos = -1;
+static UiBool splitview_window_use_prop = TRUE;
+
 static gboolean ui_window_destroy(void *data)  {
     UiObject *obj = data;
     uic_object_destroy(obj);
@@ -131,6 +134,13 @@
 }
 #endif
 
+static void save_window_splitview_pos(GtkWidget *widget, void *unused) {
+    int pos = gtk_paned_get_position(GTK_PANED(widget));
+    char buf[32];
+    snprintf(buf, 32, "%d", pos);
+    ui_set_property("ui.window.splitview.pos", buf);
+}
+
 static UiObject* create_window(const char *title, void *window_data, UiBool sidebar, UiBool splitview, UiBool simple) {
     UiObject *obj = uic_object_new_toplevel();
    
@@ -207,6 +217,24 @@
     GtkWidget *content = toolbar_view;
     if(splitview) {
         content = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
+        g_signal_connect(
+                content,
+                "destroy",
+                G_CALLBACK(save_window_splitview_pos),
+                NULL);
+        
+        const char *splitview_pos_str = ui_get_property("ui.window.splitview.pos");
+        int pos = splitview_window_default_pos;
+        if(pos < 0) {
+            pos = window_width / 2;
+        }
+        if(splitview_pos_str && splitview_window_use_prop) {
+            int splitview_pos = atoi(splitview_pos_str);
+            if(splitview_pos > 0) {
+                pos = splitview_pos;
+            }
+        }
+        gtk_paned_set_position(GTK_PANED(content), pos);
         
         GtkWidget *right_panel = adw_toolbar_view_new();
         GtkWidget *right_vbox = ui_gtk_vbox_new(0);
@@ -375,11 +403,38 @@
                 height);
 }
 
-void ui_window_default_size(UiObject *obj, int width, int height) {
+void ui_window_default_size(int width, int height) {
     window_default_width = width;
     window_default_height = height;
 }
 
+void ui_splitview_window_set_pos(UiObject *obj, int pos) {
+    GtkWidget *splitview = g_object_get_data(G_OBJECT(obj->widget), "ui_window_splitview");
+    if(splitview) {
+        gtk_paned_set_position(GTK_PANED(splitview), pos);
+    } else {
+        fprintf(stderr, "Error: window has no splitview\n");
+    }
+}
+
+int ui_splitview_window_get_pos(UiObject *obj) {
+    GtkWidget *splitview = g_object_get_data(G_OBJECT(obj->widget), "ui_window_splitview");
+    if(splitview) {
+        return gtk_paned_get_position(GTK_PANED(splitview));
+    } else {
+        fprintf(stderr, "Error: window has no splitview\n");
+    }
+    return 0;
+}
+
+void ui_splitview_window_set_default_pos(int pos) {
+    splitview_window_default_pos = pos;
+}
+
+void ui_splitview_window_use_property(UiBool enable) {
+    splitview_window_use_prop = enable;
+}
+
 #ifdef UI_LIBADWAITA
 
 static void dialog_response(AdwAlertDialog *self, gchar *response, UiEventData *data) {
--- a/ui/ui/window.h	Fri Oct 03 10:17:31 2025 +0200
+++ b/ui/ui/window.h	Fri Oct 03 11:16:24 2025 +0200
@@ -82,7 +82,12 @@
 #define ui_dialog_window0(parent) ui_dialog_window_create(parent, &(UiDialogWindowArgs){ 0 });
 
 UIEXPORT void ui_window_size(UiObject *obj, int width, int height);
-UIEXPORT void ui_window_default_size(UiObject *obj, int width, int height);
+UIEXPORT void ui_window_default_size(int width, int height);
+
+UIEXPORT void ui_splitview_window_set_pos(UiObject *obj, int pos);
+UIEXPORT int ui_splitview_window_get_pos(UiObject *obj);
+UIEXPORT void ui_splitview_window_set_default_pos(int pos);
+UIEXPORT void ui_splitview_window_use_property(UiBool enable);
 
 #define ui_dialog(parent, ...) ui_dialog_create(parent, &(UiDialogArgs){ __VA_ARGS__ } )
 

mercurial