View.Drawing.cs 30 KB

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