ViewDrawing.cs 22 KB

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