save window size (Cocoa)

Sat, 06 Jun 2026 18:37:04 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 06 Jun 2026 18:37:04 +0200
changeset 1176
87a906a990e3
parent 1175
1943f7411e50
child 1177
e5e5c1779294

save window size (Cocoa)

ui/cocoa/MainWindow.h file | annotate | diff | comparison | revisions
ui/cocoa/MainWindow.m file | annotate | diff | comparison | revisions
ui/cocoa/WindowManager.h file | annotate | diff | comparison | revisions
ui/cocoa/WindowManager.m file | annotate | diff | comparison | revisions
ui/cocoa/appdelegate.m file | annotate | diff | comparison | revisions
ui/cocoa/toolkit.h file | annotate | diff | comparison | revisions
ui/cocoa/toolkit.m file | annotate | diff | comparison | revisions
ui/cocoa/window.m file | annotate | diff | comparison | revisions
--- a/ui/cocoa/MainWindow.h	Sat Jun 06 18:08:51 2026 +0200
+++ b/ui/cocoa/MainWindow.h	Sat Jun 06 18:37:04 2026 +0200
@@ -38,7 +38,8 @@
 @property (strong) NSView *rightPanel;
 @property int topOffset;
 
-- (MainWindow*)init:(UiObject*)obj withSidebar:(BOOL)hasSidebar withSplitview:(BOOL)hasSplitview;
+- (MainWindow*)init:(UiObject*)obj withSidebar:(BOOL)hasSidebar withSplitview:(BOOL)hasSplitview width:(int)width height:(int)height;
+- (void) saveWindowSize;
 
 @end
 
--- a/ui/cocoa/MainWindow.m	Sat Jun 06 18:08:51 2026 +0200
+++ b/ui/cocoa/MainWindow.m	Sat Jun 06 18:37:04 2026 +0200
@@ -43,8 +43,8 @@
 
 @implementation MainWindow
 
-- (MainWindow*)init:(UiObject*)obj withSidebar:(BOOL)hasSidebar withSplitview:(BOOL)hasSplitview{
-    NSRect frame = NSMakeRect(300, 200, 600, 500);
+- (MainWindow*)init:(UiObject*)obj withSidebar:(BOOL)hasSidebar withSplitview:(BOOL)hasSplitview width:(int)width height:(int)height {
+    NSRect frame = NSMakeRect(300, 200, width, height);
     
     self = [self initWithContentRect:frame
                            styleMask:NSWindowStyleMaskTitled |
@@ -143,6 +143,18 @@
     return self;
 }
 
+- (void) saveWindowSize {
+    CGSize size = self.frame.size;
+    if(size.width > 0 && size.height > 0) {
+        char width_str[32];
+        char height_str[32];
+        snprintf(width_str, 32, "%d", (int)size.width);
+        snprintf(height_str, 32, "%d", (int)size.height);
+        ui_set_property("ui.window.width", width_str);
+        ui_set_property("ui.window.height", height_str);
+    }
+}
+
 - (BOOL) getIsVisible {
     return [self isVisible];
 }
@@ -175,9 +187,9 @@
     }
     
     if(obj->ref == 0) {
+        [self saveWindowSize];
         // this cleans up any widget references from the context
         uic_context_prepare_close(obj->ctx);
-        
         [[WindowManager sharedWindowManager] closeWindow:self];
     }
 }
--- a/ui/cocoa/WindowManager.h	Sat Jun 06 18:08:51 2026 +0200
+++ b/ui/cocoa/WindowManager.h	Sat Jun 06 18:37:04 2026 +0200
@@ -40,4 +40,6 @@
 
 - (void)closeWindow:(NSWindow*)win;
 
+- (void)shutdown;
+
 @end
--- a/ui/cocoa/WindowManager.m	Sat Jun 06 18:08:51 2026 +0200
+++ b/ui/cocoa/WindowManager.m	Sat Jun 06 18:37:04 2026 +0200
@@ -27,6 +27,7 @@
  */
 
 #import "WindowManager.h"
+#import "MainWindow.h"
 
 @implementation WindowManager
 
@@ -52,4 +53,13 @@
     [_windows removeObject:win.windowController];
 }
 
+- (void)shutdown {
+    if(_windows.count > 0) {
+        NSWindowController *controller = _windows.firstObject;
+        MainWindow *win = (MainWindow*)controller.window;
+        [win saveWindowSize];
+    }
+    [_windows removeAllObjects];
+}
+
 @end
--- a/ui/cocoa/appdelegate.m	Sat Jun 06 18:08:51 2026 +0200
+++ b/ui/cocoa/appdelegate.m	Sat Jun 06 18:37:04 2026 +0200
@@ -27,21 +27,26 @@
  */
 
 #import "AppDelegate.h"
+#import "WindowManager.h"
 
 #import "toolkit.h"
 #import "menu.h"
 
+#import "../common/app.h"
+
 @implementation AppDelegate
 
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
     ui_menu_init();
     NSLog(@"toolkit applicationDidFinishLaunching");
-    ui_cocoa_onstartup();
+    uic_application_startup(NULL);
 }
 
 - (void)applicationWillTerminate:(NSNotification *)aNotification {
     NSLog(@"toolkit applicationWillTerminate");
-    ui_cocoa_onexit();
+    uic_application_exit(NULL);
+    [[WindowManager sharedWindowManager] shutdown];
+    ui_app_save_settings();
 }
 
 
--- a/ui/cocoa/toolkit.h	Sat Jun 06 18:08:51 2026 +0200
+++ b/ui/cocoa/toolkit.h	Sat Jun 06 18:37:04 2026 +0200
@@ -51,7 +51,4 @@
 
 @end
 
-void ui_cocoa_onstartup(void);
-void ui_cocoa_onopen(const char *file);
-void ui_cocoa_onexit(void);
 
--- a/ui/cocoa/toolkit.m	Sat Jun 06 18:08:51 2026 +0200
+++ b/ui/cocoa/toolkit.m	Sat Jun 06 18:37:04 2026 +0200
@@ -114,36 +114,6 @@
     exit_on_shutdown = exitapp;
 }
 
-void ui_cocoa_onstartup(void) {
-    UiEvent e;
-    e.obj = NULL;
-    e.window = NULL;
-    e.document = NULL;
-    e.eventdata = NULL;
-    e.intval = 0;
-    uic_application_startup(&e);
-}
-
-void ui_cocoa_onopen(const char *file) {
-    UiEvent e;
-    e.obj = NULL;
-    e.window = NULL;
-    e.document = NULL;
-    e.eventdata = NULL;
-    e.intval = 0;
-    uic_application_open(&e);
-}
-
-void ui_cocoa_onexit(void) {
-    UiEvent e;
-    e.obj = NULL;
-    e.window = NULL;
-    e.document = NULL;
-    e.eventdata = NULL;
-    e.intval = 0;
-    uic_application_exit(&e);
-}
-
 void ui_main(void) {
     main_thr_check("ui_main");
     
--- a/ui/cocoa/window.m	Sat Jun 06 18:08:51 2026 +0200
+++ b/ui/cocoa/window.m	Sat Jun 06 18:37:04 2026 +0200
@@ -41,6 +41,7 @@
 #include "../common/menu.h"
 #include "../common/toolbar.h"
 #include "../common/object.h"
+#include "../common/utils.h"
 
 #include <cx/mempool.h>
 
@@ -52,13 +53,20 @@
 
 static void main_window_destroy(UiObject *obj) {
     MainWindow *window = (__bridge MainWindow*)obj->wobj;
+    [window saveWindowSize];
     [[WindowManager sharedWindowManager] closeWindow:window];
 }
 
 static UiObject* create_window(const char *title, BOOL simple, BOOL sidebar, BOOL splitview) {
     UiObject *obj = uic_object_new_toplevel();
     
-    MainWindow *window = [[MainWindow alloc] init:obj withSidebar:sidebar withSplitview:splitview];
+    int width = window_default_width;
+    int height = window_default_height;
+    if(!simple) {
+       ui_get_window_default_width(&width, &height);
+   }
+    
+    MainWindow *window = [[MainWindow alloc] init:obj withSidebar:sidebar withSplitview:splitview width:width height:height];
     
     obj->wobj = (__bridge void*)window;
     obj->destroy = main_window_destroy;

mercurial