ViewDrawing.cs 18 KB

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