@@ -2,6 +2,7 @@ package main
2
2
3
3
import (
4
4
_ "embed"
5
+ "fmt"
5
6
"log"
6
7
"runtime"
7
8
@@ -468,24 +469,30 @@ func main() {
468
469
469
470
// Add a button to update the submenu label in the Demo menu
470
471
labelOperations .Add ("Update Demo Submenu Label" ).OnClick (func (* application.Context ) {
471
- // Find the submenu item in the Demo menu
472
- submenuItem := myMenu .FindByLabel ("Submenu" )
473
- if submenuItem != nil {
474
- // Change the label
475
- submenuItem .SetLabel ("Updated Submenu Label" )
476
- // Update the menu to reflect the changes
477
- menu .Update ()
478
- println ("Submenu label changed to: Updated Submenu Label" )
472
+ // Use the direct reference 'myDemoMenuItemForSubmenu' captured when the menu was created.
473
+ if submenu != nil {
474
+ oldLabel := submenu .Label ()
475
+ targetNewLabel := "Updated Submenu Label"
476
+
477
+ if oldLabel == targetNewLabel {
478
+ fmt .Printf ("Demo submenu ('%s') label in myMenu is already: %s\n " , oldLabel , targetNewLabel )
479
+ return
480
+ }
479
481
480
- // Verify the change
481
- updatedItem := myMenu .FindByLabel ("Updated Submenu Label" )
482
- if updatedItem != nil {
483
- println ("Verification: Found submenu with updated label:" , updatedItem .Label ())
482
+ submenu .SetLabel (targetNewLabel )
483
+ menu .Update () // Global update
484
+
485
+ fmt .Printf ("Submenu label in myMenu changed from '%s' to: '%s'\n " , oldLabel , submenu .Label ())
486
+
487
+ // Verification (using the direct reference)
488
+ if submenu .Label () == targetNewLabel {
489
+ fmt .Printf ("Verification: Submenu MenuItem in myMenu now has label: %s\n " , submenu .Label ())
484
490
} else {
485
- println ("Verification failed: Could not find submenu with updated label" )
491
+ fmt . Printf ("Verification FAILED: Submenu MenuItem in myMenu has label: %s, but expected %s \n " , submenu . Label (), targetNewLabel )
486
492
}
487
493
} else {
488
- println ("Submenu not found!" )
494
+ // This means myDemoMenuItemForSubmenu was nil when captured (myMenu.GetItemByLabel("Submenu") failed during setup).
495
+ fmt .Println ("Error: The 'myDemoMenuItemForSubmenu' variable is nil (was not found during menu setup)." )
489
496
}
490
497
})
491
498
@@ -519,47 +526,61 @@ func main() {
519
526
520
527
// Add a button to the nested submenu to change its own label
521
528
nestedSubmenu .Add ("Change My Own Label" ).OnClick (func (* application.Context ) {
522
- // Find the nested submenu item
523
- submenuItem := labelOperations .FindByLabel ("Nested Submenu" )
524
- if submenuItem != nil {
525
- // Change the label
526
- submenuItem .SetLabel ("Self-Updated Submenu" )
527
- // Update the menu to reflect the changes
528
- menu .Update ()
529
- println ("Nested submenu changed its own label to: Self-Updated Submenu" )
529
+ // 'nestedSubmenu' is the Go variable pointing to the menu whose label we want to change.
530
+ // It's captured by this closure.
531
+ if nestedSubmenu != nil {
532
+ oldLabel := nestedSubmenu .Label ()
533
+ targetNewLabel := "Self-Updated Submenu"
534
+
535
+ // Avoid re-setting if already set, to prevent redundant updates/logs if clicked multiple times
536
+ if oldLabel == targetNewLabel {
537
+ fmt .Printf ("Nested submenu (self) label is already: %s\n " , targetNewLabel )
538
+ return
539
+ }
530
540
531
- // Verify the change
532
- updatedItem := labelOperations .FindByLabel ("Self-Updated Submenu" )
533
- if updatedItem != nil {
534
- println ("Verification: Found nested submenu with self-updated label:" , updatedItem .Label ())
541
+ nestedSubmenu .SetLabel (targetNewLabel )
542
+ menu .Update () // Or app.Update()
543
+
544
+ fmt .Printf ("Nested submenu (self) changed label from '%s' to: %s\n " , oldLabel , nestedSubmenu .Label ())
545
+
546
+ // Verification
547
+ if nestedSubmenu .Label () == targetNewLabel {
548
+ fmt .Printf ("Verification: Nested submenu (self) object now has label: %s\n " , nestedSubmenu .Label ())
535
549
} else {
536
- println ("Verification failed: Could not find nested submenu with self-updated label" )
550
+ fmt . Printf ("Verification FAILED: Nested submenu (self) object has label: %s, but expected %s \n " , nestedSubmenu . Label (), targetNewLabel )
537
551
}
538
552
} else {
539
- println ("Nested submenu not found! " )
553
+ println ("Error: The 'nestedSubmenu' variable is nil in the 'Change My Own Label' click handler scope. " )
540
554
}
541
555
})
542
556
543
557
// Add a button to update the nested submenu's label
544
558
labelOperations .Add ("Update Nested Submenu Label" ).OnClick (func (* application.Context ) {
545
- // Find the nested submenu item
546
- submenuItem := labelOperations .FindByLabel ("Nested Submenu" )
547
- if submenuItem != nil {
548
- // Change the label
549
- submenuItem .SetLabel ("Updated Nested Submenu" )
550
- // Update the menu to reflect the changes
551
- menu .Update ()
552
- println ("Nested submenu label changed to: Updated Nested Submenu" )
559
+ // 'nestedSubmenu' is the Go variable pointing to the target menu.
560
+ // It's captured by this closure.
561
+ if nestedSubmenu != nil {
562
+ oldLabel := nestedSubmenu .Label ()
563
+ targetNewLabel := "Updated Nested Submenu"
564
+
565
+ // Avoid re-setting if already set
566
+ if oldLabel == targetNewLabel {
567
+ fmt .Printf ("Nested submenu label is already: %s\n " , targetNewLabel )
568
+ return
569
+ }
553
570
554
- // Verify the change
555
- updatedItem := labelOperations .FindByLabel ("Updated Nested Submenu" )
556
- if updatedItem != nil {
557
- println ("Verification: Found nested submenu with updated label:" , updatedItem .Label ())
571
+ nestedSubmenu .SetLabel (targetNewLabel )
572
+ menu .Update () // Or app.Update()
573
+
574
+ fmt .Printf ("Nested submenu label changed from '%s' to: %s\n " , oldLabel , nestedSubmenu .Label ())
575
+
576
+ // Verification
577
+ if nestedSubmenu .Label () == targetNewLabel {
578
+ fmt .Printf ("Verification: Nested submenu object now has label: %s\n " , nestedSubmenu .Label ())
558
579
} else {
559
- println ("Verification failed: Could not find nested submenu with updated label" )
580
+ fmt . Printf ("Verification FAILED: Nested submenu object has label: %s, but expected %s \n " , nestedSubmenu . Label (), targetNewLabel )
560
581
}
561
582
} else {
562
- println ("Nested submenu not found! " )
583
+ println ("Error: The 'nestedSubmenu' variable is nil in the 'Update Nested Submenu Label' click handler scope. " )
563
584
}
564
585
})
565
586
0 commit comments