10
$\begingroup$

Intro

PaneSelector looks like a pretty idiomatic way to toggle displayed content e.g.:

PaneSelector[
  {True -> progressBar, False -> button}
, Dynamic @ processing
]

Analogous If version:

Dynamic[ If[processing, progressBar, button] ]

The difference is that PaneSelector content will be converted to boxes and each switch can be faster than Dynamic which contents are sent for typesetting every time it is refreshed. Shortly, for PS only the value of processing does the round trip while for Dynamic[If[...]], full content.

Sounds good, does not work. I regret each time I use it.

Problem 1

DynamicModule[{processing = False, longProcedure},
 longProcedure[] := Pause[2];
 With[{
   progressBar = ProgressIndicator[Appearance -> "Indeterminate"],
   button = Button["Run", processing = True; longProcedure[];         processing = False, Method -> "Queued"],
   buttonAsync = Button["Run async", processing = True;         RunScheduledTask[longProcedure[], {.1}, "EpilogFunction" :> (processing = False)], Method -> "Queued"]
   },
  Grid[{
    {"processing:", Style[Dynamic @ {processing}, Bold], SpanFromLeft},
    {"PS:", PaneSelector[{True -> progressBar, False -> button},           Dynamic@processing, ImageSize -> All]},
    {"If:", Dynamic[If[TrueQ@processing, progressBar, button]]},
    {},
    {"PS:", PaneSelector[{True -> progressBar, False -> buttonAsync},           Dynamic@processing, ImageSize -> All]},
    {"If:", Dynamic[If[TrueQ@processing, progressBar, buttonAsync]]},
    {},
    {"Just toggle:",  Button["toggle", processing = ! processing;, Method -> "Queued"]},
    {"Toggle and pause:", Button["toggle2", processing = ! processing; Pause[2];,  Method -> "Queued"]}
    }, Alignment -> Left]
  ]
 ]

enter image description here

  • Pressing Run affects only If based solutions and not PS.

  • Pressing Run async shows both progress bars but when it is done only the If based solution shows the button back.

  • Simple toggle works as expected

  • Toggle and pause triggers PS based solution only after Pause is finished.

Using FinishDynamic is an overkill here.

Problem 2

It does undocumented caching so you need to inject to your view stuff like random numbers:

Dynamic triggered but renders the same content

Why is PaneSelector caching nested Dynamics and how to switch it off?

Do you fancy debugging by inserting RandomReal[] here and there? I do not.

Problem 3

Since it usually has PaneSelector[{__}, Dynamic[_Symbol]] form, you don't know when this will hit you:

What is the difference between Dynamic[x] and Dynamic[ h[x] ] for DynamicModule variables?

Question

Did I miss the point of this function? What are the basic rules to follow to not face problems like this? Or is it better to just ignore this function which is what I would suggest to do.

$\endgroup$
6
  • $\begingroup$ cross posted in community.wolfram.com $\endgroup$
    – Kuba
    Commented May 25, 2018 at 9:05
  • 3
    $\begingroup$ "Sounds good, does not work. I regret each time I use it." QFT. That's all. $\endgroup$ Commented May 25, 2018 at 9:41
  • 5
    $\begingroup$ There's a known bug about the FE doing some overzealous optimisation so the PS versions can be made to work with adding a +0 to the Dynamic, ie PaneSelector[{True -> progressBar, False -> button}, Dynamic[processing+0], ImageSize -> All] $\endgroup$
    – Gerli
    Commented May 25, 2018 at 9:47
  • 6
    $\begingroup$ @Gerli You probably mean Problem3, nice trick! I was usually using Dynamic[ First @ {processing}] but this does not help here while your +0 does. :D How ridiculous is this discussion now? XD $\endgroup$
    – Kuba
    Commented May 25, 2018 at 9:56
  • 2
    $\begingroup$ The real problem seems to be that someone thought undocumented "optimizations" in the FE would be a good idea. After years of using and programming CDF apps (and I still have to maintain a few) I come to the conclusion that the whole Dynamic design is flawed and it probably is quite unfixable (for any really serious non-trivial application). $\endgroup$ Commented May 25, 2018 at 19:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.