From 1830c64c79fb8a72d98fb108c7a94f6fbecac012 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sat, 30 Apr 2022 21:39:19 +1000 Subject: [PATCH] Better Checked implementation --- v2/internal/frontend/desktop/darwin/Application.h | 1 + v2/internal/frontend/desktop/darwin/Application.m | 4 ++++ v2/internal/frontend/desktop/darwin/menu.go | 5 +++++ v2/pkg/menu/menuitem.go | 15 +++++++++++++++ 4 files changed, 25 insertions(+) diff --git a/v2/internal/frontend/desktop/darwin/Application.h b/v2/internal/frontend/desktop/darwin/Application.h index 91d04ba90..9c61eaae8 100644 --- a/v2/internal/frontend/desktop/darwin/Application.h +++ b/v2/internal/frontend/desktop/darwin/Application.h @@ -56,6 +56,7 @@ void AppendSubmenu(void* parent, void* child); void AppendRole(void *inctx, void *inMenu, int role); void SetAsApplicationMenu(void *inctx, void *inMenu); void UpdateApplicationMenu(void *inctx); +void SetMenuItemChecked(void* nsMenuItem, int checked); /* Tray Menu */ void* NewNSStatusItem(const char* label); diff --git a/v2/internal/frontend/desktop/darwin/Application.m b/v2/internal/frontend/desktop/darwin/Application.m index 131ef2353..9106b2983 100644 --- a/v2/internal/frontend/desktop/darwin/Application.m +++ b/v2/internal/frontend/desktop/darwin/Application.m @@ -165,6 +165,10 @@ void ToggleMaximise(void* inctx) { ); } +void SetMenuItemChecked(void* nsMenuItem, int checked) { + [(NSMenuItem*)nsMenuItem setState:(checked == 0 ? NSOffState : NSOnState)]; +} + const char* GetSize(void *inctx) { WailsContext *ctx = (__bridge WailsContext*) inctx; NSRect frame = [ctx.mainWindow frame]; diff --git a/v2/internal/frontend/desktop/darwin/menu.go b/v2/internal/frontend/desktop/darwin/menu.go index 65449f7f2..fc60f0c3b 100644 --- a/v2/internal/frontend/desktop/darwin/menu.go +++ b/v2/internal/frontend/desktop/darwin/menu.go @@ -67,6 +67,10 @@ type MenuItem struct { radioGroupMembers []*MenuItem } +func (m *MenuItem) SetChecked(value bool) { + C.SetMenuItemChecked(m.nsmenuitem, bool2Cint(value)) +} + func (m *NSMenu) AddMenuItem(menuItem *menu.MenuItem) *MenuItem { c := NewCalloc() defer c.Free() @@ -83,6 +87,7 @@ func (m *NSMenu) AddMenuItem(menuItem *menu.MenuItem) *MenuItem { result.id = createMenuItemID(result) result.nsmenuitem = C.AppendMenuItem(m.context, m.nsmenu, c.String(menuItem.Label), key, modifier, bool2Cint(menuItem.Disabled), bool2Cint(menuItem.Checked), C.int(result.id)) + menuItem.Impl = result return result } diff --git a/v2/pkg/menu/menuitem.go b/v2/pkg/menu/menuitem.go index ba9574eb3..165a4ddeb 100644 --- a/v2/pkg/menu/menuitem.go +++ b/v2/pkg/menu/menuitem.go @@ -6,6 +6,10 @@ import ( "github.com/wailsapp/wails/v2/pkg/menu/keys" ) +type MenuItemImpl interface { + SetChecked(bool) +} + // MenuItem represents a menuitem contained in a menu type MenuItem struct { // Label is what appears as the menu text @@ -53,6 +57,9 @@ type MenuItem struct { // Used for locking when removing elements removeLock sync.Mutex + + // Implementation of the runtime methods + Impl MenuItemImpl } // Parent returns the parent of the menu item. @@ -216,6 +223,14 @@ func (m *MenuItem) insertItemAtIndex(index int, target *MenuItem) bool { return true } +func (m *MenuItem) SetChecked(b bool) { + if m.Checked != b { + println("here!!!!!") + m.Checked = b + m.Impl.SetChecked(b) + } +} + // Text is a helper to create basic Text menu items func Text(label string, accelerator *keys.Accelerator, click Callback) *MenuItem { return &MenuItem{