View.Drawing.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. using System.ComponentModel;
  2. using System.Diagnostics;
  3. namespace Terminal.Gui.ViewBase;
  4. public partial class View // Drawing APIs
  5. {
  6. /// <summary>
  7. /// Draws a set of views.
  8. /// </summary>
  9. /// <param name="views">The peer views to draw.</param>
  10. /// <param name="force">If <see langword="true"/>, <see cref="View.SetNeedsDraw()"/> will be called on each view to force it to be drawn.</param>
  11. internal static void Draw (IEnumerable<View> views, bool force)
  12. {
  13. // **Snapshot once** — every recursion level gets its own frozen array
  14. View [] viewsArray = views.Snapshot ();
  15. // The draw context is used to track the region drawn by each view.
  16. DrawContext context = new DrawContext ();
  17. foreach (View view in viewsArray)
  18. {
  19. if (force)
  20. {
  21. view.SetNeedsDraw ();
  22. }
  23. view.Draw (context);
  24. }
  25. // Draw the margins (those with Shadows) last to ensure they are drawn on top of the content.
  26. Margin.DrawMargins (viewsArray);
  27. }
  28. /// <summary>
  29. /// Draws the view if it needs to be drawn.
  30. /// </summary>
  31. /// <remarks>
  32. /// <para>
  33. /// The view will only be drawn if it is visible, and has any of <see cref="NeedsDraw"/>,
  34. /// <see cref="SubViewNeedsDraw"/>,
  35. /// or <see cref="NeedsLayout"/> set.
  36. /// </para>
  37. /// <para>
  38. /// See the View Drawing Deep Dive for more information: <see href="https://gui-cs.github.io/Terminal.Gui/docs/drawing.html"/>.
  39. /// </para>
  40. /// </remarks>
  41. public void Draw (DrawContext? context = null)
  42. {
  43. if (!CanBeVisible (this))
  44. {
  45. return;
  46. }
  47. Region? originalClip = GetClip ();
  48. // TODO: This can be further optimized by checking NeedsDraw below and only
  49. // TODO: clearing, drawing text, drawing content, etc. if it is true.
  50. if (NeedsDraw || SubViewNeedsDraw)
  51. {
  52. // ------------------------------------
  53. // Draw the Border and Padding.
  54. // Note Margin with a Shadow is special-cased and drawn in a separate pass to support
  55. // transparent shadows.
  56. DoDrawAdornments (originalClip);
  57. SetClip (originalClip);
  58. // ------------------------------------
  59. // Clear the Viewport
  60. // By default, we clip to the viewport preventing drawing outside the viewport
  61. // We also clip to the content, but if a developer wants to draw outside the viewport, they can do
  62. // so via settings. SetClip honors the ViewportSettings.DisableVisibleContentClipping flag.
  63. // Get our Viewport in screen coordinates
  64. originalClip = AddViewportToClip ();
  65. // If no context ...
  66. context ??= new DrawContext ();
  67. SetAttributeForRole (Enabled ? VisualRole.Normal : VisualRole.Disabled);
  68. DoClearViewport (context);
  69. // ------------------------------------
  70. // Draw the subviews first (order matters: SubViews, Text, Content)
  71. if (SubViewNeedsDraw)
  72. {
  73. DoDrawSubViews (context);
  74. }
  75. // ------------------------------------
  76. // Draw the text
  77. SetAttributeForRole (Enabled ? VisualRole.Normal : VisualRole.Disabled);
  78. DoDrawText (context);
  79. // ------------------------------------
  80. // Draw the content
  81. DoDrawContent (context);
  82. // ------------------------------------
  83. // Draw the line canvas
  84. // Restore the clip before rendering the line canvas and adornment subviews
  85. // because they may draw outside the viewport.
  86. SetClip (originalClip);
  87. originalClip = AddFrameToClip ();
  88. DoRenderLineCanvas ();
  89. // ------------------------------------
  90. // Re-draw the border and padding subviews
  91. // HACK: This is a hack to ensure that the border and padding subviews are drawn after the line canvas.
  92. DoDrawAdornmentsSubViews ();
  93. // ------------------------------------
  94. // Advance the diagnostics draw indicator
  95. Border?.AdvanceDrawIndicator ();
  96. ClearNeedsDraw ();
  97. if (this is not Adornment && SuperView is not Adornment)
  98. {
  99. // Parent
  100. Debug.Assert (Margin!.Parent == this);
  101. Debug.Assert (Border!.Parent == this);
  102. Debug.Assert (Padding!.Parent == this);
  103. // SubViewNeedsDraw is set to false by ClearNeedsDraw.
  104. Debug.Assert (SubViewNeedsDraw == false);
  105. Debug.Assert (Margin!.SubViewNeedsDraw == false);
  106. Debug.Assert (Border!.SubViewNeedsDraw == false);
  107. Debug.Assert (Padding!.SubViewNeedsDraw == false);
  108. // NeedsDraw is set to false by ClearNeedsDraw.
  109. Debug.Assert (NeedsDraw == false);
  110. Debug.Assert (Margin!.NeedsDraw == false);
  111. Debug.Assert (Border!.NeedsDraw == false);
  112. Debug.Assert (Padding!.NeedsDraw == false);
  113. }
  114. }
  115. // ------------------------------------
  116. // This causes the Margin to be drawn in a second pass if it has a ShadowStyle
  117. // PERFORMANCE: If there is a Margin w/ Shadow, it will be redrawn each iteration of the main loop.
  118. Margin?.CacheClip ();
  119. // ------------------------------------
  120. // Reset the clip to what it was when we started
  121. SetClip (originalClip);
  122. // ------------------------------------
  123. // We're done drawing - The Clip is reset to what it was before we started.
  124. DoDrawComplete (context);
  125. }
  126. #region DrawAdornments
  127. private void DoDrawAdornmentsSubViews ()
  128. {
  129. // NOTE: We do not support subviews of Margin?
  130. if (Border?.SubViews is { } && Border.Thickness != Thickness.Empty)
  131. {
  132. // PERFORMANCE: Get the check for DrawIndicator out of this somehow.
  133. foreach (View subview in Border.SubViews.Where (v => v.Visible || v.Id == "DrawIndicator"))
  134. {
  135. if (subview.Id != "DrawIndicator")
  136. {
  137. subview.SetNeedsDraw ();
  138. }
  139. LineCanvas.Exclude (new (subview.FrameToScreen ()));
  140. }
  141. Region? saved = Border?.AddFrameToClip ();
  142. Border?.DoDrawSubViews ();
  143. SetClip (saved);
  144. }
  145. if (Padding?.SubViews is { } && Padding.Thickness != Thickness.Empty)
  146. {
  147. foreach (View subview in Padding.SubViews)
  148. {
  149. subview.SetNeedsDraw ();
  150. }
  151. Region? saved = Padding?.AddFrameToClip ();
  152. Padding?.DoDrawSubViews ();
  153. SetClip (saved);
  154. }
  155. }
  156. internal void DoDrawAdornments (Region? originalClip)
  157. {
  158. if (this is Adornment)
  159. {
  160. AddFrameToClip ();
  161. }
  162. else
  163. {
  164. // Set the clip to be just the thicknesses of the adornments
  165. // TODO: Put this union logic in a method on View?
  166. Region? clipAdornments = Margin!.Thickness.AsRegion (Margin!.FrameToScreen ());
  167. clipAdornments?.Combine (Border!.Thickness.AsRegion (Border!.FrameToScreen ()), RegionOp.Union);
  168. clipAdornments?.Combine (Padding!.Thickness.AsRegion (Padding!.FrameToScreen ()), RegionOp.Union);
  169. clipAdornments?.Combine (originalClip, RegionOp.Intersect);
  170. SetClip (clipAdornments);
  171. }
  172. if (Margin?.NeedsLayout == true)
  173. {
  174. Margin.NeedsLayout = false;
  175. Margin?.Thickness.Draw (Driver, FrameToScreen ());
  176. Margin?.Parent?.SetSubViewNeedsDraw ();
  177. }
  178. if (SubViewNeedsDraw)
  179. {
  180. // A SubView may add to the LineCanvas. This ensures any Adornment LineCanvas updates happen.
  181. Border?.SetNeedsDraw ();
  182. Padding?.SetNeedsDraw ();
  183. Margin?.SetNeedsDraw ();
  184. }
  185. if (OnDrawingAdornments ())
  186. {
  187. return;
  188. }
  189. // TODO: add event.
  190. DrawAdornments ();
  191. }
  192. /// <summary>
  193. /// Causes <see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/> to be drawn.
  194. /// </summary>
  195. /// <remarks>
  196. /// <para>
  197. /// <see cref="Margin"/> is drawn in a separate pass if <see cref="ShadowStyle"/> is set.
  198. /// </para>
  199. /// </remarks>
  200. public void DrawAdornments ()
  201. {
  202. // We do not attempt to draw Margin. It is drawn in a separate pass.
  203. // Each of these renders lines to this View's LineCanvas
  204. // Those lines will be finally rendered in OnRenderLineCanvas
  205. if (Border is { } && Border.Thickness != Thickness.Empty)
  206. {
  207. Border?.Draw ();
  208. }
  209. if (Padding is { } && Padding.Thickness != Thickness.Empty)
  210. {
  211. Padding?.Draw ();
  212. }
  213. if (Margin is { } && Margin.Thickness != Thickness.Empty/* && Margin.ShadowStyle == ShadowStyle.None*/)
  214. {
  215. //Margin?.Draw ();
  216. }
  217. }
  218. private void ClearFrame ()
  219. {
  220. if (Driver is null)
  221. {
  222. return;
  223. }
  224. // Get screen-relative coords
  225. Rectangle toClear = FrameToScreen ();
  226. Attribute prev = SetAttribute (GetAttributeForRole (VisualRole.Normal));
  227. Driver.FillRect (toClear);
  228. SetAttribute (prev);
  229. SetNeedsDraw ();
  230. }
  231. /// <summary>
  232. /// Called when the View's Adornments are to be drawn. Prepares <see cref="View.LineCanvas"/>. If
  233. /// <see cref="SuperViewRendersLineCanvas"/> is true, only the
  234. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  235. /// false (the default), this method will cause the <see cref="LineCanvas"/> be prepared to be rendered.
  236. /// </summary>
  237. /// <returns><see langword="true"/> to stop further drawing of the Adornments.</returns>
  238. protected virtual bool OnDrawingAdornments () { return false; }
  239. #endregion DrawAdornments
  240. #region ClearViewport
  241. internal void DoClearViewport (DrawContext? context = null)
  242. {
  243. if (ViewportSettings.HasFlag (ViewportSettingsFlags.Transparent) || OnClearingViewport ())
  244. {
  245. return;
  246. }
  247. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  248. ClearingViewport?.Invoke (this, dev);
  249. if (dev.Cancel)
  250. {
  251. // BUGBUG: We should add the Viewport to context.DrawRegion here?
  252. SetNeedsDraw ();
  253. return;
  254. }
  255. if (!ViewportSettings.HasFlag (ViewportSettingsFlags.Transparent))
  256. {
  257. ClearViewport (context);
  258. OnClearedViewport ();
  259. ClearedViewport?.Invoke (this, new (Viewport, Viewport, null));
  260. }
  261. }
  262. /// <summary>
  263. /// Called when the <see cref="Viewport"/> is to be cleared.
  264. /// </summary>
  265. /// <returns><see langword="true"/> to stop further clearing.</returns>
  266. protected virtual bool OnClearingViewport () { return false; }
  267. /// <summary>Event invoked when the <see cref="Viewport"/> is to be cleared.</summary>
  268. /// <remarks>
  269. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  270. /// <para>
  271. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  272. /// <see cref="View"/> .
  273. /// </para>
  274. /// </remarks>
  275. public event EventHandler<DrawEventArgs>? ClearingViewport;
  276. /// <summary>
  277. /// Called when the <see cref="Viewport"/> has been cleared
  278. /// </summary>
  279. protected virtual void OnClearedViewport () { }
  280. /// <summary>Event invoked when the <see cref="Viewport"/> has been cleared.</summary>
  281. public event EventHandler<DrawEventArgs>? ClearedViewport;
  282. /// <summary>Clears <see cref="Viewport"/> with the normal background.</summary>
  283. /// <remarks>
  284. /// <para>
  285. /// If <see cref="ViewportSettings"/> has <see cref="ViewBase.ViewportSettingsFlags.ClearContentOnly"/> only
  286. /// the portion of the content
  287. /// area that is visible within the <see cref="View.Viewport"/> will be cleared. This is useful for views that have
  288. /// a
  289. /// content area larger than the Viewport (e.g. when <see cref="ViewportSettingsFlags.AllowNegativeLocation"/> is
  290. /// enabled) and want
  291. /// the area outside the content to be visually distinct.
  292. /// </para>
  293. /// </remarks>
  294. public void ClearViewport (DrawContext? context = null)
  295. {
  296. if (Driver is null)
  297. {
  298. return;
  299. }
  300. // Get screen-relative coords
  301. Rectangle toClear = ViewportToScreen (Viewport with { Location = new (0, 0) });
  302. if (ViewportSettings.HasFlag (ViewportSettingsFlags.ClearContentOnly))
  303. {
  304. Rectangle visibleContent = ViewportToScreen (new Rectangle (new (-Viewport.X, -Viewport.Y), GetContentSize ()));
  305. toClear = Rectangle.Intersect (toClear, visibleContent);
  306. }
  307. Driver.FillRect (toClear);
  308. // context.AddDrawnRectangle (toClear);
  309. SetNeedsDraw ();
  310. }
  311. #endregion ClearViewport
  312. #region DrawText
  313. private void DoDrawText (DrawContext? context = null)
  314. {
  315. if (!NeedsDraw)
  316. {
  317. return;
  318. }
  319. if (!string.IsNullOrEmpty (TextFormatter.Text))
  320. {
  321. TextFormatter.NeedsFormat = true;
  322. }
  323. if (OnDrawingText (context))
  324. {
  325. return;
  326. }
  327. // TODO: Get rid of this vf in lieu of the one above
  328. if (OnDrawingText ())
  329. {
  330. return;
  331. }
  332. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  333. DrawingText?.Invoke (this, dev);
  334. if (dev.Cancel)
  335. {
  336. return;
  337. }
  338. DrawText (context);
  339. OnDrewText();
  340. DrewText?.Invoke(this, EventArgs.Empty);
  341. }
  342. /// <summary>
  343. /// Called when the <see cref="Text"/> of the View is to be drawn.
  344. /// </summary>
  345. /// <param name="context">The draw context to report drawn areas to.</param>
  346. /// <returns><see langword="true"/> to stop further drawing of <see cref="Text"/>.</returns>
  347. protected virtual bool OnDrawingText (DrawContext? context) { return false; }
  348. /// <summary>
  349. /// Called when the <see cref="Text"/> of the View is to be drawn.
  350. /// </summary>
  351. /// <returns><see langword="true"/> to stop further drawing of <see cref="Text"/>.</returns>
  352. protected virtual bool OnDrawingText () { return false; }
  353. /// <summary>Raised when the <see cref="Text"/> of the View is to be drawn.</summary>
  354. /// <returns>
  355. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  356. /// <see cref="Text"/>.
  357. /// </returns>
  358. public event EventHandler<DrawEventArgs>? DrawingText;
  359. /// <summary>
  360. /// Draws the <see cref="Text"/> of the View using the <see cref="TextFormatter"/>.
  361. /// </summary>
  362. /// <param name="context">The draw context to report drawn areas to.</param>
  363. public void DrawText (DrawContext? context = null)
  364. {
  365. var drawRect = new Rectangle (ContentToScreen (Point.Empty), GetContentSize ());
  366. // Use GetDrawRegion to get precise drawn areas
  367. Region textRegion = TextFormatter.GetDrawRegion (drawRect);
  368. // Report the drawn area to the context
  369. context?.AddDrawnRegion (textRegion);
  370. TextFormatter?.Draw (
  371. Driver,
  372. drawRect,
  373. HasFocus ? GetAttributeForRole (VisualRole.Focus) : GetAttributeForRole (VisualRole.Normal),
  374. HasFocus ? GetAttributeForRole (VisualRole.HotFocus) : GetAttributeForRole (VisualRole.HotNormal),
  375. Rectangle.Empty);
  376. // We assume that the text has been drawn over the entire area; ensure that the subviews are redrawn.
  377. SetSubViewNeedsDraw ();
  378. }
  379. /// <summary>
  380. /// Called when the <see cref="Text"/> of the View has been drawn.
  381. /// </summary>
  382. protected virtual void OnDrewText () { }
  383. /// <summary>Raised when the <see cref="Text"/> of the View has been drawn.</summary>
  384. public event EventHandler? DrewText;
  385. #endregion DrawText
  386. #region DrawContent
  387. private void DoDrawContent (DrawContext? context = null)
  388. {
  389. if (OnDrawingContent (context))
  390. {
  391. return;
  392. }
  393. // TODO: Upgrade all overrides of OnDrawingContent to use DrawContext and remove this override
  394. if (OnDrawingContent ())
  395. {
  396. return;
  397. }
  398. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  399. DrawingContent?.Invoke (this, dev);
  400. if (dev.Cancel)
  401. {
  402. return;
  403. }
  404. // No default drawing; let event handlers or overrides handle it
  405. }
  406. /// <summary>
  407. /// Called when the View's content is to be drawn. The default implementation does nothing.
  408. /// </summary>
  409. /// <param name="context">The draw context to report drawn areas to.</param>
  410. /// <returns><see langword="true"/> to stop further drawing content.</returns>
  411. protected virtual bool OnDrawingContent (DrawContext? context) { return false; }
  412. /// <summary>
  413. /// Called when the View's content is to be drawn. The default implementation does nothing.
  414. /// </summary>
  415. /// <returns><see langword="true"/> to stop further drawing content.</returns>
  416. protected virtual bool OnDrawingContent () { return false; }
  417. /// <summary>Raised when the View's content is to be drawn.</summary>
  418. /// <remarks>
  419. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  420. /// <para>
  421. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  422. /// <see cref="View"/> .
  423. /// </para>
  424. /// </remarks>
  425. public event EventHandler<DrawEventArgs>? DrawingContent;
  426. #endregion DrawContent
  427. #region DrawSubViews
  428. private void DoDrawSubViews (DrawContext? context = null)
  429. {
  430. if (OnDrawingSubViews (context))
  431. {
  432. return;
  433. }
  434. // TODO: Get rid of this vf in lieu of the one above
  435. if (OnDrawingSubViews ())
  436. {
  437. return;
  438. }
  439. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  440. DrawingSubViews?.Invoke (this, dev);
  441. if (dev.Cancel)
  442. {
  443. return;
  444. }
  445. if (!SubViewNeedsDraw)
  446. {
  447. return;
  448. }
  449. DrawSubViews (context);
  450. }
  451. /// <summary>
  452. /// Called when the <see cref="SubViews"/> are to be drawn.
  453. /// </summary>
  454. /// <param name="context">The draw context to report drawn areas to, or null if not tracking.</param>
  455. /// <returns><see langword="true"/> to stop further drawing of <see cref="SubViews"/>.</returns>
  456. protected virtual bool OnDrawingSubViews (DrawContext? context) { return false; }
  457. /// <summary>
  458. /// Called when the <see cref="SubViews"/> are to be drawn.
  459. /// </summary>
  460. /// <returns><see langword="true"/> to stop further drawing of <see cref="SubViews"/>.</returns>
  461. protected virtual bool OnDrawingSubViews () { return false; }
  462. /// <summary>Raised when the <see cref="SubViews"/> are to be drawn.</summary>
  463. /// <remarks>
  464. /// </remarks>
  465. /// <returns>
  466. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  467. /// <see cref="SubViews"/>.
  468. /// </returns>
  469. public event EventHandler<DrawEventArgs>? DrawingSubViews;
  470. /// <summary>
  471. /// Draws the <see cref="SubViews"/>.
  472. /// </summary>
  473. /// <param name="context">The draw context to report drawn areas to, or null if not tracking.</param>
  474. public void DrawSubViews (DrawContext? context = null)
  475. {
  476. if (InternalSubViews.Count == 0)
  477. {
  478. return;
  479. }
  480. // Draw the subviews in reverse order to leverage clipping.
  481. foreach (View view in InternalSubViews.Snapshot ().Where (v => v.Visible).Reverse ())
  482. {
  483. // TODO: HACK - This forcing of SetNeedsDraw with SuperViewRendersLineCanvas enables auto line join to work, but is brute force.
  484. if (view.SuperViewRendersLineCanvas || view.ViewportSettings.HasFlag (ViewportSettingsFlags.Transparent))
  485. {
  486. view.SetNeedsDraw ();
  487. }
  488. view.Draw (context);
  489. if (view.SuperViewRendersLineCanvas)
  490. {
  491. LineCanvas.Merge (view.LineCanvas);
  492. view.LineCanvas.Clear ();
  493. }
  494. }
  495. }
  496. #endregion DrawSubViews
  497. #region DrawLineCanvas
  498. private void DoRenderLineCanvas ()
  499. {
  500. if (OnRenderingLineCanvas ())
  501. {
  502. return;
  503. }
  504. // TODO: Add event
  505. RenderLineCanvas ();
  506. }
  507. /// <summary>
  508. /// Called when the <see cref="View.LineCanvas"/> is to be rendered. See <see cref="RenderLineCanvas"/>.
  509. /// </summary>
  510. /// <returns><see langword="true"/> to stop further drawing of <see cref="LineCanvas"/>.</returns>
  511. protected virtual bool OnRenderingLineCanvas () { return false; }
  512. /// <summary>The canvas that any line drawing that is to be shared by subviews of this view should add lines to.</summary>
  513. /// <remarks><see cref="Border"/> adds border lines to this LineCanvas.</remarks>
  514. public LineCanvas LineCanvas { get; } = new ();
  515. /// <summary>
  516. /// Gets or sets whether this View will use it's SuperView's <see cref="LineCanvas"/> for rendering any
  517. /// lines. If <see langword="true"/> the rendering of any borders drawn by this Frame will be done by its parent's
  518. /// SuperView. If <see langword="false"/> (the default) this View's <see cref="OnDrawingAdornments"/> method will
  519. /// be
  520. /// called to render the borders.
  521. /// </summary>
  522. public virtual bool SuperViewRendersLineCanvas { get; set; } = false;
  523. /// <summary>
  524. /// Causes the contents of <see cref="LineCanvas"/> to be drawn.
  525. /// If <see cref="SuperViewRendersLineCanvas"/> is true, only the
  526. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  527. /// false (the default), this method will cause the <see cref="LineCanvas"/> to be rendered.
  528. /// </summary>
  529. public void RenderLineCanvas ()
  530. {
  531. if (Driver is null)
  532. {
  533. return;
  534. }
  535. if (!SuperViewRendersLineCanvas && LineCanvas.Bounds != Rectangle.Empty)
  536. {
  537. foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
  538. {
  539. // Get the entire map
  540. if (p.Value is { })
  541. {
  542. SetAttribute (p.Value.Value.Attribute ?? GetAttributeForRole (VisualRole.Normal));
  543. Driver.Move (p.Key.X, p.Key.Y);
  544. // TODO: #2616 - Support combining sequences that don't normalize
  545. AddRune (p.Value.Value.Rune);
  546. }
  547. }
  548. LineCanvas.Clear ();
  549. }
  550. }
  551. #endregion DrawLineCanvas
  552. #region DrawComplete
  553. private void DoDrawComplete (DrawContext? context)
  554. {
  555. OnDrawComplete (context);
  556. DrawComplete?.Invoke (this, new (Viewport, Viewport, context));
  557. // Now, update the clip to exclude this view (not including Margin)
  558. if (this is not Adornment)
  559. {
  560. if (ViewportSettings.HasFlag (ViewportSettingsFlags.Transparent))
  561. {
  562. // context!.DrawnRegion is the region that was drawn by this view. It may include regions outside
  563. // the Viewport. We need to clip it to the Viewport.
  564. context!.ClipDrawnRegion (ViewportToScreen (Viewport));
  565. // Exclude the drawn region from the clip
  566. ExcludeFromClip (context!.GetDrawnRegion ());
  567. // Exclude the Border and Padding from the clip
  568. ExcludeFromClip (Border?.Thickness.AsRegion (Border.FrameToScreen ()));
  569. ExcludeFromClip (Padding?.Thickness.AsRegion (Padding.FrameToScreen ()));
  570. // QUESTION: This makes it so that no nesting of transparent views is possible, but is more correct?
  571. context = new DrawContext ();
  572. }
  573. else
  574. {
  575. // Exclude this view (not including Margin) from the Clip
  576. Rectangle borderFrame = FrameToScreen ();
  577. if (Border is { })
  578. {
  579. borderFrame = Border.FrameToScreen ();
  580. }
  581. // In the non-transparent (typical case), we want to exclude the entire view area (borderFrame) from the clip
  582. ExcludeFromClip (borderFrame);
  583. // Update context.DrawnRegion to include the entire view (borderFrame), but clipped to our SuperView's viewport
  584. // This enables the SuperView to know what was drawn by this view.
  585. context?.AddDrawnRectangle (borderFrame);
  586. }
  587. }
  588. // TODO: Determine if we need another event that conveys the FINAL DrawContext
  589. }
  590. /// <summary>
  591. /// Called when the View is completed drawing.
  592. /// </summary>
  593. /// <remarks>
  594. /// The <paramref name="context"/> parameter provides the drawn region of the View.
  595. /// </remarks>
  596. protected virtual void OnDrawComplete (DrawContext? context) { }
  597. /// <summary>Raised when the View is completed drawing.</summary>
  598. /// <remarks>
  599. /// </remarks>
  600. public event EventHandler<DrawEventArgs>? DrawComplete;
  601. #endregion DrawComplete
  602. #region NeedsDraw
  603. // TODO: Change NeedsDraw to use a Region instead of Rectangle
  604. // TODO: Make _needsDrawRect nullable instead of relying on Empty
  605. // TODO: If null, it means ?
  606. // TODO: If Empty, it means no need to redraw
  607. // TODO: If not Empty, it means the region that needs to be redrawn
  608. // The viewport-relative region that needs to be redrawn. Marked internal for unit tests.
  609. internal Rectangle NeedsDrawRect { get; set; } = Rectangle.Empty;
  610. /// <summary>Gets or sets whether the view needs to be redrawn.</summary>
  611. /// <remarks>
  612. /// <para>
  613. /// Will be <see langword="true"/> if the <see cref="NeedsLayout"/> property is <see langword="true"/> or if
  614. /// any part of the view's <see cref="Viewport"/> needs to be redrawn.
  615. /// </para>
  616. /// <para>
  617. /// Setting has no effect on <see cref="NeedsLayout"/>.
  618. /// </para>
  619. /// </remarks>
  620. public bool NeedsDraw
  621. {
  622. get => Visible && (NeedsDrawRect != Rectangle.Empty || Margin?.NeedsDraw == true || Border?.NeedsDraw == true || Padding?.NeedsDraw == true);
  623. set
  624. {
  625. if (value)
  626. {
  627. SetNeedsDraw ();
  628. }
  629. else
  630. {
  631. ClearNeedsDraw ();
  632. }
  633. }
  634. }
  635. /// <summary>Gets whether any SubViews need to be redrawn.</summary>
  636. public bool SubViewNeedsDraw { get; private set; }
  637. /// <summary>Sets that the <see cref="Viewport"/> of this View needs to be redrawn.</summary>
  638. /// <remarks>
  639. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), this method
  640. /// does nothing.
  641. /// </remarks>
  642. public void SetNeedsDraw ()
  643. {
  644. Rectangle viewport = Viewport;
  645. if (!Visible || (NeedsDrawRect != Rectangle.Empty && viewport.IsEmpty))
  646. {
  647. // This handles the case where the view has not been initialized yet
  648. return;
  649. }
  650. SetNeedsDraw (viewport);
  651. }
  652. /// <summary>Expands the area of this view needing to be redrawn to include <paramref name="viewPortRelativeRegion"/>.</summary>
  653. /// <remarks>
  654. /// <para>
  655. /// The location of <paramref name="viewPortRelativeRegion"/> is relative to the View's <see cref="Viewport"/>.
  656. /// </para>
  657. /// <para>
  658. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), the area to be
  659. /// redrawn will be the <paramref name="viewPortRelativeRegion"/>.
  660. /// </para>
  661. /// </remarks>
  662. /// <param name="viewPortRelativeRegion">The <see cref="Viewport"/>relative region that needs to be redrawn.</param>
  663. public void SetNeedsDraw (Rectangle viewPortRelativeRegion)
  664. {
  665. if (!Visible)
  666. {
  667. return;
  668. }
  669. if (NeedsDrawRect.IsEmpty)
  670. {
  671. NeedsDrawRect = viewPortRelativeRegion;
  672. }
  673. else
  674. {
  675. int x = Math.Min (Viewport.X, viewPortRelativeRegion.X);
  676. int y = Math.Min (Viewport.Y, viewPortRelativeRegion.Y);
  677. int w = Math.Max (Viewport.Width, viewPortRelativeRegion.Width);
  678. int h = Math.Max (Viewport.Height, viewPortRelativeRegion.Height);
  679. NeedsDrawRect = new (x, y, w, h);
  680. }
  681. // Do not set on Margin - it will be drawn in a separate pass.
  682. if (Border is { } && Border.Thickness != Thickness.Empty)
  683. {
  684. Border?.SetNeedsDraw ();
  685. }
  686. if (Padding is { } && Padding.Thickness != Thickness.Empty)
  687. {
  688. Padding?.SetNeedsDraw ();
  689. }
  690. SuperView?.SetSubViewNeedsDraw ();
  691. if (this is Adornment adornment)
  692. {
  693. adornment.Parent?.SetSubViewNeedsDraw ();
  694. }
  695. // There was multiple enumeration error here, so calling new snapshot collection - probably a stop gap
  696. foreach (View subview in InternalSubViews.Snapshot ())
  697. {
  698. if (subview.Frame.IntersectsWith (viewPortRelativeRegion))
  699. {
  700. Rectangle subviewRegion = Rectangle.Intersect (subview.Frame, viewPortRelativeRegion);
  701. subviewRegion.X -= subview.Frame.X;
  702. subviewRegion.Y -= subview.Frame.Y;
  703. subview.SetNeedsDraw (subviewRegion);
  704. }
  705. }
  706. }
  707. /// <summary>Sets <see cref="SubViewNeedsDraw"/> to <see langword="true"/> for this View and all Superviews.</summary>
  708. public void SetSubViewNeedsDraw ()
  709. {
  710. if (!Visible)
  711. {
  712. return;
  713. }
  714. SubViewNeedsDraw = true;
  715. if (this is Adornment adornment)
  716. {
  717. adornment.Parent?.SetSubViewNeedsDraw ();
  718. }
  719. if (SuperView is { SubViewNeedsDraw: false })
  720. {
  721. SuperView.SetSubViewNeedsDraw ();
  722. }
  723. }
  724. /// <summary>Clears <see cref="NeedsDraw"/> and <see cref="SubViewNeedsDraw"/>.</summary>
  725. protected void ClearNeedsDraw ()
  726. {
  727. NeedsDrawRect = Rectangle.Empty;
  728. SubViewNeedsDraw = false;
  729. if (Margin is { } && (Margin.Thickness != Thickness.Empty || Margin.SubViewNeedsDraw || Margin.NeedsDraw))
  730. {
  731. Margin?.ClearNeedsDraw ();
  732. }
  733. if (Border is { } && (Border.Thickness != Thickness.Empty || Border.SubViewNeedsDraw || Border.NeedsDraw))
  734. {
  735. Border?.ClearNeedsDraw ();
  736. }
  737. if (Padding is { } && (Padding.Thickness != Thickness.Empty || Padding.SubViewNeedsDraw || Padding.NeedsDraw))
  738. {
  739. Padding?.ClearNeedsDraw ();
  740. }
  741. // There was multiple enumeration error here, so calling new snapshot collection - probably a stop gap
  742. foreach (View subview in InternalSubViews.Snapshot ())
  743. {
  744. subview.ClearNeedsDraw ();
  745. }
  746. if (SuperView is { })
  747. {
  748. SuperView.SubViewNeedsDraw = false;
  749. }
  750. // This ensures LineCanvas' get redrawn
  751. if (!SuperViewRendersLineCanvas)
  752. {
  753. LineCanvas.Clear ();
  754. }
  755. }
  756. #endregion NeedsDraw
  757. }