ViewDrawing.cs 20 KB

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