Search code examples
gofyne

Change the menu text after clicking it in go fyne systray menu


Here is my systray menu, I need to change "start" button to "stop" after clicking it(and change it's action to "stop" action)
enter image description here

This issue had mentioned about this, but I can not find the refresh() method of "menu1".

BTW, I'm testing it in macOS Big Sur 11.5.1 (20G80), and my Fyne version is v2.3.3

Here's my code

package main

import (
    "fmt"

    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/driver/desktop"
    "fyne.io/fyne/v2/widget"
)

var menu1 *fyne.MenuItem

func main() {
    a := app.New()
    w := a.NewWindow("SysTray")

    if desk, ok := a.(desktop.App); ok {
        menu1 = fyne.NewMenuItem("start", func() {
            fmt.Println("started")

            // after started, I change the menu label to "stop" and change the Action to stop action,
            // but I don't know how to update this to the system tray menu
            menu1.Label = "stop"
            menu1.Action = func() {
                fmt.Println("stopped")
            }
            // There is no Refresh() method for MenuItem, only for menu1.ChildMenu
            // menu1.ChildMenu.Refresh()
        })

        menu2 := fyne.NewMenuItem("menu1", nil)
        menu2.ChildMenu = fyne.NewMenu("",
            fyne.NewMenuItem("submenu1", func() {
                fmt.Println("submenu1")
            }),
            fyne.NewMenuItem("submenu2", func() {
                fmt.Println("submenu2")
            }),
        )

        menu3 := fyne.NewMenuItem("show2", func() {
            fmt.Println("menu2")
        })

        m := fyne.NewMenu("MyApp", menu1, menu2, menu3)
        desk.SetSystemTrayMenu(m)
    }

    w.SetContent(widget.NewLabel("Fyne System Tray"))
    w.SetCloseIntercept(func() {
        w.Hide()
    })
    w.ShowAndRun()
}

Here's my go.mod

module go-GUI

go 1.19

require fyne.io/fyne/v2 v2.3.3

require (
    fyne.io/systray v1.10.1-0.20230312215936-7f71b037e260 // indirect
    github.com/benoitkugler/textlayout v0.3.0 // indirect
    github.com/davecgh/go-spew v1.1.1 // indirect
    github.com/fredbi/uri v0.1.0 // indirect
    github.com/fsnotify/fsnotify v1.5.4 // indirect
    github.com/fyne-io/gl-js v0.0.0-20220119005834-d2da28d9ccfe // indirect
    github.com/fyne-io/glfw-js v0.0.0-20220120001248-ee7290d23504 // indirect
    github.com/fyne-io/image v0.0.0-20220602074514-4956b0afb3d2 // indirect
    github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6 // indirect
    github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect
    github.com/go-text/typesetting v0.0.0-20221212183139-1eb938670a1f // indirect
    github.com/godbus/dbus/v5 v5.1.0 // indirect
    github.com/goki/freetype v0.0.0-20220119013949-7a161fd3728c // indirect
    github.com/gopherjs/gopherjs v1.17.2 // indirect
    github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e // indirect
    github.com/pmezard/go-difflib v1.0.0 // indirect
    github.com/srwiley/oksvg v0.0.0-20220731023508-a61f04f16b76 // indirect
    github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780 // indirect
    github.com/stretchr/testify v1.8.0 // indirect
    github.com/tevino/abool v1.2.0 // indirect
    github.com/yuin/goldmark v1.4.13 // indirect
    golang.org/x/image v0.0.0-20220601225756-64ec528b34cd // indirect
    golang.org/x/mobile v0.0.0-20211207041440-4e6c2922fdee // indirect
    golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
    golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
    golang.org/x/text v0.6.0 // indirect
    gopkg.in/yaml.v3 v3.0.1 // indirect
    honnef.co/go/js/dom v0.0.0-20210725211120-f030747120f2 // indirect
)

Solution

  • As was posted on Slack earlier today, and on the GitHub issue, you need to call Menu.Refresh to refresh the menu that is shown. In your code above that would be m.Refresh().