3

How to create a Button which will be displayed only when the value of some global FrontEnd setting is False and will self-destruct with entire row of the Column after pressing it setting this value to True?

I need something like this:

Column[{"Item 1", "Item 2", 
  Dynamic[If[
    Last@Last@Options[$FrontEnd, "VersionedPreferences"] === False, 
    Button["Press me!", 
     SetOptions[$FrontEnd, "VersionedPreferences" -> True]], 
    Sequence @@ {}]]}]

But with this code the Button does not disappear after pressing it. Is it possible to make it self-destructive?


The final solution based on ideas by belisarius and mikuszefski:

PreemptProtect[SetOptions[$FrontEnd, "VersionedPreferences" -> False];
   b = True];

Dynamic[Column[
  Join[{"Item 1", "Item 2"}, 
   If[Last@Last@Options[$FrontEnd, "VersionedPreferences"] === False &&
      b == True, {Button[
      Pane[Style[
        "This FrontEnd uses shared preferences file. Press this \
button to set FrontEnd to use versioned preferences file (all the \
FrontEnd settings will be reset to defaults).", Red], 300], 
      AbortProtect[
       SetOptions[$FrontEnd, "VersionedPreferences" -> True]; 
       b = False]]}, {}]], Alignment -> Center], 
 Initialization :> 
  If[! Last@Last@Options[$FrontEnd, "VersionedPreferences"], b = True,
    b = False]]

The key points are:

  • introducing additional Dynamic variable b and binding it with the value of Options[$FrontEnd, "VersionedPreferences"],
  • wrapping entire Column construct with Dynamic instead of using Dynamic inside Column.
Community
  • 1
  • 1
Alexey Popkov
  • 9,355
  • 4
  • 42
  • 93

2 Answers2

6

Perhaps

PreemptProtect[SetOptions[$FrontEnd, "VersionedPreferences" -> False]; b = True]; 

Column[{"Item 1", "Item 2", Dynamic[
   If[Last@Last@Options[$FrontEnd, "VersionedPreferences"]===False && b == True, 
    Button["Here!", SetOptions[$FrontEnd, "VersionedPreferences"->True];b=False], 
   "Done"]]}]

Edit

Answering your comment. Please try the following. Encompassing the Column[ ] with Dynamic[ ] allows resizing it:

PreemptProtect[SetOptions[$FrontEnd, "VersionedPreferences" -> False]; b = True]; 
Dynamic[
  Column[{
   "Item 1", 
   "Item 2",
   If[Last@Last@Options[$FrontEnd, "VersionedPreferences"] === False && b == True, 
    Button["Press me!", SetOptions[$FrontEnd, "VersionedPreferences" -> True]; b=False], 
    Sequence @@ {}]}]]
Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
4

Hmm, dunno if I get it right, but maybe this:

x = True;

Dynamic[Column[{Button["reset", x = True], If[x, Button["Press me", x = False]]}] ]

mikuszefski
  • 3,943
  • 1
  • 25
  • 38
  • With this code the button disappears but a white space is left on where it was. I wish the corresponding row of the `Column` to completely disappear. – Alexey Popkov Jun 17 '11 at 14:59
  • It seems that your version can be improved just by adding `Sequence @@ {}` as the third argument of `If`. – Alexey Popkov Jun 17 '11 at 15:07
  • @Alexey try x = True;Dynamic[If[x, Column[{Button["reset", x = True], Button["Press me", x = False]}], Column[{Button["reset", x = True]}]]] (of course then you have two versions of the code differing by the existence or not of the second button, but they can be constructed on the fly) – acl Jun 17 '11 at 15:09
  • 1
    This double code can be avoided by: x = True; Dynamic[ Column[Join[{"Some Stuff", Button["reset", x = True]}, If[x, { Button["Press me", x = False]}, {}]] ] ] as Join[] does not act on empty Lists. – mikuszefski Jun 17 '11 at 15:18