--- a/ui/cocoa/MainWindow.m Sat Jun 06 16:53:39 2026 +0200 +++ b/ui/cocoa/MainWindow.m Sat Jun 06 18:04:54 2026 +0200 @@ -27,6 +27,7 @@ */ #import "MainWindow.h" +#import "WindowManager.h" #import "container.h" #import "GridLayout.h" #import "BoxContainer.h" @@ -38,6 +39,8 @@ #import "menu.h" #import "Toolbar.h" + + @implementation MainWindow - (MainWindow*)init:(UiObject*)obj withSidebar:(BOOL)hasSidebar withSplitview:(BOOL)hasSplitview{ @@ -51,7 +54,7 @@ backing:NSBackingStoreBuffered defer:false]; _obj = obj; - + self.delegate = self; int top = 4; NSView *content = self.contentView; @@ -137,7 +140,6 @@ [self setToolbar:toolbar]; } - return self; } @@ -153,7 +155,37 @@ } } +- (void) windowWillClose:(NSNotification *) notification { + UiObject *obj = _obj; + + if(obj->onclose) { + UiEvent event; + memset(&event, 0, sizeof(UiEvent)); + event.obj = obj; + event.window = obj->window; + event.document = obj->ctx->document; + obj->onclose(&event, obj->onclosedata); + } + + if(obj->ref > 0) { + obj->ref--; + } else { + // warn about invalid reference counting + NSLog(@"Error: UiObject %p ref == 0\n", obj); + } + + if(obj->ref == 0) { + // this cleans up any widget references from the context + uic_context_prepare_close(obj->ctx); + + [[WindowManager sharedWindowManager] closeWindow:self]; + } +} +- (void) dealloc { + uic_object_destroy(_obj); + NSLog(@"MainWindow dealloc"); +} @end