split toolbar in left,center,right segments (WinUI3) newapi

Tue, 16 Jan 2024 12:49:46 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 16 Jan 2024 12:49:46 +0100
branch
newapi
changeset 226
4eef1d49f794
parent 225
097f45f9c1fa
child 227
04b317bc6f13

split toolbar in left,center,right segments (WinUI3)

make/vs/testapp/main.c file | annotate | diff | comparison | revisions
ui/common/toolbar.c file | annotate | diff | comparison | revisions
ui/common/toolbar.h file | annotate | diff | comparison | revisions
ui/ui/toolbar.h file | annotate | diff | comparison | revisions
ui/winui/commandbar.cpp file | annotate | diff | comparison | revisions
ui/winui/commandbar.h file | annotate | diff | comparison | revisions
ui/winui/list.h file | annotate | diff | comparison | revisions
ui/winui/window.cpp file | annotate | diff | comparison | revisions
--- a/make/vs/testapp/main.c	Sun Nov 26 15:44:28 2023 +0100
+++ b/make/vs/testapp/main.c	Tue Jan 16 12:49:46 2024 +0100
@@ -366,11 +366,11 @@
         }
     }
 
-    ui_toolbar_add_default("Test");
-    ui_toolbar_add_default("Toggle");
-    ui_toolbar_add_default("Toggle2");
-    ui_toolbar_add_default("Toggle3");
-    ui_toolbar_add_default("Menu");
+    ui_toolbar_add_default("Test", UI_TOOLBAR_LEFT);
+    ui_toolbar_add_default("Toggle", UI_TOOLBAR_LEFT);
+    ui_toolbar_add_default("Toggle2", UI_TOOLBAR_CENTER);
+    ui_toolbar_add_default("Toggle3", UI_TOOLBAR_CENTER);
+    ui_toolbar_add_default("Menu", UI_TOOLBAR_RIGHT);
 
     ui_main();
 
--- a/ui/common/toolbar.c	Sun Nov 26 15:44:28 2023 +0100
+++ b/ui/common/toolbar.c	Tue Jan 16 12:49:46 2024 +0100
@@ -32,14 +32,15 @@
 
 static CxMap* toolbar_items;
 
-static CxList* toolbar_defaults;
+static CxList* toolbar_defaults[3]; // 0: left 1: center 2: right
 
 static UiToolbarMenuItem* ui_appmenu;
 
-
 void uic_toolbar_init(void) {
 	toolbar_items = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
-	toolbar_defaults = cxLinkedListCreateSimple(CX_STORE_POINTERS);
+	toolbar_defaults[0] = cxLinkedListCreateSimple(CX_STORE_POINTERS);
+	toolbar_defaults[1] = cxLinkedListCreateSimple(CX_STORE_POINTERS);
+	toolbar_defaults[2] = cxLinkedListCreateSimple(CX_STORE_POINTERS);
 }
 
 static char* nl_strdup(char* str) {
@@ -114,17 +115,24 @@
 	return toolbar_items;
 }
 
-CxList* uic_get_toolbar_defaults(void) {
-	return toolbar_defaults;
+CxList* uic_get_toolbar_defaults(enum UiToolbarPos pos) {
+	if (pos >= 0 && pos < 3) {
+		return toolbar_defaults[pos];
+	}
 }
 
-void ui_toolbar_add_default(const char* name) {
+void ui_toolbar_add_default(const char* name, enum UiToolbarPos pos) {
 	char* cp = strdup(name);
-	cxListAdd(toolbar_defaults, cp);
+	if (pos >= 0 && pos < 3) {
+		cxListAdd(toolbar_defaults[pos], cp);
+	}
+	else {
+		// TODO: error
+	}
 }
 
 UiBool uic_toolbar_isenabled(void) {
-	return toolbar_defaults->size > 0;
+	return toolbar_defaults[0]->size + toolbar_defaults[1]->size + toolbar_defaults[2]->size > 0;
 }
 
 UiToolbarItemI* uic_toolbar_get_item(const char* name) {
--- a/ui/common/toolbar.h	Sun Nov 26 15:44:28 2023 +0100
+++ b/ui/common/toolbar.h	Tue Jan 16 12:49:46 2024 +0100
@@ -80,7 +80,7 @@
 void uic_toolbar_init(void);
 
 CxMap* uic_get_toolbar_items(void);
-CxList* uic_get_toolbar_defaults(void);
+CxList* uic_get_toolbar_defaults(enum UiToolbarPos pos);
 
 UiBool uic_toolbar_isenabled(void);
 
--- a/ui/ui/toolbar.h	Sun Nov 26 15:44:28 2023 +0100
+++ b/ui/ui/toolbar.h	Tue Jan 16 12:49:46 2024 +0100
@@ -61,6 +61,11 @@
 	const char* icon;
 } UiToolbarMenuArgs;
 
+enum UiToolbarPos {
+	UI_TOOLBAR_LEFT = 0,
+	UI_TOOLBAR_CENTER,
+	UI_TOOLBAR_RIGHT
+};
 
 #define ui_toolbar_item(name, ...) ui_toolbar_item_create(name, (UiToolbarItemArgs){ __VA_ARGS__ } )
 #define ui_toolbar_toggleitem(name, ...) ui_toolbar_toggleitem_create(name, (UiToolbarToggleItemArgs){ __VA_ARGS__ } )
@@ -89,7 +94,7 @@
 void ui_toolbar_combobox_str_deprecated(char *name, UiList *list, ui_callback f, void *udata);
 void ui_toolbar_combobox_nv_deprecated(char *name, char *listname, ui_getvaluefunc getvalue, ui_callback f, void *udata);
 
-UIEXPORT void ui_toolbar_add_default(const char *name);
+UIEXPORT void ui_toolbar_add_default(const char *name, enum UiToolbarPos pos);
 
 
 
--- a/ui/winui/commandbar.cpp	Sun Nov 26 15:44:28 2023 +0100
+++ b/ui/winui/commandbar.cpp	Tue Jan 16 12:49:46 2024 +0100
@@ -52,13 +52,12 @@
 
 static void create_appmenu_items(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarMenuItem* i);
 
-CommandBar ui_create_toolbar(UiObject *obj) {
+CommandBar ui_create_toolbar(UiObject *obj, CxList* defaults, bool addappmenu) {
 	
 	CommandBar cb = CommandBar();
 	cb.DefaultLabelPosition(CommandBarDefaultLabelPosition::Right);
 
 	// add pre-configured items
-	CxList* defaults = uic_get_toolbar_defaults();
 	CxIterator i = cxListIterator(defaults);
 	cx_foreach(char*, def, i) {
 		UiToolbarItemI* item = uic_toolbar_get_item(def);
@@ -69,11 +68,12 @@
 	}
 
 	// add appmenu
-	UiToolbarMenuItem* appmenu = uic_get_appmenu();
-	if (appmenu) {
-		create_appmenu_items(obj, cb.SecondaryCommands(), appmenu);
+	if (addappmenu) {
+		UiToolbarMenuItem* appmenu = uic_get_appmenu();
+		if (appmenu) {
+			create_appmenu_items(obj, cb.SecondaryCommands(), appmenu);
+		}
 	}
-	
 
 	return cb;
 }
--- a/ui/winui/commandbar.h	Sun Nov 26 15:44:28 2023 +0100
+++ b/ui/winui/commandbar.h	Tue Jan 16 12:49:46 2024 +0100
@@ -41,7 +41,7 @@
 #include <winrt/Microsoft.UI.Xaml.XamlTypeInfo.h>
 #include <winrt/Microsoft.UI.Xaml.Markup.h>
 
-winrt::Microsoft::UI::Xaml::Controls::CommandBar ui_create_toolbar(UiObject *obj);
+winrt::Microsoft::UI::Xaml::Controls::CommandBar ui_create_toolbar(UiObject *obj, CxList* defaults, bool addappmenu);
 
 extern "C" int64_t ui_appbar_togglebutton_get(UiInteger * integer);
 extern "C" void ui_appbar_togglebutton_set(UiInteger * integer, int64_t value);
\ No newline at end of file
--- a/ui/winui/list.h	Sun Nov 26 15:44:28 2023 +0100
+++ b/ui/winui/list.h	Tue Jan 16 12:49:46 2024 +0100
@@ -38,7 +38,5 @@
 
 extern "C" void ui_breadcrumbbar_update(UiList * list, int i);
 
-extern "C" void ui_pathbar_update(UiList * list, int i);
-
 std::vector<int> ui_create_listview_selection(winrt::Microsoft::UI::Xaml::Controls::ListView listview);
 
--- a/ui/winui/window.cpp	Sun Nov 26 15:44:28 2023 +0100
+++ b/ui/winui/window.cpp	Tue Jan 16 12:49:46 2024 +0100
@@ -109,16 +109,66 @@
 	}
 
 	if (uic_toolbar_isenabled()) {
+		// create a grid for the toolbar: ColumnDefinitions="Auto, *, Auto"
+		Grid toolbar_grid = Grid();
+		GridLength gl;
+		gl.Value = 0;
+		gl.GridUnitType = GridUnitType::Auto;
+
+		ColumnDefinition coldef0 = ColumnDefinition();
+		coldef0.Width(gl);
+		toolbar_grid.ColumnDefinitions().Append(coldef0);
+
+		gl.Value = 1;
+		gl.GridUnitType = GridUnitType::Star;
+		ColumnDefinition coldef1 = ColumnDefinition();
+		coldef1.Width(gl);
+		toolbar_grid.ColumnDefinitions().Append(coldef1);
+
+		gl.Value = 0;
+		gl.GridUnitType = GridUnitType::Auto;
+		ColumnDefinition coldef2 = ColumnDefinition();
+		coldef2.Width(gl);
+		toolbar_grid.ColumnDefinitions().Append(coldef2);
+
+		// rowdef
+		gl.Value = 0;
+		gl.GridUnitType = GridUnitType::Auto;
+		RowDefinition rowdef = RowDefinition();
+		rowdef.Height(gl);
+		toolbar_grid.RowDefinitions().Append(rowdef);
+
+
 		// create commandbar
-		CommandBar toolbar = ui_create_toolbar(obj);
-		// wrap the commandbar in a stackpanel, because we
-		// don't want to fill the horizontal space
-		StackPanel toolbar_panel = StackPanel();
-		toolbar_panel.Orientation(Orientation::Horizontal);
-		toolbar_panel.Children().Append(toolbar);
+		CxList* def_l = uic_get_toolbar_defaults(UI_TOOLBAR_LEFT);
+		CxList* def_c = uic_get_toolbar_defaults(UI_TOOLBAR_CENTER);
+		CxList* def_r = uic_get_toolbar_defaults(UI_TOOLBAR_RIGHT);
 
-		toolbar_panel.VerticalAlignment(VerticalAlignment::Top);
-		obj->container->Add(toolbar_panel, false);
+		bool addappmenu = true;
+		if (def_r->size > 0) {
+			CommandBar toolbar_r = ui_create_toolbar(obj, def_r, addappmenu);
+			toolbar_grid.SetColumn(toolbar_r, 2);
+			toolbar_grid.SetRow(toolbar_r, 0);
+			toolbar_grid.Children().Append(toolbar_r);
+			addappmenu = false;
+		}
+		if (def_c->size > 0) {
+			CommandBar toolbar_c = ui_create_toolbar(obj, def_c, addappmenu);
+			toolbar_c.HorizontalAlignment(HorizontalAlignment::Center);
+			toolbar_grid.SetColumn(toolbar_c, 1);
+			toolbar_grid.SetRow(toolbar_c, 0);
+			toolbar_grid.Children().Append(toolbar_c);
+			addappmenu = false;
+		}
+		if (def_l->size > 0) {
+			CommandBar toolbar_l = ui_create_toolbar(obj, def_l, addappmenu);
+			toolbar_grid.SetColumn(toolbar_l, 0);
+			toolbar_grid.SetRow(toolbar_l, 0);
+			toolbar_grid.Children().Append(toolbar_l);
+		}
+
+		toolbar_grid.VerticalAlignment(VerticalAlignment::Top);
+		obj->container->Add(toolbar_grid, false);
 	}
 
 	obj->window = window_data;

mercurial