lxn / walk

A Windows GUI toolkit for the Go Programming Language
Other
6.78k stars 886 forks source link

[QUESTION] How to know which action are being trigger in EventHandler #787

Open qng95 opened 2 years ago

qng95 commented 2 years ago

Hello pals,

I'm trying to create a tray icon following this example https://github.com/lxn/walk/tree/master/examples/notifyicon

The menu items inside could be dynamically created on the fly base on data that it queries from the server. Then actions are created base on that data, and event handlers for those actions should result in different method calls depends on the text value of that action.

items = GetItemsFromAPI()
for _, item := range items {
    action := walk.NewAction()
    action.SetText(item)
    action.Triggered().Attach(func() {
        // this event handler doesn't known any information about it owner action. So it should not determine which method it should call
    })
    provisionerMenu.Actions().Add(action)
}

Is there a way to let's EventHandler know about its owner Actions?

Thank you very much 👍

Cheers

lpintes commented 2 years ago

Hello Quan,

Can you be a bit more concrete?

How the method call looks like?

I believe there is a way, but I am unable to crack a simple example right now.

Dňa 6. 10. 2021 o 11:55 Quan Hong Nguyen napísal(a):

Hello pals,

I'm trying to create a tray icon following this example https://github.com/lxn/walk/tree/master/examples/notifyicon https://github.com/lxn/walk/tree/master/examples/notifyicon

The menu items inside could be dynamically created on the fly base on data that it queries from the server. Then actions are created base on that data, and event handlers for those actions should result in different method calls depends on the text value of that action.

|items = GetItemsFromAPI() for _, item := range items { action := walk.NewAction() action.SetText(item) action.Triggered().Attach(func() { // this event handler doesn't known any information about it owner action. So it should not determine which method it should call }) provisionerMenu.Actions().Add(action) }|

Is there a way to let's EventHandler know about its owner Actions?

Thank you very much 👍

Cheers

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/lxn/walk/issues/787, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATNYX6CCOACPA26MJVBZPTUFQMJDANCNFSM5FOC35PQ. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

lpintes commented 2 years ago

Hello again,

Can this simple example help you?

You may create a closure which capture some context:

packagemain import"fmt" funcCreateGreeter(name string) func() { returnfunc() {         fmt.Println("Hello,", name)     } } var( greetQuanfunc() greetLubosfunc() ) funcmain() { greetQuan= CreateGreeter("Quan") greetLubos= CreateGreeter("Lubos") greetQuan() greetLubos() } Dňa 6. 10. 2021 o 11:55 Quan Hong Nguyen napísal(a):

Hello pals,

I'm trying to create a tray icon following this example https://github.com/lxn/walk/tree/master/examples/notifyicon https://github.com/lxn/walk/tree/master/examples/notifyicon

The menu items inside could be dynamically created on the fly base on data that it queries from the server. Then actions are created base on that data, and event handlers for those actions should result in different method calls depends on the text value of that action.

|items = GetItemsFromAPI() for _, item := range items { action := walk.NewAction() action.SetText(item) action.Triggered().Attach(func() { // this event handler doesn't known any information about it owner action. So it should not determine which method it should call }) provisionerMenu.Actions().Add(action) }|

Is there a way to let's EventHandler know about its owner Actions?

Thank you very much 👍

Cheers

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/lxn/walk/issues/787, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATNYX6CCOACPA26MJVBZPTUFQMJDANCNFSM5FOC35PQ. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.