How to create expanding summary boxes like the ones displayed for InterpolatingFunction, NearestFunction, etc.?

Mathematica does it internally by using BoxForm`ArrangeSummaryBox, which is quite straightforward to figure out.

Example

ClearAll[MyObject];

MyObject /: MakeBoxes[obj : MyObject[asc_? myObjectAscQ], form : (StandardForm | TraditionalForm)] := 
    Module[{above, below},
        above = { (* example grid *)
          {BoxForm`SummaryItem[{"Name: ", asc["Name"]}], SpanFromLeft},
          {BoxForm`SummaryItem[{"Variables: ", asc["Variables"]}], BoxForm`SummaryItem[{"Length: ", asc["Length"]}]}
        };
        below = { (* example column *)
          BoxForm`SummaryItem[{"Date: ", asc["Date"]}],
          BoxForm`SummaryItem[{"Metadata: ", asc[MetaInformation]}]
        };
        BoxForm`ArrangeSummaryBox[
           MyObject, (* head *)
           obj,      (* interpretation *)
           $icon,    (* icon, use None if not needed *)
           (* above and below must be in a format suitable for Grid or Column *)
           above,    (* always shown content *)
           below,    (* expandable content *)
           form,
           "Interpretable" -> Automatic
        ]
    ];

It is useful to define a function to test whether MyObject is in the correct format (and whether a summary box can be generated with no errors).

myObjectAscQ[asc_?AssociationQ] := AllTrue[{"Name", "Variables", "Date", "Length", MetaInformation}, KeyExistsQ[asc, #]&]
myObjectAscQ[_] = False;

Summary boxes typically have icons of a certain size:

$icon = 
  Graphics[{Red,Disk[]},
     ImageSize -> Dynamic[{ (* this seems to be the standard icon size *)
         Automatic, 
         3.5 CurrentValue["FontCapHeight"]/AbsoluteCurrentValue[Magnification]
       }]
  ];

Let us test it:

MyObject[<|
  "Name" -> "My particular object",
  "Length" -> 10,
  "Variables" -> {a,b,c},
  "Date" -> Today,
  MetaInformation -> "more info..." |>]

Mathematica graphics

In its expanded form it looks like this:

Mathematica graphics

The "Interpretable" option

If "Interpretable" is set to True, the formatted object can be used directly as input, and will be interpreted as the second argument of ArrangeSummaryBox.

If "Interpretable" is set to Automatic, Mathematica 11.2 and later will decide whether to embed the data into the displayed form of the object based on $SummaryBoxDataSizeLimit. When this size is exceeded, there will be a button that can be used to embed the data.

Mathematica graphics

Usage

Let us define a property retrieval interface, so out MyObject actually does something:

MyObject[asc_?AssociationQ][prop_] := Lookup[asc, prop]

Let's copy-paste the formatted object from above as new input:

Mathematica graphics


The following shows a way to emulate the summary boxes using only documented constructs:

grid[g_] := 
 Column[Row /@ MapAt[Style[#, Gray] &, g, Table[{i, 1}, {i, Length[g]}]]]

foo /: MakeBoxes[c : foo[___], form : (StandardForm | TraditionalForm)] := 
 With[{boxes = 
    RowBox[{"foo", "[", 
      ToBoxes[Panel[
        OpenerView[
         {grid[{{"Something: ", 1}, {"Other: ", 2}}], 
          grid[{{"Extra: ", 3}}]}
        ], 
        BaselinePosition -> Baseline], 
       form], "]"}]
    }, 
    InterpretationBox[boxes, c]
 ]

foo[]

Mathematica graphics

This is a simplified version of something I used in practice.

It only makes sense to use this if you have some sort of requirement to only use documented functions.


If you do this, do not forget to also provide a nice OutputForm, which will be automatically used when running Mathematica in command line mode. It can be done like this:

Format[foo[___], OutputForm] := "foo[<>]"

Perhaps there is a more convenient way of doing this than resorting to esoteric boxes.

The following uses the code you posted to define a function:

summaryDisplay = DynamicModule[{open = True, 
  sqrplus = RawBoxes@FrontEndResource["FEBitmaps", "SquarePlusIconMedium"],
  sqrminus = RawBoxes@FrontEndResource["FEBitmaps", "SquareMinusIconMedium"],
  paneF = Pane[Button[Dynamic[#, ImageSizeCache -> {12., {0., 12.}}], 
      open = #2, Appearance -> None, Evaluator -> Automatic,  Method -> "Preemptive"], 
     Alignment -> {Center, Center}, 
     ImageSize -> Dynamic[{Automatic, 3.5 (CurrentValue["FontCapHeight"]/
           AbsoluteCurrentValue[Magnification])}]] &,
  gridF1 = Grid[#, BaseStyle -> {ShowStringCharacters -> False, 
       NumberMarks -> False, PrintPrecision -> 3, ShowSyntaxStyles -> False}, 
     Alignment -> {{{Left}}, {{Automatic}}}, 
     ItemSize -> {{{Automatic}}, {{Automatic}}}, 
     Spacings -> {{{2}}, {{Automatic}}}] &,
  gridF0 = Grid[{{##}}, AutoDelete -> False, BaselinePosition -> {1, 1}, 
     Alignment -> {{{Center}}}, ItemSize -> {{{Automatic}}, {{Automatic}}}] &}, 
 Panel[PaneSelector[{False -> 
   gridF0[paneF[sqrplus, True], gridF1[{Row[{#}]} & /@ #]], 
  True -> gridF0[paneF[sqrminus, False], 
    gridF1[{Row[{#}]} & /@ #2]]}, Dynamic[open], 
 ImageSize -> Automatic], BaselinePosition -> Baseline, 
ImageSize -> {200, Automatic}, ImageMargins -> {{0, 0}, {0, 0}}], 
DynamicModuleValues :> {}] &;

Example:

summaryDisplay[{"ClosedForm"}, {"OpenForm", "OpenForm", "OpenForm"}]

enter image description here