What are the best (most robust and most convenient) ways to create palettes that can be installed permanently (using Palettes -> Install Palette...
) and are safe to use? I'd be interested in how other people have done this in the past to learn more about idiomatic front end programming.
I put some code illustrating the pattern I am using now at the end of this post. I'd appreciate some comments on it.
Notes and requirements:
The palette should always work, regardless of whether the kernel has been quit or an evaluation is running.
The palette may have more than one button which share some code between them.
It must not pollute the
Global`
context or change the kernel state in a way that might break something unexpectedly.I'm looking for an easy way to define palettes (minimal boilerplate code and extra work)
It would be nice (non-essential) if several versions of the palette could coexist independently (my current approach doesn't have this because it uses its own context to hide its function definition, but everything in this context is shared)
It would also be nice (non-essential) to integrate documentation in an easy way (help button bringing it up maybe?)
My current approach is illustrated below. It "localizes" its symbols by putting them in a separate context, and uses DynamicModule
to ensure that all the definitions are done before any button code is run.
SetAttributes[paletteButton, HoldAll]
paletteButton[name_, tooltip_, func_, opt : OptionsPattern[]] :=
Tooltip[Button[name, Unevaluated[func], Appearance -> "Palette",
opt], tooltip, TooltipDelay -> Automatic]
Begin["SomePalette`"];
PaletteNotebook[
DynamicModule[{},
Column[{
paletteButton["One", "Button one", function[1]],
paletteButton["Two", "Button two", function[2]]
}],
Initialization :> (
function[x_] := MessageDialog[x]
)
],
WindowTitle -> "Some Palette"
]
End[];
FileNameJoin[{$UserBaseDirectory, "SystemFiles", "FrontEnd", "Palettes"}]
$\endgroup$