View.Drawing.cs 28 KB

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