From e7cb40d5ee04fee7796c4518074582503a59512f Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Sun, 11 Jul 2021 20:21:23 +1000 Subject: [PATCH] [windows] use idgen to track menu IDs. Fix chechbox typo --- v2/go.mod | 1 + v2/go.sum | 2 ++ v2/internal/ffenestri/windows_checkboxes.go | 2 +- v2/internal/ffenestri/windows_menu.go | 5 ++++- v2/internal/ffenestri/windows_menu_cache.go | 21 +++++++++++++++++---- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/v2/go.mod b/v2/go.mod index 86fc85da..094362c4 100644 --- a/v2/go.mod +++ b/v2/go.mod @@ -19,6 +19,7 @@ require ( github.com/leaanthony/go-ansi-parser v1.0.1 github.com/leaanthony/go-common-file-dialog v1.0.3 github.com/leaanthony/gosod v1.0.1 + github.com/leaanthony/idgen v1.0.0 github.com/leaanthony/slicer v1.5.0 github.com/leaanthony/webview2runtime v1.1.0 github.com/leaanthony/winicon v0.0.0-20200606125418-4419cea822a0 diff --git a/v2/go.sum b/v2/go.sum index 0ac137ca..6ab26f0d 100644 --- a/v2/go.sum +++ b/v2/go.sum @@ -99,6 +99,8 @@ github.com/leaanthony/go-common-file-dialog v1.0.3 h1:O0uGjKnWtdEADGrkg+TyAAbZyl github.com/leaanthony/go-common-file-dialog v1.0.3/go.mod h1:TGhEc9eSJgRsupZ+iH1ZgAOnEo9zp05cRH2j08RPrF0= github.com/leaanthony/gosod v1.0.1 h1:F+4c3DmEBfigi7oAswCV2RpQ+k4DcNbhuCZUGdBHacQ= github.com/leaanthony/gosod v1.0.1/go.mod h1:W8RyeSFBXu7RpIxPGEJfW4moSyGGEjlJMLV25wEbAdU= +github.com/leaanthony/idgen v1.0.0 h1:IZreR+JGEzFV4yeVuBZA25gM0keUoFy+RDUldncQ+Jw= +github.com/leaanthony/idgen v1.0.0/go.mod h1:4nBZnt8ml/f/ic/EVQuLxuj817RccT2fyrUaZFxrcVA= github.com/leaanthony/slicer v1.5.0 h1:aHYTN8xbCCLxJmkNKiLB6tgcMARl4eWmH9/F+S/0HtY= github.com/leaanthony/slicer v1.5.0/go.mod h1:FwrApmf8gOrpzEWM2J/9Lh79tyq8KTX5AzRtwV7m4AY= github.com/leaanthony/webview2runtime v1.1.0 h1:N0pv55ift8XtqozIp4PNOtRCJ/Qdd/qzx80lUpalS4c= diff --git a/v2/internal/ffenestri/windows_checkboxes.go b/v2/internal/ffenestri/windows_checkboxes.go index 65e29566..e76dfa8c 100644 --- a/v2/internal/ffenestri/windows_checkboxes.go +++ b/v2/internal/ffenestri/windows_checkboxes.go @@ -71,7 +71,7 @@ func (c *CheckboxCache) addToCheckboxCache(menu *menumanager.ProcessedMenu, item } -func (c *CheckboxCache) removeMenuFromChechboxCache(menu *menumanager.ProcessedMenu) { +func (c *CheckboxCache) removeMenuFromCheckboxCache(menu *menumanager.ProcessedMenu) { c.mutex.Lock() delete(c.cache, menu) c.mutex.Unlock() diff --git a/v2/internal/ffenestri/windows_menu.go b/v2/internal/ffenestri/windows_menu.go index 5cf81c3c..418e7929 100644 --- a/v2/internal/ffenestri/windows_menu.go +++ b/v2/internal/ffenestri/windows_menu.go @@ -153,8 +153,11 @@ func (m *Menu) processRadioGroups() error { func (m *Menu) Destroy() error { + // Release the MenuIDs + releaseMenuIDsForProcessedMenu(m.wailsMenu.Menu) + // Unload this menu's checkboxes from the cache - globalCheckboxCache.removeMenuFromChechboxCache(m.wailsMenu.Menu) + globalCheckboxCache.removeMenuFromCheckboxCache(m.wailsMenu.Menu) // Unload this menu's radio groups from the cache globalRadioGroupCache.removeMenuFromRadioBoxCache(m.wailsMenu.Menu) diff --git a/v2/internal/ffenestri/windows_menu_cache.go b/v2/internal/ffenestri/windows_menu_cache.go index e139c023..5b6762d9 100644 --- a/v2/internal/ffenestri/windows_menu_cache.go +++ b/v2/internal/ffenestri/windows_menu_cache.go @@ -3,6 +3,7 @@ package ffenestri import ( + "github.com/leaanthony/idgen" "github.com/wailsapp/wails/v2/internal/menumanager" "sync" ) @@ -30,18 +31,28 @@ type menuCacheEntry struct { processedMenu *menumanager.ProcessedMenu } -// windowsMenuIDCounter keeps track of the unique windows menu IDs -var windowsMenuIDCounter uint32 +var idGenerator = idgen.New() var menuCache = map[win32MenuItemID]*menuCacheEntry{} var menuCacheLock sync.RWMutex var wailsMenuIDtoWin32IDMap = map[wailsMenuItemID]win32MenuItemID{} +// This releases the menuIDs back to the id generator +var winIDsOwnedByProcessedMenu = map[*menumanager.ProcessedMenu][]win32MenuItemID{} + +func releaseMenuIDsForProcessedMenu(processedMenu *menumanager.ProcessedMenu) { + for _, menuID := range winIDsOwnedByProcessedMenu[processedMenu] { + idGenerator.ReleaseID(uint(menuID)) + } + delete(winIDsOwnedByProcessedMenu, processedMenu) +} + func addMenuCacheEntry(parent win32Menu, typ menuType, wailsMenuItem *menumanager.ProcessedMenuItem, processedMenu *menumanager.ProcessedMenu) win32MenuItemID { menuCacheLock.Lock() defer menuCacheLock.Unlock() - menuID := win32MenuItemID(windowsMenuIDCounter) - windowsMenuIDCounter++ + id, err := idGenerator.NewID() + checkFatal(err) + menuID := win32MenuItemID(id) menuCache[menuID] = &menuCacheEntry{ parent: parent, menuType: typ, @@ -50,6 +61,8 @@ func addMenuCacheEntry(parent win32Menu, typ menuType, wailsMenuItem *menumanage } // save the mapping wailsMenuIDtoWin32IDMap[wailsMenuItemID(wailsMenuItem.ID)] = menuID + // keep track of menuids owned by this menu (so we can release the ids) + winIDsOwnedByProcessedMenu[processedMenu] = append(winIDsOwnedByProcessedMenu[processedMenu], menuID) return menuID }