Skip to content

Commit 6be5913

Browse files
committed
Update Update()
1 parent 2602c28 commit 6be5913

2 files changed

Lines changed: 46 additions & 54 deletions

File tree

v3/examples/menu/main.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func main() {
5050
hiddenFirstItem := indexMenu.Add("Hidden First Item").SetHidden(true)
5151
indexMenu.Add("Toggle Hidden First Item").OnClick(func(*application.Context) {
5252
hiddenFirstItem.SetHidden(!hiddenFirstItem.Hidden())
53-
menu.Update(true)
53+
menu.Update()
5454
println("Toggled visibility of first item - Now hidden:", hiddenFirstItem.Hidden())
5555
})
5656
indexMenu.AddSeparator()
@@ -67,7 +67,7 @@ func main() {
6767
item.OnClick(func(*application.Context) {
6868
println("Clicked on inserted item!")
6969
})
70-
menu.Update(true)
70+
menu.Update()
7171
println("Inserted item at index 1")
7272
})
7373

@@ -77,7 +77,7 @@ func main() {
7777
checkbox.OnClick(func(ctx *application.Context) {
7878
println("Checkbox clicked, checked:", ctx.ClickedMenuItem().Checked())
7979
})
80-
menu.Update(true)
80+
menu.Update()
8181
println("Inserted checkbox at index 2")
8282
})
8383

@@ -111,7 +111,7 @@ func main() {
111111
newCheckbox.OnClick(func(ctx *application.Context) {
112112
println("Checkbox B clicked, checked:", ctx.ClickedMenuItem().Checked())
113113
})
114-
menu.Update(true)
114+
menu.Update()
115115
println("Inserted Checkbox B after Checkbox A")
116116
})
117117

@@ -148,7 +148,7 @@ func main() {
148148
newRadio.OnClick(func(ctx *application.Context) {
149149
println("Radio C clicked, checked:", ctx.ClickedMenuItem().Checked())
150150
})
151-
menu.Update(true)
151+
menu.Update()
152152
println("Inserted Radio C after Radio B")
153153
})
154154

@@ -157,7 +157,7 @@ func main() {
157157
// Demonstrate InsertSeparatorAt
158158
indexMenu.Add("Insert separator at index 3").OnClick(func(*application.Context) {
159159
indexMenu.InsertSeparatorAt(3)
160-
menu.Update(true)
160+
menu.Update()
161161
println("Inserted separator at index 3")
162162
})
163163

@@ -176,7 +176,7 @@ func main() {
176176
submenu := indexMenu.InsertSubmenuAt(count, "Inserted Submenu")
177177
submenu.Add("Submenu Item 1")
178178
submenu.Add("Submenu Item 2")
179-
menu.Update(true)
179+
menu.Update()
180180
println("Inserted submenu at end")
181181
})
182182

@@ -210,7 +210,7 @@ func main() {
210210
item := indexMenu.ItemAt(targetIndex)
211211
if item != nil {
212212
item.SetEnabled(!item.Enabled())
213-
menu.Update(true)
213+
menu.Update()
214214
println("Toggled enabled state for item at index", targetIndex, "- Now enabled:", item.Enabled())
215215
}
216216
})
@@ -245,7 +245,7 @@ func main() {
245245
item := indexMenu.ItemAt(hidableIndex)
246246
if item != nil {
247247
item.SetHidden(!item.Hidden())
248-
menu.Update(true)
248+
menu.Update()
249249
println("Toggled hidden state for item at index", hidableIndex, "- Now hidden:", item.Hidden())
250250
}
251251
})
@@ -329,7 +329,7 @@ func main() {
329329
println("Submenu is now hidden")
330330
}
331331
// Update the menu to reflect the changes
332-
menu.Update(true)
332+
menu.Update()
333333
})
334334

335335
myMenu.AddSeparator()
@@ -376,7 +376,7 @@ func main() {
376376
myMenu.Add("Append a new item").OnClick(func(*application.Context) {
377377
myMenu.AppendItem(newItem)
378378
// Use Update(true) to update everything in one call
379-
menu.Update(true)
379+
menu.Update()
380380
println("Appended a new item to the menu")
381381
})
382382

@@ -393,7 +393,7 @@ func main() {
393393
if lastIndex >= 0 {
394394
myMenu.Remove(lastIndex)
395395
// Use Update(true) to update everything in one call
396-
menu.Update(true)
396+
menu.Update()
397397
println("Removed the last item from the menu")
398398
} else {
399399
println("No items to remove")
@@ -412,7 +412,7 @@ func main() {
412412
// Add a menu item to toggle the enabled state of the disabled item
413413
disabledMenu.Add("Toggle Enabled State").OnClick(func(*application.Context) {
414414
disabledItem.SetEnabled(!disabledItem.Enabled())
415-
menu.Update(true)
415+
menu.Update()
416416
println("Toggled enabled state - Now enabled:", disabledItem.Enabled())
417417
})
418418

@@ -428,7 +428,7 @@ func main() {
428428
menuUpdateDemo.Add("Reset menu").OnClick(func(ctx *application.Context) {
429429
menuUpdateDemo.Clear()
430430
menuUpdateDemo.Add("New option")
431-
menu.Update(true)
431+
menu.Update()
432432
// Explicitly set the menu on the application and window to ensure it's updated
433433
app.SetMenu(menu)
434434
if app.CurrentWindow() != nil {
@@ -451,7 +451,7 @@ func main() {
451451
// Change the label
452452
submenuItem.SetLabel("Parent Updated Label")
453453
// Update the menu to reflect the changes
454-
menu.Update(true)
454+
menu.Update()
455455
println("Submenu label changed from parent to: Parent Updated Label")
456456

457457
// Verify the change
@@ -474,7 +474,7 @@ func main() {
474474
// Change the label
475475
submenuItem.SetLabel("Updated Submenu Label")
476476
// Update the menu to reflect the changes
477-
menu.Update(true)
477+
menu.Update()
478478
println("Submenu label changed to: Updated Submenu Label")
479479

480480
// Verify the change
@@ -497,7 +497,7 @@ func main() {
497497
// Change the label
498498
submenuItem.SetLabel("Updated Operations")
499499
// Update the menu to reflect the changes
500-
menu.Update(true)
500+
menu.Update()
501501
println("Submenu label changed to: Updated Operations")
502502

503503
// Verify the change
@@ -525,7 +525,7 @@ func main() {
525525
// Change the label
526526
submenuItem.SetLabel("Self-Updated Submenu")
527527
// Update the menu to reflect the changes
528-
menu.Update(true)
528+
menu.Update()
529529
println("Nested submenu changed its own label to: Self-Updated Submenu")
530530

531531
// Verify the change
@@ -548,7 +548,7 @@ func main() {
548548
// Change the label
549549
submenuItem.SetLabel("Updated Nested Submenu")
550550
// Update the menu to reflect the changes
551-
menu.Update(true)
551+
menu.Update()
552552
println("Nested submenu label changed to: Updated Nested Submenu")
553553

554554
// Verify the change

v3/pkg/application/menu.go

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -69,53 +69,45 @@ func (m *Menu) AddRadio(label string, enabled bool) *MenuItem {
6969
return result
7070
}
7171

72-
// Update updates the menu and optionally propagates the changes to the application and all windows
72+
// Update updates the menu and propagates the changes to the application and all windows
7373
// that have the menu attached.
7474
//
75-
// If propagate is true, the changes will be propagated to the application and all windows
76-
// that have the menu attached. This is equivalent to calling UpdateAll().
77-
func (m *Menu) Update(propagate ...bool) *Menu {
75+
// The propagate parameter is kept for backward compatibility but is ignored.
76+
// Changes are always propagated to the application and all windows that have the menu attached.
77+
func (m *Menu) Update() *Menu {
7878
// Process radio groups and update the menu
7979
m.processRadioGroups()
8080
if m.impl == nil {
8181
m.impl = newMenuImpl(m)
8282
}
8383
m.impl.update()
8484

85-
// Check if we should propagate the changes
86-
shouldPropagate := false
87-
if len(propagate) > 0 {
88-
shouldPropagate = propagate[0]
85+
// If globalApplication is not initialized yet, just return
86+
if globalApplication == nil {
87+
return m
8988
}
9089

91-
if shouldPropagate {
92-
// If globalApplication is not initialized yet, just return
93-
if globalApplication == nil {
94-
return m
95-
}
96-
97-
// Get a copy of the attached windows
98-
m.attachedWindowsLock.RLock()
99-
attachedWindowsCopy := make(map[uint]bool)
100-
for id, attached := range m.attachedWindows {
101-
attachedWindowsCopy[id] = attached
102-
}
103-
m.attachedWindowsLock.RUnlock()
90+
// Get a copy of the attached windows
91+
m.attachedWindowsLock.RLock()
92+
attachedWindowsCopy := make(map[uint]bool)
93+
for id, attached := range m.attachedWindows {
94+
attachedWindowsCopy[id] = attached
95+
}
96+
m.attachedWindowsLock.RUnlock()
10497

105-
// Check if this menu is the application menu (ID 0)
106-
if attachedWindowsCopy[0] {
107-
// Update the application menu
108-
globalApplication.SetMenu(m)
109-
}
98+
// Check if this menu is the application menu (ID 0)
99+
if attachedWindowsCopy[0] {
100+
// Update the application menu
101+
globalApplication.SetMenu(m)
102+
}
110103

111-
// Update all windows that use this menu
112-
globalApplication.windowsLock.RLock()
113-
defer globalApplication.windowsLock.RUnlock()
104+
// Update all windows that use this menu
105+
globalApplication.windowsLock.RLock()
106+
defer globalApplication.windowsLock.RUnlock()
114107

115-
for id, window := range globalApplication.windows {
116-
if attachedWindowsCopy[id] {
117-
window.SetMenu(m)
118-
}
108+
for id, window := range globalApplication.windows {
109+
if attachedWindowsCopy[id] {
110+
window.SetMenu(m)
119111
}
120112
}
121113

@@ -373,9 +365,9 @@ func (m *Menu) unregisterWindow(windowID uint) {
373365
// UpdateAll updates the menu and propagates the changes to the application and all windows
374366
// This is a convenience method that handles all the necessary update steps after modifying a menu
375367
//
376-
// Deprecated: Use Update(true) instead.
368+
// Deprecated: Use Update() instead as it now always propagates changes.
377369
func (m *Menu) UpdateAll() *Menu {
378-
return m.Update(true)
370+
return m.Update()
379371
}
380372

381373
// Prepend menu before an existing menu

0 commit comments

Comments
 (0)