View.Drawing.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. public partial class View // Drawing APIs
  4. {
  5. private ColorScheme? _colorScheme;
  6. /// <summary>The color scheme for this view, if it is not defined, it returns the <see cref="SuperView"/>'s color scheme.</summary>
  7. public virtual ColorScheme? ColorScheme
  8. {
  9. get
  10. {
  11. if (_colorScheme is null)
  12. {
  13. return SuperView?.ColorScheme;
  14. }
  15. return _colorScheme;
  16. }
  17. set
  18. {
  19. if (_colorScheme != value)
  20. {
  21. _colorScheme = value;
  22. if (Border is { } && Border.LineStyle != LineStyle.None && Border.ColorScheme is { })
  23. {
  24. Border.ColorScheme = _colorScheme;
  25. }
  26. SetNeedsDisplay ();
  27. }
  28. }
  29. }
  30. /// <summary>The canvas that any line drawing that is to be shared by subviews of this view should add lines to.</summary>
  31. /// <remarks><see cref="Border"/> adds border lines to this LineCanvas.</remarks>
  32. public LineCanvas LineCanvas { get; } = new ();
  33. // The view-relative region that needs to be redrawn. Marked internal for unit tests.
  34. internal Rectangle _needsDisplayRect = Rectangle.Empty;
  35. /// <summary>Gets or sets whether the view needs to be redrawn.</summary>
  36. public bool NeedsDisplay
  37. {
  38. get => _needsDisplayRect != Rectangle.Empty;
  39. set
  40. {
  41. if (value)
  42. {
  43. SetNeedsDisplay ();
  44. }
  45. else
  46. {
  47. ClearNeedsDisplay ();
  48. }
  49. }
  50. }
  51. /// <summary>Gets whether any Subviews need to be redrawn.</summary>
  52. public bool SubViewNeedsDisplay { get; private set; }
  53. /// <summary>
  54. /// Gets or sets whether this View will use it's SuperView's <see cref="LineCanvas"/> for rendering any
  55. /// lines. If <see langword="true"/> the rendering of any borders drawn by this Frame will be done by its parent's
  56. /// SuperView. If <see langword="false"/> (the default) this View's <see cref="OnDrawAdornments"/> method will be
  57. /// called to render the borders.
  58. /// </summary>
  59. public virtual bool SuperViewRendersLineCanvas { get; set; } = false;
  60. /// <summary>Draws the specified character in the specified viewport-relative column and row of the View.</summary>
  61. /// <para>
  62. /// If the provided coordinates are outside the visible content area, this method does nothing.
  63. /// </para>
  64. /// <remarks>
  65. /// The top-left corner of the visible content area is <c>ViewPort.Location</c>.
  66. /// </remarks>
  67. /// <param name="col">Column (viewport-relative).</param>
  68. /// <param name="row">Row (viewport-relative).</param>
  69. /// <param name="rune">The Rune.</param>
  70. public void AddRune (int col, int row, Rune rune)
  71. {
  72. if (Move (col, row))
  73. {
  74. Driver.AddRune (rune);
  75. }
  76. }
  77. /// <summary>Clears <see cref="Viewport"/> with the normal background.</summary>
  78. /// <remarks>
  79. /// <para>
  80. /// If <see cref="ViewportSettings"/> has <see cref="Gui.ViewportSettings.ClearContentOnly"/> only
  81. /// the portion of the content
  82. /// area that is visible within the <see cref="View.Viewport"/> will be cleared. This is useful for views that have
  83. /// a
  84. /// content area larger than the Viewport (e.g. when <see cref="ViewportSettings.AllowNegativeLocation"/> is
  85. /// enabled) and want
  86. /// the area outside the content to be visually distinct.
  87. /// </para>
  88. /// </remarks>
  89. public void Clear ()
  90. {
  91. if (Driver is null)
  92. {
  93. return;
  94. }
  95. // Get screen-relative coords
  96. Rectangle toClear = ViewportToScreen (Viewport with { Location = new (0, 0) });
  97. Rectangle prevClip = Driver.Clip;
  98. if (ViewportSettings.HasFlag (ViewportSettings.ClearContentOnly))
  99. {
  100. Rectangle visibleContent = ViewportToScreen (new Rectangle (new (-Viewport.X, -Viewport.Y), GetContentSize ()));
  101. toClear = Rectangle.Intersect (toClear, visibleContent);
  102. }
  103. Attribute prev = Driver.SetAttribute (GetNormalColor ());
  104. Driver.FillRect (toClear);
  105. Driver.SetAttribute (prev);
  106. Driver.Clip = prevClip;
  107. }
  108. /// <summary>Fills the specified <see cref="Viewport"/>-relative rectangle with the specified color.</summary>
  109. /// <param name="rect">The Viewport-relative rectangle to clear.</param>
  110. /// <param name="color">The color to use to fill the rectangle. If not provided, the Normal background color will be used.</param>
  111. public void FillRect (Rectangle rect, Color? color = null)
  112. {
  113. if (Driver is null)
  114. {
  115. return;
  116. }
  117. // Get screen-relative coords
  118. Rectangle toClear = ViewportToScreen (rect);
  119. Rectangle prevClip = Driver.Clip;
  120. Driver.Clip = Rectangle.Intersect (prevClip, ViewportToScreen (Viewport with { Location = new (0, 0) }));
  121. Attribute prev = Driver.SetAttribute (new (color ?? GetNormalColor ().Background));
  122. Driver.FillRect (toClear);
  123. Driver.SetAttribute (prev);
  124. Driver.Clip = prevClip;
  125. }
  126. /// <summary>Sets the <see cref="ConsoleDriver"/>'s clip region to <see cref="Viewport"/>.</summary>
  127. /// <remarks>
  128. /// <para>
  129. /// By default, the clip rectangle is set to the intersection of the current clip region and the
  130. /// <see cref="Viewport"/>. This ensures that drawing is constrained to the viewport, but allows
  131. /// content to be drawn beyond the viewport.
  132. /// </para>
  133. /// <para>
  134. /// If <see cref="ViewportSettings"/> has <see cref="Gui.ViewportSettings.ClipContentOnly"/> set, clipping will be
  135. /// applied to just the visible content area.
  136. /// </para>
  137. /// </remarks>
  138. /// <returns>
  139. /// The current screen-relative clip region, which can be then re-applied by setting
  140. /// <see cref="ConsoleDriver.Clip"/>.
  141. /// </returns>
  142. public Rectangle SetClip ()
  143. {
  144. if (Driver is null)
  145. {
  146. return Rectangle.Empty;
  147. }
  148. Rectangle previous = Driver.Clip;
  149. // Clamp the Clip to the entire visible area
  150. Rectangle clip = Rectangle.Intersect (ViewportToScreen (Viewport with { Location = Point.Empty }), previous);
  151. if (ViewportSettings.HasFlag (ViewportSettings.ClipContentOnly))
  152. {
  153. // Clamp the Clip to the just content area that is within the viewport
  154. Rectangle visibleContent = ViewportToScreen (new Rectangle (new (-Viewport.X, -Viewport.Y), GetContentSize ()));
  155. clip = Rectangle.Intersect (clip, visibleContent);
  156. }
  157. Driver.Clip = clip;
  158. return previous;
  159. }
  160. /// <summary>
  161. /// Draws the view if it needs to be drawn. Causes the following virtual methods to be called (along with their related events):
  162. /// <see cref="OnDrawContent"/>, <see cref="OnDrawContentComplete"/>.
  163. /// </summary>
  164. /// <remarks>
  165. /// <para>
  166. /// The view will only be drawn if it is visible, and has any of <see cref="NeedsDisplay"/>, <see cref="SubViewNeedsDisplay"/>,
  167. /// or <see cref="LayoutNeeded"/> set.
  168. /// </para>
  169. /// <para>
  170. /// Always use <see cref="Viewport"/> (view-relative) when calling <see cref="OnDrawContent(Rectangle)"/>, NOT
  171. /// <see cref="Frame"/> (superview-relative).
  172. /// </para>
  173. /// <para>
  174. /// Views should set the color that they want to use on entry, as otherwise this will inherit the last color that
  175. /// was set globally on the driver.
  176. /// </para>
  177. /// <para>
  178. /// Overrides of <see cref="OnDrawContent(Rectangle)"/> must ensure they do not set <c>Driver.Clip</c> to a clip
  179. /// region larger than the <ref name="Viewport"/> property, as this will cause the driver to clip the entire
  180. /// region.
  181. /// </para>
  182. /// </remarks>
  183. public void Draw ()
  184. {
  185. if (!CanBeVisible (this))
  186. {
  187. return;
  188. }
  189. // TODO: This ensures overlapped views are drawn correctly. However, this is inefficient.
  190. // TODO: The correct fix is to implement non-rectangular clip regions: https://github.com/gui-cs/Terminal.Gui/issues/3413
  191. if (Arrangement.HasFlag (ViewArrangement.Overlapped))
  192. {
  193. SetNeedsDisplay ();
  194. }
  195. if (!NeedsDisplay && !SubViewNeedsDisplay && !LayoutNeeded)
  196. {
  197. return;
  198. }
  199. OnDrawAdornments ();
  200. if (ColorScheme is { })
  201. {
  202. //Driver.SetAttribute (HasFocus ? GetFocusColor () : GetNormalColor ());
  203. Driver?.SetAttribute (GetNormalColor ());
  204. }
  205. // By default, we clip to the viewport preventing drawing outside the viewport
  206. // We also clip to the content, but if a developer wants to draw outside the viewport, they can do
  207. // so via settings. SetClip honors the ViewportSettings.DisableVisibleContentClipping flag.
  208. Rectangle prevClip = SetClip ();
  209. // Invoke DrawContentEvent
  210. var dev = new DrawEventArgs (Viewport, Rectangle.Empty);
  211. DrawContent?.Invoke (this, dev);
  212. if (!dev.Cancel)
  213. {
  214. OnDrawContent (Viewport);
  215. }
  216. if (Driver is { })
  217. {
  218. Driver.Clip = prevClip;
  219. }
  220. OnRenderLineCanvas ();
  221. // TODO: This is a hack to force the border subviews to draw.
  222. if (Border?.Subviews is { })
  223. {
  224. foreach (View view in Border.Subviews)
  225. {
  226. view.SetNeedsDisplay ();
  227. view.Draw ();
  228. }
  229. }
  230. // Invoke DrawContentCompleteEvent
  231. OnDrawContentComplete (Viewport);
  232. // BUGBUG: v2 - We should be able to use View.SetClip here and not have to resort to knowing Driver details.
  233. ClearLayoutNeeded ();
  234. ClearNeedsDisplay ();
  235. }
  236. /// <summary>Event invoked when the content area of the View is to be drawn.</summary>
  237. /// <remarks>
  238. /// <para>Will be invoked before any subviews added with <see cref="Add(View)"/> have been drawn.</para>
  239. /// <para>
  240. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  241. /// <see cref="View"/> .
  242. /// </para>
  243. /// </remarks>
  244. public event EventHandler<DrawEventArgs>? DrawContent;
  245. /// <summary>Event invoked when the content area of the View is completed drawing.</summary>
  246. /// <remarks>
  247. /// <para>Will be invoked after any subviews removed with <see cref="Remove(View)"/> have been completed drawing.</para>
  248. /// <para>
  249. /// Rect provides the view-relative rectangle describing the currently visible viewport into the
  250. /// <see cref="View"/> .
  251. /// </para>
  252. /// </remarks>
  253. public event EventHandler<DrawEventArgs>? DrawContentComplete;
  254. /// <summary>Utility function to draw strings that contain a hotkey.</summary>
  255. /// <param name="text">String to display, the hotkey specifier before a letter flags the next letter as the hotkey.</param>
  256. /// <param name="hotColor">Hot color.</param>
  257. /// <param name="normalColor">Normal color.</param>
  258. /// <remarks>
  259. /// <para>
  260. /// The hotkey is any character following the hotkey specifier, which is the underscore ('_') character by
  261. /// default.
  262. /// </para>
  263. /// <para>The hotkey specifier can be changed via <see cref="HotKeySpecifier"/></para>
  264. /// </remarks>
  265. public void DrawHotString (string text, Attribute hotColor, Attribute normalColor)
  266. {
  267. Rune hotkeySpec = HotKeySpecifier == (Rune)0xffff ? (Rune)'_' : HotKeySpecifier;
  268. Application.Driver?.SetAttribute (normalColor);
  269. foreach (Rune rune in text.EnumerateRunes ())
  270. {
  271. if (rune == new Rune (hotkeySpec.Value))
  272. {
  273. Application.Driver?.SetAttribute (hotColor);
  274. continue;
  275. }
  276. Application.Driver?.AddRune (rune);
  277. Application.Driver?.SetAttribute (normalColor);
  278. }
  279. }
  280. /// <summary>
  281. /// Utility function to draw strings that contains a hotkey using a <see cref="ColorScheme"/> and the "focused"
  282. /// state.
  283. /// </summary>
  284. /// <param name="text">String to display, the underscore before a letter flags the next letter as the hotkey.</param>
  285. /// <param name="focused">
  286. /// If set to <see langword="true"/> this uses the focused colors from the color scheme, otherwise
  287. /// the regular ones.
  288. /// </param>
  289. public void DrawHotString (string text, bool focused)
  290. {
  291. if (focused)
  292. {
  293. DrawHotString (text, GetHotFocusColor (), GetFocusColor ());
  294. }
  295. else
  296. {
  297. DrawHotString (
  298. text,
  299. Enabled ? GetHotNormalColor () : ColorScheme.Disabled,
  300. Enabled ? GetNormalColor () : ColorScheme.Disabled
  301. );
  302. }
  303. }
  304. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  305. /// <returns>
  306. /// <see cref="ColorScheme.Focus"/> if <see cref="Enabled"/> is <see langword="true"/> or
  307. /// <see cref="ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  308. /// overridden can return other values.
  309. /// </returns>
  310. public virtual Attribute GetFocusColor ()
  311. {
  312. ColorScheme? cs = ColorScheme;
  313. if (cs is null)
  314. {
  315. cs = new ();
  316. }
  317. return Enabled ? GetColor (cs.Focus) : cs.Disabled;
  318. }
  319. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  320. /// <returns>
  321. /// <see cref="ColorScheme.Focus"/> if <see cref="Enabled"/> is <see langword="true"/> or
  322. /// <see cref="ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  323. /// overridden can return other values.
  324. /// </returns>
  325. public virtual Attribute GetHotFocusColor ()
  326. {
  327. ColorScheme cs = ColorScheme;
  328. if (cs is null)
  329. {
  330. cs = new ();
  331. }
  332. return Enabled ? GetColor (cs.HotFocus) : cs.Disabled;
  333. }
  334. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  335. /// <returns>
  336. /// <see cref="Terminal.Gui.ColorScheme.HotNormal"/> if <see cref="Enabled"/> is <see langword="true"/> or
  337. /// <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  338. /// overridden can return other values.
  339. /// </returns>
  340. public virtual Attribute GetHotNormalColor ()
  341. {
  342. ColorScheme? cs = ColorScheme;
  343. if (cs is null)
  344. {
  345. cs = new ();
  346. }
  347. return Enabled ? GetColor (cs.HotNormal) : cs.Disabled;
  348. }
  349. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  350. /// <returns>
  351. /// <see cref="Terminal.Gui.ColorScheme.Normal"/> if <see cref="Enabled"/> is <see langword="true"/> or
  352. /// <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  353. /// overridden can return other values.
  354. /// </returns>
  355. public virtual Attribute GetNormalColor ()
  356. {
  357. ColorScheme? cs = ColorScheme;
  358. if (cs is null)
  359. {
  360. cs = new ();
  361. }
  362. Attribute disabled = new (cs.Disabled.Foreground, cs.Disabled.Background);
  363. if (Diagnostics.HasFlag (ViewDiagnosticFlags.Hover) && _hovering)
  364. {
  365. disabled = new (disabled.Foreground.GetDarkerColor (), disabled.Background.GetDarkerColor ());
  366. }
  367. return Enabled ? GetColor (cs.Normal) : disabled;
  368. }
  369. private Attribute GetColor (Attribute inputAttribute)
  370. {
  371. Attribute attr = inputAttribute;
  372. if (Diagnostics.HasFlag (ViewDiagnosticFlags.Hover) && _hovering)
  373. {
  374. attr = new (attr.Foreground.GetDarkerColor (), attr.Background.GetDarkerColor ());
  375. }
  376. return attr;
  377. }
  378. /// <summary>Moves the drawing cursor to the specified <see cref="Viewport"/>-relative location in the view.</summary>
  379. /// <remarks>
  380. /// <para>
  381. /// If the provided coordinates are outside the visible content area, this method does nothing.
  382. /// </para>
  383. /// <para>
  384. /// The top-left corner of the visible content area is <c>ViewPort.Location</c>.
  385. /// </para>
  386. /// </remarks>
  387. /// <param name="col">Column (viewport-relative).</param>
  388. /// <param name="row">Row (viewport-relative).</param>
  389. public bool Move (int col, int row)
  390. {
  391. if (Driver is null || Driver?.Rows == 0)
  392. {
  393. return false;
  394. }
  395. if (col < 0 || row < 0 || col >= Viewport.Width || row >= Viewport.Height)
  396. {
  397. return false;
  398. }
  399. Point screen = ViewportToScreen (new Point (col, row));
  400. Driver?.Move (screen.X, screen.Y);
  401. return true;
  402. }
  403. // TODO: Make this cancelable
  404. /// <summary>
  405. /// Prepares <see cref="View.LineCanvas"/>. If <see cref="SuperViewRendersLineCanvas"/> is true, only the
  406. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  407. /// false (the default), this method will cause the <see cref="LineCanvas"/> be prepared to be rendered.
  408. /// </summary>
  409. /// <returns></returns>
  410. public virtual bool OnDrawAdornments ()
  411. {
  412. if (!IsInitialized)
  413. {
  414. return false;
  415. }
  416. // Each of these renders lines to either this View's LineCanvas
  417. // Those lines will be finally rendered in OnRenderLineCanvas
  418. Margin?.OnDrawContent (Margin.Viewport);
  419. Border?.OnDrawContent (Border.Viewport);
  420. Padding?.OnDrawContent (Padding.Viewport);
  421. return true;
  422. }
  423. /// <summary>
  424. /// Draws the view's content, including Subviews.
  425. /// </summary>
  426. /// <remarks>
  427. /// <para>
  428. /// The <paramref name="viewport"/> parameter is provided as a convenience; it has the same values as the
  429. /// <see cref="Viewport"/> property.
  430. /// </para>
  431. /// <para>
  432. /// The <see cref="Viewport"/> Location and Size indicate what part of the View's content, defined
  433. /// by <see cref="GetContentSize ()"/>, is visible and should be drawn. The coordinates taken by <see cref="Move"/>
  434. /// and
  435. /// <see cref="AddRune"/> are relative to <see cref="Viewport"/>, thus if <c>ViewPort.Location.Y</c> is <c>5</c>
  436. /// the 6th row of the content should be drawn using <c>MoveTo (x, 5)</c>.
  437. /// </para>
  438. /// <para>
  439. /// If <see cref="GetContentSize ()"/> is larger than <c>ViewPort.Size</c> drawing code should use
  440. /// <see cref="Viewport"/>
  441. /// to constrain drawing for better performance.
  442. /// </para>
  443. /// <para>
  444. /// The <see cref="ConsoleDriver.Clip"/> may define smaller area than <see cref="Viewport"/>; complex drawing code
  445. /// can be more
  446. /// efficient by using <see cref="ConsoleDriver.Clip"/> to constrain drawing for better performance.
  447. /// </para>
  448. /// <para>
  449. /// Overrides should loop through the subviews and call <see cref="Draw"/>.
  450. /// </para>
  451. /// </remarks>
  452. /// <param name="viewport">
  453. /// The rectangle describing the currently visible viewport into the <see cref="View"/>; has the same value as
  454. /// <see cref="Viewport"/>.
  455. /// </param>
  456. public virtual void OnDrawContent (Rectangle viewport)
  457. {
  458. if (NeedsDisplay)
  459. {
  460. if (!CanBeVisible (this))
  461. {
  462. return;
  463. }
  464. // BUGBUG: this clears way too frequently. Need to optimize this.
  465. if (SuperView is { } || Arrangement.HasFlag (ViewArrangement.Overlapped))
  466. {
  467. Clear ();
  468. }
  469. if (!string.IsNullOrEmpty (TextFormatter.Text))
  470. {
  471. if (TextFormatter is { })
  472. {
  473. TextFormatter.NeedsFormat = true;
  474. }
  475. }
  476. // This should NOT clear
  477. // TODO: If the output is not in the Viewport, do nothing
  478. var drawRect = new Rectangle (ContentToScreen (Point.Empty), GetContentSize ());
  479. TextFormatter?.Draw (
  480. drawRect,
  481. HasFocus ? GetFocusColor () : GetNormalColor (),
  482. HasFocus ? GetHotFocusColor () : GetHotNormalColor (),
  483. Rectangle.Empty
  484. );
  485. SetSubViewNeedsDisplay ();
  486. }
  487. // TODO: Move drawing of subviews to a separate OnDrawSubviews virtual method
  488. // Draw subviews
  489. // TODO: Implement OnDrawSubviews (cancelable);
  490. if (_subviews is { } && SubViewNeedsDisplay)
  491. {
  492. IEnumerable<View> subviewsNeedingDraw = _subviews.Where (
  493. view => view.Visible
  494. && (view.NeedsDisplay
  495. || view.SubViewNeedsDisplay
  496. || view.LayoutNeeded
  497. || view.Arrangement.HasFlag (ViewArrangement.Overlapped)
  498. ));
  499. foreach (View view in subviewsNeedingDraw)
  500. {
  501. if (view.LayoutNeeded)
  502. {
  503. view.LayoutSubviews ();
  504. }
  505. // TODO: This ensures overlapped views are drawn correctly. However, this is inefficient.
  506. // TODO: The correct fix is to implement non-rectangular clip regions: https://github.com/gui-cs/Terminal.Gui/issues/3413
  507. if (view.Arrangement.HasFlag (ViewArrangement.Overlapped))
  508. {
  509. view.SetNeedsDisplay ();
  510. }
  511. view.Draw ();
  512. }
  513. }
  514. }
  515. /// <summary>
  516. /// Called after <see cref="OnDrawContent"/> to enable overrides.
  517. /// </summary>
  518. /// <param name="viewport">
  519. /// The viewport-relative rectangle describing the currently visible viewport into the
  520. /// <see cref="View"/>
  521. /// </param>
  522. public virtual void OnDrawContentComplete (Rectangle viewport) { DrawContentComplete?.Invoke (this, new (viewport, Rectangle.Empty)); }
  523. // TODO: Make this cancelable
  524. /// <summary>
  525. /// Renders <see cref="View.LineCanvas"/>. If <see cref="SuperViewRendersLineCanvas"/> is true, only the
  526. /// <see cref="LineCanvas"/> of this view's subviews will be rendered. If <see cref="SuperViewRendersLineCanvas"/> is
  527. /// false (the default), this method will cause the <see cref="LineCanvas"/> to be rendered.
  528. /// </summary>
  529. /// <returns></returns>
  530. public virtual bool OnRenderLineCanvas ()
  531. {
  532. if (!IsInitialized || Driver is null)
  533. {
  534. return false;
  535. }
  536. // If we have a SuperView, it'll render our frames.
  537. if (!SuperViewRendersLineCanvas && LineCanvas.Viewport != Rectangle.Empty)
  538. {
  539. foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
  540. {
  541. // Get the entire map
  542. if (p.Value is { })
  543. {
  544. Driver.SetAttribute (p.Value.Value.Attribute ?? ColorScheme!.Normal);
  545. Driver.Move (p.Key.X, p.Key.Y);
  546. // TODO: #2616 - Support combining sequences that don't normalize
  547. Driver.AddRune (p.Value.Value.Rune);
  548. }
  549. }
  550. LineCanvas.Clear ();
  551. }
  552. if (Subviews.Any (s => s.SuperViewRendersLineCanvas))
  553. {
  554. foreach (View subview in Subviews.Where (s => s.SuperViewRendersLineCanvas))
  555. {
  556. // Combine the LineCanvas'
  557. LineCanvas.Merge (subview.LineCanvas);
  558. subview.LineCanvas.Clear ();
  559. }
  560. foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
  561. {
  562. // Get the entire map
  563. if (p.Value is { })
  564. {
  565. Driver.SetAttribute (p.Value.Value.Attribute ?? ColorScheme!.Normal);
  566. Driver.Move (p.Key.X, p.Key.Y);
  567. // TODO: #2616 - Support combining sequences that don't normalize
  568. Driver.AddRune (p.Value.Value.Rune);
  569. }
  570. }
  571. LineCanvas.Clear ();
  572. }
  573. return true;
  574. }
  575. /// <summary>Sets the area of this view needing to be redrawn to <see cref="Viewport"/>.</summary>
  576. /// <remarks>
  577. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), this method
  578. /// does nothing.
  579. /// </remarks>
  580. public void SetNeedsDisplay () { SetNeedsDisplay (Viewport); }
  581. /// <summary>Expands the area of this view needing to be redrawn to include <paramref name="region"/>.</summary>
  582. /// <remarks>
  583. /// <para>
  584. /// The location of <paramref name="region"/> is relative to the View's content, bound by <c>Size.Empty</c> and
  585. /// <see cref="GetContentSize ()"/>.
  586. /// </para>
  587. /// <para>
  588. /// If the view has not been initialized (<see cref="IsInitialized"/> is <see langword="false"/>), the area to be
  589. /// redrawn will be the <paramref name="region"/>.
  590. /// </para>
  591. /// </remarks>
  592. /// <param name="region">The content-relative region that needs to be redrawn.</param>
  593. public void SetNeedsDisplay (Rectangle region)
  594. {
  595. if (_needsDisplayRect.IsEmpty)
  596. {
  597. _needsDisplayRect = region;
  598. }
  599. else
  600. {
  601. int x = Math.Min (_needsDisplayRect.X, region.X);
  602. int y = Math.Min (_needsDisplayRect.Y, region.Y);
  603. int w = Math.Max (_needsDisplayRect.Width, region.Width);
  604. int h = Math.Max (_needsDisplayRect.Height, region.Height);
  605. _needsDisplayRect = new (x, y, w, h);
  606. }
  607. Margin?.SetNeedsDisplay ();
  608. Border?.SetNeedsDisplay ();
  609. Padding?.SetNeedsDisplay ();
  610. SuperView?.SetSubViewNeedsDisplay ();
  611. foreach (View subview in Subviews)
  612. {
  613. if (subview.Frame.IntersectsWith (region))
  614. {
  615. Rectangle subviewRegion = Rectangle.Intersect (subview.Frame, region);
  616. subviewRegion.X -= subview.Frame.X;
  617. subviewRegion.Y -= subview.Frame.Y;
  618. subview.SetNeedsDisplay (subviewRegion);
  619. }
  620. }
  621. }
  622. /// <summary>Sets <see cref="SubViewNeedsDisplay"/> to <see langword="true"/> for this View and all Superviews.</summary>
  623. public void SetSubViewNeedsDisplay ()
  624. {
  625. SubViewNeedsDisplay = true;
  626. if (this is Adornment adornment)
  627. {
  628. adornment.Parent?.SetSubViewNeedsDisplay ();
  629. }
  630. if (SuperView is { SubViewNeedsDisplay: false })
  631. {
  632. SuperView.SetSubViewNeedsDisplay ();
  633. }
  634. }
  635. /// <summary>Clears <see cref="NeedsDisplay"/> and <see cref="SubViewNeedsDisplay"/>.</summary>
  636. protected void ClearNeedsDisplay ()
  637. {
  638. _needsDisplayRect = Rectangle.Empty;
  639. SubViewNeedsDisplay = false;
  640. Margin?.ClearNeedsDisplay ();
  641. Border?.ClearNeedsDisplay ();
  642. Padding?.ClearNeedsDisplay ();
  643. foreach (View subview in Subviews)
  644. {
  645. subview.ClearNeedsDisplay ();
  646. }
  647. }
  648. }