11
$\begingroup$

I can't figure out why the following stops working when "WindowAbsolute" is replaced with "Graphics"?

MathLink`CallFrontEnd[
  FrontEnd`AttachCell[EvaluationCell[], 
   Cell[BoxData[
     DynamicBox[
      ToBoxes[MousePosition["WindowAbsolute"]]]], 
    "Output", 
    Background -> LightBlue], {Offset[{0, -15}, 15], {Left, 
     Bottom}}, {Left, Top}, "ClosingActions" -> {"OutsideMouseClick"}]];

enter image description here

$\endgroup$
6
  • 1
    $\begingroup$ interestingly, the attached cell contents update properly to show positions in graphics objects in other open notebooks (not those in the evaluation notebook). $\endgroup$
    – kglr
    Sep 24, 2018 at 21:48
  • $\begingroup$ Perhaps this isn't the correct way to instantiate an attached cell? $\endgroup$
    – M.R.
    Sep 25, 2018 at 22:17
  • 2
    $\begingroup$ M.R. curious about the use case(s) you have in mind. $\endgroup$
    – kglr
    Sep 25, 2018 at 22:20
  • $\begingroup$ Just spelunking into attached cells $\endgroup$
    – M.R.
    Sep 26, 2018 at 0:34
  • 1
    $\begingroup$ Perhaps there’s some other workaround way to get the graphics mouse position? $\endgroup$
    – M.R.
    Jan 16, 2019 at 15:27

1 Answer 1

7
+200
$\begingroup$

I contacted WRI and it is a bug. Sadly it won't be fixed for v12 because it requires rewriting few fundamentals parts of code for AttachedCells.

As a workaround you can emit graphics position from graphics you care about. So wrap an EventHandler around your plots that will myVariable = MousePosition["Graphics"] on MouseMoved event.

You can use regular myVariable but you need to take care about scoping/collisions which I think is case dependent.

Let's create an example which assumes that everyiong is revolving around a single UI as opposed to a whole notebook. We can utilize Dynamic Wormholes for that:

DynamicModule[{position} 
, EventHandler[
    ListPlot @ Prime @ Range @ 25
  , { "MouseMoved" :> (position = CurrentValue[{"MousePosition", "Graphics"}])
    , "MouseExited" :> (position = None)
    }
  ] 
, Initialization :> (
   position = None

 ; MathLink`CallFrontEnd @ FrontEnd`AttachCell[
     EvaluationCell[]
   , Cell[ 
       BoxData @ ToBoxes @ Framed @ DynamicModule[
         {}, Dynamic @ position, InheritScope -> True
       ]
     , "Output"
     ]
   , {Offset[{0, -15}, 15], {Left, Bottom}}
   , {Left, Top}
   , "ClosingActions" -> {"OutsideMouseClick"}
   ]
  )
]

enter image description here

For general, notebook wide solution, you could save it in CurrentValue[nb, {TaggingRules, "GraphicsPosition"}] but somehow refreshing of Dynamic @ CurrentValue[nb, {TaggingRules, "GraphicsPosition"}] reacts very slowly to such changes (see answer edit history for a code example).

$\endgroup$
2
  • $\begingroup$ Ah right, duh, so this is definitely faster, obviously the to-from the kernel isn't the bottleneck here... $\endgroup$
    – M.R.
    Jan 21, 2019 at 22:19
  • $\begingroup$ @M.R. please see the edit, let me know if there is anything missing. $\endgroup$
    – Kuba
    Jan 22, 2019 at 22:51

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.