Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Background: I have several PopupViews of which the loadtime adds to the startup time of a program. To improve the perceived performance I want to defer the loading of the PopupView to the moment that they are actually used. I thought about this:

  Clear[filelist];
  Clear[obj];
  filelist = {};
  Dynamic@PopupView[filelist, Dynamic[obj, (obj = #;) &], 
    Button["Edit", filelist = {1, 2, 3}, ImageSize -> 80]]

The idea works but not as expected, the popupview is not displayed correctly until filelist is loaded. I want to correct this or find an alternative way to set this up.

Question: How can I defer the loading of a PopupView control until it is used ?

share|improve this question
    
A simple If[filelist==={}, #, PopupView[filelist, ...]]&[Button...] can fix the layout, but you would still need to click Edit twice before the popup appears –  Rojo Jun 25 '12 at 14:11
    
Clicking twice is too sloppy. Hmmm. –  ndroock1 Jun 25 '12 at 14:28
    
Let me make sure I understand. You have a PopupView whose list elements take long to execute, and you want them to only execute when you open the PopupView? –  Rojo Jun 25 '12 at 14:30

1 Answer 1

up vote 9 down vote accepted

First, define PopupView with empty reference list and only substitute real reference list ref into the PopupView if it is first clicked. This is done by wrapping the PopupView in an EventHandler. The dynamically displayed list next to the initially empty PopupView shows that before clicking, it is empty, and only gains its value when first clicked.

DynamicModule[{obj = {}, list = {""}, ref = Range@100, firstClick = True},
 {
  EventHandler[Dynamic@PopupView[list, Dynamic[obj, obj = #; &]],
   {"MouseDown" :> (If[firstClick, list = ref]; firstClick = False)},
   PassEventsDown -> True
   ],
  Dynamic@list
  }
 ]

Mathematica graphics

I've tried to solve this with the $f_{start}$ argument of Dynamic's second argument list, but it does not work as it requires not just clicking on the empty PopupView but also selecting one value (even if it is Null) to make the assignment inside Dynamic's second argument happen.

share|improve this answer
    
What can I say? Superb! - Thank you very much. –  ndroock1 Jun 25 '12 at 19:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.