View.Drawing.cs 27 KB

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