ScrollBarView.cs 27 KB

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