[windows] use idgen to track menu IDs. Fix chechbox typo

This commit is contained in:
Lea Anthony
2021-07-11 20:21:23 +10:00
parent f409dbdab1
commit e7cb40d5ee
5 changed files with 25 additions and 6 deletions

View File

@@ -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

View File

@@ -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=

View File

@@ -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()

View File

@@ -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)

View File

@@ -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
}