ViewDrawing.cs 17 KB

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