ScrollBarView.cs 35 KB

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