View.Drawing.cs 31 KB

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