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