ScrollBarView.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. //
  2. // ScrollBarView.cs: ScrollBarView view.
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System.Diagnostics;
  8. namespace Terminal.Gui;
  9. /// <summary>ScrollBarViews are views that display a 1-character scrollbar, either horizontal or vertical</summary>
  10. /// <remarks>
  11. /// <para>
  12. /// The scrollbar is drawn to be a representation of the Size, assuming that the scroll position is set at
  13. /// Position.
  14. /// </para>
  15. /// <para>If the region to display the scrollbar is larger than three characters, arrow indicators are drawn.</para>
  16. /// </remarks>
  17. public class ScrollBarView : View
  18. {
  19. private bool _autoHideScrollBars = true;
  20. private View _contentBottomRightCorner;
  21. private bool _hosted;
  22. private bool _keepContentAlwaysInViewport = true;
  23. private int _lastLocation = -1;
  24. private ScrollBarView _otherScrollBarView;
  25. private int _posBarOffset;
  26. private int _posBottomTee;
  27. private int _posLeftTee;
  28. private int _posRightTee;
  29. private int _posTopTee;
  30. private bool _showScrollIndicator;
  31. private int _size, _position;
  32. private bool _vertical;
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class.
  35. /// </summary>
  36. public ScrollBarView ()
  37. {
  38. WantContinuousButtonPressed = true;
  39. Added += (s, e) => CreateBottomRightCorner (e.SuperView);
  40. Initialized += ScrollBarView_Initialized;
  41. }
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class.
  44. /// </summary>
  45. /// <param name="host">The view that will host this scrollbar.</param>
  46. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.</param>
  47. /// <param name="showBothScrollIndicator">
  48. /// If set to <c>true (default)</c> will have the other scrollbar, otherwise will
  49. /// have only one.
  50. /// </param>
  51. public ScrollBarView (View host, bool isVertical, bool showBothScrollIndicator = true)
  52. {
  53. if (host is null)
  54. {
  55. throw new ArgumentNullException ("The host parameter can't be null.");
  56. }
  57. if (host.SuperView is null)
  58. {
  59. throw new ArgumentNullException ("The host SuperView parameter can't be null.");
  60. }
  61. _hosted = true;
  62. IsVertical = isVertical;
  63. ColorScheme = host.ColorScheme;
  64. X = isVertical ? Pos.Right (host) - 1 : Pos.Left (host);
  65. Y = isVertical ? Pos.Top (host) : Pos.Bottom (host) - 1;
  66. Host = host;
  67. CanFocus = false;
  68. Enabled = host.Enabled;
  69. Visible = host.Visible;
  70. Initialized += ScrollBarView_Initialized;
  71. //Host.CanFocusChanged += Host_CanFocusChanged;
  72. Host.EnabledChanged += Host_EnabledChanged;
  73. Host.VisibleChanged += Host_VisibleChanged;
  74. Host.SuperView.Add (this);
  75. AutoHideScrollBars = true;
  76. if (showBothScrollIndicator)
  77. {
  78. OtherScrollBarView = new ScrollBarView
  79. {
  80. IsVertical = !isVertical,
  81. ColorScheme = host.ColorScheme,
  82. Host = host,
  83. CanFocus = false,
  84. Enabled = host.Enabled,
  85. Visible = host.Visible,
  86. OtherScrollBarView = this
  87. };
  88. OtherScrollBarView._hosted = true;
  89. OtherScrollBarView.X = OtherScrollBarView.IsVertical ? Pos.Right (host) - 1 : Pos.Left (host);
  90. OtherScrollBarView.Y = OtherScrollBarView.IsVertical ? Pos.Top (host) : Pos.Bottom (host) - 1;
  91. OtherScrollBarView.Host.SuperView.Add (OtherScrollBarView);
  92. OtherScrollBarView.ShowScrollIndicator = true;
  93. }
  94. ShowScrollIndicator = true;
  95. CreateBottomRightCorner (Host);
  96. }
  97. /// <summary>If true the vertical/horizontal scroll bars won't be showed if it's not needed.</summary>
  98. public bool AutoHideScrollBars
  99. {
  100. get => _autoHideScrollBars;
  101. set
  102. {
  103. if (_autoHideScrollBars != value)
  104. {
  105. _autoHideScrollBars = value;
  106. SetNeedsDisplay ();
  107. }
  108. }
  109. }
  110. // BUGBUG: v2 - for consistency this should be named "Parent" not "Host"
  111. /// <summary>Get or sets the view that host this <see cref="ScrollBarView"/></summary>
  112. public View Host { get; internal set; }
  113. /// <summary>If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.</summary>
  114. public bool IsVertical
  115. {
  116. get => _vertical;
  117. set
  118. {
  119. _vertical = value;
  120. if (IsInitialized)
  121. {
  122. SetWidthHeight ();
  123. }
  124. }
  125. }
  126. /// <summary>Get or sets if the view-port is kept always visible in the area of this <see cref="ScrollBarView"/></summary>
  127. public bool KeepContentAlwaysInViewport
  128. {
  129. get => _keepContentAlwaysInViewport;
  130. set
  131. {
  132. if (_keepContentAlwaysInViewport != value)
  133. {
  134. _keepContentAlwaysInViewport = value;
  135. var pos = 0;
  136. if (value && !_vertical && _position + Host.Viewport.Width > _size)
  137. {
  138. pos = _size - Host.Viewport.Width + (_showBothScrollIndicator ? 1 : 0);
  139. }
  140. if (value && _vertical && _position + Host.Viewport.Height > _size)
  141. {
  142. pos = _size - Host.Viewport.Height + (_showBothScrollIndicator ? 1 : 0);
  143. }
  144. if (pos != 0)
  145. {
  146. Position = pos;
  147. }
  148. if (OtherScrollBarView is { } && OtherScrollBarView._keepContentAlwaysInViewport != value)
  149. {
  150. OtherScrollBarView.KeepContentAlwaysInViewport = value;
  151. }
  152. if (pos == 0)
  153. {
  154. Refresh ();
  155. }
  156. }
  157. }
  158. }
  159. /// <summary>Represent a vertical or horizontal ScrollBarView other than this.</summary>
  160. public ScrollBarView OtherScrollBarView
  161. {
  162. get => _otherScrollBarView;
  163. set
  164. {
  165. if (value is { } && ((value.IsVertical && _vertical) || (!value.IsVertical && !_vertical)))
  166. {
  167. throw new ArgumentException (
  168. $"There is already a {(_vertical ? "vertical" : "horizontal")} ScrollBarView."
  169. );
  170. }
  171. _otherScrollBarView = value;
  172. }
  173. }
  174. /// <summary>The position, relative to <see cref="Size"/>, to set the scrollbar at.</summary>
  175. /// <value>The position.</value>
  176. public int Position
  177. {
  178. get => _position;
  179. set
  180. {
  181. if (_position == value)
  182. {
  183. return;
  184. }
  185. SetPosition (value);
  186. }
  187. }
  188. // BUGBUG: v2 - Why can't we get rid of this and just use Visible?
  189. /// <summary>Gets or sets the visibility for the vertical or horizontal scroll indicator.</summary>
  190. /// <value><c>true</c> if show vertical or horizontal scroll indicator; otherwise, <c>false</c>.</value>
  191. public bool ShowScrollIndicator
  192. {
  193. get => _showScrollIndicator && Visible;
  194. set
  195. {
  196. //if (value == showScrollIndicator) {
  197. // return;
  198. //}
  199. _showScrollIndicator = value;
  200. if (IsInitialized)
  201. {
  202. SetNeedsLayout ();
  203. if (value)
  204. {
  205. Visible = true;
  206. }
  207. else
  208. {
  209. Visible = false;
  210. Position = 0;
  211. }
  212. SetWidthHeight ();
  213. }
  214. }
  215. }
  216. /// <summary>The size of content the scrollbar represents.</summary>
  217. /// <value>The size.</value>
  218. /// <remarks>
  219. /// The <see cref="Size"/> is typically the size of the virtual content. E.g. when a Scrollbar is part of a
  220. /// <see cref="View"/> the Size is set to the appropriate dimension of <see cref="Host"/>.
  221. /// </remarks>
  222. public int Size
  223. {
  224. get => _size;
  225. set
  226. {
  227. _size = value;
  228. if (IsInitialized)
  229. {
  230. SetRelativeLayout (SuperView?.Frame.Size ?? Host.Frame.Size);
  231. ShowHideScrollBars (false);
  232. SetNeedsLayout ();
  233. }
  234. }
  235. }
  236. private bool _showBothScrollIndicator => OtherScrollBarView?.ShowScrollIndicator == true && ShowScrollIndicator;
  237. /// <summary>This event is raised when the position on the scrollbar has changed.</summary>
  238. public event EventHandler ChangedPosition;
  239. /// <inheritdoc/>
  240. protected override bool OnMouseEvent (MouseEventArgs mouseEvent)
  241. {
  242. if (mouseEvent.Flags != MouseFlags.Button1Pressed
  243. && mouseEvent.Flags != MouseFlags.Button1DoubleClicked
  244. && !mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)
  245. && mouseEvent.Flags != MouseFlags.Button1Released
  246. && mouseEvent.Flags != MouseFlags.WheeledDown
  247. && mouseEvent.Flags != MouseFlags.WheeledUp
  248. && mouseEvent.Flags != MouseFlags.WheeledRight
  249. && mouseEvent.Flags != MouseFlags.WheeledLeft
  250. && mouseEvent.Flags != MouseFlags.Button1TripleClicked)
  251. {
  252. return false;
  253. }
  254. if (!Host.CanFocus)
  255. {
  256. return true;
  257. }
  258. if (Host?.HasFocus == false)
  259. {
  260. Host.SetFocus ();
  261. }
  262. int location = _vertical ? mouseEvent.Position.Y : mouseEvent.Position.X;
  263. int barsize = _vertical ? Viewport.Height : Viewport.Width;
  264. int posTopLeftTee = _vertical ? _posTopTee + 1 : _posLeftTee + 1;
  265. int posBottomRightTee = _vertical ? _posBottomTee + 1 : _posRightTee + 1;
  266. barsize -= 2;
  267. int pos = Position;
  268. if (mouseEvent.Flags != MouseFlags.Button1Released && (Application.MouseGrabView is null || Application.MouseGrabView != this))
  269. {
  270. Application.GrabMouse (this);
  271. }
  272. else if (mouseEvent.Flags == MouseFlags.Button1Released && Application.MouseGrabView is { } && Application.MouseGrabView == this)
  273. {
  274. _lastLocation = -1;
  275. Application.UngrabMouse ();
  276. return true;
  277. }
  278. if (ShowScrollIndicator
  279. && (mouseEvent.Flags == MouseFlags.WheeledDown
  280. || mouseEvent.Flags == MouseFlags.WheeledUp
  281. || mouseEvent.Flags == MouseFlags.WheeledRight
  282. || mouseEvent.Flags == MouseFlags.WheeledLeft))
  283. {
  284. return Host.NewMouseEvent (mouseEvent) == true;
  285. }
  286. if (mouseEvent.Flags == MouseFlags.Button1Pressed && location == 0)
  287. {
  288. if (pos > 0)
  289. {
  290. Position = pos - 1;
  291. }
  292. }
  293. else if (mouseEvent.Flags == MouseFlags.Button1Pressed && location == barsize + 1)
  294. {
  295. if (CanScroll (1, out _, _vertical))
  296. {
  297. Position = pos + 1;
  298. }
  299. }
  300. else if (location > 0 && location < barsize + 1)
  301. {
  302. //var b1 = pos * (Size > 0 ? barsize / Size : 0);
  303. //var b2 = Size > 0
  304. // ? (KeepContentAlwaysInViewport ? Math.Min (((pos + barsize) * barsize / Size) + 1, barsize - 1) : (pos + barsize) * barsize / Size)
  305. // : 0;
  306. //if (KeepContentAlwaysInViewport && b1 == b2) {
  307. // b1 = Math.Max (b1 - 1, 0);
  308. //}
  309. if (_lastLocation > -1
  310. || (location >= posTopLeftTee
  311. && location <= posBottomRightTee
  312. && mouseEvent.Flags.HasFlag (
  313. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  314. )))
  315. {
  316. if (_lastLocation == -1)
  317. {
  318. _lastLocation = location;
  319. _posBarOffset = _keepContentAlwaysInViewport
  320. ? Math.Max (location - posTopLeftTee, 1)
  321. : 0;
  322. return true;
  323. }
  324. if (location > _lastLocation)
  325. {
  326. if (location - _posBarOffset < barsize)
  327. {
  328. int np = (location - _posBarOffset) * Size / barsize + Size / barsize;
  329. if (CanScroll (np - pos, out int nv, _vertical))
  330. {
  331. Position = pos + nv;
  332. }
  333. }
  334. else if (CanScroll (Size - pos, out int nv, _vertical))
  335. {
  336. Position = Math.Min (pos + nv, Size);
  337. }
  338. }
  339. else if (location < _lastLocation)
  340. {
  341. if (location - _posBarOffset > 0)
  342. {
  343. int np = (location - _posBarOffset) * Size / barsize - Size / barsize;
  344. if (CanScroll (np - pos, out int nv, _vertical))
  345. {
  346. Position = pos + nv;
  347. }
  348. }
  349. else
  350. {
  351. Position = 0;
  352. }
  353. }
  354. else if (location - _posBarOffset >= barsize && posBottomRightTee - posTopLeftTee >= 3 && CanScroll (Size - pos, out int nv, _vertical))
  355. {
  356. Position = Math.Min (pos + nv, Size);
  357. }
  358. else if (location - _posBarOffset >= barsize - 1 && posBottomRightTee - posTopLeftTee <= 3 && CanScroll (Size - pos, out nv, _vertical))
  359. {
  360. Position = Math.Min (pos + nv, Size);
  361. }
  362. else if (location - _posBarOffset <= 0 && posBottomRightTee - posTopLeftTee <= 3)
  363. {
  364. Position = 0;
  365. }
  366. }
  367. else if (location > posBottomRightTee)
  368. {
  369. if (CanScroll (barsize, out int nv, _vertical))
  370. {
  371. Position = pos + nv;
  372. }
  373. }
  374. else if (location < posTopLeftTee)
  375. {
  376. if (CanScroll (-barsize, out int nv, _vertical))
  377. {
  378. Position = pos + nv;
  379. }
  380. }
  381. else if (location == 1 && posTopLeftTee <= 3)
  382. {
  383. Position = 0;
  384. }
  385. else if (location == barsize)
  386. {
  387. if (CanScroll (Size - pos, out int nv, _vertical))
  388. {
  389. Position = Math.Min (pos + nv, Size);
  390. }
  391. }
  392. }
  393. return true;
  394. }
  395. /// <summary>Virtual method to invoke the <see cref="ChangedPosition"/> action event.</summary>
  396. public virtual void OnChangedPosition () { ChangedPosition?.Invoke (this, EventArgs.Empty); }
  397. /// <inheritdoc/>
  398. protected override bool OnDrawingContent (Rectangle viewport)
  399. {
  400. if (ColorScheme is null || ((!ShowScrollIndicator || Size == 0) && AutoHideScrollBars && Visible))
  401. {
  402. if ((!ShowScrollIndicator || Size == 0) && AutoHideScrollBars && Visible)
  403. {
  404. ShowHideScrollBars (false);
  405. }
  406. return false;
  407. }
  408. if (Size == 0 || (_vertical && Viewport.Height == 0) || (!_vertical && Viewport.Width == 0))
  409. {
  410. return false;
  411. }
  412. Driver.SetAttribute (Host.HasFocus ? ColorScheme.Focus : GetNormalColor ());
  413. if (_vertical)
  414. {
  415. if (Viewport.Right < Viewport.Width - 1)
  416. {
  417. return true;
  418. }
  419. int col = Viewport.Width - 1;
  420. int bh = Viewport.Height;
  421. Rune special;
  422. if (bh < 4)
  423. {
  424. int by1 = _position * bh / Size;
  425. int by2 = (_position + bh) * bh / Size;
  426. Move (col, 0);
  427. if (Viewport.Height == 1)
  428. {
  429. Driver.AddRune (Glyphs.Diamond);
  430. }
  431. else
  432. {
  433. Driver.AddRune (Glyphs.UpArrow);
  434. }
  435. if (Viewport.Height == 3)
  436. {
  437. Move (col, 1);
  438. Driver.AddRune (Glyphs.Diamond);
  439. }
  440. if (Viewport.Height > 1)
  441. {
  442. Move (col, Viewport.Height - 1);
  443. Driver.AddRune (Glyphs.DownArrow);
  444. }
  445. }
  446. else
  447. {
  448. bh -= 2;
  449. int by1 = KeepContentAlwaysInViewport
  450. ? _position * bh / Size
  451. : _position * bh / (Size + bh);
  452. int by2 = KeepContentAlwaysInViewport
  453. ? Math.Min ((_position + bh) * bh / Size + 1, bh - 1)
  454. : (_position + bh) * bh / (Size + bh);
  455. if (KeepContentAlwaysInViewport && by1 == by2)
  456. {
  457. by1 = Math.Max (by1 - 1, 0);
  458. }
  459. AddRune (col, 0, Glyphs.UpArrow);
  460. var hasTopTee = false;
  461. var hasDiamond = false;
  462. var hasBottomTee = false;
  463. for (var y = 0; y < bh; y++)
  464. {
  465. if ((y < by1 || y > by2) && ((_position > 0 && !hasTopTee) || (hasTopTee && hasBottomTee)))
  466. {
  467. special = Glyphs.Stipple;
  468. }
  469. else
  470. {
  471. if (y != by2 && y > 1 && by2 - by1 == 0 && by1 < bh - 1 && hasTopTee && !hasDiamond)
  472. {
  473. hasDiamond = true;
  474. special = Glyphs.Diamond;
  475. }
  476. else
  477. {
  478. if (y == by1 && !hasTopTee)
  479. {
  480. hasTopTee = true;
  481. _posTopTee = y;
  482. special = Glyphs.TopTee;
  483. }
  484. else if (((_position == 0 && y == bh - 1) || y >= by2 || by2 == 0) && !hasBottomTee)
  485. {
  486. hasBottomTee = true;
  487. _posBottomTee = y;
  488. special = Glyphs.BottomTee;
  489. }
  490. else
  491. {
  492. special = Glyphs.VLine;
  493. }
  494. }
  495. }
  496. AddRune (col, y + 1, special);
  497. }
  498. if (!hasTopTee)
  499. {
  500. AddRune (col, Viewport.Height - 2, Glyphs.TopTee);
  501. }
  502. AddRune (col, Viewport.Height - 1, Glyphs.DownArrow);
  503. }
  504. }
  505. else
  506. {
  507. if (Viewport.Bottom < Viewport.Height - 1)
  508. {
  509. return true;
  510. }
  511. int row = Viewport.Height - 1;
  512. int bw = Viewport.Width;
  513. Rune special;
  514. if (bw < 4)
  515. {
  516. int bx1 = _position * bw / Size;
  517. int bx2 = (_position + bw) * bw / Size;
  518. Move (0, row);
  519. Driver.AddRune (Glyphs.LeftArrow);
  520. Driver.AddRune (Glyphs.RightArrow);
  521. }
  522. else
  523. {
  524. bw -= 2;
  525. int bx1 = KeepContentAlwaysInViewport
  526. ? _position * bw / Size
  527. : _position * bw / (Size + bw);
  528. int bx2 = KeepContentAlwaysInViewport
  529. ? Math.Min ((_position + bw) * bw / Size + 1, bw - 1)
  530. : (_position + bw) * bw / (Size + bw);
  531. if (KeepContentAlwaysInViewport && bx1 == bx2)
  532. {
  533. bx1 = Math.Max (bx1 - 1, 0);
  534. }
  535. Move (0, row);
  536. Driver.AddRune (Glyphs.LeftArrow);
  537. var hasLeftTee = false;
  538. var hasDiamond = false;
  539. var hasRightTee = false;
  540. for (var x = 0; x < bw; x++)
  541. {
  542. if ((x < bx1 || x >= bx2 + 1) && ((_position > 0 && !hasLeftTee) || (hasLeftTee && hasRightTee)))
  543. {
  544. special = Glyphs.Stipple;
  545. }
  546. else
  547. {
  548. if (x != bx2 && x > 1 && bx2 - bx1 == 0 && bx1 < bw - 1 && hasLeftTee && !hasDiamond)
  549. {
  550. hasDiamond = true;
  551. special = Glyphs.Diamond;
  552. }
  553. else
  554. {
  555. if (x == bx1 && !hasLeftTee)
  556. {
  557. hasLeftTee = true;
  558. _posLeftTee = x;
  559. special = Glyphs.LeftTee;
  560. }
  561. else if (((_position == 0 && x == bw - 1) || x >= bx2 || bx2 == 0) && !hasRightTee)
  562. {
  563. hasRightTee = true;
  564. _posRightTee = x;
  565. special = Glyphs.RightTee;
  566. }
  567. else
  568. {
  569. special = Glyphs.HLine;
  570. }
  571. }
  572. }
  573. Driver.AddRune (special);
  574. }
  575. if (!hasLeftTee)
  576. {
  577. Move (Viewport.Width - 2, row);
  578. Driver.AddRune (Glyphs.LeftTee);
  579. }
  580. Driver.AddRune (Glyphs.RightArrow);
  581. }
  582. }
  583. return false;
  584. }
  585. /// <summary>Only used for a hosted view that will update and redraw the scrollbars.</summary>
  586. public virtual void Refresh () { ShowHideScrollBars (); }
  587. internal bool CanScroll (int n, out int max, bool isVertical = false)
  588. {
  589. if (Host?.Viewport.IsEmpty != false)
  590. {
  591. max = 0;
  592. return false;
  593. }
  594. int s = GetBarsize (isVertical);
  595. int newSize = Math.Max (Math.Min (_size - s, _position + n), 0);
  596. max = _size > s + newSize ? newSize == 0 ? -_position : n : _size - (s + _position) - 1;
  597. if (_size >= s + newSize && max != 0)
  598. {
  599. return true;
  600. }
  601. return false;
  602. }
  603. private bool CheckBothScrollBars (ScrollBarView scrollBarView, bool pending = false)
  604. {
  605. int barsize = scrollBarView._vertical ? scrollBarView.Viewport.Height : scrollBarView.Viewport.Width;
  606. if (barsize == 0 || barsize >= scrollBarView._size)
  607. {
  608. if (scrollBarView.ShowScrollIndicator)
  609. {
  610. scrollBarView.ShowScrollIndicator = false;
  611. }
  612. if (scrollBarView.Visible)
  613. {
  614. scrollBarView.Visible = false;
  615. }
  616. }
  617. else if (barsize > 0 && barsize == scrollBarView._size && scrollBarView.OtherScrollBarView is { } && pending)
  618. {
  619. if (scrollBarView.ShowScrollIndicator)
  620. {
  621. scrollBarView.ShowScrollIndicator = false;
  622. }
  623. if (scrollBarView.Visible)
  624. {
  625. scrollBarView.Visible = false;
  626. }
  627. if (scrollBarView.OtherScrollBarView is { } && scrollBarView._showBothScrollIndicator)
  628. {
  629. scrollBarView.OtherScrollBarView.ShowScrollIndicator = false;
  630. }
  631. if (scrollBarView.OtherScrollBarView.Visible)
  632. {
  633. scrollBarView.OtherScrollBarView.Visible = false;
  634. }
  635. }
  636. else if (barsize > 0 && barsize == _size && scrollBarView.OtherScrollBarView is { } && !pending)
  637. {
  638. pending = true;
  639. }
  640. else
  641. {
  642. if (scrollBarView.OtherScrollBarView is { } && pending)
  643. {
  644. if (!scrollBarView._showBothScrollIndicator)
  645. {
  646. scrollBarView.OtherScrollBarView.ShowScrollIndicator = true;
  647. }
  648. if (!scrollBarView.OtherScrollBarView.Visible)
  649. {
  650. scrollBarView.OtherScrollBarView.Visible = true;
  651. }
  652. }
  653. if (!scrollBarView.ShowScrollIndicator)
  654. {
  655. scrollBarView.ShowScrollIndicator = true;
  656. }
  657. if (!scrollBarView.Visible)
  658. {
  659. scrollBarView.Visible = true;
  660. }
  661. }
  662. return pending;
  663. }
  664. private void ContentBottomRightCorner_DrawContent (object sender, DrawEventArgs e)
  665. {
  666. Driver.SetAttribute (Host.HasFocus ? ColorScheme.Focus : GetNormalColor ());
  667. // I'm forced to do this here because the Clear method is
  668. // changing the color attribute and is different of this one
  669. Driver.FillRect (Driver.Clip);
  670. e.Cancel = true;
  671. }
  672. //private void Host_CanFocusChanged ()
  673. //{
  674. // CanFocus = Host.CanFocus;
  675. // if (otherScrollBarView is { }) {
  676. // otherScrollBarView.CanFocus = CanFocus;
  677. // }
  678. //}
  679. private void ContentBottomRightCorner_MouseClick (object sender, MouseEventArgs me)
  680. {
  681. if (me.Flags == MouseFlags.WheeledDown
  682. || me.Flags == MouseFlags.WheeledUp
  683. || me.Flags == MouseFlags.WheeledRight
  684. || me.Flags == MouseFlags.WheeledLeft)
  685. {
  686. NewMouseEvent (me);
  687. }
  688. else if (me.Flags == MouseFlags.Button1Clicked)
  689. {
  690. Host.SetFocus ();
  691. }
  692. me.Handled = true;
  693. }
  694. private void CreateBottomRightCorner (View host)
  695. {
  696. if (Host is null)
  697. {
  698. Host = host;
  699. }
  700. if (Host != null
  701. && ((_contentBottomRightCorner is null && OtherScrollBarView is null)
  702. || (_contentBottomRightCorner is null && OtherScrollBarView is { } && OtherScrollBarView._contentBottomRightCorner is null)))
  703. {
  704. _contentBottomRightCorner = new ContentBottomRightCorner { Visible = Host.Visible };
  705. if (_hosted)
  706. {
  707. Host.SuperView.Add (_contentBottomRightCorner);
  708. _contentBottomRightCorner.X = Pos.Right (Host) - 1;
  709. _contentBottomRightCorner.Y = Pos.Bottom (Host) - 1;
  710. }
  711. else
  712. {
  713. Host.Add (_contentBottomRightCorner);
  714. _contentBottomRightCorner.X = Pos.AnchorEnd (1);
  715. _contentBottomRightCorner.Y = Pos.AnchorEnd (1);
  716. }
  717. _contentBottomRightCorner.Width = 1;
  718. _contentBottomRightCorner.Height = 1;
  719. _contentBottomRightCorner.MouseClick += ContentBottomRightCorner_MouseClick;
  720. _contentBottomRightCorner.DrawingContent += ContentBottomRightCorner_DrawContent;
  721. }
  722. }
  723. private int GetBarsize (bool isVertical)
  724. {
  725. if (Host?.Viewport.IsEmpty != false)
  726. {
  727. return 0;
  728. }
  729. return isVertical ? KeepContentAlwaysInViewport
  730. ? Host.Viewport.Height + (_showBothScrollIndicator ? -2 : -1)
  731. : 0 :
  732. KeepContentAlwaysInViewport ? Host.Viewport.Width + (_showBothScrollIndicator ? -2 : -1) : 0;
  733. }
  734. private void Host_EnabledChanged (object sender, EventArgs e)
  735. {
  736. Enabled = Host.Enabled;
  737. if (_otherScrollBarView is { })
  738. {
  739. _otherScrollBarView.Enabled = Enabled;
  740. }
  741. _contentBottomRightCorner.Enabled = Enabled;
  742. }
  743. private void Host_VisibleChanged (object sender, EventArgs e)
  744. {
  745. if (!Host.Visible)
  746. {
  747. Visible = Host.Visible;
  748. if (_otherScrollBarView is { })
  749. {
  750. _otherScrollBarView.Visible = Visible;
  751. }
  752. _contentBottomRightCorner.Visible = Visible;
  753. }
  754. else
  755. {
  756. ShowHideScrollBars ();
  757. }
  758. }
  759. private void ScrollBarView_Initialized (object sender, EventArgs e)
  760. {
  761. SetWidthHeight ();
  762. SetRelativeLayout (SuperView?.Frame.Size ?? Host?.Frame.Size ?? Frame.Size);
  763. if (OtherScrollBarView is null)
  764. {
  765. // Only do this once if both scrollbars are enabled
  766. ShowHideScrollBars ();
  767. }
  768. SetPosition (Position);
  769. }
  770. // Helper to assist Initialized event handler
  771. private void SetPosition (int newPosition)
  772. {
  773. if (!IsInitialized)
  774. {
  775. // We're not initialized so we can't do anything fancy. Just cache value.
  776. _position = newPosition;
  777. return;
  778. }
  779. if (newPosition < 0)
  780. {
  781. _position = 0;
  782. SetNeedsDisplay ();
  783. return;
  784. }
  785. else if (CanScroll (newPosition - _position, out int max, _vertical))
  786. {
  787. if (max == newPosition - _position)
  788. {
  789. _position = newPosition;
  790. }
  791. else
  792. {
  793. _position = Math.Max (_position + max, 0);
  794. }
  795. }
  796. else if (max < 0)
  797. {
  798. _position = Math.Max (_position + max, 0);
  799. }
  800. else
  801. {
  802. _position = Math.Max (newPosition, 0);
  803. }
  804. OnChangedPosition ();
  805. SetNeedsDisplay ();
  806. }
  807. // BUGBUG: v2 - rationalize this with View.SetMinWidthHeight
  808. private void SetWidthHeight ()
  809. {
  810. // BUGBUG: v2 - If Host is also the ScrollBarView's superview, this is all bogus because it's not
  811. // supported that a view can reference it's superview's Dims. This code also assumes the host does
  812. // not have a margin/borderframe/padding.
  813. if (!IsInitialized || _otherScrollBarView is { IsInitialized: false })
  814. {
  815. return;
  816. }
  817. if (_showBothScrollIndicator)
  818. {
  819. Width = _vertical ? 1 :
  820. Host != SuperView ? Dim.Width (Host) - 1 : Dim.Fill () - 1;
  821. Height = _vertical ? Host != SuperView ? Dim.Height (Host) - 1 : Dim.Fill () - 1 : 1;
  822. _otherScrollBarView.Width = _otherScrollBarView._vertical ? 1 :
  823. Host != SuperView ? Dim.Width (Host) - 1 : Dim.Fill () - 1;
  824. _otherScrollBarView.Height = _otherScrollBarView._vertical
  825. ? Host != SuperView ? Dim.Height (Host) - 1 : Dim.Fill () - 1
  826. : 1;
  827. }
  828. else if (ShowScrollIndicator)
  829. {
  830. Width = _vertical ? 1 :
  831. Host != SuperView ? Dim.Width (Host) : Dim.Fill ();
  832. Height = _vertical ? Host != SuperView ? Dim.Height (Host) : Dim.Fill () : 1;
  833. }
  834. else if (_otherScrollBarView?.ShowScrollIndicator == true)
  835. {
  836. _otherScrollBarView.Width = _otherScrollBarView._vertical ? 1 :
  837. Host != SuperView ? Dim.Width (Host) : Dim.Fill () - 0;
  838. _otherScrollBarView.Height = _otherScrollBarView._vertical
  839. ? Host != SuperView ? Dim.Height (Host) : Dim.Fill () - 0
  840. : 1;
  841. }
  842. }
  843. private void ShowHideScrollBars (bool redraw = true)
  844. {
  845. if (!_hosted || (_hosted && !_autoHideScrollBars))
  846. {
  847. if (_contentBottomRightCorner is { } && _contentBottomRightCorner.Visible)
  848. {
  849. _contentBottomRightCorner.Visible = false;
  850. }
  851. else if (_otherScrollBarView != null
  852. && _otherScrollBarView._contentBottomRightCorner != null
  853. && _otherScrollBarView._contentBottomRightCorner.Visible)
  854. {
  855. _otherScrollBarView._contentBottomRightCorner.Visible = false;
  856. }
  857. return;
  858. }
  859. bool pending = CheckBothScrollBars (this);
  860. if (_otherScrollBarView is { })
  861. {
  862. CheckBothScrollBars (_otherScrollBarView, pending);
  863. }
  864. SetWidthHeight ();
  865. SetRelativeLayout (SuperView?.Frame.Size ?? Host.Frame.Size);
  866. if (_otherScrollBarView is { })
  867. {
  868. OtherScrollBarView.SetRelativeLayout (SuperView?.Frame.Size ?? Host.Frame.Size);
  869. }
  870. if (_showBothScrollIndicator)
  871. {
  872. if (_contentBottomRightCorner is { })
  873. {
  874. _contentBottomRightCorner.Visible = true;
  875. }
  876. else if (_otherScrollBarView is { } && _otherScrollBarView._contentBottomRightCorner is { })
  877. {
  878. _otherScrollBarView._contentBottomRightCorner.Visible = true;
  879. }
  880. }
  881. else if (!ShowScrollIndicator)
  882. {
  883. if (_contentBottomRightCorner is { })
  884. {
  885. _contentBottomRightCorner.Visible = false;
  886. }
  887. else if (_otherScrollBarView is { } && _otherScrollBarView._contentBottomRightCorner is { })
  888. {
  889. _otherScrollBarView._contentBottomRightCorner.Visible = false;
  890. }
  891. if (Application.MouseGrabView is { } && Application.MouseGrabView == this)
  892. {
  893. Application.UngrabMouse ();
  894. }
  895. }
  896. else if (_contentBottomRightCorner is { })
  897. {
  898. _contentBottomRightCorner.Visible = false;
  899. }
  900. else if (_otherScrollBarView is { } && _otherScrollBarView._contentBottomRightCorner is { })
  901. {
  902. _otherScrollBarView._contentBottomRightCorner.Visible = false;
  903. }
  904. if (Host?.Visible == true && ShowScrollIndicator && !Visible)
  905. {
  906. Visible = true;
  907. }
  908. if (Host?.Visible == true && _otherScrollBarView?.ShowScrollIndicator == true && !_otherScrollBarView.Visible)
  909. {
  910. _otherScrollBarView.Visible = true;
  911. }
  912. if (!redraw)
  913. {
  914. return;
  915. }
  916. if (ShowScrollIndicator)
  917. {
  918. Draw ();
  919. }
  920. if (_otherScrollBarView is { } && _otherScrollBarView.ShowScrollIndicator)
  921. {
  922. _otherScrollBarView.Draw ();
  923. }
  924. if (_contentBottomRightCorner is { } && _contentBottomRightCorner.Visible)
  925. {
  926. _contentBottomRightCorner.Draw ();
  927. }
  928. else if (_otherScrollBarView is { } && _otherScrollBarView._contentBottomRightCorner is { } && _otherScrollBarView._contentBottomRightCorner.Visible)
  929. {
  930. _otherScrollBarView._contentBottomRightCorner.Draw ();
  931. }
  932. }
  933. internal class ContentBottomRightCorner : View
  934. {
  935. public ContentBottomRightCorner ()
  936. {
  937. ColorScheme = ColorScheme;
  938. }
  939. }
  940. }