Shortcut.cs 25 KB

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