Home of the Squeezebox™ & Transporter® network music players.
Results 1 to 5 of 5
  1. #1
    Senior Member chill's Avatar
    Join Date
    Mar 2007
    Posts
    1,222

    Changing choice menu programmatically

    I'm finding this very slow going, and would appreciate some tips to help me understand one or two issues.

    I have a very simple menu:
    Code:
    	menu = SimpleMenu("menu",
    		{	
    		})
    
    		menu:addItem({
    			text = self:string("SQUEEZEEXT"),
    			style = 'item_choice',
    			iconStyle = "digital",
    			check = Choice(
    				"choice", 
    				{ "On", "Off" },
    				function(obj, selectedIndex)
    					log:info(selectedIndex)
    				end,
    				2
    			)
    		})
    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!


  2. #2
    Babelfish's Best Boy mherger's Avatar
    Join Date
    Apr 2005
    Location
    Switzerland
    Posts
    20,294

    [SlimDevices: Jive] Changing choice menu programmatically

    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.

    --

    Michael

  3. #3
    Senior Member chill's Avatar
    Join Date
    Mar 2007
    Posts
    1,222
    Thanks Michael

    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.

  4. #4
    Senior Member chill's Avatar
    Join Date
    Mar 2007
    Posts
    1,222
    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.

    Code:
    		menu:addItem({
    			text = self:string("SQUEEZEEXT"),
    			style = 'item_choice',
    			iconStyle = "digital",
    			check = Choice(
    				"choice", 
    				{ "On", "Off" },
    				function(obj, selectedIndex)
    					log:info(selectedIndex)
    					obj:setSelectedIndex(2)
    				end,
    				2
    			)
    		})
    That's a bit more chipped away......
    Last edited by chill; 2019-02-11 at 01:49.

  5. #5
    Senior Member chill's Avatar
    Join Date
    Mar 2007
    Posts
    1,222
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •