3
$\begingroup$

Both DefaultButton and CancelButton are very convenient, in that they allow one to use FEPrivate`FindAndClickDefaultButton and FEPrivate`FindAndClickCancelButton. Unfortunately DefaultButton doesn't allow me to use custom appearances. How can I get that behavior?

$\endgroup$
  • $\begingroup$ According to the documentation, DefaultButton should have the same options as Button. It seems that the Appearance option should work. You should report to WRI to see if it is a bug. $\endgroup$ – Edmund Aug 8 '17 at 21:10
3
$\begingroup$

The trick is in the Appearance list. I found this here:

FileNameJoin@{
  PacletFind["CloudObject"][[1]]["Location"],
  "Kernel",
  "Dialogs.m"
  }

The appearances use listed default / hover / pressed / disabled appearances a la this question. There's another appearance option called "ButtonType" which can be set to "Cancel" or "Default" to get the desired behavior. e.g.:

Append[
 FrontEndResource["NotebookTemplatingExpressions", 
  "ButtonDefaultAppearance"],
 "ButtonType" -> "Default"
 ]

I use this, for example, here

This works, of course, because CreateDialog sticks this into a dialog by default:

NotebookEventActions -> {"ReturnKeyDown" :> 
   FE`Evaluate[FEPrivate`FindAndClickDefaultButton[]], {"MenuCommand",
     "EvaluateCells"} :> 
   FE`Evaluate[FEPrivate`FindAndClickDefaultButton[]], {"MenuCommand",
     "HandleShiftReturn"} :> 
   FE`Evaluate[FEPrivate`FindAndClickDefaultButton[]], {"MenuCommand",
     "EvaluateNextCell"} :> 
   FE`Evaluate[FEPrivate`FindAndClickDefaultButton[]], 
  "EscapeKeyDown" :> (FE`Evaluate[
     FEPrivate`FindAndClickCancelButton[]]; DialogReturn[$Failed]), 
  "WindowClose" :> (FE`Evaluate[FEPrivate`FindAndClickCancelButton[]];
     DialogReturn[$Failed])}

And then we can imitate that like so:

CreateDocument[
 {
  Hyperlink["Home Page", 
   "https://www.wolframcloud.com/objects/b3m2a1/home/main.html",
   Appearance ->
    {
     "Default" -> None,
     "ButtonType" -> "Default"
     }
   ],
  Hyperlink["Cancel Page", "https://mathematica.stackexchange.com",
   Appearance ->
    {
     "Default" -> None,
     "ButtonType" -> "Cancel"
     }
   ]
  },
 NotebookEventActions -> {"ReturnKeyDown" :> 
    FE`Evaluate[
     FEPrivate`FindAndClickDefaultButton[]], {"MenuCommand", 
     "EvaluateCells"} :> 
    FE`Evaluate[
     FEPrivate`FindAndClickDefaultButton[]], {"MenuCommand", 
     "HandleShiftReturn"} :> 
    FE`Evaluate[
     FEPrivate`FindAndClickDefaultButton[]], {"MenuCommand", 
     "EvaluateNextCell"} :> 
    FE`Evaluate[FEPrivate`FindAndClickDefaultButton[]], 
   "EscapeKeyDown" :> (FE`Evaluate[
      FEPrivate`FindAndClickCancelButton[]]; DialogReturn[$Failed]), 
   "WindowClose" :> (FE`Evaluate[
      FEPrivate`FindAndClickCancelButton[]]; DialogReturn[$Failed])}
 ]

Where just pressing Enter or Cancel presses those Hyperlinks.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.