ViewDrawing.cs 24 KB

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