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