ViewDrawing.cs 22 KB

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