View.Drawing.cs 32 KB

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