Getting CellDingbat to remember its state between Mathematica sessions

后端 未结 1 2030
既然无缘
既然无缘 2021-01-05 02:37

I have modified my notebook\'s stylesheet to include a StyleData[\"Todo\"] that inherits from StyleData[\"Item\"]. It changes the cell dingbat to a

相关标签:
1条回答
  • 2021-01-05 03:40

    The problem with your code (I think) is that a new DynamicModule does not get created each time you create a new "ToDo" cell. So there is nowhere that the state of each Checkbox can get saved.

    The simplest solution I could think of for storing the state of the Checkbox for each "ToDo" cell is to overwrite the CellDingbat the first time that the Checkbox is activated. (Other options I played with were using TaggingRules, toggling between "ToDo" and "ToDone" styles, etc...)

    However, even a plain Checkbox in a CellDingbat does not store its state - try running the following then cycle the output through a Show Expression cycle.

    CellPrint[Cell["test", "Text", CellDingbat -> ToBoxes[Checkbox[]]]]
    

    To get around this, I used Checkbox with the definite argument True or False wrapped up in a button that changes the state. This is stupid and inefficient, but it works!

    So, my code for the cell style

    Cell[StyleData["ToDo", StyleDefinitions -> StyleData["Item"]],
     CellDingbat -> ButtonBox[CheckboxBox[False], 
       ButtonFunction :> (SelectionMove[ButtonNotebook[], All, ButtonCell]; 
         With[{$CellContext`new = ReplaceAll[
              Options[NotebookSelection[ButtonNotebook[]], CellDingbat], 
              CheckboxBox[Pattern[$CellContext`x, Alternatives[True, False]]] :> CheckboxBox[Not[$CellContext`x]]]}, 
            SetOptions[NotebookSelection[ButtonNotebook[]], $CellContext`new]]; 
          SelectionMove[ButtonNotebook[], After, CellContents]), 
        Appearance -> None, Method -> "Preemptive", Evaluator -> Automatic]]
    

    Screenshota

    I'm not happy with this solution, but it's the best I've come up with. An improvement would be to move the button function code out of the cell so that it is not repeated for every checked ToDo cell. Also to make it run without a ReplaceAll so that the kernel is not needed and the function can be run using just the frontend.

    0 讨论(0)
提交回复
热议问题