Skip to content

Commit

Permalink
Merge pull request #39 from Redth/scrollbarvisibility
Browse files Browse the repository at this point in the history
Add support for scrollbar visibility
  • Loading branch information
Redth authored Apr 20, 2024
2 parents 245a2d6 + 6889e7a commit e7b420a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
10 changes: 10 additions & 0 deletions VirtualListView/Apple/VirtualListViewHandler.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ void UpdateEmptyView()
}
}

void UpdateVerticalScrollbarVisibility(ScrollBarVisibility scrollBarVisibility)
{
PlatformView.ShowsVerticalScrollIndicator = scrollBarVisibility == ScrollBarVisibility.Always || scrollBarVisibility == ScrollBarVisibility.Default;
}

void UpdateHorizontalScrollbarVisibility(ScrollBarVisibility scrollBarVisibility)
{
PlatformView.ShowsHorizontalScrollIndicator = scrollBarVisibility == ScrollBarVisibility.Always || scrollBarVisibility == ScrollBarVisibility.Default;
}

public void InvalidateData()
{
this.PlatformView.InvokeOnMainThread(() => {
Expand Down
18 changes: 18 additions & 0 deletions VirtualListView/Controls/VirtualListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,25 @@ public VirtualListViewItemTemplateSelector ItemTemplateSelector
public static readonly BindableProperty ItemTemplateSelectorProperty =
BindableProperty.Create(nameof(ItemTemplateSelector), typeof(VirtualListViewItemTemplateSelector), typeof(VirtualListView), default);


public ScrollBarVisibility VerticalScrollbarVisibility
{
get => (ScrollBarVisibility)GetValue(VerticalScrollbarVisibilityProperty);
set => SetValue(VerticalScrollbarVisibilityProperty, value);
}

public static readonly BindableProperty VerticalScrollbarVisibilityProperty =
BindableProperty.Create(nameof(VerticalScrollbarVisibility), typeof(ScrollBarVisibility), typeof(VirtualListView), ScrollBarVisibility.Default);


public ScrollBarVisibility HorizontalScrollbarVisibility
{
get => (ScrollBarVisibility)GetValue(HorizontalScrollbarVisibilityProperty);
set => SetValue(HorizontalScrollbarVisibilityProperty, value);
}

public static readonly BindableProperty HorizontalScrollbarVisibilityProperty =
BindableProperty.Create(nameof(HorizontalScrollbarVisibility), typeof(ScrollBarVisibility), typeof(VirtualListView), ScrollBarVisibility.Default);

public DataTemplate SectionHeaderTemplate
{
Expand Down
4 changes: 4 additions & 0 deletions VirtualListView/IVirtualListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public interface IVirtualListView : IView
IList<ItemPosition> SelectedItems { get; set; }

ItemPosition? SelectedItem { get; set; }

ScrollBarVisibility VerticalScrollbarVisibility { get; set; }

ScrollBarVisibility HorizontalScrollbarVisibility { get; set; }

event EventHandler<SelectedItemsChangedEventArgs> OnSelectedItemsChanged;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,35 @@ void UpdateEmptyView()
UpdateEmptyViewVisibility();
}
}

ScrollBarVisibility _defaultHorizontalScrollVisibility = ScrollBarVisibility.Default;
ScrollBarVisibility _defaultVerticalScrollVisibility = ScrollBarVisibility.Default;

void UpdateVerticalScrollbarVisibility(ScrollBarVisibility scrollBarVisibility)
{
if (_defaultVerticalScrollVisibility == ScrollBarVisibility.Default)
_defaultVerticalScrollVisibility =
recyclerView.VerticalScrollBarEnabled ? ScrollBarVisibility.Always : ScrollBarVisibility.Never;

var newVerticalScrollVisiblility = scrollBarVisibility;

if (newVerticalScrollVisiblility == ScrollBarVisibility.Default)
newVerticalScrollVisiblility = _defaultVerticalScrollVisibility;

recyclerView.VerticalScrollBarEnabled = newVerticalScrollVisiblility == ScrollBarVisibility.Always;
}

void UpdateHorizontalScrollbarVisibility(ScrollBarVisibility scrollBarVisibility)
{
if (_defaultHorizontalScrollVisibility == ScrollBarVisibility.Default)
_defaultHorizontalScrollVisibility =
recyclerView.HorizontalScrollBarEnabled ? ScrollBarVisibility.Always : ScrollBarVisibility.Never;

var newHorizontalScrollVisiblility = scrollBarVisibility;

if (newHorizontalScrollVisiblility == ScrollBarVisibility.Default)
newHorizontalScrollVisiblility = _defaultHorizontalScrollVisibility;

recyclerView.HorizontalScrollBarEnabled = newHorizontalScrollVisiblility == ScrollBarVisibility.Always;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using WGrid = Microsoft.UI.Xaml.Controls.Grid;
using WVisibility = Microsoft.UI.Xaml.Visibility;
using WFrameworkElement = Microsoft.UI.Xaml.FrameworkElement;
using WScrollBarVisibility = Microsoft.UI.Xaml.Controls.ScrollBarVisibility;
using Microsoft.Maui.Platform;
using Microsoft.UI.Xaml;

Expand Down Expand Up @@ -164,4 +165,26 @@ void UpdateEmptyView()

UpdateEmptyViewVisibility();
}

void UpdateVerticalScrollbarVisibility(ScrollBarVisibility scrollBarVisibility)
{
scrollViewer.VerticalScrollBarVisibility = scrollBarVisibility switch
{
ScrollBarVisibility.Default => WScrollBarVisibility.Auto,
ScrollBarVisibility.Always => WScrollBarVisibility.Visible,
ScrollBarVisibility.Never => WScrollBarVisibility.Hidden,
_ => WScrollBarVisibility.Auto
};
}

void UpdateHorizontalScrollbarVisibility(ScrollBarVisibility scrollBarVisibility)
{
scrollViewer.HorizontalScrollBarVisibility = scrollBarVisibility switch
{
ScrollBarVisibility.Default => WScrollBarVisibility.Auto,
ScrollBarVisibility.Always => WScrollBarVisibility.Visible,
ScrollBarVisibility.Never => WScrollBarVisibility.Hidden,
_ => WScrollBarVisibility.Auto
};
}
}
12 changes: 12 additions & 0 deletions VirtualListView/VirtualListViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public partial class VirtualListViewHandler
[nameof(IVirtualListView.IsRefreshEnabled)] = MapIsRefreshEnabled,
[nameof(IVirtualListView.EmptyView)] = MapEmptyView,
[nameof(IVirtualListView.SelectedItems)] = MapSelectedItems,
[nameof(IVirtualListView.VerticalScrollbarVisibility)] = MapVerticalScrollbarVisibility,
[nameof(IVirtualListView.HorizontalScrollbarVisibility)] = MapHorizontalScrollbarVisibility,
};

public static CommandMapper<IVirtualListView, VirtualListViewHandler> CommandMapper = new(ViewCommandMapper)
Expand Down Expand Up @@ -135,5 +137,15 @@ public static void MapIsFooterVisible(VirtualListViewHandler handler, IVirtualLi
{
handler?.InvalidateData();
}

public static void MapVerticalScrollbarVisibility(VirtualListViewHandler handler, IVirtualListView virtualListView)
{
handler?.UpdateVerticalScrollbarVisibility(virtualListView.VerticalScrollbarVisibility);
}

public static void MapHorizontalScrollbarVisibility(VirtualListViewHandler handler, IVirtualListView virtualListView)
{
handler?.UpdateHorizontalScrollbarVisibility(virtualListView.HorizontalScrollbarVisibility);
}

}

0 comments on commit e7b420a

Please sign in to comment.