8

I've often wanted to set the size of an interface element to something like Full - 50, so as to not over-constrain box sizes.

One approach to this would be to simply use a Dynamic[boxsize - 50] as my ImageSize.

Unfortunately, for boxes that information isn't possible to extract by normal means.

I've always resorted to hacks and workarounds, but I don't see what that info shouldn't be possible to extract from the FE.

In digging I found FrontEnd`GetBoundingBoxSizePacket, but it always returns one of two things. Either it gives:

FrontEndExecute[FrontEnd`GetBoundingBoxSizePacket@EvaluationCell[]]

{{34., 13., 7.}}

Or

FrontEndExecute[FrontEnd`GetBoundingBoxSizePacket@1]

$Failed

No matter what FE object I pass to it, it only ever returns one size.

Does anyone know can I get the size of a BoxObject?

b3m2a1
  • 46,870
  • 3
  • 92
  • 239

1 Answers1

4

GetBoundingBoxSizePacket seems to only operate on Cell expressions, so we can do:

getBoxSize[c_Cell] := {#[[1]], Total@#[[2 ;;]]} &@
   First@FrontEndExecute@GetBoundingBoxSizePacket[c];
getBoxSize[c_CellObject] := getBoxSize[NotebookRead[c]];
getBoxSize[b_BoxObject] :=
  getBoxSize[
   Cell[BoxData@NotebookRead[b],
    "Output",
    PageWidth -> Infinity,
    ShowCellBracket -> False,
    CellMargins -> {{0, 0}, {0, 0}}
    ]
   ];
getBoxSize[e_] :=
  getBoxSize[
   Cell[BoxData@ToBoxes[e],
    "Output",
    PageWidth -> Infinity,
    ShowCellBracket -> False,
    CellMargins -> {{0, 0}, {0, 0}}
    ]
   ];

And we get what we expect:

c = EvaluationCell[];
getBoxSize@EvaluationCell[]
Rasterize[c, "RasterSize"]

{215., 60.}

{215, 60}

Dynamic[boxo = EvaluationBox[]]

Rasterize[Dynamic[EvaluationBox[]], "RasterSize"]
getBoxSize@boxo

{136, 26}

{135., 26.}
b3m2a1
  • 46,870
  • 3
  • 92
  • 239