View.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. using System.Collections.Concurrent;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. namespace Terminal.Gui.ViewBase;
  5. #region API Docs
  6. /// <summary>
  7. /// View is the base class all visible elements. View can render itself and
  8. /// contains zero or more nested views, called SubViews. View provides basic functionality for layout, arrangement, and
  9. /// drawing. In addition, View provides keyboard and mouse event handling.
  10. /// <para>
  11. /// See the
  12. /// <see href="../docs/view.md">
  13. /// View
  14. /// Deep Dive
  15. /// </see>
  16. /// for more.
  17. /// </para>
  18. /// </summary>
  19. #endregion API Docs
  20. public partial class View : IDisposable, ISupportInitializeNotification
  21. {
  22. private bool _disposedValue;
  23. /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resource.</summary>
  24. public void Dispose ()
  25. {
  26. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  27. Disposing?.Invoke (this, EventArgs.Empty);
  28. Dispose (true);
  29. GC.SuppressFinalize (this);
  30. #if DEBUG_IDISPOSABLE
  31. WasDisposed = true;
  32. // Safely remove any disposed views from the Instances list
  33. List<View> itemsToKeep = Instances.Where (view => !view.WasDisposed).ToList ();
  34. Instances = new (itemsToKeep);
  35. #endif
  36. }
  37. /// <summary>
  38. /// Riased when the <see cref="View"/> is being disposed.
  39. /// </summary>
  40. public event EventHandler? Disposing;
  41. /// <summary>Pretty prints the View</summary>
  42. /// <returns></returns>
  43. public override string ToString () => $"{GetType ().Name}({Id}){Frame}";
  44. /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
  45. /// <remarks>
  46. /// If disposing equals true, the method has been called directly or indirectly by a user's code. Managed and
  47. /// unmanaged resources can be disposed. If disposing equals false, the method has been called by the runtime from
  48. /// inside the finalizer and you should not reference other objects. Only unmanaged resources can be disposed.
  49. /// </remarks>
  50. /// <param name="disposing"></param>
  51. protected virtual void Dispose (bool disposing)
  52. {
  53. if (disposing)
  54. {
  55. LineCanvas.Dispose ();
  56. DisposeMouse ();
  57. DisposeKeyboard ();
  58. DisposeAdornments ();
  59. DisposeScrollBars ();
  60. if (App?.Mouse.MouseGrabView == this)
  61. {
  62. App.Mouse.UngrabMouse ();
  63. }
  64. for (int i = InternalSubViews.Count - 1; i >= 0; i--)
  65. {
  66. View subview = InternalSubViews [i];
  67. Remove (subview);
  68. subview.Dispose ();
  69. }
  70. if (!_disposedValue)
  71. {
  72. if (disposing)
  73. {
  74. // TODO: dispose managed state (managed objects)
  75. }
  76. _disposedValue = true;
  77. }
  78. Debug.Assert (InternalSubViews.Count == 0);
  79. }
  80. }
  81. #region Constructors and Initialization
  82. /// <summary>Gets or sets arbitrary data for the view.</summary>
  83. /// <remarks>This property is not used internally.</remarks>
  84. public object? Data { get; set; }
  85. /// <summary>Gets or sets an identifier for the view;</summary>
  86. /// <value>The identifier.</value>
  87. /// <remarks>The id should be unique across all Views that share a SuperView.</remarks>
  88. public string Id { get; set; } = "";
  89. private IApplication? _app;
  90. /// <summary>
  91. /// Gets the <see cref="IApplication"/> instance this view is running in. If this view is at the top of the view
  92. /// hierarchy, returns <see langword="null"/>.
  93. /// </summary>
  94. /// <remarks>
  95. /// <para>
  96. /// If not explicitly set on an instance, this property will retrieve the value from the view at the top
  97. /// of the View hierarchy (the top-most SuperView).
  98. /// </para>
  99. /// </remarks>
  100. public IApplication? App
  101. {
  102. get => GetApp ();
  103. internal set => _app = value;
  104. }
  105. /// <summary>
  106. /// Gets the <see cref="IApplication"/> instance this view is running in. Used internally to allow overrides by
  107. /// <see cref="Adornment"/>.
  108. /// </summary>
  109. /// <returns>
  110. /// If this view is at the top of the view hierarchy, and <see cref="App"/> was not explicitly set,
  111. /// returns <see langword="null"/>.
  112. /// </returns>
  113. protected virtual IApplication? GetApp () => _app ?? SuperView?.App ?? null;
  114. private IDriver? _driver;
  115. /// <summary>
  116. /// INTERNAL: Use <see cref="Application.Driver"/> instead. Points to the current driver in use by the view, it is a
  117. /// convenience property for simplifying the development
  118. /// of new views.
  119. /// </summary>
  120. internal IDriver? Driver
  121. {
  122. get => GetDriver ();
  123. set => _driver = value;
  124. }
  125. /// <summary>
  126. /// Gets the <see cref="IDriver"/> instance for this view. Used internally to allow overrides by
  127. /// <see cref="Adornment"/>.
  128. /// </summary>
  129. /// <returns>If this view is at the top of the view hierarchy, returns <see langword="null"/>.</returns>
  130. protected virtual IDriver? GetDriver () => _driver ?? App?.Driver ?? SuperView?.Driver /*?? ApplicationImpl.Instance.Driver*/;
  131. /// <summary>
  132. /// Gets the screen buffer contents. This is a convenience property for Views that need direct access to the
  133. /// screen buffer.
  134. /// </summary>
  135. protected Cell [,]? ScreenContents => Driver?.Contents;
  136. /// <summary>Initializes a new instance of <see cref="View"/>.</summary>
  137. /// <remarks>
  138. /// <para>
  139. /// Use <see cref="X"/>, <see cref="Y"/>, <see cref="Width"/>, and <see cref="Height"/> properties to dynamically
  140. /// control the size and location of the view.
  141. /// </para>
  142. /// </remarks>
  143. public View ()
  144. {
  145. #if DEBUG_IDISPOSABLE
  146. Instances.Add (this);
  147. #endif
  148. SetupAdornments ();
  149. SetupCommands ();
  150. SetupKeyboard ();
  151. SetupMouse ();
  152. SetupText ();
  153. SetupScrollBars ();
  154. }
  155. /// <summary>
  156. /// Raised during <see cref="EndInit"/> before initialization completes. Can be canceled by setting
  157. /// <see cref="System.ComponentModel.CancelEventArgs.Cancel"/> to <see langword="true"/>. Follows the Cancellable Work Pattern (CWP).
  158. /// </summary>
  159. /// <remarks>
  160. /// <para>
  161. /// This event is raised after the virtual method <see cref="OnInitializing"/> is called and before
  162. /// the initialization work is performed. Subscribe to this event to perform pre-initialization work
  163. /// or to cancel initialization.
  164. /// </para>
  165. /// <para>
  166. /// This event follows Terminal.Gui's Cancellable Work Pattern. The pattern is:
  167. /// 1. <see cref="OnInitializing"/> (pre-notification, can cancel)
  168. /// 2. <see cref="Initializing"/> event (can cancel)
  169. /// 3. Initialization work (sets <see cref="IsInitialized"/> = true, layouts, etc.)
  170. /// 4. <see cref="OnInitialized"/> (post-notification)
  171. /// 5. <see cref="Initialized"/> event
  172. /// </para>
  173. /// </remarks>
  174. public event EventHandler<System.ComponentModel.CancelEventArgs>? Initializing;
  175. /// <summary>
  176. /// Raised once when the <see cref="View"/> has been initialized for the first time. Allows
  177. /// configurations and assignments to be performed after the <see cref="View"/> has been laid out.
  178. /// View implements <see cref="ISupportInitializeNotification"/> to allow for more sophisticated initialization.
  179. /// </summary>
  180. /// <remarks>
  181. /// This is the post-notification event in the Cancellable Work Pattern pair with <see cref="Initializing"/>.
  182. /// </remarks>
  183. public event EventHandler? Initialized;
  184. /// <summary>
  185. /// Get or sets if the <see cref="View"/> has been initialized (via <see cref="ISupportInitialize.BeginInit"/>
  186. /// and <see cref="ISupportInitialize.EndInit"/>).
  187. /// </summary>
  188. /// <para>
  189. /// If first-run-only initialization is preferred, overrides to
  190. /// <see cref="ISupportInitializeNotification.IsInitialized"/> can be implemented, in which case the
  191. /// <see cref="ISupportInitialize"/> methods will only be called if
  192. /// <see cref="ISupportInitializeNotification.IsInitialized"/> is <see langword="false"/>. This allows proper
  193. /// <see cref="View"/> inheritance hierarchies to override base class layout code optimally by doing so only on first
  194. /// run, instead of on every run.
  195. /// </para>
  196. public virtual bool IsInitialized { get; set; }
  197. /// <summary>Signals the View that initialization is starting. See <see cref="ISupportInitialize"/>.</summary>
  198. /// <remarks>
  199. /// <para>
  200. /// Views can opt-in to more sophisticated initialization by implementing overrides to
  201. /// <see cref="ISupportInitialize.BeginInit"/> and <see cref="ISupportInitialize.EndInit"/> which will be called
  202. /// when the <see cref="SuperView"/> is initialized.
  203. /// </para>
  204. /// <para>
  205. /// If first-run-only initialization is preferred, overrides to <see cref="ISupportInitializeNotification"/> can
  206. /// be implemented too, in which case the <see cref="ISupportInitialize"/> methods will only be called if
  207. /// <see cref="ISupportInitializeNotification.IsInitialized"/> is <see langword="false"/>. This allows proper
  208. /// <see cref="View"/> inheritance hierarchies to override base class layout code optimally by doing so only on
  209. /// first run, instead of on every run.
  210. /// </para>
  211. /// </remarks>
  212. public virtual void BeginInit ()
  213. {
  214. if (IsInitialized)
  215. {
  216. throw new InvalidOperationException ("The view is already initialized.");
  217. }
  218. #if AUTO_CANFOCUS
  219. _oldCanFocus = CanFocus;
  220. _oldTabIndex = _tabIndex;
  221. #endif
  222. BeginInitAdornments ();
  223. if (InternalSubViews?.Count > 0)
  224. {
  225. foreach (View view in InternalSubViews)
  226. {
  227. if (!view.IsInitialized)
  228. {
  229. view.BeginInit ();
  230. }
  231. }
  232. }
  233. }
  234. // TODO: Implement logic that allows EndInit to throw if BeginInit has not been called
  235. // TODO: See EndInit_Called_Without_BeginInit_Throws test.
  236. /// <summary>Signals the View that initialization is ending. See <see cref="ISupportInitialize"/>.</summary>
  237. /// <remarks>
  238. /// <para>
  239. /// Initializes all SubViews and invokes the <see cref="Initialized"/> event. Follows the Cancellable
  240. /// Work Pattern (CWP).
  241. /// </para>
  242. /// <para>
  243. /// The initialization flow is:
  244. /// 1. <see cref="OnInitializing"/> (pre-notification, can cancel)
  245. /// 2. <see cref="Initializing"/> event (can cancel)
  246. /// 3. Initialization work (sets <see cref="IsInitialized"/> = true, layouts, etc.)
  247. /// 4. <see cref="OnInitialized"/> (post-notification)
  248. /// 5. <see cref="Initialized"/> event
  249. /// </para>
  250. /// </remarks>
  251. public virtual void EndInit ()
  252. {
  253. if (IsInitialized)
  254. {
  255. throw new InvalidOperationException ("The view is already initialized.");
  256. }
  257. // CWP Phase 1: Pre-notification via virtual method (can cancel)
  258. if (OnInitializing ())
  259. {
  260. return; // Initialization canceled
  261. }
  262. // CWP Phase 2: Event notification (can cancel)
  263. var args = new System.ComponentModel.CancelEventArgs ();
  264. Initializing?.Invoke (this, args);
  265. if (args.Cancel)
  266. {
  267. return; // Initialization canceled
  268. }
  269. // CWP Phase 3: Perform initialization work
  270. IsInitialized = true;
  271. EndInitAdornments ();
  272. // TODO: Move these into ViewText.cs as EndInit_Text() to consolidate.
  273. // TODO: Verify UpdateTextDirection really needs to be called here.
  274. // These calls were moved from BeginInit as they access Viewport which is indeterminate until EndInit is called.
  275. UpdateTextDirection (TextDirection);
  276. UpdateTextFormatterText ();
  277. foreach (View view in InternalSubViews)
  278. {
  279. if (!view.IsInitialized)
  280. {
  281. view.EndInit ();
  282. }
  283. }
  284. // Force a layout each time a View is initialized
  285. // See: https://github.com/gui-cs/Terminal.Gui/issues/3951
  286. // See: https://github.com/gui-cs/Terminal.Gui/issues/4204
  287. Layout (); // the EventLog in AllViewsTester fails to layout correctly if this is not here (convoluted Dim.Fill(Func)).
  288. // Complex layout scenarios (e.g. DimAuto and PosAlign) may require multiple layouts to be performed.
  289. // Thus, we call SetNeedsLayout() to ensure that the layout is performed at least once.
  290. SetNeedsLayout ();
  291. // CWP Phase 4: Post-notification via virtual method
  292. OnInitialized ();
  293. // CWP Phase 5: Post-notification event
  294. Initialized?.Invoke (this, EventArgs.Empty);
  295. }
  296. /// <summary>
  297. /// Called before the <see cref="Initializing"/> event and before initialization work is performed.
  298. /// Override to provide custom pre-initialization logic or to cancel initialization.
  299. /// </summary>
  300. /// <returns><see langword="true"/> to cancel initialization; <see langword="false"/> to proceed.</returns>
  301. /// <remarks>
  302. /// <para>
  303. /// This is the first phase of the Cancellable Work Pattern (CWP) for view initialization.
  304. /// Default implementation returns <see langword="false"/> (allow initialization to proceed).
  305. /// </para>
  306. /// <para>
  307. /// If this method returns <see langword="true"/>, initialization is canceled, and neither the
  308. /// <see cref="Initializing"/> event nor <see cref="Initialized"/> event will be raised.
  309. /// </para>
  310. /// </remarks>
  311. protected virtual bool OnInitializing ()
  312. {
  313. return false; // Default: allow initialization
  314. }
  315. /// <summary>
  316. /// Called after initialization work completes and before the <see cref="Initialized"/> event is raised.
  317. /// Override to provide custom post-initialization logic.
  318. /// </summary>
  319. /// <remarks>
  320. /// This is the fourth phase of the Cancellable Work Pattern (CWP) for view initialization.
  321. /// At this point, <see cref="IsInitialized"/> is <see langword="true"/>, layout has been performed,
  322. /// and all subviews have been initialized.
  323. /// </remarks>
  324. protected virtual void OnInitialized ()
  325. {
  326. // Default: do nothing
  327. }
  328. #endregion Constructors and Initialization
  329. #region Visibility
  330. private bool _enabled = true;
  331. /// <summary>Gets or sets a value indicating whether this <see cref="View"/> can respond to user interaction.</summary>
  332. public bool Enabled
  333. {
  334. get => _enabled;
  335. set
  336. {
  337. if (_enabled == value)
  338. {
  339. return;
  340. }
  341. _enabled = value;
  342. if (!_enabled && HasFocus)
  343. {
  344. HasFocus = false;
  345. }
  346. if (_enabled
  347. && CanFocus
  348. && Visible
  349. && !HasFocus
  350. && SuperView is null or { HasFocus: true, Visible: true, Enabled: true, Focused: null })
  351. {
  352. SetFocus ();
  353. }
  354. OnEnabledChanged ();
  355. SetNeedsDraw ();
  356. if (Border is { })
  357. {
  358. Border.Enabled = _enabled;
  359. }
  360. foreach (View view in InternalSubViews)
  361. {
  362. view.Enabled = Enabled;
  363. }
  364. }
  365. }
  366. /// <summary>Raised when the <see cref="Enabled"/> value is being changed.</summary>
  367. public event EventHandler? EnabledChanged;
  368. // TODO: Change this event to match the standard TG event model.
  369. /// <summary>Invoked when the <see cref="Enabled"/> property from a view is changed.</summary>
  370. public virtual void OnEnabledChanged () { EnabledChanged?.Invoke (this, EventArgs.Empty); }
  371. private bool _visible = true;
  372. // TODO: Remove virtual once Menu/MenuBar are removed. MenuBar is the only override.
  373. /// <summary>Gets or sets a value indicating whether this <see cref="View"/> is visible.</summary>
  374. public virtual bool Visible
  375. {
  376. get => _visible;
  377. set
  378. {
  379. if (_visible == value)
  380. {
  381. return;
  382. }
  383. if (OnVisibleChanging ())
  384. {
  385. return;
  386. }
  387. CancelEventArgs<bool> args = new (in _visible, ref value);
  388. VisibleChanging?.Invoke (this, args);
  389. if (args.Cancel)
  390. {
  391. return;
  392. }
  393. _visible = value;
  394. if (!_visible)
  395. {
  396. // BUGBUG: Ideally we'd reset _previouslyFocused to the first focusable subview
  397. _previouslyFocused = SubViews.FirstOrDefault (v => v.CanFocus);
  398. if (HasFocus)
  399. {
  400. HasFocus = false;
  401. }
  402. }
  403. if (_visible
  404. && CanFocus
  405. && Enabled
  406. && !HasFocus
  407. && SuperView is null or { HasFocus: true, Visible: true, Enabled: true, Focused: null })
  408. {
  409. SetFocus ();
  410. }
  411. OnVisibleChanged ();
  412. VisibleChanged?.Invoke (this, EventArgs.Empty);
  413. SetNeedsLayout ();
  414. SuperView?.SetNeedsLayout ();
  415. SetNeedsDraw ();
  416. if (SuperView is { })
  417. {
  418. SuperView?.SetNeedsDraw ();
  419. }
  420. else
  421. {
  422. NeedsClearScreenNextIteration ();
  423. }
  424. }
  425. }
  426. /// <summary>Called when <see cref="Visible"/> is changing. Can be cancelled by returning <see langword="true"/>.</summary>
  427. protected virtual bool OnVisibleChanging () => false;
  428. /// <summary>
  429. /// Raised when the <see cref="Visible"/> value is being changed. Can be cancelled by setting Cancel to
  430. /// <see langword="true"/>.
  431. /// </summary>
  432. public event EventHandler<CancelEventArgs<bool>>? VisibleChanging;
  433. /// <summary>Called when <see cref="Visible"/> has changed.</summary>
  434. protected virtual void OnVisibleChanged () { }
  435. /// <summary>Raised when <see cref="Visible"/> has changed.</summary>
  436. public event EventHandler? VisibleChanged;
  437. /// <summary>
  438. /// INTERNAL Indicates whether all views up the Superview hierarchy are visible.
  439. /// </summary>
  440. /// <param name="view">The view to test.</param>
  441. /// <returns>
  442. /// <see langword="false"/> if `view.Visible` is <see langword="false"/> or any Superview is not visible,
  443. /// <see langword="true"/> otherwise.
  444. /// </returns>
  445. internal static bool CanBeVisible (View view)
  446. {
  447. if (!view.Visible)
  448. {
  449. return false;
  450. }
  451. for (View? c = view.SuperView; c != null; c = c.SuperView)
  452. {
  453. if (!c.Visible)
  454. {
  455. return false;
  456. }
  457. }
  458. return true;
  459. }
  460. #endregion Visibility
  461. #region Title
  462. private string _title = string.Empty;
  463. /// <summary>Gets the <see cref="Text.TextFormatter"/> used to format <see cref="Title"/>.</summary>
  464. internal TextFormatter TitleTextFormatter { get; init; } = new ();
  465. /// <summary>
  466. /// The title to be displayed for this <see cref="View"/>. The title will be displayed if <see cref="Border"/>.
  467. /// <see cref="Thickness.Top"/> is greater than 0. The title can be used to set the <see cref="HotKey"/>
  468. /// for the view by prefixing character with <see cref="HotKeySpecifier"/> (e.g. <c>"T_itle"</c>).
  469. /// </summary>
  470. /// <remarks>
  471. /// <para>
  472. /// Set the <see cref="HotKeySpecifier"/> to enable hotkey support. To disable Title-based hotkey support set
  473. /// <see cref="HotKeySpecifier"/> to <c>(Rune)0xffff</c>.
  474. /// </para>
  475. /// <para>
  476. /// Only the first HotKey specifier found in <see cref="Title"/> is supported.
  477. /// </para>
  478. /// <para>
  479. /// To cause the hotkey to be rendered with <see cref="Text"/>,
  480. /// set <c>View.</c><see cref="TextFormatter.HotKeySpecifier"/> to the desired character.
  481. /// </para>
  482. /// </remarks>
  483. /// <value>The title.</value>
  484. public string Title
  485. {
  486. get { return _title; }
  487. set
  488. {
  489. #if DEBUG_IDISPOSABLE
  490. if (EnableDebugIDisposableAsserts && WasDisposed)
  491. {
  492. throw new ObjectDisposedException (GetType ().FullName);
  493. }
  494. #endif
  495. if (value == _title)
  496. {
  497. return;
  498. }
  499. if (!OnTitleChanging (ref value))
  500. {
  501. string old = _title;
  502. _title = value;
  503. TitleTextFormatter.Text = _title;
  504. SetTitleTextFormatterSize ();
  505. SetHotKeyFromTitle ();
  506. SetNeedsDraw ();
  507. OnTitleChanged ();
  508. }
  509. }
  510. }
  511. private void SetTitleTextFormatterSize ()
  512. {
  513. TitleTextFormatter.ConstrainToSize = new (
  514. TextFormatter.GetWidestLineLength (TitleTextFormatter.Text)
  515. - (TitleTextFormatter.Text?.Contains ((char)HotKeySpecifier.Value) == true
  516. ? Math.Max (HotKeySpecifier.GetColumns (), 0)
  517. : 0),
  518. 1);
  519. }
  520. // TODO: Change this event to match the standard TG event model.
  521. /// <summary>Called when the <see cref="View.Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.</summary>
  522. protected void OnTitleChanged () { TitleChanged?.Invoke (this, new (in _title)); }
  523. /// <summary>
  524. /// Called before the <see cref="View.Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can
  525. /// be cancelled.
  526. /// </summary>
  527. /// <param name="newTitle">The new <see cref="View.Title"/> to be replaced.</param>
  528. /// <returns>`true` if an event handler canceled the Title change.</returns>
  529. protected bool OnTitleChanging (ref string newTitle)
  530. {
  531. CancelEventArgs<string> args = new (ref _title, ref newTitle);
  532. TitleChanging?.Invoke (this, args);
  533. return args.Cancel;
  534. }
  535. /// <summary>Raised after the <see cref="View.Title"/> has been changed.</summary>
  536. public event EventHandler<EventArgs<string>>? TitleChanged;
  537. /// <summary>
  538. /// Raised when the <see cref="View.Title"/> is changing. Set <see cref="CancelEventArgs.Cancel"/> to `true`
  539. /// to cancel the Title change.
  540. /// </summary>
  541. public event EventHandler<CancelEventArgs<string>>? TitleChanging;
  542. #endregion
  543. #if DEBUG_IDISPOSABLE
  544. #pragma warning disable CS0419 // Ambiguous reference in cref attribute
  545. /// <summary>
  546. /// Gets or sets whether failure to appropriately call Dispose() on a View will result in an Assert.
  547. /// The default is <see langword="true"/>.
  548. /// Note, this is a static property and will affect all Views.
  549. /// For debug purposes to verify objects are being disposed properly.
  550. /// Only valid when DEBUG_IDISPOSABLE is defined.
  551. /// </summary>
  552. public static bool EnableDebugIDisposableAsserts { get; set; } = true;
  553. /// <summary>
  554. /// Gets whether <see cref="View.Dispose"/> was called on this view or not.
  555. /// For debug purposes to verify objects are being disposed properly.
  556. /// Only valid when DEBUG_IDISPOSABLE is defined.
  557. /// </summary>
  558. public bool WasDisposed { get; private set; }
  559. /// <summary>
  560. /// Gets the number of times <see cref="View.Dispose"/> was called on this view.
  561. /// For debug purposes to verify objects are being disposed properly.
  562. /// Only valid when DEBUG_IDISPOSABLE is defined.
  563. /// </summary>
  564. public int DisposedCount { get; private set; } = 0;
  565. /// <summary>
  566. /// Gets the list of Views that have been created and not yet disposed.
  567. /// Note, this is a static property and will affect all Views.
  568. /// For debug purposes to verify objects are being disposed properly.
  569. /// Only valid when DEBUG_IDISPOSABLE is defined.
  570. /// </summary>
  571. public static ConcurrentBag<View> Instances { get; private set; } = [];
  572. #pragma warning restore CS0419 // Ambiguous reference in cref attribute
  573. #endif
  574. }