Allow to add callbacks directly to items, handle item ids internally - #4
Conversation
Hey @ttytm , I like it! Will do some testing on my end to make sure everything is still working as intended, even though there are some breaking changes, currently VTray is in very early stages, so I am expecting some breaking changes 😆 . I would like to have VTray as simple and easy to use as possible for the future as I can definitely see a Electron / Tauri framework coming to V in the near future with your webview library 😄 , so creating a simple and easy to use system tray is a MUST! |
|
Hey thanks for the review! Awsm! Lets build things brick by brick 👍 |
|
Giving it a short run there are some more awesomeizations to add. They would build on the changes so I'm gonna add it here as they might fit into a breaking merge. The changes add an API that prevents friction points and helps to keep usage as clean possible. E.g. usage now: module main
import ouri028.vtray
fn main() {
icon := $if macos {
'${@VMODROOT}/assets/icon.png'
} $else {
'${@VMODROOT}/assets/icon.ico'
}
mut tray := vtray.create(icon, tooltip: 'VTray Demo!')
tray.add_item(vtray.MenuItem{
text: 'Edit'
checkable: true
})
tray.add_item(vtray.MenuItem{
text: 'Copy'
disabled: true
})
tray.add_item(vtray.MenuItem{
text: 'Quit'
on_click: fn [tray] () {
tray.destroy()
}
})
tray.init()
tray.run()
tray.destroy()
}
pub fn create(icon_path string, opts CreatOptions) &Tray {It includes renaming of structs like VTrayMenuItem to MenuItem. Not prefixing with the lib name is the convention for V as it's already clear from usage and it would be redundant e.g. Gonna refine it to not need |
|
Okay now it might be it. Sorry about the spam 😅. Result: module main
import ouri028.vtray
fn main() {
icon := $if macos {
'${@VMODROOT}/assets/icon.png'
} $else {
'${@VMODROOT}/assets/icon.ico'
}
mut tray := vtray.create(icon, tooltip: 'VTray Demo!')
tray.add_item('Edit', checkable: true)
tray.add_item('Copy', disabled: true)
tray.add_item('Quit', on_click: tray.destroy)
tray.run()
tray.destroy()
}Just the definitions in the readme would need an update. Eventually, using vdoc to generate a doc site automatically on changes and hosting it for free via github pages as V webview does instead could be nice: https://ttytm.github.io/webview/webview.html |
All good, just glad you took an interest in it 😄 . This does look a lot cleaner compared to defining the complete struct, will test it and merge it 👍 I agree regarding the V doc, will have a look at implementing that as well as better code comments. As a quick update as well, I have added two new functions pub fn (v &VTrayApp) set_icon(icon string) Which allows the end user to update the icon and tooltip in realtime without needing to restart the app. |
|
I was thinking it would be great to have an option to get the menu item values, so how does this look? tray.add_item('Edit',
on_click: fn [tray] () {
if x := tray.get_item('Edit') {
println(x.checked)
}
}
checkable: true
)// get_item returns the menu item with the given text.
pub fn (t &Tray) get_item(item string) ?&MenuItem {
for menu_item in t.items {
if menu_item.text == item {
return menu_item
}
}
return none
}We also add the MenuItem to the heap and make the items public. [heap]
struct MenuItem {
pub:
id int
text string
checked bool
checkable bool
disabled bool
} |
Yep that'll be useful.
// get_item returns the menu item with the given text.
pub fn (t &Tray) get_item(item string) ?&MenuItem {
return t.items.filter(it.text == item)[0] or { return none }
}Edit: Sorry, yours is more efficient. My mistake. |
|
It maybe even better to store the items in a map then. I'll just send the lib.v without further refinements for copy pasting, probably easiest to see the diffs in your editor and work with it then:
|
Yeah. This will make it more easier to work with and we avoid unnecessary looping. Thanks! 😄 |
Awesome updates @Ouri028 made it a lot cleaner 💙!
The change I thought of that I mentioned here #2 is still applicable.
The changes will allow to add
on_clickcallbacks directly to items instead of needing to create an additional handler and match for items ids. It might make vtray even more simple and straightforward to use. It's a breaking change. Let me know your thoughts.Example current:
Example with the PRs changes: