ViewDrawing.cs 18 KB

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