Shortcut.cs 25 KB

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