I saw a Dynamic
snippet in this answer, which wrote
DynamicModule[{font}, Row[{PopupMenu[Dynamic[font], FE`Evaluate[FEPrivate`GetPopupList["MenuListFonts"]]], Spacer[20], Style["The quick brown fox", 20, FontFamily -> font]}]]
Things confusing me most are that I thought the Style[ ... ]
part should be wrapped in a Dynamic[ ... ]
, but to my surprise it doesn't need one in the above code for (at least looks like) working correctly!
So I did some experiments in Mathematica 9:
Working:
DynamicModule[{col},
Row[{
PopupMenu[Dynamic[col], {Red -> 1, Blue -> 2}],
Style["text", FontColor -> col, 20]
}]
]
DynamicModule[{col},
Row[{
PopupMenu[
Dynamic[col], {RGBColor[1, 0, 0] -> 1, RGBColor[0, 0, 1] -> 2}],
"\!\(\*StyleBox[\"\\\"text\\\"\", Rule[FontColor, $CellContext`col$$]]\)"
}]
]
Not Working:
DynamicModule[{col},
Row[{
PopupMenu[Dynamic[col], {Red -> 1, Blue -> 2}],
col
}]
]
DynamicModule[{col},
Row[{
PopupMenu[Dynamic[col], {Red -> 1, Blue -> 2}],
Style["text", col, 20]
}]
]
DynamicModule[{col},
Row[{
PopupMenu[
Dynamic[col], {RGBColor[1, 0, 0] -> 1, RGBColor[0, 0, 1] -> 2}],
"\!\(\*StyleBox[\"\\\"text\\\"\", $CellContext`col$$]\)"
}]
]
Questions:
Why is this
Dynamic
-wrapping-free behavior ofStyle
? My personal suspicion is, a proper form (i.e. with explicit option names) ofStyle[ ... ]
is handled by the FrontEnd. Please correct me if I'm wrong.Should I take advantage of this feature(?), or should I generally avoid it?
DynamicModule[{col}, Row[{PopupMenu[Dynamic[col], {Red->1, Blue->2}], Plot[x^2, {x, -2, 2}, BaseStyle->{FontColor->col}]}]]
-- the color change is off by one, if you toggle back and forth. $\endgroup$