[Apologies if this is a duplicate: I have looked at quite a few related questions but have not seen this issue dealt with]
I have built a custom control, using the following method:
customControl[Dynamic[x_]] := True; Manipulate[{x, 1, other}, {{x, 0}, customControl[#1] &}, {{y, 0}, ControlType -> None}, {other, 0, 1}])]
and now all I need to do is wrap the definition of customControl
inside DynamicModule
.
I thought I would know how to do this, from questions such as this and answers such as Federico's. But I'm meeting a problem with internalising a custom control to a DynamicModule
and/or Manipulate
. The example below is the simplest one I could come up with that illustrates the problem, which appears to be one of pattern-matching rather than the definition of my customControl
. This is why the controls I'm defining don't actually do anything: if I can get the pattern to match, then I think I'm in business. So consider the following, which yields the same output as my example above:
Clear[customControl] DynamicModule[{customControl}, (customControl[Dynamic[x_], _] := True; Manipulate[{x, 1, other}, {{x, 0}, customControl[#1, y] &}, {{y, 0}, ControlType -> None}, {other, 0, 1}])]
So far so good: customControl
is called and returns True
as expected. A small change--which you might think is inconsequential, because it involves dropping an argument to customControl
which is never used, yields a different result:
Clear[customControl] DynamicModule[{customControl}, (customControl[Dynamic[x_]] := True; Manipulate[{x, 1, other}, {{x, 0}, customControl[#1] &}, {{y, 0}, ControlType -> None}, {other, 0, 1}])]
So it might seem that x
is not considered as a Dynamic
object when customControl
is called on it unless it is somehow linked to another Dynamic
object through a common invocation, even if that invocation doesn't actually do anything. My first example does not work if the 2nd argument to customControl
is a constant such as 0; it needs to be y. [Or x? I didn't try that...]
[The reason I'm asking this specific question: the code is destined for a CDF interface. SaveDefinitions -> True
is of no help or hope in my application; I need to localise everything within DynamicModule
s. It works fine with Module
but of course that's deprecated (and in Mma10 this is very visible with red highlighting)]
Now I've written all that, I do have a dim memory of a similar question involving dynamic variables and needing to have more than one around...but I haven't found it.
{{x,0},Dynamic@customControl[#] &}
? $\endgroup${{x, 0}, (y; customControl[#]) &}
or{{x, 0}, (other; customControl[#]) &}
? $\endgroup$Dynamic[Control[{{x, 0}, customControl[#]&}]]
and{{x, 0}, customControl[Dynamic[x]] &}
? $\endgroup$