在go fyne systray菜单中单击后更改菜单文本

yks3o0rb  于 2023-04-18  发布在  Go
关注(0)|答案(2)|浏览(166)

这是我的系统托盘菜单,我需要更改“开始”按钮后,点击它“停止”(并更改其行动“停止”行动)

This issue曾经提到过这一点,但我找不到“menu1”的refresh()方法。
顺便说一句,我正在macOS Big Sur 11.5.1(20G80)中测试它,我的Fyne版本是v2.3.3
这是我的代码

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

我的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
)
ffscu2ro

ffscu2ro1#

正如今天早些时候在Slack上发布的那样,在GitHub issue上,您需要调用Menu.Refresh来刷新显示的菜单。在上面的代码中,这将是m.Refresh()

vptzau2j

vptzau2j2#

这是工作正常的代码

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
var menu *fyne.Menu

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

    if desk, ok := a.(desktop.App); ok {
        menu1 = fyne.NewMenuItem("start", func() {
            if menu1.Label == "start" {
                fmt.Println("started")
                menu1.Label = "stop"
            } else {
                fmt.Println("stopped")
                menu1.Label = "start"
            }
            // after changing the menu label, use this to refresh the menu
            // menu is return
            menu.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")
        })

        menu = fyne.NewMenu("MyApp", menu1, menu2, menu3)

        desk.SetSystemTrayMenu(menu)
    }

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

相关问题