View.Drawing.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. #nullable enable
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. namespace Terminal.Gui.ViewBase;
  5. public partial class View // Drawing APIs
  6. {
  7. /// <summary>
  8. /// Draws a set of views.
  9. /// </summary>
  10. /// <param name="views">The peer views to draw.</param>
  11. /// <param name="force">If <see langword="true"/>, <see cref="View.SetNeedsDraw()"/> will be called on each view to force it to be drawn.</param>
  12. internal static void Draw (IEnumerable<View> views, bool force)
  13. {
  14. // **Snapshot once** — every recursion level gets its own frozen array
  15. View [] viewsArray = views.Snapshot ();
  16. // The draw context is used to track the region drawn by each view.
  17. DrawContext context = new DrawContext ();
  18. foreach (View view in viewsArray)
  19. {
  20. if (force)
  21. {
  22. view.SetNeedsDraw ();
  23. }
  24. view.Draw (context);
  25. }
  26. // Draw the margins (those with Shadows) last to ensure they are drawn on top of the content.
  27. Margin.DrawMargins (viewsArray);
  28. }
  29. /// <summary>
  30. /// Draws the view if it needs to be drawn.
  31. /// </summary>
  32. /// <remarks>
  33. /// <para>
  34. /// The view will only be drawn if it is visible, and has any of <see cref="NeedsDraw"/>,
  35. /// <see cref="SubViewNeedsDraw"/>,
  36. /// or <see cref="NeedsLayout"/> set.
  37. /// </para>
  38. /// <para>
  39. /// See the View Drawing Deep Dive for more information: <see href="https://gui-cs.github.io/Terminal.Gui/docs/drawing.html"/>.
  40. /// </para>
  41. /// </remarks>
  42. public void Draw (DrawContext? context = null)
  43. {
  44. if (!CanBeVisible (this))
  45. {
  46. return;
  47. }
  48. Region? originalClip = GetClip ();
  49. // TODO: This can be further optimized by checking NeedsDraw below and only
  50. // TODO: clearing, drawing text, drawing content, etc. if it is true.
  51. if (NeedsDraw || SubViewNeedsDraw)
  52. {
  53. // ------------------------------------
  54. // Draw the Border and Padding.
  55. // Note Margin with a Shadow is special-cased and drawn in a separate pass to support
  56. // transparent shadows.
  57. DoDrawAdornments (originalClip);
  58. SetClip (originalClip);
  59. // ------------------------------------
  60. // Clear the Viewport
  61. // By default, we clip to the viewport preventing drawing outside the viewport
  62. // We also clip to the content, but if a developer wants to draw outside the viewport, they can do
  63. // so via settings. SetClip honors the ViewportSettings.DisableVisibleContentClipping flag.
  64. // Get our Viewport in screen coordinates
  65. originalClip = AddViewportToClip ();
  66. // If no context ...
  67. context ??= new DrawContext ();
  68. SetAttributeForRole (Enabled ? VisualRole.Normal : VisualRole.Disabled);
  69. DoClearViewport (context);
  70. // ------------------------------------
  71. // Draw the subviews first (order matters: SubViews, Text, Content)
  72. if (SubViewNeedsDraw)
  73. {
  74. DoDrawSubViews (context);
  75. }
  76. // ------------------------------------
  77. // Draw the text
  78. SetAttributeForRole (Enabled ? VisualRole.Normal : VisualRole.Disabled);
  79. DoDrawText (context);
  80. // ------------------------------------
  81. // Draw the content
  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. DoDrawAdornmentsSubViews ();
  94. // ------------------------------------
  95. // Advance the diagnostics draw indicator
  96. Border?.AdvanceDrawIndicator ();
  97. ClearNeedsDraw ();
  98. if (this is not Adornment && SuperView is not Adornment)
  99. {
  100. // Parent
  101. Debug.Assert (Margin!.Parent == this);
  102. Debug.Assert (Border!.Parent == this);
  103. Debug.Assert (Padding!.Parent == this);
  104. // SubViewNeedsDraw is set to false by ClearNeedsDraw.
  105. Debug.Assert (SubViewNeedsDraw == false);
  106. Debug.Assert (Margin!.SubViewNeedsDraw == false);
  107. Debug.Assert (Border!.SubViewNeedsDraw == false);
  108. Debug.Assert (Padding!.SubViewNeedsDraw == false);
  109. // NeedsDraw is set to false by ClearNeedsDraw.
  110. Debug.Assert (NeedsDraw == false);
  111. Debug.Assert (Margin!.NeedsDraw == false);
  112. Debug.Assert (Border!.NeedsDraw == false);
  113. Debug.Assert (Padding!.NeedsDraw == false);
  114. }
  115. }
  116. // ------------------------------------
  117. // This causes the Margin to be drawn in a second pass if it has a ShadowStyle
  118. // PERFORMANCE: If there is a Margin w/ Shadow, it will be redrawn each iteration of the main loop.
  119. Margin?.CacheClip ();
  120. // ------------------------------------
  121. // Reset the clip to what it was when we started
  122. SetClip (originalClip);
  123. // ------------------------------------
  124. // We're done drawing - The Clip is reset to what it was before we started.
  125. DoDrawComplete (context);
  126. }
  127. #region DrawAdornments
  128. private void DoDrawAdornmentsSubViews ()
  129. {
  130. // NOTE: We do not support subviews of Margin?
  131. if (Border?.SubViews is { } && Border.Thickness != Thickness.Empty)
  132. {
  133. // PERFORMANCE: Get the check for DrawIndicator out of this somehow.
  134. foreach (View subview in Border.SubViews.Where (v => v.Visible || v.Id == "DrawIndicator"))
  135. {
  136. if (subview.Id != "DrawIndicator")
  137. {
  138. subview.SetNeedsDraw ();
  139. }
  140. LineCanvas.Exclude (new (subview.FrameToScreen ()));
  141. }
  142. Region? saved = Border?.AddFrameToClip ();
  143. Border?.DoDrawSubViews ();
  144. SetClip (saved);
  145. }
  146. if (Padding?.SubViews is { } && Padding.Thickness != Thickness.Empty)
  147. {
  148. foreach (View subview in Padding.SubViews)
  149. {
  150. subview.SetNeedsDraw ();
  151. }
  152. Region? saved = Padding?.AddFrameToClip ();
  153. Padding?.DoDrawSubViews ();
  154. SetClip (saved);
  155. }
  156. }
  157. internal void DoDrawAdornments (Region? originalClip)
  158. {
  159. if (this is Adornment)
  160. {
  161. AddFrameToClip ();
  162. }
  163. else
  164. {
  165. // Set the clip to be just the thicknesses of the adornments
  166. // TODO: Put this union logic in a method on View?
  167. Region? clipAdornments = Margin!.Thickness.AsRegion (Margin!.FrameToScreen ());
  168. clipAdornments?.Combine (Border!.Thickness.AsRegion (Border!.FrameToScreen ()), RegionOp.Union);
  169. clipAdornments?.Combine (Padding!.Thickness.AsRegion (Padding!.FrameToScreen ()), RegionOp.Union);
  170. clipAdornments?.Combine (originalClip, RegionOp.Intersect);
  171. SetClip (clipAdornments);
  172. }
  173. if (Margin?.NeedsLayout == true)
  174. {
  175. Margin.NeedsLayout = false;
  176. Margin?.Thickness.Draw (FrameToScreen ());
  177. Margin?.Parent?.SetSubViewNeedsDraw ();
  178. }
  179. if (SubViewNeedsDraw)
  180. {
  181. // A SubView may add to the LineCanvas. This ensures any Adornment LineCanvas updates happen.
  182. Border?.SetNeedsDraw ();
  183. Padding?.SetNeedsDraw ();
  184. Margin?.SetNeedsDraw ();
  185. }
  186. if (OnDrawingAdornments ())
  187. {
  188. return;
  189. }
  190. // TODO: add event.
  191. DrawAdornments ();
  192. }
  193. /// <summary>
  194. /// Causes <see cref="Margin"/>, <see cref="Border"/>, and <see cref="Padding"/> to be drawn.
  195. /// </summary>
  196. /// <remarks>
  197. /// <para>
  198. /// <see cref="Margin"/> is drawn in a separate pass if <see cref="ShadowStyle"/> is set.
  199. /// </para>
  200. /// </remarks>
  201. public void DrawAdornments ()
  202. {
  203. // We do not attempt to draw Margin. It is drawn in a separate pass.
  204. // Each of these renders lines to this View's LineCanvas
  205. // Those lines will be finally rendered in OnRenderLineCanvas
  206. if (Border is { } && Border.Thickness != Thickness.Empty)
  207. {
  208. Border?.Draw ();
  209. }
  210. if (Padding is { } && Padding.Thickness != Thickness.Empty)
  211. {
  212. Padding?.Draw ();
  213. }
  214. if (Margin is { } && Margin.Thickness != Thickness.Empty/* && Margin.ShadowStyle == ShadowStyle.None*/)
  215. {
  216. //Margin?.Draw ();
  217. }
  218. }
  219. private void ClearFrame ()
  220. {
  221. if (Driver is null)
  222. {
  223. return;
  224. }
  225. // Get screen-relative coords
  226. Rectangle toClear = FrameToScreen ();
  227. Attribute prev = SetAttribute (GetAttributeForRole (VisualRole.Normal));
  228. Driver.FillRect (toClear);
  229. SetAttribute (prev);
  230. SetNeedsDraw ();
  231. }
  232. /// <summary>
  233. /// Called when the View's Adornments are to be drawn. Prepares <see cref="View.LineCanvas"/>. If
  234. /// <see cref="SuperViewRendersLineCanvas"/> is true, only the
  235. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  236. /// false (the default), this method will cause the <see cref="LineCanvas"/> be prepared to be rendered.
  237. /// </summary>
  238. /// <returns><see langword="true"/> to stop further drawing of the Adornments.</returns>
  239. protected virtual bool OnDrawingAdornments () { return false; }
  240. #endregion DrawAdornments
  241. #region ClearViewport
  242. internal void DoClearViewport (DrawContext? context = null)
  243. {
  244. if (ViewportSettings.HasFlag (ViewportSettingsFlags.Transparent) || OnClearingViewport ())
  245. {
  246. return;
  247. }
  248. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  249. ClearingViewport?.Invoke (this, dev);
  250. if (dev.Cancel)
  251. {
  252. // BUGBUG: We should add the Viewport to context.DrawRegion here?
  253. SetNeedsDraw ();
  254. return;
  255. }
  256. if (!ViewportSettings.HasFlag (ViewportSettingsFlags.Transparent))
  257. {
  258. ClearViewport (context);
  259. OnClearedViewport ();
  260. ClearedViewport?.Invoke (this, new (Viewport, Viewport, null));
  261. }
  262. }
  263. /// <summary>
  264. /// Called when the <see cref="Viewport"/> is to be cleared.
  265. /// </summary>
  266. /// <returns><see langword="true"/> to stop further clearing.</returns>
  267. protected virtual bool OnClearingViewport () { return false; }
  268. /// <summary>Event invoked when the <see cref="Viewport"/> is to be cleared.</summary>
  269. /// <remarks>
  270. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  271. /// <para>
  272. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  273. /// <see cref="View"/> .
  274. /// </para>
  275. /// </remarks>
  276. public event EventHandler<DrawEventArgs>? ClearingViewport;
  277. /// <summary>
  278. /// Called when the <see cref="Viewport"/> has been cleared
  279. /// </summary>
  280. protected virtual void OnClearedViewport () { }
  281. /// <summary>Event invoked when the <see cref="Viewport"/> has been cleared.</summary>
  282. public event EventHandler<DrawEventArgs>? ClearedViewport;
  283. /// <summary>Clears <see cref="Viewport"/> with the normal background.</summary>
  284. /// <remarks>
  285. /// <para>
  286. /// If <see cref="ViewportSettings"/> has <see cref="ViewBase.ViewportSettingsFlags.ClearContentOnly"/> only
  287. /// the portion of the content
  288. /// area that is visible within the <see cref="View.Viewport"/> will be cleared. This is useful for views that have
  289. /// a
  290. /// content area larger than the Viewport (e.g. when <see cref="ViewportSettingsFlags.AllowNegativeLocation"/> is
  291. /// enabled) and want
  292. /// the area outside the content to be visually distinct.
  293. /// </para>
  294. /// </remarks>
  295. public void ClearViewport (DrawContext? context = null)
  296. {
  297. if (Driver is null)
  298. {
  299. return;
  300. }
  301. // Get screen-relative coords
  302. Rectangle toClear = ViewportToScreen (Viewport with { Location = new (0, 0) });
  303. if (ViewportSettings.HasFlag (ViewportSettingsFlags.ClearContentOnly))
  304. {
  305. Rectangle visibleContent = ViewportToScreen (new Rectangle (new (-Viewport.X, -Viewport.Y), GetContentSize ()));
  306. toClear = Rectangle.Intersect (toClear, visibleContent);
  307. }
  308. Driver.FillRect (toClear);
  309. // context.AddDrawnRectangle (toClear);
  310. SetNeedsDraw ();
  311. }
  312. #endregion ClearViewport
  313. #region DrawText
  314. private void DoDrawText (DrawContext? context = null)
  315. {
  316. if (!NeedsDraw)
  317. {
  318. return;
  319. }
  320. if (!string.IsNullOrEmpty (TextFormatter.Text))
  321. {
  322. TextFormatter.NeedsFormat = true;
  323. }
  324. if (OnDrawingText (context))
  325. {
  326. return;
  327. }
  328. // TODO: Get rid of this vf in lieu of the one above
  329. if (OnDrawingText ())
  330. {
  331. return;
  332. }
  333. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  334. DrawingText?.Invoke (this, dev);
  335. if (dev.Cancel)
  336. {
  337. return;
  338. }
  339. DrawText (context);
  340. OnDrewText();
  341. DrewText?.Invoke(this, EventArgs.Empty);
  342. }
  343. /// <summary>
  344. /// Called when the <see cref="Text"/> of the View is to be drawn.
  345. /// </summary>
  346. /// <param name="context">The draw context to report drawn areas to.</param>
  347. /// <returns><see langword="true"/> to stop further drawing of <see cref="Text"/>.</returns>
  348. protected virtual bool OnDrawingText (DrawContext? context) { return false; }
  349. /// <summary>
  350. /// Called when the <see cref="Text"/> of the View is to be drawn.
  351. /// </summary>
  352. /// <returns><see langword="true"/> to stop further drawing of <see cref="Text"/>.</returns>
  353. protected virtual bool OnDrawingText () { return false; }
  354. /// <summary>Raised when the <see cref="Text"/> of the View is to be drawn.</summary>
  355. /// <returns>
  356. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  357. /// <see cref="Text"/>.
  358. /// </returns>
  359. public event EventHandler<DrawEventArgs>? DrawingText;
  360. /// <summary>
  361. /// Draws the <see cref="Text"/> of the View using the <see cref="TextFormatter"/>.
  362. /// </summary>
  363. /// <param name="context">The draw context to report drawn areas to.</param>
  364. public void DrawText (DrawContext? context = null)
  365. {
  366. var drawRect = new Rectangle (ContentToScreen (Point.Empty), GetContentSize ());
  367. // Use GetDrawRegion to get precise drawn areas
  368. Region textRegion = TextFormatter.GetDrawRegion (drawRect);
  369. // Report the drawn area to the context
  370. context?.AddDrawnRegion (textRegion);
  371. TextFormatter?.Draw (
  372. drawRect,
  373. HasFocus ? GetAttributeForRole (VisualRole.Focus) : GetAttributeForRole (VisualRole.Normal),
  374. HasFocus ? GetAttributeForRole (VisualRole.HotFocus) : GetAttributeForRole (VisualRole.HotNormal),
  375. Rectangle.Empty
  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. }