Shortcut.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// Displays a command, help text, and a key binding. When the key specified by <see cref="Key"/> is pressed, the
  4. /// command will be invoked. Useful for
  5. /// displaying a command in <see cref="Bar"/> such as a
  6. /// menu, toolbar, or status bar.
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>
  10. /// The following user actions will invoke the <see cref="Command.Accept"/>, causing the
  11. /// <see cref="View.Accept"/> event to be fired:
  12. /// - Clicking on the <see cref="Shortcut"/>.
  13. /// - Pressing the key specified by <see cref="Key"/>.
  14. /// - Pressing the HotKey specified by <see cref="CommandView"/>.
  15. /// </para>
  16. /// <para>
  17. /// If <see cref="KeyBindingScope"/> is <see cref="KeyBindingScope.Application"/>, <see cref="Key"/> will invoked
  18. /// <see cref="Command.Accept"/>
  19. /// command regardless of what View has focus, enabling an application-wide keyboard shortcut.
  20. /// </para>
  21. /// <para>
  22. /// By default, a Shortcut displays the command text on the left side, the help text in the middle, and the key
  23. /// binding on the
  24. /// right side. Set <see cref="AlignmentModes"/> to <see cref="AlignmentModes.EndToStart"/> to reverse the order.
  25. /// </para>
  26. /// <para>
  27. /// The command text can be set by setting the <see cref="CommandView"/>'s Text property or by setting
  28. /// <see cref="View.Title"/>.
  29. /// </para>
  30. /// <para>
  31. /// The help text can be set by setting the <see cref="HelpText"/> property or by setting <see cref="View.Text"/>.
  32. /// </para>
  33. /// <para>
  34. /// The key text is set by setting the <see cref="Key"/> property.
  35. /// If the <see cref="Key"/> is <see cref="Key.Empty"/>, the <see cref="Key"/> text is not displayed.
  36. /// </para>
  37. /// </remarks>
  38. public class Shortcut : View, IOrientation, IDesignable
  39. {
  40. /// <summary>
  41. /// Creates a new instance of <see cref="Shortcut"/>.
  42. /// </summary>
  43. /// <remarks>
  44. /// <para>
  45. /// This is a helper API that mimics the V1 API for creating StatusItems.
  46. /// </para>
  47. /// </remarks>
  48. /// <param name="key"></param>
  49. /// <param name="commandText"></param>
  50. /// <param name="action"></param>
  51. /// <param name="helpText"></param>
  52. public Shortcut (Key key, string commandText, Action action, string helpText = null)
  53. {
  54. Id = "_shortcut";
  55. HighlightStyle = HighlightStyle.Pressed;
  56. CanFocus = true;
  57. Width = GetWidthDimAuto ();
  58. Height = Dim.Auto (DimAutoStyle.Content, 1);
  59. _orientationHelper = new (this);
  60. _orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e);
  61. _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e);
  62. AddCommand (Command.HotKey, ctx => OnAccept (ctx));
  63. AddCommand (Command.Accept, ctx => OnAccept (ctx));
  64. AddCommand (Command.Select, ctx => OnSelect (ctx));
  65. TitleChanged += Shortcut_TitleChanged; // This needs to be set before CommandView is set
  66. CommandView = new ()
  67. {
  68. Width = Dim.Auto (),
  69. Height = Dim.Auto (DimAutoStyle.Auto, minimumContentDim: 1)
  70. };
  71. HelpView.Id = "_helpView";
  72. HelpView.CanFocus = false;
  73. HelpView.Text = helpText;
  74. Add (HelpView);
  75. KeyView.Id = "_keyView";
  76. KeyView.CanFocus = false;
  77. Add (KeyView);
  78. // If the user clicks anywhere on the Shortcut, other than the CommandView, invoke the Command
  79. MouseClick += Shortcut_MouseClick;
  80. HelpView.MouseClick += Subview_MouseClick;
  81. KeyView.MouseClick += Subview_MouseClick;
  82. LayoutStarted += OnLayoutStarted;
  83. Initialized += OnInitialized;
  84. if (key is null)
  85. {
  86. key = Key.Empty;
  87. }
  88. Key = key;
  89. Title = commandText;
  90. Action = action;
  91. return;
  92. void OnInitialized (object sender, EventArgs e)
  93. {
  94. SuperViewRendersLineCanvas = true;
  95. Border.Settings &= ~BorderSettings.Title;
  96. ShowHide ();
  97. // Force Width to DimAuto to calculate natural width and then set it back
  98. Dim savedDim = Width;
  99. Width = GetWidthDimAuto ();
  100. _minimumDimAutoWidth = Frame.Width;
  101. Width = savedDim;
  102. SetCommandViewDefaultLayout ();
  103. SetHelpViewDefaultLayout ();
  104. SetKeyViewDefaultLayout ();
  105. SetColors ();
  106. }
  107. // Helper to set Width consistently
  108. Dim GetWidthDimAuto ()
  109. {
  110. // TODO: PosAlign.CalculateMinDimension is a hack. Need to figure out a better way of doing this.
  111. return Dim.Auto (
  112. DimAutoStyle.Content,
  113. Dim.Func (() => PosAlign.CalculateMinDimension (0, Subviews, Dimension.Width)),
  114. Dim.Func (() => PosAlign.CalculateMinDimension (0, Subviews, Dimension.Width)));
  115. }
  116. }
  117. /// <summary>
  118. /// Creates a new instance of <see cref="Shortcut"/>.
  119. /// </summary>
  120. public Shortcut () : this (Key.Empty, string.Empty, null) { }
  121. private readonly OrientationHelper _orientationHelper;
  122. private AlignmentModes _alignmentModes = AlignmentModes.StartToEnd | AlignmentModes.IgnoreFirstOrLast;
  123. // This is used to calculate the minimum width of the Shortcut when the width is NOT Dim.Auto
  124. private int? _minimumDimAutoWidth;
  125. /// <inheritdoc />
  126. protected override bool OnHighlight (CancelEventArgs<HighlightStyle> args)
  127. {
  128. if (args.NewValue.HasFlag (HighlightStyle.Hover))
  129. {
  130. HasFocus = true;
  131. }
  132. return true;
  133. }
  134. /// <inheritdoc/>
  135. public bool EnableForDesign ()
  136. {
  137. Title = "_Shortcut";
  138. HelpText = "Shortcut help";
  139. Key = Key.F1;
  140. return true;
  141. }
  142. /// <summary>
  143. /// Gets or sets the <see cref="AlignmentModes"/> for this <see cref="Shortcut"/>.
  144. /// </summary>
  145. /// <remarks>
  146. /// <para>
  147. /// The default is <see cref="AlignmentModes.StartToEnd"/>. This means that the CommandView will be on the left,
  148. /// HelpView in the middle, and KeyView on the right.
  149. /// </para>
  150. /// </remarks>
  151. public AlignmentModes AlignmentModes
  152. {
  153. get => _alignmentModes;
  154. set
  155. {
  156. _alignmentModes = value;
  157. SetCommandViewDefaultLayout ();
  158. SetHelpViewDefaultLayout ();
  159. SetKeyViewDefaultLayout ();
  160. }
  161. }
  162. /// <inheritdoc/>
  163. protected override void Dispose (bool disposing)
  164. {
  165. if (disposing)
  166. {
  167. if (CommandView?.IsAdded == false)
  168. {
  169. CommandView.Dispose ();
  170. }
  171. if (HelpView?.IsAdded == false)
  172. {
  173. HelpView.Dispose ();
  174. }
  175. if (KeyView?.IsAdded == false)
  176. {
  177. KeyView.Dispose ();
  178. }
  179. }
  180. base.Dispose (disposing);
  181. }
  182. // When one of the subviews is "empty" we don't want to show it. So we
  183. // Use Add/Remove. We need to be careful to add them in the right order
  184. // so Pos.Align works correctly.
  185. internal void ShowHide ()
  186. {
  187. RemoveAll ();
  188. if (CommandView.Visible)
  189. {
  190. Add (CommandView);
  191. }
  192. if (HelpView.Visible && !string.IsNullOrEmpty (HelpView.Text))
  193. {
  194. Add (HelpView);
  195. }
  196. if (KeyView.Visible && Key != Key.Empty)
  197. {
  198. Add (KeyView);
  199. }
  200. }
  201. private Thickness GetMarginThickness ()
  202. {
  203. if (Orientation == Orientation.Vertical)
  204. {
  205. return new (1, 0, 1, 0);
  206. }
  207. return new (1, 0, 1, 0);
  208. }
  209. // When layout starts, we need to adjust the layout of the HelpView and KeyView
  210. private void OnLayoutStarted (object sender, LayoutEventArgs e)
  211. {
  212. if (Width is DimAuto widthAuto)
  213. {
  214. _minimumDimAutoWidth = Frame.Width;
  215. }
  216. else
  217. {
  218. if (string.IsNullOrEmpty (HelpView.Text))
  219. {
  220. return;
  221. }
  222. int currentWidth = Frame.Width;
  223. // If our width is smaller than the natural width then reduce width of HelpView first.
  224. // Then KeyView.
  225. // Don't ever reduce CommandView (it should spill).
  226. // When Horizontal, Key is first, then Help, then Command.
  227. // When Vertical, Command is first, then Help, then Key.
  228. // BUGBUG: This does not do what the above says.
  229. // TODO: Add Unit tests for this.
  230. if (currentWidth < _minimumDimAutoWidth)
  231. {
  232. int delta = _minimumDimAutoWidth.Value - currentWidth;
  233. int maxHelpWidth = int.Max (0, HelpView.Text.GetColumns () + Margin.Thickness.Horizontal - delta);
  234. switch (maxHelpWidth)
  235. {
  236. case 0:
  237. // Hide HelpView
  238. HelpView.Visible = false;
  239. HelpView.X = 0;
  240. break;
  241. case 1:
  242. // Scrunch it by removing margins
  243. HelpView.Margin.Thickness = new (0, 0, 0, 0);
  244. break;
  245. case 2:
  246. // Scrunch just the right margin
  247. Thickness t = GetMarginThickness ();
  248. HelpView.Margin.Thickness = new (t.Right, t.Top, t.Left - 1, t.Bottom);
  249. break;
  250. default:
  251. // Default margin
  252. HelpView.Margin.Thickness = GetMarginThickness ();
  253. break;
  254. }
  255. if (maxHelpWidth > 0)
  256. {
  257. HelpView.X = Pos.Align (Alignment.End, AlignmentModes);
  258. // Leverage Dim.Auto's max:
  259. HelpView.Width = Dim.Auto (DimAutoStyle.Text, maximumContentDim: maxHelpWidth);
  260. HelpView.Visible = true;
  261. }
  262. }
  263. else
  264. {
  265. // Reset to default
  266. //SetCommandViewDefaultLayout();
  267. SetHelpViewDefaultLayout ();
  268. //SetKeyViewDefaultLayout ();
  269. }
  270. }
  271. }
  272. private bool? OnSelect (CommandContext ctx)
  273. {
  274. if (CommandView.GetSupportedCommands ().Contains (Command.Select))
  275. {
  276. return CommandView.InvokeCommand (Command.Select, ctx.Key, ctx.KeyBinding);
  277. }
  278. return false;
  279. }
  280. private void Shortcut_MouseClick (object sender, MouseEventEventArgs e)
  281. {
  282. // When the Shortcut is clicked, we want to invoke the Command and Set focus
  283. var view = sender as View;
  284. if (!e.Handled)
  285. {
  286. // If the subview (likely CommandView) didn't handle the mouse click, invoke the command.
  287. e.Handled = InvokeCommand (Command.Accept) == true;
  288. }
  289. if (CanFocus)
  290. {
  291. SetFocus ();
  292. }
  293. }
  294. private void Subview_MouseClick (object sender, MouseEventEventArgs e)
  295. {
  296. // TODO: Remove. This does nothing.
  297. }
  298. #region IOrientation members
  299. /// <summary>
  300. /// Gets or sets the <see cref="Orientation"/> for this <see cref="Bar"/>. The default is
  301. /// <see cref="Orientation.Horizontal"/>.
  302. /// </summary>
  303. /// <remarks>
  304. /// <para>
  305. /// Horizontal orientation arranges the command, help, and key parts of each <see cref="Shortcut"/>s from right to
  306. /// left
  307. /// Vertical orientation arranges the command, help, and key parts of each <see cref="Shortcut"/>s from left to
  308. /// right.
  309. /// </para>
  310. /// </remarks>
  311. public Orientation Orientation
  312. {
  313. get => _orientationHelper.Orientation;
  314. set => _orientationHelper.Orientation = value;
  315. }
  316. /// <inheritdoc/>
  317. public event EventHandler<CancelEventArgs<Orientation>> OrientationChanging;
  318. /// <inheritdoc/>
  319. public event EventHandler<EventArgs<Orientation>> OrientationChanged;
  320. /// <summary>Called when <see cref="Orientation"/> has changed.</summary>
  321. /// <param name="newOrientation"></param>
  322. public void OnOrientationChanged (Orientation newOrientation)
  323. {
  324. // TODO: Determine what, if anything, is opinionated about the orientation.
  325. SetNeedsLayout ();
  326. }
  327. #endregion
  328. #region Command
  329. private View _commandView = new ();
  330. /// <summary>
  331. /// Gets or sets the View that displays the command text and hotkey.
  332. /// </summary>
  333. /// <remarks>
  334. /// <para>
  335. /// By default, the <see cref="View.Title"/> of the <see cref="CommandView"/> is displayed as the Shortcut's
  336. /// command text.
  337. /// </para>
  338. /// <para>
  339. /// By default, the CommandView is a <see cref="View"/> with <see cref="View.CanFocus"/> set to
  340. /// <see langword="false"/>.
  341. /// </para>
  342. /// <para>
  343. /// Setting the <see cref="CommandView"/> will add it to the <see cref="Shortcut"/> and remove any existing
  344. /// <see cref="CommandView"/>.
  345. /// </para>
  346. /// </remarks>
  347. /// <example>
  348. /// <para>
  349. /// This example illustrates how to add a <see cref="Shortcut"/> to a <see cref="StatusBar"/> that toggles the
  350. /// <see cref="Application.Force16Colors"/> property.
  351. /// </para>
  352. /// <code>
  353. /// var force16ColorsShortcut = new Shortcut
  354. /// {
  355. /// Key = Key.F6,
  356. /// KeyBindingScope = KeyBindingScope.HotKey,
  357. /// CommandView = new CheckBox { Text = "Force 16 Colors" }
  358. /// };
  359. /// var cb = force16ColorsShortcut.CommandView as CheckBox;
  360. /// cb.Checked = Application.Force16Colors;
  361. ///
  362. /// cb.Toggled += (s, e) =>
  363. /// {
  364. /// var cb = s as CheckBox;
  365. /// Application.Force16Colors = cb!.Checked == true;
  366. /// Application.Refresh();
  367. /// };
  368. /// StatusBar.Add(force16ColorsShortcut);
  369. /// </code>
  370. /// </example>
  371. public View CommandView
  372. {
  373. get => _commandView;
  374. set
  375. {
  376. if (value == null)
  377. {
  378. throw new ArgumentNullException ();
  379. }
  380. if (_commandView is { })
  381. {
  382. Remove (_commandView);
  383. _commandView?.Dispose ();
  384. }
  385. _commandView = value;
  386. _commandView.Id = "_commandView";
  387. // The default behavior is for CommandView to not get focus. I
  388. // If you want it to get focus, you need to set it.
  389. _commandView.CanFocus = false;
  390. _commandView.HotKeyChanged += (s, e) =>
  391. {
  392. if (e.NewKey != Key.Empty)
  393. {
  394. // Add it
  395. AddKeyBindingsForHotKey (e.OldKey, e.NewKey);
  396. }
  397. };
  398. _commandView.HotKeySpecifier = new ('_');
  399. Title = _commandView.Text;
  400. SetCommandViewDefaultLayout ();
  401. SetHelpViewDefaultLayout ();
  402. SetKeyViewDefaultLayout ();
  403. ShowHide ();
  404. UpdateKeyBinding (Key.Empty);
  405. }
  406. }
  407. private void SetCommandViewDefaultLayout ()
  408. {
  409. CommandView.Margin.Thickness = GetMarginThickness ();
  410. CommandView.X = Pos.Align (Alignment.End, AlignmentModes);
  411. CommandView.Y = 0; //Pos.Center ();
  412. HelpView.HighlightStyle = HighlightStyle.None;
  413. }
  414. private void Shortcut_TitleChanged (object sender, EventArgs<string> e)
  415. {
  416. // If the Title changes, update the CommandView text.
  417. // This is a helper to make it easier to set the CommandView text.
  418. // CommandView is public and replaceable, but this is a convenience.
  419. _commandView.Text = Title;
  420. }
  421. #endregion Command
  422. #region Help
  423. /// <summary>
  424. /// The subview that displays the help text for the command. Internal for unit testing.
  425. /// </summary>
  426. internal View HelpView { get; } = new ();
  427. private void SetHelpViewDefaultLayout ()
  428. {
  429. HelpView.Margin.Thickness = GetMarginThickness ();
  430. HelpView.X = Pos.Align (Alignment.End, AlignmentModes);
  431. HelpView.Y = 0; //Pos.Center ();
  432. HelpView.Width = Dim.Auto (DimAutoStyle.Text);
  433. HelpView.Height = CommandView?.Visible == true ? Dim.Height (CommandView) : 1;
  434. HelpView.Visible = true;
  435. HelpView.VerticalTextAlignment = Alignment.Center;
  436. HelpView.HighlightStyle = HighlightStyle.None;
  437. }
  438. /// <summary>
  439. /// Gets or sets the help text displayed in the middle of the Shortcut. Identical in function to <see cref="HelpText"/>
  440. /// .
  441. /// </summary>
  442. public override string Text
  443. {
  444. get => HelpView?.Text;
  445. set
  446. {
  447. if (HelpView is { })
  448. {
  449. HelpView.Text = value;
  450. ShowHide ();
  451. }
  452. }
  453. }
  454. /// <summary>
  455. /// Gets or sets the help text displayed in the middle of the Shortcut.
  456. /// </summary>
  457. public string HelpText
  458. {
  459. get => HelpView?.Text;
  460. set
  461. {
  462. if (HelpView is { })
  463. {
  464. HelpView.Text = value;
  465. ShowHide ();
  466. }
  467. }
  468. }
  469. #endregion Help
  470. #region Key
  471. private Key _key = Key.Empty;
  472. /// <summary>
  473. /// Gets or sets the <see cref="Key"/> that will be bound to the <see cref="Command.Accept"/> command.
  474. /// </summary>
  475. public Key Key
  476. {
  477. get => _key;
  478. set
  479. {
  480. if (value == null)
  481. {
  482. throw new ArgumentNullException ();
  483. }
  484. Key oldKey = _key;
  485. _key = value;
  486. UpdateKeyBinding (oldKey);
  487. KeyView.Text = Key == Key.Empty ? string.Empty : $"{Key}";
  488. ShowHide ();
  489. }
  490. }
  491. private KeyBindingScope _keyBindingScope = KeyBindingScope.HotKey;
  492. /// <summary>
  493. /// Gets or sets the scope for the key binding for how <see cref="Key"/> is bound to <see cref="Command"/>.
  494. /// </summary>
  495. public KeyBindingScope KeyBindingScope
  496. {
  497. get => _keyBindingScope;
  498. set
  499. {
  500. if (value == _keyBindingScope)
  501. {
  502. return;
  503. }
  504. if (_keyBindingScope == KeyBindingScope.Application)
  505. {
  506. Application.KeyBindings.Remove (Key);
  507. }
  508. if (_keyBindingScope is KeyBindingScope.HotKey or KeyBindingScope.Focused)
  509. {
  510. KeyBindings.Remove (Key);
  511. }
  512. _keyBindingScope = value;
  513. UpdateKeyBinding (Key.Empty);
  514. }
  515. }
  516. /// <summary>
  517. /// Gets the subview that displays the key. Internal for unit testing.
  518. /// </summary>
  519. internal View KeyView { get; } = new ();
  520. private int _minimumKeyTextSize;
  521. /// <summary>
  522. /// Gets or sets the minimum size of the key text. Useful for aligning the key text with other <see cref="Shortcut"/>s.
  523. /// </summary>
  524. public int MinimumKeyTextSize
  525. {
  526. get => _minimumKeyTextSize;
  527. set
  528. {
  529. if (value == _minimumKeyTextSize)
  530. {
  531. //return;
  532. }
  533. _minimumKeyTextSize = value;
  534. SetKeyViewDefaultLayout ();
  535. CommandView.SetNeedsLayout ();
  536. HelpView.SetNeedsLayout ();
  537. KeyView.SetNeedsLayout ();
  538. SetSubViewNeedsDisplay ();
  539. }
  540. }
  541. private int GetMinimumKeyViewSize () { return MinimumKeyTextSize; }
  542. private void SetKeyViewDefaultLayout ()
  543. {
  544. KeyView.Margin.Thickness = GetMarginThickness ();
  545. KeyView.X = Pos.Align (Alignment.End, AlignmentModes);
  546. KeyView.Y = 0; //Pos.Center ();
  547. KeyView.Width = Dim.Auto (DimAutoStyle.Text, Dim.Func (GetMinimumKeyViewSize));
  548. KeyView.Height = CommandView?.Visible == true ? Dim.Height (CommandView) : 1;
  549. KeyView.Visible = true;
  550. // Right align the text in the keyview
  551. KeyView.TextAlignment = Alignment.End;
  552. KeyView.VerticalTextAlignment = Alignment.Center;
  553. KeyView.KeyBindings.Clear ();
  554. HelpView.HighlightStyle = HighlightStyle.None;
  555. }
  556. private void UpdateKeyBinding (Key oldKey)
  557. {
  558. if (Key != null && Key.IsValid)
  559. {
  560. // Disable the command view key bindings
  561. CommandView.KeyBindings.Remove (Key);
  562. CommandView.KeyBindings.Remove (CommandView.HotKey);
  563. if (KeyBindingScope.FastHasFlags (KeyBindingScope.Application))
  564. {
  565. if (oldKey != Key.Empty)
  566. {
  567. Application.KeyBindings.Remove (oldKey);
  568. }
  569. Application.KeyBindings.Remove (Key);
  570. Application.KeyBindings.Add (Key, this, Command.Accept);
  571. }
  572. else
  573. {
  574. if (oldKey != Key.Empty)
  575. {
  576. KeyBindings.Remove (oldKey);
  577. }
  578. KeyBindings.Remove (Key);
  579. KeyBindings.Add (Key, KeyBindingScope | KeyBindingScope.HotKey, Command.Accept);
  580. }
  581. }
  582. }
  583. #endregion Key
  584. #region Accept Handling
  585. /// <summary>
  586. /// Called when the <see cref="Command.Accept"/> command is received. This
  587. /// occurs
  588. /// - if the user clicks anywhere on the shortcut with the mouse
  589. /// - if the user presses Key
  590. /// - if the user presses the HotKey specified by CommandView
  591. /// - if HasFocus and the user presses Space or Enter (or any other key bound to Command.Accept).
  592. /// </summary>
  593. protected bool? OnAccept (CommandContext ctx)
  594. {
  595. var cancel = false;
  596. switch (ctx.KeyBinding?.Scope)
  597. {
  598. case KeyBindingScope.Application:
  599. cancel = base.RaiseAcceptEvent () == true;
  600. break;
  601. case KeyBindingScope.Focused:
  602. base.RaiseAcceptEvent ();
  603. // cancel if we're focused
  604. cancel = true;
  605. break;
  606. case KeyBindingScope.HotKey:
  607. //if (!CanBeVisible(this))
  608. //{
  609. // return true;
  610. //}
  611. cancel = base.RaiseAcceptEvent () == true;
  612. if (CanFocus)
  613. {
  614. SetFocus ();
  615. cancel = true;
  616. }
  617. break;
  618. default:
  619. // Mouse
  620. cancel = base.RaiseAcceptEvent () == true;
  621. break;
  622. }
  623. CommandView.InvokeCommand (Command.Accept, ctx.Key, ctx.KeyBinding);
  624. if (Action is { })
  625. {
  626. Action.Invoke ();
  627. // Assume if there's a subscriber to Action, it's handled.
  628. cancel = true;
  629. }
  630. return cancel;
  631. }
  632. /// <summary>
  633. /// Gets or sets the action to be invoked when the shortcut key is pressed or the shortcut is clicked on with the
  634. /// mouse.
  635. /// </summary>
  636. /// <remarks>
  637. /// Note, the <see cref="View.Accept"/> event is fired first, and if cancelled, the event will not be invoked.
  638. /// </remarks>
  639. [CanBeNull]
  640. public Action Action { get; set; }
  641. #endregion Accept Handling
  642. #region Focus
  643. /// <inheritdoc/>
  644. public override ColorScheme ColorScheme
  645. {
  646. get => base.ColorScheme;
  647. set
  648. {
  649. base.ColorScheme = value;
  650. SetColors ();
  651. }
  652. }
  653. /// <summary>
  654. /// </summary>
  655. internal void SetColors (bool highlight = false)
  656. {
  657. // Border should match superview.
  658. Border.ColorScheme = SuperView?.ColorScheme;
  659. if (HasFocus || highlight)
  660. {
  661. base.ColorScheme ??= new (Attribute.Default);
  662. // When we have focus, we invert the colors
  663. base.ColorScheme = new (base.ColorScheme)
  664. {
  665. Normal = base.ColorScheme.Focus,
  666. HotNormal = base.ColorScheme.HotFocus,
  667. HotFocus = base.ColorScheme.HotNormal,
  668. Focus = base.ColorScheme.Normal
  669. };
  670. }
  671. else
  672. {
  673. base.ColorScheme = SuperView?.ColorScheme ?? base.ColorScheme;
  674. }
  675. // Set KeyView's colors to show "hot"
  676. if (IsInitialized && base.ColorScheme is { })
  677. {
  678. var cs = new ColorScheme (base.ColorScheme)
  679. {
  680. Normal = base.ColorScheme.HotNormal,
  681. HotNormal = base.ColorScheme.Normal
  682. };
  683. KeyView.ColorScheme = cs;
  684. }
  685. }
  686. /// <inheritdoc/>
  687. protected override void OnHasFocusChanged (bool newHasFocus, View previousFocusedView, View view)
  688. {
  689. SetColors ();
  690. }
  691. #endregion Focus
  692. }