View.Drawing.cs 32 KB

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