ScrollBarView.cs 35 KB

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