ScrollBarView.cs 35 KB

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