MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Help with dynamic functionality

  • To: mathgroup at smc.vnet.net
  • Subject: [mg128828] Re: Help with dynamic functionality
  • From: John Fultz <jfultz at wolfram.com>
  • Date: Thu, 29 Nov 2012 06:04:26 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <20121128081734.90AAB68D0@smc.vnet.net>

There are two problems going on here.

The problem I spotted immediately is that Dynamics are updated based 
upon a dependency graph.  All variables which are resolved by the 
evaluation of the first argument of Dynamic are recorded in this 
dependency graph and, if any of them change, the Dynamic is invalidated 
and updated.  Your Slider depends only upon 'x', and if 'x' isn't 
updating, the Slider doesn't update.

So, I immediately thought, let's just add 'enabled' to that first 
argument.  I.e.,

Slider[Dynamic[enabled; x, 

And that didn't work.  That gave me some pause.  Until I realized the 
second problem here.  Yes, I had actually gotten the Dynamic to update, 
but what I hadn't done was to trigger the second argument.  The second 
argument only gets triggered when you're actively assigning a value.  In 
this case, that means only when a user is actually moving the slider.  
This is exactly the documented behavior, incidentally; I was just being 
a bit thick about realizing this.

Which means that you're going to have to repeat the setting 
functionality in the first *and* second arguments. It's got to be in the 
first argument because external updates have to set x.  It's got to be 
in the second argument because the user can still move the mouse, and 
you'll need to tell Mathematica how to assign the value to what will now 
be a complex first argument.

So something like this works:

Clear[y, SliderModule];
SetAttributes[SliderModule, HoldAll];
SliderModule[enabled_] :=
 DynamicModule[{x = 0}, {Slider[
    Dynamic[If[! enabled, x = 0]; x, (x = If[enabled, #, 0]) &]],
   Dynamic[x]}]
{Checkbox[Dynamic[y]], Dynamic[y], SliderModule[y]}


John Fultz
jfultz at wolfram.com
User Interface Group
Wolfram Research, Inc.



On Nov 28, 2012, at 2:17 AM, samid.hoda at gmail.com wrote:

> My actual problem is quite involved but I am including a simple 
example for
> illustrative purposes. I am trying to reproduce "Enabled"-like 
functionality as follows:
>
> Clear[y, SliderModule];
> SetAttributes[SliderModule, HoldAll];
> SliderModule[enabled_] :=
> DynamicModule[{x = 0}, {Slider[
>    Dynamic[x, (x = If[enabled, #, 0]) &]], Dynamic[x]}]
> {Checkbox[Dynamic[y]], Dynamic[y], SliderModule[y]}
>
> Ideally when the checkbox is checked then the user is able to move the 
slider; but when it is unchecked the slider resets to zero. The problem 
is when I uncheck the checkbox the slider doesn't reset to zero until I 
click on the slider itself. Is there a way to get the slider to 
immediately reset when I uncheck the checkbox? Please remember that this 
is a simplification of my actual problem so try to keep the SliderModule 
structure the same.
>
> Cheers,
> Sam
>




  • Prev by Date: Integral that should converge, but Mathematica says it does not
  • Next by Date: Color according to concavity
  • Previous by thread: Re: Help with dynamic functionality
  • Next by thread: Re: Help with dynamic functionality