Help with script to change button icon based on a var

Hi,

I am trying to get an icon for a button to changed based on whether a var is set or not. I can get it to work if @toggle is enabled for the var, but if @toggle is disabled (which is what I want) then the icon switching stops working as well.

Here is the working button code. If you swap the comments (//) on the toggle lines then the icon switching stops working. If there is a way to have the icon changing with toggle disabled that would be great!

<?xml version="1.0"?>
<button 3dborders="no" backcol="none" display="icon" icon_size="large" textcol="none">
	<icon1>#DOpusBasic:copysourcedest_down</icon1>
	<function type="normal">
		<instruction>@toggle:if $lst:vViewPin</instruction>
		<instruction>//@toggle:disable</instruction>
		<instruction />
		<instruction>//if var exists</instruction>
		<instruction>@ifset:$lst:vViewPin</instruction>
		<instruction>@set lst:vViewPin</instruction>
		<instruction />
		<instruction>//if var does not exist</instruction>
		<instruction>@ifset:else</instruction>
		<instruction>@set lst:vViewPin=on</instruction>
		<instruction>@icon:copysourcedest_up,$lst:vViewPin</instruction>
	</function>
</button>

Many thanks,
Ving

It works ok for me just as it is.

Hi Jon,

Is there a way to do it without the blue toggle background though?

Cheers,
Ving

Ah, I see what you mean. The problem is that the button isn't being redrawn without the @toggle directive in there, which means even though the variable changes the appearance doesn't update. You can use @toggle:update to force the redraw to happen manually:

@icon:copysourcedest_up,$lst:vViewPin
@ifset:$lst:vViewPin
@set lst:vViewPin
@ifset:else
@set lst:vViewPin=on
@ifset:common
@toggle:update

Thanks, Jon. That's perfect!

Using Jon's code, I note that the icon toggle works on a fixed toolbar but not on a floating toolbar.

Regards, AB

lst: variables are scoped to a Lister, and a floating toolbar doesn't have a Lister.