ScrollBarView.cs 27 KB

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