2
0

ViewDrawing.cs 23 KB

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