View.Drawing.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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. SetNormalAttribute ();
  69. DoClearViewport (context);
  70. // ------------------------------------
  71. // Draw the subviews first (order matters: SubViews, Text, Content)
  72. if (SubViewNeedsDraw)
  73. {
  74. SetNormalAttribute ();
  75. DoDrawSubViews (context);
  76. }
  77. // ------------------------------------
  78. // Draw the text
  79. SetNormalAttribute ();
  80. DoDrawText (context);
  81. // ------------------------------------
  82. // Draw the content
  83. SetNormalAttribute ();
  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 ClearViewport
  227. internal void DoClearViewport (DrawContext? context = null)
  228. {
  229. if (ViewportSettings.HasFlag (ViewportSettings.Transparent) || OnClearingViewport ())
  230. {
  231. return;
  232. }
  233. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  234. ClearingViewport?.Invoke (this, dev);
  235. if (dev.Cancel)
  236. {
  237. // BUGBUG: We should add the Viewport to context.DrawRegion here?
  238. SetNeedsDraw ();
  239. return;
  240. }
  241. if (!ViewportSettings.HasFlag (ViewportSettings.Transparent))
  242. {
  243. ClearViewport (context);
  244. OnClearedViewport ();
  245. ClearedViewport?.Invoke (this, new (Viewport, Viewport, null));
  246. }
  247. }
  248. /// <summary>
  249. /// Called when the <see cref="Viewport"/> is to be cleared.
  250. /// </summary>
  251. /// <returns><see langword="true"/> to stop further clearing.</returns>
  252. protected virtual bool OnClearingViewport () { return false; }
  253. /// <summary>Event invoked when the <see cref="Viewport"/> is to be cleared.</summary>
  254. /// <remarks>
  255. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  256. /// <para>
  257. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  258. /// <see cref="View"/> .
  259. /// </para>
  260. /// </remarks>
  261. public event EventHandler<DrawEventArgs>? ClearingViewport;
  262. /// <summary>
  263. /// Called when the <see cref="Viewport"/> has been cleared
  264. /// </summary>
  265. protected virtual void OnClearedViewport () { }
  266. /// <summary>Event invoked when the <see cref="Viewport"/> has been cleared.</summary>
  267. public event EventHandler<DrawEventArgs>? ClearedViewport;
  268. /// <summary>Clears <see cref="Viewport"/> with the normal background.</summary>
  269. /// <remarks>
  270. /// <para>
  271. /// If <see cref="ViewportSettings"/> has <see cref="Gui.ViewportSettings.ClearContentOnly"/> only
  272. /// the portion of the content
  273. /// area that is visible within the <see cref="View.Viewport"/> will be cleared. This is useful for views that have
  274. /// a
  275. /// content area larger than the Viewport (e.g. when <see cref="ViewportSettings.AllowNegativeLocation"/> is
  276. /// enabled) and want
  277. /// the area outside the content to be visually distinct.
  278. /// </para>
  279. /// </remarks>
  280. public void ClearViewport (DrawContext? context = null)
  281. {
  282. if (Driver is null)
  283. {
  284. return;
  285. }
  286. // Get screen-relative coords
  287. Rectangle toClear = ViewportToScreen (Viewport with { Location = new (0, 0) });
  288. if (ViewportSettings.HasFlag (ViewportSettings.ClearContentOnly))
  289. {
  290. Rectangle visibleContent = ViewportToScreen (new Rectangle (new (-Viewport.X, -Viewport.Y), GetContentSize ()));
  291. toClear = Rectangle.Intersect (toClear, visibleContent);
  292. }
  293. Attribute prev = SetAttribute (GetNormalColor ());
  294. Driver.FillRect (toClear);
  295. // context.AddDrawnRectangle (toClear);
  296. SetAttribute (prev);
  297. SetNeedsDraw ();
  298. }
  299. #endregion ClearViewport
  300. #region DrawText
  301. private void DoDrawText (DrawContext? context = null)
  302. {
  303. if (OnDrawingText (context))
  304. {
  305. return;
  306. }
  307. // TODO: Get rid of this vf in lieu of the one above
  308. if (OnDrawingText ())
  309. {
  310. return;
  311. }
  312. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  313. DrawingText?.Invoke (this, dev);
  314. if (dev.Cancel)
  315. {
  316. return;
  317. }
  318. DrawText (context);
  319. }
  320. /// <summary>
  321. /// Called when the <see cref="Text"/> of the View is to be drawn.
  322. /// </summary>
  323. /// <param name="context">The draw context to report drawn areas to.</param>
  324. /// <returns><see langword="true"/> to stop further drawing of <see cref="Text"/>.</returns>
  325. protected virtual bool OnDrawingText (DrawContext? context) { return false; }
  326. /// <summary>
  327. /// Called when the <see cref="Text"/> of the View is to be drawn.
  328. /// </summary>
  329. /// <returns><see langword="true"/> to stop further drawing of <see cref="Text"/>.</returns>
  330. protected virtual bool OnDrawingText () { return false; }
  331. /// <summary>Raised when the <see cref="Text"/> of the View is to be drawn.</summary>
  332. /// <returns>
  333. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  334. /// <see cref="Text"/>.
  335. /// </returns>
  336. public event EventHandler<DrawEventArgs>? DrawingText;
  337. /// <summary>
  338. /// Draws the <see cref="Text"/> of the View using the <see cref="TextFormatter"/>.
  339. /// </summary>
  340. /// <param name="context">The draw context to report drawn areas to.</param>
  341. public void DrawText (DrawContext? context = null)
  342. {
  343. if (!string.IsNullOrEmpty (TextFormatter.Text))
  344. {
  345. TextFormatter.NeedsFormat = true;
  346. }
  347. var drawRect = new Rectangle (ContentToScreen (Point.Empty), GetContentSize ());
  348. // Use GetDrawRegion to get precise drawn areas
  349. Region textRegion = TextFormatter.GetDrawRegion (drawRect);
  350. // Report the drawn area to the context
  351. context?.AddDrawnRegion (textRegion);
  352. if (!NeedsDraw)
  353. {
  354. return;
  355. }
  356. TextFormatter?.Draw (
  357. drawRect,
  358. HasFocus ? GetFocusColor () : GetNormalColor (),
  359. HasFocus ? GetHotFocusColor () : GetHotNormalColor (),
  360. Rectangle.Empty
  361. );
  362. // We assume that the text has been drawn over the entire area; ensure that the subviews are redrawn.
  363. SetSubViewNeedsDraw ();
  364. }
  365. #endregion DrawText
  366. #region DrawContent
  367. private void DoDrawContent (DrawContext? context = null)
  368. {
  369. if (OnDrawingContent (context))
  370. {
  371. return;
  372. }
  373. // TODO: Upgrade all overrides of OnDrawingContent to use DrawContext and remove this override
  374. if (OnDrawingContent ())
  375. {
  376. return;
  377. }
  378. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  379. DrawingContent?.Invoke (this, dev);
  380. if (dev.Cancel)
  381. {
  382. return;
  383. }
  384. // No default drawing; let event handlers or overrides handle it
  385. }
  386. /// <summary>
  387. /// Called when the View's content is to be drawn. The default implementation does nothing.
  388. /// </summary>
  389. /// <param name="context">The draw context to report drawn areas to.</param>
  390. /// <returns><see langword="true"/> to stop further drawing content.</returns>
  391. protected virtual bool OnDrawingContent (DrawContext? context) { return false; }
  392. /// <summary>
  393. /// Called when the View's content is to be drawn. The default implementation does nothing.
  394. /// </summary>
  395. /// <returns><see langword="true"/> to stop further drawing content.</returns>
  396. protected virtual bool OnDrawingContent () { return false; }
  397. /// <summary>Raised when the View's content is to be drawn.</summary>
  398. /// <remarks>
  399. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  400. /// <para>
  401. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  402. /// <see cref="View"/> .
  403. /// </para>
  404. /// </remarks>
  405. public event EventHandler<DrawEventArgs>? DrawingContent;
  406. #endregion DrawContent
  407. #region DrawSubViews
  408. private void DoDrawSubViews (DrawContext? context = null)
  409. {
  410. if (OnDrawingSubViews (context))
  411. {
  412. return;
  413. }
  414. // TODO: Get rid of this vf in lieu of the one above
  415. if (OnDrawingSubViews ())
  416. {
  417. return;
  418. }
  419. var dev = new DrawEventArgs (Viewport, Rectangle.Empty, context);
  420. DrawingSubViews?.Invoke (this, dev);
  421. if (dev.Cancel)
  422. {
  423. return;
  424. }
  425. if (!SubViewNeedsDraw)
  426. {
  427. return;
  428. }
  429. DrawSubViews (context);
  430. }
  431. /// <summary>
  432. /// Called when the <see cref="SubViews"/> are to be drawn.
  433. /// </summary>
  434. /// <param name="context">The draw context to report drawn areas to, or null if not tracking.</param>
  435. /// <returns><see langword="true"/> to stop further drawing of <see cref="SubViews"/>.</returns>
  436. protected virtual bool OnDrawingSubViews (DrawContext? context) { return false; }
  437. /// <summary>
  438. /// Called when the <see cref="SubViews"/> are to be drawn.
  439. /// </summary>
  440. /// <returns><see langword="true"/> to stop further drawing of <see cref="SubViews"/>.</returns>
  441. protected virtual bool OnDrawingSubViews () { return false; }
  442. /// <summary>Raised when the <see cref="SubViews"/> are to be drawn.</summary>
  443. /// <remarks>
  444. /// </remarks>
  445. /// <returns>
  446. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> to stop further drawing of
  447. /// <see cref="SubViews"/>.
  448. /// </returns>
  449. public event EventHandler<DrawEventArgs>? DrawingSubViews;
  450. /// <summary>
  451. /// Draws the <see cref="SubViews"/>.
  452. /// </summary>
  453. /// <param name="context">The draw context to report drawn areas to, or null if not tracking.</param>
  454. public void DrawSubViews (DrawContext? context = null)
  455. {
  456. if (InternalSubViews.Count == 0)
  457. {
  458. return;
  459. }
  460. // Draw the subviews in reverse order to leverage clipping.
  461. foreach (View view in InternalSubViews.Where (view => view.Visible).Reverse ())
  462. {
  463. // TODO: HACK - This forcing of SetNeedsDraw with SuperViewRendersLineCanvas enables auto line join to work, but is brute force.
  464. if (view.SuperViewRendersLineCanvas || view.ViewportSettings.HasFlag (ViewportSettings.Transparent))
  465. {
  466. view.SetNeedsDraw ();
  467. }
  468. view.Draw (context);
  469. if (view.SuperViewRendersLineCanvas)
  470. {
  471. LineCanvas.Merge (view.LineCanvas);
  472. view.LineCanvas.Clear ();
  473. }
  474. }
  475. }
  476. #endregion DrawSubViews
  477. #region DrawLineCanvas
  478. private void DoRenderLineCanvas ()
  479. {
  480. if (OnRenderingLineCanvas ())
  481. {
  482. return;
  483. }
  484. // TODO: Add event
  485. RenderLineCanvas ();
  486. }
  487. /// <summary>
  488. /// Called when the <see cref="View.LineCanvas"/> is to be rendered. See <see cref="RenderLineCanvas"/>.
  489. /// </summary>
  490. /// <returns><see langword="true"/> to stop further drawing of <see cref="LineCanvas"/>.</returns>
  491. protected virtual bool OnRenderingLineCanvas () { return false; }
  492. /// <summary>The canvas that any line drawing that is to be shared by subviews of this view should add lines to.</summary>
  493. /// <remarks><see cref="Border"/> adds border lines to this LineCanvas.</remarks>
  494. public LineCanvas LineCanvas { get; } = new ();
  495. /// <summary>
  496. /// Gets or sets whether this View will use it's SuperView's <see cref="LineCanvas"/> for rendering any
  497. /// lines. If <see langword="true"/> the rendering of any borders drawn by this Frame will be done by its parent's
  498. /// SuperView. If <see langword="false"/> (the default) this View's <see cref="OnDrawingAdornments"/> method will
  499. /// be
  500. /// called to render the borders.
  501. /// </summary>
  502. public virtual bool SuperViewRendersLineCanvas { get; set; } = false;
  503. /// <summary>
  504. /// Causes the contents of <see cref="LineCanvas"/> to be drawn.
  505. /// If <see cref="SuperViewRendersLineCanvas"/> is true, only the
  506. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  507. /// false (the default), this method will cause the <see cref="LineCanvas"/> to be rendered.
  508. /// </summary>
  509. public void RenderLineCanvas ()
  510. {
  511. if (Driver is null)
  512. {
  513. return;
  514. }
  515. if (!SuperViewRendersLineCanvas && LineCanvas.Bounds != Rectangle.Empty)
  516. {
  517. foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
  518. {
  519. // Get the entire map
  520. if (p.Value is { })
  521. {
  522. SetAttribute (p.Value.Value.Attribute ?? GetNormalColor ());
  523. Driver.Move (p.Key.X, p.Key.Y);
  524. // TODO: #2616 - Support combining sequences that don't normalize
  525. Driver.AddRune (p.Value.Value.Rune);
  526. }
  527. }
  528. LineCanvas.Clear ();
  529. }
  530. }
  531. #endregion DrawLineCanvas
  532. #region DrawComplete
  533. private void DoDrawComplete (DrawContext? context)
  534. {
  535. OnDrawComplete (context);
  536. DrawComplete?.Invoke (this, new (Viewport, Viewport, context));
  537. // Now, update the clip to exclude this view (not including Margin)
  538. if (this is not Adornment)
  539. {
  540. if (ViewportSettings.HasFlag (ViewportSettings.Transparent))
  541. {
  542. // context!.DrawnRegion is the region that was drawn by this view. It may include regions outside
  543. // the Viewport. We need to clip it to the Viewport.
  544. context!.ClipDrawnRegion (ViewportToScreen (Viewport));
  545. // Exclude the drawn region from the clip
  546. ExcludeFromClip (context!.GetDrawnRegion ());
  547. // Exclude the Border and Padding from the clip
  548. ExcludeFromClip (Border?.Thickness.AsRegion (FrameToScreen ()));
  549. ExcludeFromClip (Padding?.Thickness.AsRegion (FrameToScreen ()));
  550. // QUESTION: This makes it so that no nesting of transparent views is possible, but is more correct?
  551. //context = new DrawContext ();
  552. }
  553. else
  554. {
  555. // Exclude this view (not including Margin) from the Clip
  556. Rectangle borderFrame = FrameToScreen ();
  557. if (Border is { })
  558. {
  559. borderFrame = Border.FrameToScreen ();
  560. }
  561. // In the non-transparent (typical case), we want to exclude the entire view area (borderFrame) from the clip
  562. ExcludeFromClip (borderFrame);
  563. // BUGBUG: There looks like a bug in Region where this Union call is not adding the rectangle right
  564. // Update context.DrawnRegion to include the entire view (borderFrame), but clipped to our SuperView's viewport
  565. // This enables the SuperView to know what was drawn by this view.
  566. context?.AddDrawnRectangle (borderFrame);
  567. }
  568. }
  569. // TODO: Determine if we need another event that conveys the FINAL DrawContext
  570. }
  571. /// <summary>
  572. /// Called when the View is completed drawing.
  573. /// </summary>
  574. /// <remarks>
  575. /// The <paramref name="context"/> parameter provides the drawn region of the View.
  576. /// </remarks>
  577. protected virtual void OnDrawComplete (DrawContext? context) { }
  578. /// <summary>Raised when the View is completed drawing.</summary>
  579. /// <remarks>
  580. /// </remarks>
  581. public event EventHandler<DrawEventArgs>? DrawComplete;
  582. #endregion DrawComplete
  583. #region NeedsDraw
  584. // TODO: Change NeedsDraw to use a Region instead of Rectangle
  585. // TODO: Make _needsDrawRect nullable instead of relying on Empty
  586. // TODO: If null, it means ?
  587. // TODO: If Empty, it means no need to redraw
  588. // TODO: If not Empty, it means the region that needs to be redrawn
  589. // The viewport-relative region that needs to be redrawn. Marked internal for unit tests.
  590. internal Rectangle _needsDrawRect = Rectangle.Empty;
  591. /// <summary>Gets or sets whether the view needs to be redrawn.</summary>
  592. /// <remarks>
  593. /// <para>
  594. /// Will be <see langword="true"/> if the <see cref="NeedsLayout"/> property is <see langword="true"/> or if
  595. /// any part of the view's <see cref="Viewport"/> needs to be redrawn.
  596. /// </para>
  597. /// <para>
  598. /// Setting has no effect on <see cref="NeedsLayout"/>.
  599. /// </para>
  600. /// </remarks>
  601. public bool NeedsDraw
  602. {
  603. // TODO: Figure out if we can decouple NeedsDraw from NeedsLayout.
  604. get => Visible && (_needsDrawRect != Rectangle.Empty || NeedsLayout);
  605. set
  606. {
  607. if (value)
  608. {
  609. SetNeedsDraw ();
  610. }
  611. else
  612. {
  613. ClearNeedsDraw ();
  614. }
  615. }
  616. }
  617. /// <summary>Gets whether any SubViews need to be redrawn.</summary>
  618. public bool SubViewNeedsDraw { get; private set; }
  619. /// <summary>Sets that the <see cref="Viewport"/> of this View needs to be redrawn.</summary>
  620. /// <remarks>
  621. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), this method
  622. /// does nothing.
  623. /// </remarks>
  624. public void SetNeedsDraw ()
  625. {
  626. Rectangle viewport = Viewport;
  627. if (!Visible || (_needsDrawRect != Rectangle.Empty && viewport.IsEmpty))
  628. {
  629. // This handles the case where the view has not been initialized yet
  630. return;
  631. }
  632. SetNeedsDraw (viewport);
  633. }
  634. /// <summary>Expands the area of this view needing to be redrawn to include <paramref name="viewPortRelativeRegion"/>.</summary>
  635. /// <remarks>
  636. /// <para>
  637. /// The location of <paramref name="viewPortRelativeRegion"/> is relative to the View's <see cref="Viewport"/>.
  638. /// </para>
  639. /// <para>
  640. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), the area to be
  641. /// redrawn will be the <paramref name="viewPortRelativeRegion"/>.
  642. /// </para>
  643. /// </remarks>
  644. /// <param name="viewPortRelativeRegion">The <see cref="Viewport"/>relative region that needs to be redrawn.</param>
  645. public void SetNeedsDraw (Rectangle viewPortRelativeRegion)
  646. {
  647. if (!Visible)
  648. {
  649. return;
  650. }
  651. if (_needsDrawRect.IsEmpty)
  652. {
  653. _needsDrawRect = viewPortRelativeRegion;
  654. }
  655. else
  656. {
  657. int x = Math.Min (Viewport.X, viewPortRelativeRegion.X);
  658. int y = Math.Min (Viewport.Y, viewPortRelativeRegion.Y);
  659. int w = Math.Max (Viewport.Width, viewPortRelativeRegion.Width);
  660. int h = Math.Max (Viewport.Height, viewPortRelativeRegion.Height);
  661. _needsDrawRect = new (x, y, w, h);
  662. }
  663. // Do not set on Margin - it will be drawn in a separate pass.
  664. if (Border is { } && Border.Thickness != Thickness.Empty)
  665. {
  666. Border?.SetNeedsDraw ();
  667. }
  668. if (Padding is { } && Padding.Thickness != Thickness.Empty)
  669. {
  670. Padding?.SetNeedsDraw ();
  671. }
  672. SuperView?.SetSubViewNeedsDraw ();
  673. if (this is Adornment adornment)
  674. {
  675. adornment.Parent?.SetSubViewNeedsDraw ();
  676. }
  677. // There was multiple enumeration error here, so calling ToArray - probably a stop gap
  678. foreach (View subview in SubViews.ToArray ())
  679. {
  680. if (subview.Frame.IntersectsWith (viewPortRelativeRegion))
  681. {
  682. Rectangle subviewRegion = Rectangle.Intersect (subview.Frame, viewPortRelativeRegion);
  683. subviewRegion.X -= subview.Frame.X;
  684. subviewRegion.Y -= subview.Frame.Y;
  685. subview.SetNeedsDraw (subviewRegion);
  686. }
  687. }
  688. }
  689. /// <summary>Sets <see cref="SubViewNeedsDraw"/> to <see langword="true"/> for this View and all Superviews.</summary>
  690. public void SetSubViewNeedsDraw ()
  691. {
  692. if (!Visible)
  693. {
  694. return;
  695. }
  696. SubViewNeedsDraw = true;
  697. if (this is Adornment adornment)
  698. {
  699. adornment.Parent?.SetSubViewNeedsDraw ();
  700. }
  701. if (SuperView is { SubViewNeedsDraw: false })
  702. {
  703. SuperView.SetSubViewNeedsDraw ();
  704. }
  705. }
  706. /// <summary>Clears <see cref="NeedsDraw"/> and <see cref="SubViewNeedsDraw"/>.</summary>
  707. protected void ClearNeedsDraw ()
  708. {
  709. _needsDrawRect = Rectangle.Empty;
  710. SubViewNeedsDraw = false;
  711. if (Margin is { } && Margin.Thickness != Thickness.Empty)
  712. {
  713. Margin?.ClearNeedsDraw ();
  714. }
  715. if (Border is { } && Border.Thickness != Thickness.Empty)
  716. {
  717. Border?.ClearNeedsDraw ();
  718. }
  719. if (Padding is { } && Padding.Thickness != Thickness.Empty)
  720. {
  721. Padding?.ClearNeedsDraw ();
  722. }
  723. foreach (View subview in SubViews)
  724. {
  725. subview.ClearNeedsDraw ();
  726. }
  727. if (SuperView is { })
  728. {
  729. SuperView.SubViewNeedsDraw = false;
  730. }
  731. // This ensures LineCanvas' get redrawn
  732. if (!SuperViewRendersLineCanvas)
  733. {
  734. LineCanvas.Clear ();
  735. }
  736. }
  737. #endregion NeedsDraw
  738. }