123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- #nullable enable
- namespace Terminal.Gui;
- public partial class View
- {
- private Lazy<ScrollBar> _horizontalScrollBar;
- private Lazy<ScrollBar> _verticalScrollBar;
- /// <summary>
- /// Initializes the ScrollBars of the View. Called by the constructor.
- /// </summary>
- private void SetupScrollBars ()
- {
- _horizontalScrollBar = new (
- () =>
- {
- var scrollBar = new ScrollBar
- {
- Orientation = Orientation.Horizontal,
- X = 0,
- Y = Pos.AnchorEnd (),
- Width = Dim.Fill (
- Dim.Func (
- () =>
- {
- if (_verticalScrollBar.IsValueCreated)
- {
- return _verticalScrollBar.Value.Visible ? 1 : 0;
- }
- return 0;
- })),
- Size = GetContentSize ().Width,
- Visible = false
- };
- Padding?.Add (scrollBar);
- scrollBar.Initialized += (_, _) =>
- {
- Padding!.Thickness = Padding.Thickness with
- {
- Bottom = scrollBar.Visible ? Padding.Thickness.Bottom + 1 : 0
- };
- scrollBar.SliderPositionChanged += (_, args) =>
- {
- Viewport = Viewport with { X = args.CurrentValue };
- };
- scrollBar.VisibleChanged += (_, _) =>
- {
- Padding.Thickness = Padding.Thickness with
- {
- Bottom = scrollBar.Visible
- ? Padding.Thickness.Bottom + 1
- : Padding.Thickness.Bottom - 1
- };
- };
- };
- return scrollBar;
- });
- _verticalScrollBar = new (
- () =>
- {
- var scrollBar = new ScrollBar
- {
- Orientation = Orientation.Vertical,
- X = Pos.AnchorEnd (),
- Y = Pos.Func (() => Padding.Thickness.Top),
- Height = Dim.Fill (
- Dim.Func (
- () =>
- {
- if (_horizontalScrollBar.IsValueCreated)
- {
- return _horizontalScrollBar.Value.Visible ? 1 : 0;
- }
- return 0;
- })),
- Size = GetContentSize ().Height,
- Visible = false
- };
- Padding?.Add (scrollBar);
- scrollBar.Initialized += (_, _) =>
- {
- if (Padding is { })
- {
- Padding.Thickness = Padding.Thickness with
- {
- Right = scrollBar.Visible ? Padding.Thickness.Right + 1 : 0
- };
- scrollBar.SliderPositionChanged += (_, args) =>
- {
- Viewport = Viewport with { Y = args.CurrentValue };
- };
- scrollBar.VisibleChanged += (_, _) =>
- {
- Padding.Thickness = Padding.Thickness with
- {
- Right = scrollBar.Visible
- ? Padding.Thickness.Right + 1
- : Padding.Thickness.Right - 1
- };
- };
- }
- };
- return scrollBar;
- });
- ViewportChanged += (_, _) =>
- {
- if (_verticalScrollBar.IsValueCreated)
- {
- _verticalScrollBar.Value.SliderPosition = Viewport.Y;
- }
- if (_horizontalScrollBar.IsValueCreated)
- {
- _horizontalScrollBar.Value.SliderPosition = Viewport.X;
- }
- };
- ContentSizeChanged += (_, _) =>
- {
- if (_verticalScrollBar.IsValueCreated)
- {
- _verticalScrollBar.Value.Size = GetContentSize ().Height;
- }
- if (_horizontalScrollBar.IsValueCreated)
- {
- _horizontalScrollBar.Value.Size = GetContentSize ().Width;
- }
- };
- }
- /// <summary>
- /// </summary>
- public ScrollBar HorizontalScrollBar => _horizontalScrollBar.Value;
- /// <summary>
- /// </summary>
- public ScrollBar VerticalScrollBar => _verticalScrollBar.Value;
- /// <summary>
- /// Clean up the ScrollBars of the View. Called by View.Dispose.
- /// </summary>
- private void DisposeScrollBars ()
- {
- if (_horizontalScrollBar.IsValueCreated)
- {
- Padding?.Remove (_horizontalScrollBar.Value);
- _horizontalScrollBar.Value.Dispose ();
- }
- if (_verticalScrollBar.IsValueCreated)
- {
- Padding?.Remove (_verticalScrollBar.Value);
- _verticalScrollBar.Value.Dispose ();
- }
- }
- private void SetScrollBarsKeepContentInAllViewport (ViewportSettings viewportSettings)
- {
- if (viewportSettings == ViewportSettings.None)
- {
- _horizontalScrollBar.Value.KeepContentInAllViewport = true;
- _verticalScrollBar.Value.KeepContentInAllViewport = true;
- }
- else if (viewportSettings.HasFlag (ViewportSettings.AllowNegativeX))
- {
- _horizontalScrollBar.Value.AutoHide = false;
- }
- else if (viewportSettings.HasFlag (ViewportSettings.AllowNegativeY))
- {
- _verticalScrollBar.Value.AutoHide = false;
- }
- else if (viewportSettings.HasFlag (ViewportSettings.AllowNegativeLocation))
- {
- _horizontalScrollBar.Value.AutoHide = false;
- _verticalScrollBar.Value.AutoHide = false;
- }
- else if (viewportSettings.HasFlag (ViewportSettings.AllowXGreaterThanContentWidth))
- {
- _horizontalScrollBar.Value.KeepContentInAllViewport = false;
- }
- else if (viewportSettings.HasFlag (ViewportSettings.AllowYGreaterThanContentHeight))
- {
- _verticalScrollBar.Value.KeepContentInAllViewport = false;
- }
- else if (viewportSettings.HasFlag (ViewportSettings.AllowLocationGreaterThanContentSize))
- {
- _horizontalScrollBar.Value.KeepContentInAllViewport = false;
- _verticalScrollBar.Value.KeepContentInAllViewport = false;
- }
- }
- }
|