0
$\begingroup$

I created a drag and drop attached cell which can be moved and anchored anywhere in a notebook window.

enter image description here

To try it out, execute the following code and click the Start button. Then move the panel labeled "Move Me 3 Times". It stops working after 3 moves. To start over, click "Reset" (which deletes all attached cells attached to the notebook).

Notice the first move is caused by the first "SetOptions" in the code below. The second and third moves are caused by the second and third "SetOptions" in the code. Notice the code for the second and third are identical, except the latter is coded inside the former, recursively.

To go beyond 3 moves, I can simply copy and paste a few times to extend the chain of recursive code. However, I'd like to allow infinitely moves. Can someone help me condense the code so that it allows for infinite moves? I'm not sure how to condense the code into something that works recursively, i.e. ad infinitum.

mysimpletoy= Panel["Move Me 3 Times",Background->LightRed];
DynamicModule[{init, attachedCell, k, p},
 
 Row[{
   Button["Reset", 
    NotebookDelete[Cells[EvaluationNotebook[], AttachedCell -> True]]],
   Button["Start",
    SelectionMove[InputNotebook[], After, CellContents];
    init = AttachCell[InputNotebook[], mysimpletoy, Bottom, -200];


    SetOptions[init, CellEventActions -> {
       "MouseDragged" :> (
         NotebookDelete[init];
         NotebookDelete[attachedCell];
          attachedCell = 
          AttachCell[InputNotebook[], mysimpletoy, {Left, Top}, 
           Offset[MousePosition["WindowAbsolute"]*{1, -1}, 0]];
         
         
         SetOptions[attachedCell, CellEventActions -> {
            "MouseDragged" :> (
              NotebookDelete[attachedCell];
               attachedCell = 
               AttachCell[InputNotebook[], mysimpletoy, {Left, Top}, 
                Offset[MousePosition["WindowAbsolute"]*{1, -1}, 0]];



              SetOptions[attachedCell, CellEventActions -> {
                 "MouseDragged" :> (
                   NotebookDelete[attachedCell];
                   attachedCell = 
                    AttachCell[InputNotebook[], 
                    mysimpletoy, {Left, Top}, 
                    Offset[MousePosition["WindowAbsolute"]*{1, -1}, 0]]
                   )} ]
              )} ]
         
         )} ];
    ]}]]
$\endgroup$

1 Answer 1

1
$\begingroup$

Cute idea. Use a function for the dragging to have it go forever.

dragCell[Dynamic[attachedCell_], baseCell_] := (
   NotebookDelete[attachedCell];
   attachedCell = AttachCell[
     InputNotebook[], mysimpletoy,
     {Left, Top}, Offset[MousePosition["WindowAbsolute"]*{1, -1}, 0]
     ];
   SetOptions[attachedCell, 
    "MouseDragged" :> dragCell[Dynamic[attachedCell], baseCell]
    ]);
attachDraggableCell[Dynamic[attachedCell_], baseCell_, initPoint__] := (
  attachedCell = AttachCell[InputNotebook[], baseCell, initPoint];
  SetOptions[attachedCell,
   "MouseDragged" :> dragCell[Dynamic[attachedCell], baseCell]
   ];
  attachedCell
  )

I couldn't test that because my version of Mathematica is too old, but I think that's all you need.

Then you'd use it like

attachDraggableCell[Dynamic[ac], mysimpletoy, Bottom, -200]
$\endgroup$
2
  • $\begingroup$ Thank you. Unfortunately, it doesn't seem to work. : / $\endgroup$
    – B flat
    Commented May 7, 2022 at 1:05
  • $\begingroup$ Thanks for your help. It makes sense to use functions. I was able to make it work with your help. Probably not the cleanest code... but I made a pretty cool drag and drop that works with rotatable graphics. It took me probably 30 hours... I'm slow. Here it is: mathematica.stackexchange.com/a/267929/33996 $\endgroup$
    – B flat
    Commented May 7, 2022 at 6:14

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.