View.Drawing.cs 25 KB

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