ScrollBarView.cs 35 KB

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