0
$\begingroup$

I had this working fine this morning, but I proceeded to refactor, and somehow I cannot get it to toggle like before. It does initialize, and it does go from state-1 to state-2 but not back to state-1. That's what I cannot figure out. If you have any suggestions, please help.

ClearAll[autosave];
 
Options[autosave] = {"ButtonOptions" -> {}};
autosave[bgs_ : {Darker@Blue, Darker@Green}, cols_ : {White, White}, 
   labs_ : {"AutoSave", "Saving"}, size_ : {75, 25}, 
   opts : OptionsPattern[]] := Module[{
    state := CurrentValue[EvaluationNotebook[], NotebookAutoSave],
    flip := 
     SetOptions[EvaluationNotebook[], 
      NotebookAutoSave -> If[TrueQ[state], False, True]]
    },
   toggle[flp_ : True] := CompoundExpression[
     If[TrueQ[flp], flip],
     Echo@{flp, state};
     col = If[TrueQ[state], Last[cols], First[cols]],
     label = If[TrueQ[state], Last[labs], First[labs]],
     bg = If[TrueQ[state], Last[bgs], First[bgs]]];
   Dynamic[
    Button[label, toggle[], BaseStyle -> col, 
     Background -> Dynamic[bg], ImageSize -> size, 
     Apply[Sequence, OptionValue["ButtonOptions"]]], 
    Initialization :> toggle[False]]];

autosave[]
CurrentValue[EvaluationNotebook[], NotebookAutoSave] = False
$\endgroup$

1 Answer 1

1
$\begingroup$

You forget to reset the variable "state":

autosave[bgs_ : {Darker@Blue, Darker@Green}, cols_ : {White, White}, 
   labs_ : {"AutoSave", "Saving"}, size_ : {75, 25}, 
   opts : OptionsPattern[]] := 
  Module[{state := 
     CurrentValue[EvaluationNotebook[], NotebookAutoSave], 
    flip := SetOptions[EvaluationNotebook[], 
      NotebookAutoSave -> (state = If[TrueQ[state], False, True])]}, 
   toggle[flp_ : True] := 
    CompoundExpression[If[TrueQ[flp], flip], Echo@{flp, state};
     col = If[TrueQ[state], Last[cols], First[cols]], 
     label = If[TrueQ[state], Last[labs], First[labs]], 
     bg = If[TrueQ[state], Last[bgs], First[bgs]]];
   Dynamic[
    Button[label, toggle[], BaseStyle -> col, 
     Background -> Dynamic[bg], ImageSize -> size, 
     Apply[Sequence, OptionValue["ButtonOptions"]]], 
    Initialization :> toggle[False]]];
$\endgroup$
3
  • $\begingroup$ this is maybe the 5th or 6th you came through for me. thank you so much! just one question. why did state:=CurrentValue[...] need to be reset if its definition uses SetDelay so that it acts like a regular function always performing its instructions fresh each time it's called? $\endgroup$ Sep 5, 2022 at 9:25
  • $\begingroup$ state simply reads the current value but does not toggle it. $\endgroup$ Sep 5, 2022 at 10:24
  • $\begingroup$ yes i know. that's why I have flip and toggle functions. here is the definition from my post: state := CurrentValue[EvaluationNotebook[], NotebookAutoSave]. funny thing is it worked when i used it every time i needed a property (i toggle up to 4 of them and 1 more for flip) and I thought that was very inefficient so i refactored it to only call state twice instead of 5 times. $\endgroup$ Sep 5, 2022 at 11:53

Your Answer

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

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