ScrollBarView.cs 28 KB

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