ScrollBarView.cs 27 KB

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