View.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. #nullable enable
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. namespace Terminal.Gui;
  5. #region API Docs
  6. /// <summary>
  7. /// View is the base class for all views on the screen and represents a visible element that can render itself and
  8. /// contains zero or more nested views, called SubViews. View provides basic functionality for layout, positioning, and
  9. /// drawing. In addition, View provides keyboard and mouse event handling.
  10. /// </summary>
  11. /// <remarks>
  12. /// <list type="table">
  13. /// <listheader>
  14. /// <term>Term</term><description>Definition</description>
  15. /// </listheader>
  16. /// <item>
  17. /// <term>SubView</term>
  18. /// <description>
  19. /// A View that is contained in another view and will be rendered as part of the containing view's
  20. /// ContentArea. SubViews are added to another view via the <see cref="View.Add(View)"/>` method. A View
  21. /// may only be a SubView of a single View.
  22. /// </description>
  23. /// </item>
  24. /// <item>
  25. /// <term>SuperView</term><description>The View that is a container for SubViews.</description>
  26. /// </item>
  27. /// </list>
  28. /// <para>
  29. /// Focus is a concept that is used to describe which View is currently receiving user input. Only Views that are
  30. /// <see cref="Enabled"/>, <see cref="Visible"/>, and <see cref="CanFocus"/> will receive focus.
  31. /// </para>
  32. /// <para>
  33. /// Views that are focusable should override <see cref="PositionCursor"/> to make sure that the cursor is
  34. /// placed in a location that makes sense. Some terminals do not have a way of hiding the cursor, so it can be
  35. /// distracting to have the cursor left at the last focused view. So views should make sure that they place the
  36. /// cursor in a visually sensible place. The default implementation of <see cref="PositionCursor"/> will place the
  37. /// cursor at either the hotkey (if defined) or <c>0,0</c>.
  38. /// </para>
  39. /// <para>
  40. /// The View defines the base functionality for user interface elements in Terminal.Gui. Views can contain one or
  41. /// more subviews, can respond to user input and render themselves on the screen.
  42. /// </para>
  43. /// <para>
  44. /// To create a View using Absolute layout, call a constructor that takes a Rect parameter to specify the
  45. /// absolute position and size or simply set <see cref="View.Frame "/>). To create a View using Computed layout use
  46. /// a constructor that does not take a Rect parameter and set the X, Y, Width and Height properties on the view to
  47. /// non-absolute values. Both approaches use coordinates that are relative to the <see cref="Viewport"/> of the
  48. /// <see cref="SuperView"/> the View is added to.
  49. /// </para>
  50. /// <para>
  51. /// Computed layout is more flexible and supports dynamic console apps where controls adjust layout as the
  52. /// terminal resizes or other Views change size or position. The <see cref="X"/>, <see cref="Y"/>,
  53. /// <see cref="Width"/>, and <see cref="Height"/> properties are <see cref="Dim"/> and <see cref="Pos"/> objects
  54. /// that dynamically update the position of a view. The X and Y properties are of type <see cref="Pos"/> and you
  55. /// can use either absolute positions, percentages, or anchor points. The Width and Height properties are of type
  56. /// <see cref="Dim"/> and can use absolute position, percentages, and anchors. These are useful as they will take
  57. /// care of repositioning views when view's adornments are resized or if the terminal size changes.
  58. /// </para>
  59. /// <para>
  60. /// Absolute layout requires specifying coordinates and sizes of Views explicitly, and the View will typically
  61. /// stay in a fixed position and size. To change the position and size use the <see cref="Frame"/> property.
  62. /// </para>
  63. /// <para>
  64. /// Subviews (child views) can be added to a View by calling the <see cref="Add(View)"/> method. The container of
  65. /// a View can be accessed with the <see cref="SuperView"/> property.
  66. /// </para>
  67. /// <para>
  68. /// To flag a region of the View's <see cref="Viewport"/> to be redrawn call
  69. /// <see cref="SetNeedsDisplay(Rectangle)"/>
  70. /// .
  71. /// To flag the entire view for redraw call <see cref="SetNeedsDisplay()"/>.
  72. /// </para>
  73. /// <para>
  74. /// The <see cref="LayoutSubviews"/> method is invoked when the size or layout of a view has changed.
  75. /// </para>
  76. /// <para>
  77. /// Views have a <see cref="ColorScheme"/> property that defines the default colors that subviews should use for
  78. /// rendering. This ensures that the views fit in the context where they are being used, and allows for themes to
  79. /// be plugged in. For example, the default colors for windows and Toplevels uses a blue background, while it uses
  80. /// a white background for dialog boxes and a red background for errors.
  81. /// </para>
  82. /// <para>
  83. /// Subclasses should not rely on <see cref="ColorScheme"/> being set at construction time. If a
  84. /// <see cref="ColorScheme"/> is not set on a view, the view will inherit the value from its
  85. /// <see cref="SuperView"/> and the value might only be valid once a view has been added to a SuperView.
  86. /// </para>
  87. /// <para>By using <see cref="ColorScheme"/> applications will work both in color as well as black and white displays.</para>
  88. /// <para>
  89. /// Views can also opt-in to more sophisticated initialization by implementing overrides to
  90. /// <see cref="ISupportInitialize.BeginInit"/> and <see cref="ISupportInitialize.EndInit"/> which will be called
  91. /// when the view is added to a <see cref="SuperView"/>.
  92. /// </para>
  93. /// <para>
  94. /// If first-run-only initialization is preferred, overrides to <see cref="ISupportInitializeNotification"/> can
  95. /// be implemented, in which case the <see cref="ISupportInitialize"/> methods will only be called if
  96. /// <see cref="ISupportInitializeNotification.IsInitialized"/> is <see langword="false"/>. This allows proper
  97. /// <see cref="View"/> inheritance hierarchies to override base class layout code optimally by doing so only on
  98. /// first run, instead of on every run.
  99. /// </para>
  100. /// <para>See <see href="../docs/keyboard.md">for an overview of View keyboard handling.</see></para>
  101. /// ///
  102. /// </remarks>
  103. #endregion API Docs
  104. public partial class View : Responder, ISupportInitializeNotification
  105. {
  106. #region Constructors and Initialization
  107. /// <summary>Gets or sets arbitrary data for the view.</summary>
  108. /// <remarks>This property is not used internally.</remarks>
  109. public object? Data { get; set; }
  110. /// <summary>Gets or sets an identifier for the view;</summary>
  111. /// <value>The identifier.</value>
  112. /// <remarks>The id should be unique across all Views that share a SuperView.</remarks>
  113. public string Id { get; set; } = "";
  114. /// <summary>
  115. /// Points to the current driver in use by the view, it is a convenience property for simplifying the development
  116. /// of new views.
  117. /// </summary>
  118. public static ConsoleDriver Driver => Application.Driver!;
  119. /// <summary>Initializes a new instance of <see cref="View"/>.</summary>
  120. /// <remarks>
  121. /// <para>
  122. /// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically
  123. /// control the size and location of the view.
  124. /// </para>
  125. /// </remarks>
  126. public View ()
  127. {
  128. SetupAdornments ();
  129. SetupKeyboard ();
  130. //SetupMouse ();
  131. SetupText ();
  132. // By default, the Select command does nothing
  133. AddCommand (Command.Select, RaiseSelectEvent);
  134. // By default, the HotKey command sets the focus
  135. AddCommand (Command.HotKey, RaiseHotKeyEvent);
  136. // By default, the Accept command sets the focus
  137. AddCommand (Command.Accept, RaiseAcceptEvent);
  138. }
  139. /// <summary>
  140. /// Event called only once when the <see cref="View"/> is being initialized for the first time. Allows
  141. /// configurations and assignments to be performed before the <see cref="View"/> being shown.
  142. /// View implements <see cref="ISupportInitializeNotification"/> to allow for more sophisticated initialization.
  143. /// </summary>
  144. public event EventHandler? Initialized;
  145. /// <summary>
  146. /// Get or sets if the <see cref="View"/> has been initialized (via <see cref="ISupportInitialize.BeginInit"/>
  147. /// and <see cref="ISupportInitialize.EndInit"/>).
  148. /// </summary>
  149. /// <para>
  150. /// If first-run-only initialization is preferred, overrides to
  151. /// <see cref="ISupportInitializeNotification.IsInitialized"/> can be implemented, in which case the
  152. /// <see cref="ISupportInitialize"/> methods will only be called if
  153. /// <see cref="ISupportInitializeNotification.IsInitialized"/> is <see langword="false"/>. This allows proper
  154. /// <see cref="View"/> inheritance hierarchies to override base class layout code optimally by doing so only on first
  155. /// run, instead of on every run.
  156. /// </para>
  157. public virtual bool IsInitialized { get; set; }
  158. /// <summary>Signals the View that initialization is starting. See <see cref="ISupportInitialize"/>.</summary>
  159. /// <remarks>
  160. /// <para>
  161. /// Views can opt-in to more sophisticated initialization by implementing overrides to
  162. /// <see cref="ISupportInitialize.BeginInit"/> and <see cref="ISupportInitialize.EndInit"/> which will be called
  163. /// when the <see cref="SuperView"/> is initialized.
  164. /// </para>
  165. /// <para>
  166. /// If first-run-only initialization is preferred, overrides to <see cref="ISupportInitializeNotification"/> can
  167. /// be implemented too, in which case the <see cref="ISupportInitialize"/> methods will only be called if
  168. /// <see cref="ISupportInitializeNotification.IsInitialized"/> is <see langword="false"/>. This allows proper
  169. /// <see cref="View"/> inheritance hierarchies to override base class layout code optimally by doing so only on
  170. /// first run, instead of on every run.
  171. /// </para>
  172. /// </remarks>
  173. public virtual void BeginInit ()
  174. {
  175. if (IsInitialized)
  176. {
  177. throw new InvalidOperationException ("The view is already initialized.");
  178. }
  179. #if AUTO_CANFOCUS
  180. _oldCanFocus = CanFocus;
  181. _oldTabIndex = _tabIndex;
  182. #endif
  183. BeginInitAdornments ();
  184. if (_subviews?.Count > 0)
  185. {
  186. foreach (View view in _subviews)
  187. {
  188. if (!view.IsInitialized)
  189. {
  190. view.BeginInit ();
  191. }
  192. }
  193. }
  194. }
  195. // TODO: Implement logic that allows EndInit to throw if BeginInit has not been called
  196. // TODO: See EndInit_Called_Without_BeginInit_Throws test.
  197. /// <summary>Signals the View that initialization is ending. See <see cref="ISupportInitialize"/>.</summary>
  198. /// <remarks>
  199. /// <para>Initializes all Subviews and Invokes the <see cref="Initialized"/> event.</para>
  200. /// </remarks>
  201. public virtual void EndInit ()
  202. {
  203. if (IsInitialized)
  204. {
  205. throw new InvalidOperationException ("The view is already initialized.");
  206. }
  207. IsInitialized = true;
  208. EndInitAdornments ();
  209. // TODO: Move these into ViewText.cs as EndInit_Text() to consolidate.
  210. // TODO: Verify UpdateTextDirection really needs to be called here.
  211. // These calls were moved from BeginInit as they access Viewport which is indeterminate until EndInit is called.
  212. UpdateTextDirection (TextDirection);
  213. UpdateTextFormatterText ();
  214. OnResizeNeeded ();
  215. if (_subviews is { })
  216. {
  217. foreach (View view in _subviews)
  218. {
  219. if (!view.IsInitialized)
  220. {
  221. view.EndInit ();
  222. }
  223. }
  224. }
  225. Initialized?.Invoke (this, EventArgs.Empty);
  226. }
  227. #endregion Constructors and Initialization
  228. #region Base Command Events
  229. /// <summary>
  230. /// Called when the <see cref="Command.Accept"/> command is invoked. Raises <see cref="Accept"/>
  231. /// event.
  232. /// </summary>
  233. /// <returns>
  234. /// If <see langword="true"/> the event was canceled. If <see langword="false"/> the event was raised but not canceled.
  235. /// If <see langword="null"/> no event was raised.
  236. /// </returns>
  237. protected bool? RaiseAcceptEvent ()
  238. {
  239. HandledEventArgs args = new ();
  240. // Best practice is to invoke the virtual method first.
  241. // This allows derived classes to handle the event and potentially cancel it.
  242. if (OnAccept (args) || args.Handled)
  243. {
  244. return true;
  245. }
  246. // If the event is not canceled by the virtual method, raise the event to notify any external subscribers.
  247. Accept?.Invoke (this, args);
  248. return Accept is null ? null : args.Handled;
  249. }
  250. /// <summary>
  251. /// Called when the <see cref="Command.Accept"/> command is received. Set <see cref="HandledEventArgs.Handled"/> to
  252. /// <see langword="true"/> to stop processing.
  253. /// </summary>
  254. /// <remarks>
  255. /// <para>
  256. /// The base implementation calls <see cref="SetFocus"/>.
  257. /// </para>
  258. /// </remarks>
  259. /// <param name="args"></param>
  260. /// <returns><see langword="true"/> to stop processing.</returns>
  261. protected virtual bool OnAccept (HandledEventArgs args)
  262. {
  263. SetFocus ();
  264. return false;
  265. }
  266. /// <summary>
  267. /// Cancelable event raised when the <see cref="Command.Accept"/> command is invoked. Set
  268. /// <see cref="HandledEventArgs.Handled"/>
  269. /// to cancel the event.
  270. /// </summary>
  271. public event EventHandler<HandledEventArgs>? Accept;
  272. /// <summary>
  273. /// Called when the <see cref="Command.Select"/> command is invoked. Raises <see cref="Select"/>
  274. /// event.
  275. /// </summary>
  276. /// <returns>
  277. /// If <see langword="true"/> the event was canceled. If <see langword="false"/> the event was raised but not canceled.
  278. /// If <see langword="null"/> no event was raised.
  279. /// </returns>
  280. protected bool? RaiseSelectEvent ()
  281. {
  282. HandledEventArgs args = new ();
  283. // Best practice is to invoke the virtual method first.
  284. // This allows derived classes to handle the event and potentially cancel it.
  285. if (OnSelect (args) || args.Handled)
  286. {
  287. return true;
  288. }
  289. // If the event is not canceled by the virtual method, raise the event to notify any external subscribers.
  290. Select?.Invoke (this, args);
  291. return Select is null ? null : args.Handled;
  292. }
  293. /// <summary>
  294. /// Called when the <see cref="Command.Select"/> command is received. Set <see cref="HandledEventArgs.Handled"/> to
  295. /// <see langword="true"/> to stop processing.
  296. /// </summary>
  297. /// <remarks>
  298. /// <para>
  299. /// The base implementation calls <see cref="SetFocus"/>.
  300. /// </para>
  301. /// </remarks>
  302. /// <param name="args"></param>
  303. /// <returns><see langword="true"/> to stop processing.</returns>
  304. protected virtual bool OnSelect (HandledEventArgs args)
  305. {
  306. SetFocus ();
  307. return false;
  308. }
  309. /// <summary>
  310. /// Cancelable event raised when the <see cref="Command.Select"/> command is invoked. Set
  311. /// <see cref="HandledEventArgs.Handled"/>
  312. /// to cancel the event.
  313. /// </summary>
  314. public event EventHandler<HandledEventArgs>? Select;
  315. /// <summary>
  316. /// Called when the <see cref="Command.HotKey"/> command is invoked. Raises <see cref="HotKey"/>
  317. /// event.
  318. /// </summary>
  319. /// <returns>
  320. /// If <see langword="true"/> the event was canceled. If <see langword="false"/> the event was raised but not canceled.
  321. /// If <see langword="null"/> no event was raised.
  322. /// </returns>
  323. protected bool? RaiseHotKeyEvent ()
  324. {
  325. HandledEventArgs args = new ();
  326. // Best practice is to invoke the virtual method first.
  327. // This allows derived classes to handle the event and potentially cancel it.
  328. if (OnHotKey (args) || args.Handled)
  329. {
  330. return true;
  331. }
  332. // If the event is not canceled by the virtual method, raise the event to notify any external subscribers.
  333. HotKeyCommand?.Invoke (this, args);
  334. return HotKeyCommand is null ? null : args.Handled;
  335. }
  336. /// <summary>
  337. /// Called when the <see cref="Command.HotKey"/> command is received. Set <see cref="HandledEventArgs.Handled"/> to
  338. /// <see langword="true"/> to stop processing.
  339. /// </summary>
  340. /// <remarks>
  341. /// <para>
  342. /// The base implementation calls <see cref="SetFocus"/>.
  343. /// </para>
  344. /// </remarks>
  345. /// <param name="args"></param>
  346. /// <returns><see langword="true"/> to stop processing.</returns>
  347. protected virtual bool OnHotKey (HandledEventArgs args)
  348. {
  349. SetFocus ();
  350. return false;
  351. }
  352. /// <summary>
  353. /// Cancelable event raised when the <see cref="Command.HotKey"/> command is invoked. Set
  354. /// <see cref="HandledEventArgs.Handled"/>
  355. /// to cancel the event.
  356. /// </summary>
  357. public event EventHandler<HandledEventArgs>? HotKeyCommand;
  358. #endregion Base Command Events
  359. #region Visibility
  360. private bool _enabled = true;
  361. // This is a cache of the Enabled property so that we can restore it when the superview is re-enabled.
  362. private bool _oldEnabled;
  363. /// <summary>Gets or sets a value indicating whether this <see cref="Responder"/> can respond to user interaction.</summary>
  364. public bool Enabled
  365. {
  366. get => _enabled;
  367. set
  368. {
  369. if (_enabled == value)
  370. {
  371. return;
  372. }
  373. _enabled = value;
  374. if (!_enabled && HasFocus)
  375. {
  376. HasFocus = false;
  377. }
  378. if (_enabled
  379. && CanFocus
  380. && Visible
  381. && !HasFocus
  382. && SuperView is null or { HasFocus: true, Visible: true, Enabled: true, Focused: null })
  383. {
  384. SetFocus ();
  385. }
  386. OnEnabledChanged ();
  387. SetNeedsDisplay ();
  388. if (_subviews is null)
  389. {
  390. return;
  391. }
  392. foreach (View view in _subviews)
  393. {
  394. if (!_enabled)
  395. {
  396. view._oldEnabled = view.Enabled;
  397. view.Enabled = _enabled;
  398. }
  399. else
  400. {
  401. view.Enabled = view._oldEnabled;
  402. #if AUTO_CANFOCUS
  403. view._addingViewSoCanFocusAlsoUpdatesSuperView = _enabled;
  404. #endif
  405. }
  406. }
  407. }
  408. }
  409. /// <summary>Event fired when the <see cref="Enabled"/> value is being changed.</summary>
  410. public event EventHandler? EnabledChanged;
  411. /// <summary>Method invoked when the <see cref="Enabled"/> property from a view is changed.</summary>
  412. public virtual void OnEnabledChanged () { EnabledChanged?.Invoke (this, EventArgs.Empty); }
  413. private bool _visible = true;
  414. // TODO: Remove virtual once Menu/MenuBar are removed. MenuBar is the only override.
  415. /// <summary>Gets or sets a value indicating whether this <see cref="View"/> is visible.</summary>
  416. public virtual bool Visible
  417. {
  418. get => _visible;
  419. set
  420. {
  421. if (_visible == value)
  422. {
  423. return;
  424. }
  425. if (OnVisibleChanging ())
  426. {
  427. return;
  428. }
  429. CancelEventArgs<bool> args = new (in _visible, ref value);
  430. VisibleChanging?.Invoke (this, args);
  431. if (args.Cancel)
  432. {
  433. return;
  434. }
  435. _visible = value;
  436. if (!_visible)
  437. {
  438. if (HasFocus)
  439. {
  440. HasFocus = false;
  441. }
  442. }
  443. if (_visible
  444. && CanFocus
  445. && Enabled
  446. && !HasFocus
  447. && SuperView is null or { HasFocus: true, Visible: true, Enabled: true, Focused: null })
  448. {
  449. SetFocus ();
  450. }
  451. OnVisibleChanged ();
  452. VisibleChanged?.Invoke (this, EventArgs.Empty);
  453. SetNeedsDisplay ();
  454. }
  455. }
  456. /// <summary>Called when <see cref="Visible"/> is changing. Can be cancelled by returning <see langword="true"/>.</summary>
  457. protected virtual bool OnVisibleChanging () { return false; }
  458. /// <summary>
  459. /// Raised when the <see cref="Visible"/> value is being changed. Can be cancelled by setting Cancel to
  460. /// <see langword="true"/>.
  461. /// </summary>
  462. public event EventHandler<CancelEventArgs<bool>>? VisibleChanging;
  463. /// <summary>Called when <see cref="Visible"/> has changed.</summary>
  464. protected virtual void OnVisibleChanged () { }
  465. /// <summary>Raised when <see cref="Visible"/> has changed.</summary>
  466. public event EventHandler? VisibleChanged;
  467. // TODO: This API is a hack. We should make Visible propogate automatically, no? See https://github.com/gui-cs/Terminal.Gui/issues/3703
  468. /// <summary>
  469. /// INTERNAL Indicates whether all views up the Superview hierarchy are visible.
  470. /// </summary>
  471. /// <param name="view">The view to test.</param>
  472. /// <returns>
  473. /// <see langword="false"/> if `view.Visible` is <see langword="false"/> or any Superview is not visible,
  474. /// <see langword="true"/> otherwise.
  475. /// </returns>
  476. internal static bool CanBeVisible (View view)
  477. {
  478. if (!view.Visible)
  479. {
  480. return false;
  481. }
  482. for (View? c = view.SuperView; c != null; c = c.SuperView)
  483. {
  484. if (!c.Visible)
  485. {
  486. return false;
  487. }
  488. }
  489. return true;
  490. }
  491. #endregion Visibility
  492. #region Title
  493. private string _title = string.Empty;
  494. /// <summary>Gets the <see cref="Gui.TextFormatter"/> used to format <see cref="Title"/>.</summary>
  495. internal TextFormatter TitleTextFormatter { get; init; } = new ();
  496. /// <summary>
  497. /// The title to be displayed for this <see cref="View"/>. The title will be displayed if <see cref="Border"/>.
  498. /// <see cref="Thickness.Top"/> is greater than 0. The title can be used to set the <see cref="HotKey"/>
  499. /// for the view by prefixing character with <see cref="HotKeySpecifier"/> (e.g. <c>"T_itle"</c>).
  500. /// </summary>
  501. /// <remarks>
  502. /// <para>
  503. /// Set the <see cref="HotKeySpecifier"/> to enable hotkey support. To disable Title-based hotkey support set
  504. /// <see cref="HotKeySpecifier"/> to <c>(Rune)0xffff</c>.
  505. /// </para>
  506. /// <para>
  507. /// Only the first HotKey specifier found in <see cref="Title"/> is supported.
  508. /// </para>
  509. /// <para>
  510. /// To cause the hotkey to be rendered with <see cref="Text"/>,
  511. /// set <c>View.</c><see cref="TextFormatter.HotKeySpecifier"/> to the desired character.
  512. /// </para>
  513. /// </remarks>
  514. /// <value>The title.</value>
  515. public string Title
  516. {
  517. get
  518. {
  519. #if DEBUG_IDISPOSABLE
  520. if (WasDisposed)
  521. {
  522. throw new ObjectDisposedException (GetType ().FullName);
  523. }
  524. #endif
  525. return _title;
  526. }
  527. set
  528. {
  529. #if DEBUG_IDISPOSABLE
  530. if (WasDisposed)
  531. {
  532. throw new ObjectDisposedException (GetType ().FullName);
  533. }
  534. #endif
  535. if (value == _title)
  536. {
  537. return;
  538. }
  539. if (!OnTitleChanging (ref value))
  540. {
  541. string old = _title;
  542. _title = value;
  543. TitleTextFormatter.Text = _title;
  544. SetTitleTextFormatterSize ();
  545. SetHotKeyFromTitle ();
  546. SetNeedsDisplay ();
  547. #if DEBUG
  548. if (string.IsNullOrEmpty (Id))
  549. {
  550. Id = _title;
  551. }
  552. #endif // DEBUG
  553. OnTitleChanged ();
  554. }
  555. }
  556. }
  557. private void SetTitleTextFormatterSize ()
  558. {
  559. TitleTextFormatter.ConstrainToSize = new (
  560. TextFormatter.GetWidestLineLength (TitleTextFormatter.Text)
  561. - (TitleTextFormatter.Text?.Contains ((char)HotKeySpecifier.Value) == true
  562. ? Math.Max (HotKeySpecifier.GetColumns (), 0)
  563. : 0),
  564. 1);
  565. }
  566. /// <summary>Called when the <see cref="View.Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.</summary>
  567. protected void OnTitleChanged () { TitleChanged?.Invoke (this, new (in _title)); }
  568. /// <summary>
  569. /// Called before the <see cref="View.Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can
  570. /// be cancelled.
  571. /// </summary>
  572. /// <param name="newTitle">The new <see cref="View.Title"/> to be replaced.</param>
  573. /// <returns>`true` if an event handler canceled the Title change.</returns>
  574. protected bool OnTitleChanging (ref string newTitle)
  575. {
  576. CancelEventArgs<string> args = new (ref _title, ref newTitle);
  577. TitleChanging?.Invoke (this, args);
  578. return args.Cancel;
  579. }
  580. /// <summary>Event fired after the <see cref="View.Title"/> has been changed.</summary>
  581. public event EventHandler<EventArgs<string>>? TitleChanged;
  582. /// <summary>
  583. /// Event fired when the <see cref="View.Title"/> is changing. Set <see cref="CancelEventArgs.Cancel"/> to `true`
  584. /// to cancel the Title change.
  585. /// </summary>
  586. public event EventHandler<CancelEventArgs<string>>? TitleChanging;
  587. #endregion
  588. /// <summary>Pretty prints the View</summary>
  589. /// <returns></returns>
  590. public override string ToString () { return $"{GetType ().Name}({Id}){Frame}"; }
  591. /// <inheritdoc/>
  592. protected override void Dispose (bool disposing)
  593. {
  594. LineCanvas.Dispose ();
  595. DisposeKeyboard ();
  596. DisposeAdornments ();
  597. for (int i = InternalSubviews.Count - 1; i >= 0; i--)
  598. {
  599. View subview = InternalSubviews [i];
  600. Remove (subview);
  601. subview.Dispose ();
  602. }
  603. base.Dispose (disposing);
  604. Debug.Assert (InternalSubviews.Count == 0);
  605. }
  606. }