ScrollBarView.cs 35 KB

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