If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.
It's been years since I've done any applet development... and it wasn't
even too serious back then. What I'd suggest is you look for an existing
menu showing the same or similar behaviour and try to find und
understand its code.
This creates a simple one-line menu item that toggles between 'On' and 'Off'. It starts with 'Off' (as given by the index value of 2). 'selectedIndex' is logged to the terminal, and I can see that it corresponds to the currently selected value.
What I'd like to know is whether it's possible to set the choice to 'On' or 'Off' programmatically, from within the callback function. In my intended code I'd like to call some code when 'On' is selected, which may or may not succeed - if it doesn't succeed, then I'd like to revert to 'Off' again, programmatically (*see below for the reasoning).
The Jive Lua source code conventions page in the wiki says that 'setSelectedIndex' can be used for this, and that doing so will call the callback function again.
Code:
-- New choice with options On and Off, Off is selected
local choice = jive.ui.Choice(
"choice",
{ "On", "Off" },
function(object, selectedIndex)
print("Choice is " .. tostring(selectedIndex))
end,
2
)
-- Change checkbox state
choice:setSelectedIndex(1)
I have all sorts of problems with this.
1) The sample code above uses the word 'choice' in a couple of places ('local choice =', "choice") and it's not clear to me which instance is the one referred to by 'choice:setSelectedIndex'. So in my own code at the top, should I use 'check:setSelectedIndex' or 'choice:setSelectedIndex'? Neither seems to work.
2) If setting the selected index from within the function calls the callback function again, it seems like this could go on forever. If I can't set the index from within the callback function, where else can I set it? My menu is set up inside 'function setupJogglerSqueezelite(self, menuItem)' - this sets up the menus once when the window is loaded, and thereafter the menus operate autonomously. It's the result of selecting a menu item that I want to act on.
*What I'm trying to achieve:
I'm trying to start squeezelite on an external USB device. I can list the devices, and select from them as a Radio Group. The problem is that when I attempt to start squeezelite on some of the listed devices it won't start (e.g. on surround21, surround41, surround51). I can detect those cases, but the menu item displays 'On', since it was toggled from 'Off' when I selected it in order to try to start squeezelite. So in the cases where squeezelite fails to start I'd like to programmatically toggle the menu back to 'Off', so that it's clear to the user that squeezelite didn't start. I'm also trying to put up a popup message when that happens - see my other question about popup messages not disappearing after the specified time - honestly, trying to program in Jive/Lua, with very little experience in this type of language, has been like pulling teeth!
That's indeed the only way I've managed to get as far as I have - one or two applets have helped me with the basics. The 'SetupWallpaper' applet showed me how to use a RadioGroup, and 'LogSettings' helped with the 'Choice' type of menu. But I haven't managed to identify one that changes the 'choice' programmatically. Maybe I'm trying to do something that's not supported, but the existence of that 'choice:setSelectedIndex(1)' suggests it is possible if I can only figure out how to use it.
From within my callback function I've tried:
setSelectedIndex(1)
choice:setSelectedIndex(1)
check:setSelectedIndex(1)
self:setSelectedIndex(1)
None of those seem to do anything. If I put that line outside the callback function code, i.e. outside of the menu item block, then the applet doesn't even display, so that must be a serious syntax error!
So I think my question simply boils down to how should I use the 'setSelectedIndex' method. I'll do a bulk search within the existing applets to see if I can find that method being used.
It's making more sense now. I can indeed change the choice programmatically by referencing the object defined in the callback function - in my case below that is 'obj'. So the code below forces the index to be 2 (Off) every time that menu item is selected, so it no longer toggles.
With that little logjam broken, my JogglerSqueezelite applet is now working exactly as planned. I can toggle either of the top two menu items to create or kill an instance of squeezelite on either interface. For the USB interface, if I select a device from the list that squeezelite can't start (e.g. 'surround21:CARD=DragonFly,DEV=0'), then the second menu item remains 'Off'.
In the absence of a working popup message I think this is clear enough for the user, but ideally I'd like to flash up a popup message when the selected device can't be started by squeezelite, so if anyone can help with this enquiry I'd be grateful.
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, personalize advertising, and to analyze site activity. We may share certain information about our users with our advertising and analytics partners. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment