There is no dynamic controller that can represent a hierarchical menu structure: ActionMenu
can only handle flat action lists. Thanks to Kuba, we know a lot about FrontEnd`AttachCell
(also here), which seems to be exactly the tool for this. However, I have problems making it work correctly in my rudimentary approach:
ClearAll[subMenu];
subMenu[lbl_, top_: False, menu : {___RuleDelayed}] :=
DynamicModule[{parent},
EventHandler[If[TrueQ@top, Panel, Identity]@Grid[{{lbl, "\[FilledRightTriangle]"}}], {"MouseEntered" :> (MathLink`CallFrontEnd[FrontEnd`AttachCell[
parent,
ToBoxes@ExpressionCell[
Panel[Column[
DynamicModule[{over = False},
EventHandler[
Style[First@#,
Background ->
Dynamic@If[over, Hue[.66, .7, .9, .5]]], {
"MouseEntered" :> (over = True),
"MouseExited" :> (over = False;),
"MouseClicked" :> (NotebookDelete@
ParentCell@EvaluationBox[]; Last@#;)
}]] & /@ menu, Alignment -> Left],
FrameMargins -> 2],
StripOnInput -> True],
{Automatic, {Right, Top}},
{Left, Top},
"ClosingActions" -> If[TrueQ@top,
{"SelectionDeparture"(*,"MouseExited","MouseClicked",
"ParentChanged","EvaluatorQuit","OutsideMouseClick"*)},
{"ParentChanged"}]
]])}],
Initialization :> (parent = EvaluationBox[];)];
subMenu[lbl_, lvl_: False, menu_List] := subMenu[lbl, lvl,
If[MatchQ[#, _RuleDelayed | _Rule], #, # :> {}] & /@ menu];
subMenu[lbl_, lvl_: False, menu_] := subMenu[lbl, lvl, {menu}];
And my example structure is:
subMenu["Menu", Top, {
Style["choices", Gray],
subMenu["sub 1",
{"subsub 1.1" :> Print@11, "subsub 1.2" :> Print@12}],
"sub 2" :> Print@2,
subMenu["sub 3",
{"subsub 3.1" :> Print@31, "subsub 3.2" :> Print@32}]
}]
Somehow I couldn't get a grip on how the children should be destroyed. Also, highlighting is only working partime.