ScrollBarView.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. //
  2. // ScrollBarView.cs: ScrollBarView view.
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. namespace Terminal.Gui {
  9. /// <summary>
  10. /// ScrollBarViews are views that display a 1-character scrollbar, either horizontal or vertical
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>
  14. /// The scrollbar is drawn to be a representation of the Size, assuming that the
  15. /// scroll position is set at Position.
  16. /// </para>
  17. /// <para>
  18. /// If the region to display the scrollbar is larger than three characters,
  19. /// arrow indicators are drawn.
  20. /// </para>
  21. /// </remarks>
  22. public class ScrollBarView : View {
  23. bool vertical;
  24. int size, position;
  25. bool showScrollIndicator;
  26. bool keepContentAlwaysInViewport = true;
  27. bool autoHideScrollBars = true;
  28. Dim originalHostWidth, originalHostHeight;
  29. bool hosted;
  30. bool showBothScrollIndicator => OtherScrollBarView != null && OtherScrollBarView.showScrollIndicator && showScrollIndicator;
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  33. /// </summary>
  34. /// <param name="rect">Frame for the scrollbar.</param>
  35. public ScrollBarView (Rect rect) : this (rect, 0, 0, false) { }
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  38. /// </summary>
  39. /// <param name="rect">Frame for the scrollbar.</param>
  40. /// <param name="size">The size that this scrollbar represents. Sets the <see cref="Size"/> property.</param>
  41. /// <param name="position">The position within this scrollbar. Sets the <see cref="Position"/> property.</param>
  42. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal. Sets the <see cref="IsVertical"/> property.</param>
  43. public ScrollBarView (Rect rect, int size, int position, bool isVertical) : base (rect)
  44. {
  45. Init (size, position, isVertical);
  46. }
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  49. /// </summary>
  50. public ScrollBarView () : this (0, 0, false) { }
  51. /// <summary>
  52. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  53. /// </summary>
  54. /// <param name="size">The size that this scrollbar represents.</param>
  55. /// <param name="position">The position within this scrollbar.</param>
  56. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.</param>
  57. public ScrollBarView (int size, int position, bool isVertical) : base ()
  58. {
  59. Init (size, position, isVertical);
  60. }
  61. /// <summary>
  62. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  63. /// </summary>
  64. public ScrollBarView (View host, bool isVertical) : this (0, 0, isVertical)
  65. {
  66. if (host == null) {
  67. throw new ArgumentNullException ("The host parameter can't be null.");
  68. } else if (host.SuperView == null) {
  69. throw new ArgumentNullException ("The host SuperView parameter can't be null.");
  70. }
  71. hosted = true;
  72. originalHostWidth = host.Width;
  73. originalHostHeight = host.Height;
  74. X = isVertical ? Pos.Right(host) : Pos.Left (host);
  75. Y = isVertical ? Pos.Top (host) : Pos.Bottom (host);
  76. Host = host;
  77. Host.SuperView.Add (this);
  78. ShowScrollIndicator = true;
  79. AutoHideScrollBars = true;
  80. }
  81. void Init (int size, int position, bool isVertical)
  82. {
  83. vertical = isVertical;
  84. this.position = position;
  85. this.size = size;
  86. WantContinuousButtonPressed = true;
  87. }
  88. /// <summary>
  89. /// If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.
  90. /// </summary>
  91. public bool IsVertical {
  92. get => vertical;
  93. set {
  94. vertical = value;
  95. SetNeedsDisplay ();
  96. }
  97. }
  98. /// <summary>
  99. /// The size of content the scrollbar represents.
  100. /// </summary>
  101. /// <value>The size.</value>
  102. /// <remarks>The <see cref="Size"/> is typically the size of the virtual content. E.g. when a Scrollbar is
  103. /// part of a <see cref="View"/> the Size is set to the appropriate dimension of <see cref="Host"/>.</remarks>
  104. public int Size {
  105. get => size;
  106. set {
  107. size = value;
  108. ShowHideScrollBars ();
  109. SetNeedsDisplay ();
  110. }
  111. }
  112. /// <summary>
  113. /// This event is raised when the position on the scrollbar has changed.
  114. /// </summary>
  115. public event Action ChangedPosition;
  116. /// <summary>
  117. /// The position, relative to <see cref="Size"/>, to set the scrollbar at.
  118. /// </summary>
  119. /// <value>The position.</value>
  120. public int Position {
  121. get => position;
  122. set {
  123. if (position != value) {
  124. if (CanScroll (value - position, out int max, vertical) || max > 0) {
  125. if (max > 0 && max == value - position) {
  126. position = value;
  127. } else {
  128. position = Math.Max (max, 0);
  129. }
  130. }
  131. OnChangedPosition ();
  132. SetNeedsDisplay ();
  133. }
  134. }
  135. }
  136. /// <summary>
  137. /// Get or sets the view that host this <see cref="View"/>
  138. /// </summary>
  139. public View Host { get; internal set; }
  140. /// <summary>
  141. /// Represent a vertical or horizontal ScrollBarView other than this.
  142. /// </summary>
  143. public ScrollBarView OtherScrollBarView { get; set; }
  144. /// <summary>
  145. /// Gets or sets the visibility for the vertical or horizontal scroll indicator.
  146. /// </summary>
  147. /// <value><c>true</c> if show vertical or horizontal scroll indicator; otherwise, <c>false</c>.</value>
  148. public bool ShowScrollIndicator {
  149. get => showScrollIndicator;
  150. set {
  151. if (value == showScrollIndicator) {
  152. return;
  153. }
  154. showScrollIndicator = value;
  155. SetNeedsLayout ();
  156. if (value) {
  157. Visible = true;
  158. } else {
  159. Visible = false;
  160. }
  161. Width = vertical ? 1 : Dim.Width (Host);
  162. Height = vertical ? Dim.Height (Host) : 1;
  163. if (vertical) {
  164. Host.Width = showScrollIndicator ? originalHostWidth - 1 : originalHostWidth;
  165. } else {
  166. Host.Height = showScrollIndicator ? originalHostHeight - 1 : originalHostHeight;
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// Get or sets if the view-port is kept always visible in the area of this <see cref="ScrollBarView"/>
  172. /// </summary>
  173. public bool KeepContentAlwaysInViewport {
  174. get { return keepContentAlwaysInViewport; }
  175. set {
  176. if (keepContentAlwaysInViewport != value) {
  177. keepContentAlwaysInViewport = value;
  178. int pos = 0;
  179. if (value && !vertical && position + Host.Bounds.Width > size) {
  180. pos = size - Host.Bounds.Width + (showBothScrollIndicator ? 1 : 0);
  181. }
  182. if (value && vertical && position + Host.Bounds.Height > size) {
  183. pos = size - Host.Bounds.Height + (showBothScrollIndicator ? 1 : 0);
  184. }
  185. if (pos != 0) {
  186. Position = pos;
  187. }
  188. if (OtherScrollBarView != null && OtherScrollBarView.keepContentAlwaysInViewport != value) {
  189. OtherScrollBarView.KeepContentAlwaysInViewport = value;
  190. }
  191. }
  192. }
  193. }
  194. /// <summary>
  195. /// If true the vertical/horizontal scroll bars won't be showed if it's not needed.
  196. /// </summary>
  197. public bool AutoHideScrollBars {
  198. get => autoHideScrollBars;
  199. set {
  200. if (autoHideScrollBars != value) {
  201. autoHideScrollBars = value;
  202. SetNeedsDisplay ();
  203. }
  204. }
  205. }
  206. void SetPosition (int newPos)
  207. {
  208. Position = newPos;
  209. OnChangedPosition ();
  210. }
  211. /// <summary>
  212. /// Virtual method to invoke the <see cref="ChangedPosition"/> action event.
  213. /// </summary>
  214. public virtual void OnChangedPosition ()
  215. {
  216. ChangedPosition?.Invoke ();
  217. }
  218. internal bool pending;
  219. void ShowHideScrollBars ()
  220. {
  221. if (!hosted || !autoHideScrollBars) {
  222. return;
  223. }
  224. int barsize = vertical ? Bounds.Height : Bounds.Width;
  225. if (barsize == 0 || barsize > size) {
  226. if (showScrollIndicator) {
  227. ShowScrollIndicator = false;
  228. }
  229. } else if (barsize > 0 && barsize == size && OtherScrollBarView != null && OtherScrollBarView.pending) {
  230. if (showScrollIndicator) {
  231. ShowScrollIndicator = false;
  232. }
  233. if (OtherScrollBarView != null && showBothScrollIndicator) {
  234. OtherScrollBarView.ShowScrollIndicator = false;
  235. }
  236. } else if (barsize > 0 && barsize == size && OtherScrollBarView != null && !OtherScrollBarView.pending) {
  237. pending = true;
  238. OtherScrollBarView.Redraw (OtherScrollBarView.Bounds);
  239. } else {
  240. if (OtherScrollBarView != null && OtherScrollBarView.pending) {
  241. if (!showBothScrollIndicator) {
  242. OtherScrollBarView.ShowScrollIndicator = true;
  243. OtherScrollBarView.Redraw (OtherScrollBarView.Bounds);
  244. }
  245. }
  246. if (!showScrollIndicator) {
  247. ShowScrollIndicator = true;
  248. }
  249. }
  250. if (OtherScrollBarView != null) {
  251. OtherScrollBarView.pending = false;
  252. }
  253. }
  254. int posTopTee;
  255. int posLeftTee;
  256. int posBottomTee;
  257. int posRightTee;
  258. ///<inheritdoc/>
  259. public override void Redraw (Rect region)
  260. {
  261. if (ColorScheme == null || Size == 0) {
  262. return;
  263. }
  264. Driver.SetAttribute (ColorScheme.Normal);
  265. if ((vertical && Bounds.Height == 0) || (!vertical && Bounds.Width == 0)) {
  266. return;
  267. }
  268. if (vertical) {
  269. if (region.Right < Bounds.Width - 1) {
  270. return;
  271. }
  272. var col = Bounds.Width - 1;
  273. var bh = Bounds.Height;
  274. Rune special;
  275. if (bh < 4) {
  276. var by1 = position * bh / Size;
  277. var by2 = (position + bh) * bh / Size;
  278. Move (col, 0);
  279. if (Bounds.Height == 1) {
  280. Driver.AddRune (Driver.Diamond);
  281. } else {
  282. Driver.AddRune (Driver.UpArrow);
  283. }
  284. if (Bounds.Height == 3) {
  285. Move (col, 1);
  286. Driver.AddRune (Driver.Diamond);
  287. }
  288. if (Bounds.Height > 1) {
  289. Move (col, Bounds.Height - 1);
  290. Driver.AddRune (Driver.DownArrow);
  291. }
  292. } else {
  293. bh -= 2;
  294. var by1 = position * bh / Size;
  295. var by2 = KeepContentAlwaysInViewport ? Math.Min (((position + bh) * bh / Size) + 1, bh - 1) : (position + bh) * bh / Size;
  296. if (KeepContentAlwaysInViewport && by1 == by2) {
  297. by1 = Math.Max (by1 - 1, 0);
  298. }
  299. Move (col, 0);
  300. Driver.AddRune (Driver.UpArrow);
  301. Move (col, Bounds.Height - 1);
  302. Driver.AddRune (Driver.DownArrow);
  303. bool hasTopTee = false;
  304. bool hasDiamond = false;
  305. bool hasBottomTee = false;
  306. for (int y = 0; y < bh; y++) {
  307. Move (col, y + 1);
  308. if ((y < by1 || y > by2) && ((position > 0 && !hasTopTee) || (hasTopTee && hasBottomTee))) {
  309. special = Driver.Stipple;
  310. } else {
  311. if (y != by2 && y > 1 && by2 - by1 == 0 && by1 < bh - 1 && hasTopTee && !hasDiamond) {
  312. hasDiamond = true;
  313. special = Driver.Diamond;
  314. } else {
  315. if (y == by1 && !hasTopTee) {
  316. hasTopTee = true;
  317. posTopTee = y;
  318. special = Driver.TopTee;
  319. } else if ((position == 0 && y == bh - 1 || y >= by2 || by2 == 0) && !hasBottomTee) {
  320. hasBottomTee = true;
  321. posBottomTee = y;
  322. special = Driver.BottomTee;
  323. } else {
  324. special = Driver.VLine;
  325. }
  326. }
  327. }
  328. Driver.AddRune (special);
  329. }
  330. if (!hasTopTee) {
  331. Move (col, Bounds.Height - 2);
  332. Driver.AddRune (Driver.TopTee);
  333. }
  334. }
  335. } else {
  336. if (region.Bottom < Bounds.Height - 1) {
  337. return;
  338. }
  339. var row = Bounds.Height - 1;
  340. var bw = Bounds.Width;
  341. Rune special;
  342. if (bw < 4) {
  343. var bx1 = position * bw / Size;
  344. var bx2 = (position + bw) * bw / Size;
  345. Move (0, row);
  346. Driver.AddRune (Driver.LeftArrow);
  347. Driver.AddRune (Driver.RightArrow);
  348. } else {
  349. bw -= 2;
  350. var bx1 = position * bw / Size;
  351. var bx2 = KeepContentAlwaysInViewport ? Math.Min (((position + bw) * bw / Size) + 1, bw - 1) : (position + bw) * bw / Size;
  352. if (KeepContentAlwaysInViewport && bx1 == bx2) {
  353. bx1 = Math.Max (bx1 - 1, 0);
  354. }
  355. Move (0, row);
  356. Driver.AddRune (Driver.LeftArrow);
  357. bool hasLeftTee = false;
  358. bool hasDiamond = false;
  359. bool hasRightTee = false;
  360. for (int x = 0; x < bw; x++) {
  361. if ((x < bx1 || x >= bx2 + 1) && ((position > 0 && !hasLeftTee) || (hasLeftTee && hasRightTee))) {
  362. special = Driver.Stipple;
  363. } else {
  364. if (x != bx2 && x > 1 && bx2 - bx1 == 0 && bx1 < bw - 1 && hasLeftTee && !hasDiamond) {
  365. hasDiamond = true;
  366. special = Driver.Diamond;
  367. } else {
  368. if (x == bx1 && !hasLeftTee) {
  369. hasLeftTee = true;
  370. posLeftTee = x;
  371. special = Driver.LeftTee;
  372. } else if ((position == 0 && x == bw - 1 || x >= bx2 || bx2 == 0) && !hasRightTee) {
  373. hasRightTee = true;
  374. posRightTee = x;
  375. special = Driver.RightTee;
  376. } else {
  377. special = Driver.HLine;
  378. }
  379. }
  380. }
  381. Driver.AddRune (special);
  382. }
  383. if (!hasLeftTee) {
  384. Move (Bounds.Width - 2, row);
  385. Driver.AddRune (Driver.LeftTee);
  386. }
  387. Driver.AddRune (Driver.RightArrow);
  388. }
  389. }
  390. }
  391. int lastLocation = -1;
  392. ///<inheritdoc/>
  393. public override bool MouseEvent (MouseEvent me)
  394. {
  395. if (me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  396. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  397. return false;
  398. }
  399. if (!me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  400. lastLocation = -1;
  401. }
  402. int location = vertical ? me.Y : me.X;
  403. int barsize = vertical ? Bounds.Height : Bounds.Width;
  404. int posTopLeftTee = vertical ? posTopTee : posLeftTee;
  405. int posBottomRightTee = vertical ? posBottomTee : posRightTee;
  406. barsize -= 2;
  407. var pos = Position;
  408. if (location == 0) {
  409. if (pos > 0) {
  410. SetPosition (pos - 1);
  411. }
  412. } else if (location == barsize + 1) {
  413. if (CanScroll (1, out _, vertical)) {
  414. SetPosition (pos + 1);
  415. }
  416. } else if (location > 0 && location < barsize + 1) {
  417. var b1 = pos * barsize / Size;
  418. var b2 = KeepContentAlwaysInViewport ? Math.Min (((pos + barsize) * barsize / Size) + 1, barsize - 1) : (pos + barsize) * barsize / Size;
  419. if (KeepContentAlwaysInViewport && b1 == b2) {
  420. b1 = Math.Max (b1 - 1, 0);
  421. }
  422. if (location > b1 && location <= b2 + 1) {
  423. if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1Clicked) {
  424. if (location == 1) {
  425. SetPosition (0);
  426. } else if (location == barsize) {
  427. CanScroll (Size - pos, out int nv, vertical);
  428. if (nv > 0) {
  429. SetPosition (Math.Min (pos + nv, Size));
  430. }
  431. }
  432. } else if (me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  433. var mb = (b2 - b1) / 2;
  434. var ml = mb + b1 + (mb == 0 ? 1 : 0);
  435. if ((location >= b1 && location <= ml) || (location < lastLocation && lastLocation > -1)) {
  436. lastLocation = location;
  437. var np = b1 * Size / barsize;
  438. SetPosition (np);
  439. } else if (location > lastLocation) {
  440. var np = location * Size / barsize;
  441. CanScroll (np - pos, out int nv, vertical);
  442. if (nv > 0) {
  443. SetPosition (pos + nv);
  444. }
  445. }
  446. }
  447. } else {
  448. if (location >= b2 + 1 && location > posTopLeftTee && location > b1 && location > posBottomRightTee && posBottomRightTee > 0) {
  449. CanScroll (location, out int nv, vertical);
  450. if (nv > 0) {
  451. SetPosition (Math.Min (pos + nv, Size));
  452. }
  453. } else if (location <= b1) {
  454. SetPosition (Math.Max (pos - barsize - location, 0));
  455. }
  456. }
  457. }
  458. return true;
  459. }
  460. internal bool CanScroll (int n, out int max, bool isVertical = false)
  461. {
  462. var s = isVertical ?
  463. (KeepContentAlwaysInViewport ? Host.Bounds.Height + (showBothScrollIndicator ? -2 : -1) : 0) :
  464. (KeepContentAlwaysInViewport ? Host.Bounds.Width + (showBothScrollIndicator ? -2 : -1) : 0);
  465. var newSize = Math.Min (size, position + n);
  466. max = size > s + newSize ? n : size - (s + position) - 1;
  467. if (size > s + newSize) {
  468. return true;
  469. }
  470. return false;
  471. }
  472. }
  473. }