diff -r 31213068c2ba -r be34594ee942 ui/winui/window.cpp
--- a/ui/winui/window.cpp	Wed Feb 26 17:06:56 2025 +0100
+++ b/ui/winui/window.cpp	Wed Feb 26 17:39:03 2025 +0100
@@ -63,6 +63,11 @@
 
 UiWindow::UiWindow(winrt::Microsoft::UI::Xaml::Window& win) : window(win) {}
 
+extern "C" static void ui_window_widget_destroy(UiObject *obj) {
+	obj->ref = 1;
+	obj->wobj->window.Close();
+}
+
 UiObject* ui_window(const char* title, void* window_data) {
 	UiObject* obj = ui_simple_window(title, window_data);
 
@@ -178,17 +183,14 @@
 	obj->wobj = new UiWindow(window);
 	ui_context_add_window_destructor(obj->ctx, obj->wobj);
 
-	window.Closed([obj](IInspectable const& sender, WindowEventArgs) {
-		if (obj->ctx->close_callback) {
-			UiEvent evt;
-			evt.obj = obj;
-			evt.document = obj->ctx->document;
-			evt.window = obj->window;
-			evt.eventdata = NULL;
-			evt.intval = 0;
-			obj->ctx->close_callback(&evt, obj->ctx->close_data);
+	window.Closed([obj](IInspectable const& sender, WindowEventArgs e) {
+		uic_context_prepare_close(obj->ctx);
+		obj->ref--;
+		if (obj->ref > 0) {
+			obj->wobj->window.AppWindow().Hide();
+			e.Handled(true);
 		} else {
-			ui_context_destroy(obj->ctx);
+			uic_object_destroy(obj);
 		}
 		});