add cleanup code for wrapper objects (WinUI3) newapi

Thu, 12 Oct 2023 14:09:04 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 12 Oct 2023 14:09:04 +0200
branch
newapi
changeset 212
ad5c74af14c4
parent 211
5d71a36b833b
child 213
7e39db525fd9

add cleanup code for wrapper objects (WinUI3)

ui/winui/container.cpp file | annotate | diff | comparison | revisions
ui/winui/toolkit.cpp file | annotate | diff | comparison | revisions
ui/winui/toolkit.h file | annotate | diff | comparison | revisions
ui/winui/window.cpp file | annotate | diff | comparison | revisions
--- a/ui/winui/container.cpp	Thu Oct 12 13:52:18 2023 +0200
+++ b/ui/winui/container.cpp	Thu Oct 12 14:09:04 2023 +0200
@@ -62,9 +62,11 @@
 
 	UIElement elm = grid;
 	UiWidget* widget = new UiWidget(elm);
+	ui_context_add_widget_destructor(current->ctx, widget);
 
 	UiObject* newobj = uic_object_new(obj, widget);
 	newobj->container = new UiBoxContainer(grid, type, args.margin, args.spacing);
+	ui_context_add_container_destructor(current->ctx, newobj->container);
 	uic_obj_add(obj, newobj);
 
 	return widget;
@@ -155,9 +157,11 @@
 
 	UIElement elm = grid;
 	UiWidget* widget = new UiWidget(elm);
+	ui_context_add_widget_destructor(current->ctx, widget);
 
 	UiObject* newobj = uic_object_new(obj, widget);
 	newobj->container = new UiGridContainer(grid, args.margin, args.columnspacing, args.rowspacing);
+	ui_context_add_container_destructor(current->ctx, newobj->container);
 	uic_obj_add(obj, newobj);
 
 	return widget;
@@ -300,6 +304,7 @@
 
 	UIElement elm = frame;
 	UiWidget* widget = new UiWidget(elm);
+	ui_context_add_widget_destructor(current->ctx, widget);
 
 	// sub container
 	UiContainer* ctn = nullptr;
@@ -318,6 +323,7 @@
 			break;
 		}
 	}
+	ui_context_add_container_destructor(current->ctx, ctn);
 
 	UiObject* newobj = uic_object_new(obj, widget);
 	newobj->container = ctn;
@@ -344,6 +350,7 @@
 
 	UIElement elm = expander;
 	UiWidget* widget = new UiWidget(elm);
+	ui_context_add_widget_destructor(current->ctx, widget);
 
 	Grid content = Grid();
 	expander.Content(content);
@@ -364,6 +371,7 @@
 			break;
 		}
 	}
+	ui_context_add_container_destructor(current->ctx, ctn);
 
 	UiObject* newobj = uic_object_new(obj, widget);
 	newobj->container = ctn;
@@ -384,6 +392,7 @@
 
 	UIElement elm = scrollW;
 	UiWidget* widget = new UiWidget(elm);
+	ui_context_add_widget_destructor(current->ctx, widget);
 
 	// create child container
 	Grid content = Grid();
@@ -391,20 +400,21 @@
 
 	UiContainer* ctn = nullptr;
 	switch (args.subcontainer) {
-	default:
-	case UI_CONTAINER_VBOX: {
-		ctn = new UiBoxContainer(content, UI_BOX_CONTAINER_VBOX, args.margin, args.spacing);
-		break;
+		default:
+		case UI_CONTAINER_VBOX: {
+			ctn = new UiBoxContainer(content, UI_BOX_CONTAINER_VBOX, args.margin, args.spacing);
+			break;
+		}
+		case UI_CONTAINER_HBOX: {
+			ctn = new UiBoxContainer(content, UI_BOX_CONTAINER_HBOX, args.margin, args.spacing);
+			break;
+		}
+		case UI_CONTAINER_GRID: {
+			ctn = new UiGridContainer(content, args.margin, args.columnspacing, args.rowspacing);
+			break;
+		}
 	}
-	case UI_CONTAINER_HBOX: {
-		ctn = new UiBoxContainer(content, UI_BOX_CONTAINER_HBOX, args.margin, args.spacing);
-		break;
-	}
-	case UI_CONTAINER_GRID: {
-		ctn = new UiGridContainer(content, args.margin, args.columnspacing, args.rowspacing);
-		break;
-	}
-	}
+	ui_context_add_container_destructor(current->ctx, ctn);
 
 	UiObject* newobj = uic_object_new(obj, widget);
 	newobj->container = ctn;
@@ -426,23 +436,25 @@
 static UiObject* create_subcontainer_obj(UiObject* current, Grid subcontainer, UiSubContainerType type, int margin, int spacing, int columnspacing, int rowspacing) {
 	UiContainer* ctn = nullptr;
 	switch (type) {
-	default:
-	case UI_CONTAINER_VBOX: {
-		ctn = new UiBoxContainer(subcontainer, UI_BOX_CONTAINER_VBOX, margin, spacing);
-		break;
+		default:
+		case UI_CONTAINER_VBOX: {
+			ctn = new UiBoxContainer(subcontainer, UI_BOX_CONTAINER_VBOX, margin, spacing);
+			break;
+		}
+		case UI_CONTAINER_HBOX: {
+			ctn = new UiBoxContainer(subcontainer, UI_BOX_CONTAINER_HBOX, margin, spacing);
+			break;
+		}
+		case UI_CONTAINER_GRID: {
+			ctn = new UiGridContainer(subcontainer, margin, columnspacing, rowspacing);
+			break;
+		}
 	}
-	case UI_CONTAINER_HBOX: {
-		ctn = new UiBoxContainer(subcontainer, UI_BOX_CONTAINER_HBOX, margin, spacing);
-		break;
-	}
-	case UI_CONTAINER_GRID: {
-		ctn = new UiGridContainer(subcontainer, margin, columnspacing, rowspacing);
-		break;
-	}
-	}
+	ui_context_add_container_destructor(current->ctx, ctn);
 
 	UIElement elm = subcontainer;
 	UiWidget* widget = new UiWidget(elm);
+	ui_context_add_widget_destructor(current->ctx, widget);
 	UiObject* newobj = uic_object_new(current, widget);
 	newobj->container = ctn;
 	return newobj;
@@ -627,6 +639,7 @@
 
 	UIElement elm = tabview->GetFrameworkElement();
 	UiWidget* widget = new UiWidget(elm);
+	ui_context_add_widget_destructor(current->ctx, widget);
 	widget->data1 = tabview;
 
 	UiObject* newobj = uic_object_new(obj, widget);
--- a/ui/winui/toolkit.cpp	Thu Oct 12 13:52:18 2023 +0200
+++ b/ui/winui/toolkit.cpp	Thu Oct 12 14:09:04 2023 +0200
@@ -108,12 +108,21 @@
 	delete widget;
 }
 
+extern "C" void destroy_ui_container_wrapper(void* ptr) {
+	UiContainer* ctn = (UiContainer*)ptr;
+	delete ctn;
+}
+
 void ui_context_add_window_destructor(UiContext* ctx, UiWindow* win) {
-	// TODO:
+	cxMempoolRegister(ctx->mp, win, destroy_ui_window_wrapper);
 }
 
 void ui_context_add_widget_destructor(UiContext* ctx, UiWidget* widget) {
-	// TODO:
+	cxMempoolRegister(ctx->mp, widget, destroy_ui_widget_wrapper);
+}
+
+void ui_context_add_container_destructor(UiContext* ctx, UiContainer *container) {
+	cxMempoolRegister(ctx->mp, container, destroy_ui_container_wrapper);
 }
 
 
--- a/ui/winui/toolkit.h	Thu Oct 12 13:52:18 2023 +0200
+++ b/ui/winui/toolkit.h	Thu Oct 12 14:09:04 2023 +0200
@@ -37,7 +37,8 @@
 extern "C" void destroy_ui_window_wrapper(void* ptr);
 extern "C" void destroy_ui_widget_wrapper(void* ptr);
 
-void ui_context_add_window_destructor(UiContext* ctx, UiWindow *win);
+void ui_context_add_window_destructor(UiContext* ctx, UiWindow* win);
 void ui_context_add_widget_destructor(UiContext* ctx, UiWidget* widget);
+void ui_context_add_container_destructor(UiContext* ctx, UiContainer *container);
 
 UiEvent ui_create_int_event(UiObject* obj, int64_t i);
\ No newline at end of file
--- a/ui/winui/window.cpp	Thu Oct 12 13:52:18 2023 +0200
+++ b/ui/winui/window.cpp	Thu Oct 12 14:09:04 2023 +0200
@@ -63,15 +63,6 @@
 	//Window window = Window();
 	Window window = make<winui::implementation::MainWindow>();
 	window.ExtendsContentIntoTitleBar(true);
-	if (title) {
-		wchar_t *wtitle = str2wstr(title, nullptr);
-		window.Title(wtitle);
-		free(wtitle);
-	}
-
-	//auto backdrop = winrt::Microsoft::UI::Xaml::Media::MicaBackdrop();
-	//backdrop.Kind(winrt::Microsoft::UI::Composition::SystemBackdrops::MicaKind::Base); // alternative: BaseAlt
-	// TODO: https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/system-backdrop-controller#example-use-mica-in-a-windows-appsdkwinui-3-app
 
 	Grid grid = Grid();
 	window.Content(grid);
@@ -81,17 +72,22 @@
 	titleBar.Padding(titleBarPadding);
 	titleBar.Orientation(Orientation::Horizontal);
 	TextBlock titleLabel = TextBlock();
-	titleLabel.Text(hstring(L"Window Title"));
 	titleBar.Children().Append(titleLabel);
 
+	if (title) {
+		wchar_t* wtitle = str2wstr(title, nullptr);
+		window.Title(wtitle);
+		titleLabel.Text(hstring(wtitle));
+		free(wtitle);
+	}
+
 	window.SetTitleBar(titleBar);
 
 	obj->wobj = new UiWindow(window);
-	
+	ui_context_add_window_destructor(obj->ctx, obj->wobj);
 
 	window.Closed([obj](IInspectable const& sender, WindowEventArgs) {
-		// TODO: destroy UiObject*, context, ...
-		delete obj->wobj;
+		cxMempoolDestroy(obj->ctx->mp);
 	});
 
 	obj->container = new UiBoxContainer(grid, UI_BOX_CONTAINER_VBOX, 0, 0);

mercurial