ui/winui/commandbar.cpp

branch
newapi
changeset 207
93b9f502cb88
parent 206
7ebc5a747c6f
child 208
f632bc0589ab
--- a/ui/winui/commandbar.cpp	Wed Oct 11 10:54:24 2023 +0200
+++ b/ui/winui/commandbar.cpp	Wed Oct 11 19:11:38 2023 +0200
@@ -35,6 +35,7 @@
 #include "../common/context.h"
 
 #include "button.h"
+#include "appmenu.h"
 
 using namespace winrt;
 using namespace Microsoft::UI::Xaml;
@@ -43,9 +44,12 @@
 using namespace Microsoft::UI::Xaml::Markup;
 using namespace Windows::UI::Xaml::Interop;
 
-static void create_item(UiObject* obj, CommandBar cb, UiToolbarItemI* i);
-static void create_cmditem(UiObject* obj, CommandBar cb, UiToolbarItem* item);
-static void create_toggleitem(UiObject* obj, CommandBar cb, UiToolbarToggleItem* item);
+static void create_item(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarItemI* i);
+static void create_cmditem(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarItem* item);
+static void create_toggleitem(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarToggleItem* item);
+static void create_menuitem(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarMenuItem* item);
+
+static void create_appmenu_items(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarMenuItem* i);
 
 CommandBar ui_create_toolbar(UiObject *obj) {
 	
@@ -60,12 +64,20 @@
 		if (!item) {
 			exit(-1); // TODO: maybe an error dialog?
 		}
-		create_item(obj, cb, item);
+		create_item(obj, cb.PrimaryCommands(), item);
 	}
+
+	// add appmenu
+	UiToolbarMenuItem* appmenu = uic_get_appmenu();
+	if (appmenu) {
+		create_appmenu_items(obj, cb.SecondaryCommands(), appmenu);
+	}
+	
+
 	return cb;
 }
 
-static void create_item(UiObject* obj, CommandBar cb, UiToolbarItemI* i) {
+static void create_item(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarItemI* i) {
 	switch (i->type) {
 		case UI_TOOLBAR_ITEM: {
 			create_cmditem(obj, cb, (UiToolbarItem*)i);
@@ -75,14 +87,48 @@
 			create_toggleitem(obj, cb, (UiToolbarToggleItem*)i);
 			break;
 		}
+		case UI_TOOLBAR_MENU: {
+			create_menuitem(obj, cb, (UiToolbarMenuItem*)i);
+			break;
+		}
 	}
 }
 
-static void create_cmditem(UiObject* obj, CommandBar cb, UiToolbarItem* item) {
+static void create_appmenu_items(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarMenuItem* i) {
+	for (UiMenuItemI* mi = i->menu.items_begin; mi; mi = mi->next) {
+		// convert UiMenuItemI to UiToolbarItemI
+		switch (mi->type) {
+			case UI_MENU_SUBMENU: {
+				UiMenu* mitem = (UiMenu*)mi;
+				UiToolbarMenuItem tbitem;
+				memset(&tbitem, 0, sizeof(UiToolbarMenuItem));
+				tbitem.item.type = UI_TOOLBAR_MENU;
+				tbitem.args.label = mitem->label;
+				tbitem.menu.items_begin = mitem->items_begin;
+				tbitem.menu.items_end = mitem->items_end;
+				create_menuitem(obj, cb, &tbitem);
+				break;
+			}
+			case UI_MENU_ITEM: {
+				UiMenuItem* mitem = (UiMenuItem*)mi;
+				UiToolbarItem tbitem;
+				memset(&tbitem, 0, sizeof(UiToolbarItem));
+				tbitem.item.type = UI_TOOLBAR_ITEM;
+				tbitem.args.label = mitem->label;
+				tbitem.args.onclick = mitem->callback;
+				tbitem.args.onclickdata = mitem->userdata;
+				create_cmditem(obj, cb, &tbitem);
+				break;
+			}
+		}
+	}
+}
+
+static void create_cmditem(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarItem* item) {
 	AppBarButton button = AppBarButton();
 	if (item->args.label) {
 		wchar_t* wlabel = str2wstr(item->args.label, nullptr);
-		button.Content(box_value(wlabel));
+		button.Label(wlabel);
 		free(wlabel);
 	}
 
@@ -101,16 +147,14 @@
 			});
 	}
 
-	cb.PrimaryCommands().Append(button);
+	cb.Append(button);
 }
 
-
-
-static void create_toggleitem(UiObject *obj, CommandBar cb, UiToolbarToggleItem* item) {
+static void create_toggleitem(UiObject *obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarToggleItem* item) {
 	AppBarToggleButton button = AppBarToggleButton();
 	if (item->args.label) {
 		wchar_t* wlabel = str2wstr(item->args.label, nullptr);
-		button.Content(box_value(wlabel));
+		button.Label(wlabel);
 		free(wlabel);
 	}
 
@@ -138,7 +182,21 @@
 	togglebutton_register_callback(button, obj, args);
 
 
-	cb.PrimaryCommands().Append(button);
+	cb.Append(button);
 }
 
+static void create_menuitem(UiObject* obj, Windows::Foundation::Collections::IObservableVector<ICommandBarElement> cb, UiToolbarMenuItem* item) {
+	AppBarButton button = AppBarButton();
+	if (item->args.label) {
+		wchar_t* wlabel = str2wstr(item->args.label, nullptr);
+		button.Label(wlabel);
+		free(wlabel);
+	}
 
+	MenuFlyoutItem mi = MenuFlyoutItem();
+
+	MenuFlyout flyout = ui_create_menu_flyout(obj, &item->menu);
+	button.Flyout(flyout);
+
+	cb.Append(button);
+}

mercurial