View.Drawing.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. if (OnDrawingContent ())
  401. {
  402. return;
  403. }
  404. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  405. DrawingContent?.Invoke (this, dev);
  406. if (dev.Cancel)
  407. {
  408. return;
  409. }
  410. // No default drawing; let event handlers or overrides handle it
  411. }
  412. /// <summary>
  413. /// Called when the View's content is to be drawn. The default implementation does nothing.
  414. /// </summary>
  415. /// <param name="context">The draw context to report drawn areas to.</param>
  416. /// <returns><see langword="true"/> to stop further drawing content.</returns>
  417. protected virtual bool OnDrawingContent (DrawContext? context = null) { return false; }
  418. /// <summary>
  419. /// Called when the View's content is to be drawn. The default implementation does nothing.
  420. /// </summary>
  421. /// <returns><see langword="true"/> to stop further drawing content.</returns>
  422. protected virtual bool OnDrawingContent () { return false; }
  423. /// <summary>Raised when the View's content is to be drawn.</summary>
  424. /// <remarks>
  425. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  426. /// <para>
  427. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  428. /// <see cref="View"/> .
  429. /// </para>
  430. /// </remarks>
  431. public event EventHandler<DrawEventArgs>? DrawingContent;
  432. #endregion DrawContent
  433. #region DrawSubviews
  434. private void DoDrawSubviews (DrawContext? context = null)
  435. {
  436. if (OnDrawingSubviews (context))
  437. {
  438. return;
  439. }
  440. if (OnDrawingSubviews ())
  441. {
  442. return;
  443. }
  444. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  445. DrawingSubviews?.Invoke (this, dev);
  446. if (dev.Cancel)
  447. {
  448. return;
  449. }
  450. if (!SubViewNeedsDraw)
  451. {
  452. return;
  453. }
  454. DrawSubviews (context);
  455. }
  456. /// <summary>
  457. /// Called when the <see cref="Subviews"/> are to be drawn.
  458. /// </summary>
  459. /// <param name="context">The draw context to report drawn areas to, or null if not tracking.</param>
  460. /// <returns><see langword="true"/> to stop further drawing of <see cref="Subviews"/>.</returns>
  461. protected virtual bool OnDrawingSubviews (DrawContext? context) { return false; }
  462. /// <summary>
  463. /// Called when the <see cref="Subviews"/> are to be drawn.
  464. /// </summary>
  465. /// <returns><see langword="true"/> to stop further drawing of <see cref="Subviews"/>.</returns>
  466. protected virtual bool OnDrawingSubviews () { return false; }
  467. /// <summary>Raised when the <see cref="Subviews"/> are to be drawn.</summary>
  468. /// <remarks>
  469. /// </remarks>
  470. /// <returns>
  471. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  472. /// <see cref="Subviews"/>.
  473. /// </returns>
  474. public event EventHandler<DrawEventArgs>? DrawingSubviews;
  475. /// <summary>
  476. /// Draws the <see cref="Subviews"/>.
  477. /// </summary>
  478. /// <param name="context">The draw context to report drawn areas to, or null if not tracking.</param>
  479. public void DrawSubviews (DrawContext? context = null)
  480. {
  481. if (_subviews is null)
  482. {
  483. return;
  484. }
  485. // Draw the subviews in reverse order to leverage clipping.
  486. foreach (View view in _subviews.Where (view => view.Visible).Reverse ())
  487. {
  488. // TODO: HACK - This forcing of SetNeedsDraw with SuperViewRendersLineCanvas enables auto line join to work, but is brute force.
  489. if (view.SuperViewRendersLineCanvas || view.ViewportSettings.HasFlag (ViewportSettings.Transparent))
  490. {
  491. view.SetNeedsDraw ();
  492. }
  493. view.Draw (context);
  494. if (view.SuperViewRendersLineCanvas)
  495. {
  496. LineCanvas.Merge (view.LineCanvas);
  497. view.LineCanvas.Clear ();
  498. }
  499. }
  500. }
  501. #endregion DrawSubviews
  502. #region DrawLineCanvas
  503. private void DoRenderLineCanvas ()
  504. {
  505. if (OnRenderingLineCanvas ())
  506. {
  507. return;
  508. }
  509. // TODO: Add event
  510. RenderLineCanvas ();
  511. }
  512. /// <summary>
  513. /// Called when the <see cref="View.LineCanvas"/> is to be rendered. See <see cref="RenderLineCanvas"/>.
  514. /// </summary>
  515. /// <returns><see langword="true"/> to stop further drawing of <see cref="LineCanvas"/>.</returns>
  516. protected virtual bool OnRenderingLineCanvas () { return false; }
  517. /// <summary>The canvas that any line drawing that is to be shared by subviews of this view should add lines to.</summary>
  518. /// <remarks><see cref="Border"/> adds border lines to this LineCanvas.</remarks>
  519. public LineCanvas LineCanvas { get; } = new ();
  520. /// <summary>
  521. /// Gets or sets whether this View will use it's SuperView's <see cref="LineCanvas"/> for rendering any
  522. /// lines. If <see langword="true"/> the rendering of any borders drawn by this Frame will be done by its parent's
  523. /// SuperView. If <see langword="false"/> (the default) this View's <see cref="OnDrawingBorderAndPadding"/> method will
  524. /// be
  525. /// called to render the borders.
  526. /// </summary>
  527. public virtual bool SuperViewRendersLineCanvas { get; set; } = false;
  528. /// <summary>
  529. /// Causes the contents of <see cref="LineCanvas"/> to be drawn.
  530. /// If <see cref="SuperViewRendersLineCanvas"/> is true, only the
  531. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  532. /// false (the default), this method will cause the <see cref="LineCanvas"/> to be rendered.
  533. /// </summary>
  534. public void RenderLineCanvas ()
  535. {
  536. if (Driver is null)
  537. {
  538. return;
  539. }
  540. if (!SuperViewRendersLineCanvas && LineCanvas.Bounds != Rectangle.Empty)
  541. {
  542. foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
  543. {
  544. // Get the entire map
  545. if (p.Value is { })
  546. {
  547. SetAttribute (p.Value.Value.Attribute ?? ColorScheme!.Normal);
  548. Driver.Move (p.Key.X, p.Key.Y);
  549. // TODO: #2616 - Support combining sequences that don't normalize
  550. Driver.AddRune (p.Value.Value.Rune);
  551. }
  552. }
  553. LineCanvas.Clear ();
  554. }
  555. }
  556. #endregion DrawLineCanvas
  557. #region DrawComplete
  558. private void DoDrawComplete (DrawContext? context)
  559. {
  560. OnDrawComplete (context);
  561. DrawComplete?.Invoke (this, new (Viewport, Viewport, context));
  562. // Now, update the clip to exclude this view (not including Margin)
  563. if (this is not Adornment)
  564. {
  565. if (ViewportSettings.HasFlag (ViewportSettings.Transparent))
  566. {
  567. // context!.DrawnRegion is the region that was drawn by this view. It may include regions outside
  568. // the Viewport. We need to clip it to the Viewport.
  569. context!.ClipDrawnRegion (ViewportToScreen (Viewport));
  570. // Exclude the drawn region from the clip
  571. ExcludeFromClip (context!.GetDrawnRegion ());
  572. // Exclude the Border and Padding from the clip
  573. ExcludeFromClip (Border?.Thickness.AsRegion (FrameToScreen ()));
  574. ExcludeFromClip (Padding?.Thickness.AsRegion (FrameToScreen ()));
  575. }
  576. else
  577. {
  578. // Exclude this view (not including Margin) from the Clip
  579. Rectangle borderFrame = FrameToScreen ();
  580. if (Border is { })
  581. {
  582. borderFrame = Border.FrameToScreen ();
  583. }
  584. // In the non-transparent (typical case), we want to exclude the entire view area (borderFrame) from the clip
  585. ExcludeFromClip (borderFrame);
  586. // Update context.DrawnRegion to include the entire view (borderFrame), but clipped to our SuperView's viewport
  587. // This enables the SuperView to know what was drawn by this view.
  588. context?.AddDrawnRectangle (borderFrame);
  589. }
  590. }
  591. // TODO: Determine if we need another event that conveys the FINAL DrawContext
  592. }
  593. /// <summary>
  594. /// Called when the View is completed drawing.
  595. /// </summary>
  596. /// <remarks>
  597. /// The <paramref name="context"/> parameter provides the drawn region of the View.
  598. /// </remarks>
  599. protected virtual void OnDrawComplete (DrawContext? context) { }
  600. /// <summary>Raised when the View is completed drawing.</summary>
  601. /// <remarks>
  602. /// </remarks>
  603. public event EventHandler<DrawEventArgs>? DrawComplete;
  604. #endregion DrawComplete
  605. #region NeedsDraw
  606. // TODO: Change NeedsDraw to use a Region instead of Rectangle
  607. // TODO: Make _needsDrawRect nullable instead of relying on Empty
  608. // TODO: If null, it means ?
  609. // TODO: If Empty, it means no need to redraw
  610. // TODO: If not Empty, it means the region that needs to be redrawn
  611. // The viewport-relative region that needs to be redrawn. Marked internal for unit tests.
  612. internal Rectangle _needsDrawRect = Rectangle.Empty;
  613. /// <summary>Gets or sets whether the view needs to be redrawn.</summary>
  614. /// <remarks>
  615. /// <para>
  616. /// Will be <see langword="true"/> if the <see cref="NeedsLayout"/> property is <see langword="true"/> or if
  617. /// any part of the view's <see cref="Viewport"/> needs to be redrawn.
  618. /// </para>
  619. /// <para>
  620. /// Setting has no effect on <see cref="NeedsLayout"/>.
  621. /// </para>
  622. /// </remarks>
  623. public bool NeedsDraw
  624. {
  625. // TODO: Figure out if we can decouple NeedsDraw from NeedsLayout.
  626. get => Visible && (_needsDrawRect != Rectangle.Empty || NeedsLayout);
  627. set
  628. {
  629. if (value)
  630. {
  631. SetNeedsDraw ();
  632. }
  633. else
  634. {
  635. ClearNeedsDraw ();
  636. }
  637. }
  638. }
  639. /// <summary>Gets whether any Subviews need to be redrawn.</summary>
  640. public bool SubViewNeedsDraw { get; private set; }
  641. /// <summary>Sets that the <see cref="Viewport"/> of this View needs to be redrawn.</summary>
  642. /// <remarks>
  643. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), this method
  644. /// does nothing.
  645. /// </remarks>
  646. public void SetNeedsDraw ()
  647. {
  648. Rectangle viewport = Viewport;
  649. if (!Visible || (_needsDrawRect != Rectangle.Empty && viewport.IsEmpty))
  650. {
  651. // This handles the case where the view has not been initialized yet
  652. return;
  653. }
  654. SetNeedsDraw (viewport);
  655. }
  656. /// <summary>Expands the area of this view needing to be redrawn to include <paramref name="viewPortRelativeRegion"/>.</summary>
  657. /// <remarks>
  658. /// <para>
  659. /// The location of <paramref name="viewPortRelativeRegion"/> is relative to the View's <see cref="Viewport"/>.
  660. /// </para>
  661. /// <para>
  662. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), the area to be
  663. /// redrawn will be the <paramref name="viewPortRelativeRegion"/>.
  664. /// </para>
  665. /// </remarks>
  666. /// <param name="viewPortRelativeRegion">The <see cref="Viewport"/>relative region that needs to be redrawn.</param>
  667. public void SetNeedsDraw (Rectangle viewPortRelativeRegion)
  668. {
  669. if (!Visible)
  670. {
  671. return;
  672. }
  673. if (_needsDrawRect.IsEmpty)
  674. {
  675. _needsDrawRect = viewPortRelativeRegion;
  676. }
  677. else
  678. {
  679. int x = Math.Min (Viewport.X, viewPortRelativeRegion.X);
  680. int y = Math.Min (Viewport.Y, viewPortRelativeRegion.Y);
  681. int w = Math.Max (Viewport.Width, viewPortRelativeRegion.Width);
  682. int h = Math.Max (Viewport.Height, viewPortRelativeRegion.Height);
  683. _needsDrawRect = new (x, y, w, h);
  684. }
  685. // Do not set on Margin - it will be drawn in a separate pass.
  686. if (Border is { } && Border.Thickness != Thickness.Empty)
  687. {
  688. Border?.SetNeedsDraw ();
  689. }
  690. if (Padding is { } && Padding.Thickness != Thickness.Empty)
  691. {
  692. Padding?.SetNeedsDraw ();
  693. }
  694. SuperView?.SetSubViewNeedsDraw ();
  695. if (this is Adornment adornment)
  696. {
  697. adornment.Parent?.SetSubViewNeedsDraw ();
  698. }
  699. // There was multiple enumeration error here, so calling ToArray - probably a stop gap
  700. foreach (View subview in Subviews.ToArray ())
  701. {
  702. if (subview.Frame.IntersectsWith (viewPortRelativeRegion))
  703. {
  704. Rectangle subviewRegion = Rectangle.Intersect (subview.Frame, viewPortRelativeRegion);
  705. subviewRegion.X -= subview.Frame.X;
  706. subviewRegion.Y -= subview.Frame.Y;
  707. subview.SetNeedsDraw (subviewRegion);
  708. }
  709. }
  710. }
  711. /// <summary>Sets <see cref="SubViewNeedsDraw"/> to <see langword="true"/> for this View and all Superviews.</summary>
  712. public void SetSubViewNeedsDraw ()
  713. {
  714. if (!Visible)
  715. {
  716. return;
  717. }
  718. SubViewNeedsDraw = true;
  719. if (this is Adornment adornment)
  720. {
  721. adornment.Parent?.SetSubViewNeedsDraw ();
  722. }
  723. if (SuperView is { SubViewNeedsDraw: false })
  724. {
  725. SuperView.SetSubViewNeedsDraw ();
  726. }
  727. }
  728. /// <summary>Clears <see cref="NeedsDraw"/> and <see cref="SubViewNeedsDraw"/>.</summary>
  729. protected void ClearNeedsDraw ()
  730. {
  731. _needsDrawRect = Rectangle.Empty;
  732. SubViewNeedsDraw = false;
  733. if (Margin is { } && Margin.Thickness != Thickness.Empty)
  734. {
  735. Margin?.ClearNeedsDraw ();
  736. }
  737. if (Border is { } && Border.Thickness != Thickness.Empty)
  738. {
  739. Border?.ClearNeedsDraw ();
  740. }
  741. if (Padding is { } && Padding.Thickness != Thickness.Empty)
  742. {
  743. Padding?.ClearNeedsDraw ();
  744. }
  745. foreach (View subview in Subviews)
  746. {
  747. subview.ClearNeedsDraw ();
  748. }
  749. if (SuperView is { })
  750. {
  751. SuperView.SubViewNeedsDraw = false;
  752. }
  753. // This ensures LineCanvas' get redrawn
  754. if (!SuperViewRendersLineCanvas)
  755. {
  756. LineCanvas.Clear ();
  757. }
  758. }
  759. #endregion NeedsDraw
  760. }