Skip to content

Commit

Permalink
feat(scrollviewer): Add ability to configure scrollbar's auto-hide de…
Browse files Browse the repository at this point in the history
…lay, and fully disable auto-hide
  • Loading branch information
dr1rrb committed May 7, 2021
1 parent 534d3ba commit a5ec556
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/Uno.UI/FeatureConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,14 @@ public static class ScrollViewer
/// </remarks>
public static ScrollViewerUpdatesMode DefaultUpdatesMode { get; set; } = ScrollViewerUpdatesMode.AsynchronousIdle;

/// <summary>
/// Defines the delay after which the scrollbars hide themselves when pointer is not over.<br/>
/// Default is 4 sec.<br/>
/// Setting this to <see cref="TimeSpan.MaxValue"/> will completely disable the auto hide feature.
/// </summary>
/// <remarks>This is effective only for managed scrollbars (WASM, macOS and Skia for now)</remarks>
public static TimeSpan? DefaultAutoHideDelay { get; set; }

#if __ANDROID__
/// <summary>
/// This value defines an optional delay to be set for native ScrollBar thumbs to disapear. The
Expand Down
36 changes: 30 additions & 6 deletions src/Uno.UI/UI/Xaml/Controls/ScrollViewer/ScrollViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ protected override void OnApplyTemplate()

OnBringIntoViewOnFocusChangeChangedPartial(BringIntoViewOnFocusChange);

ResetScrollIndicator(forced: true);
PrepareScrollIndicator();
}

partial void OnApplyTemplatePartial();
Expand Down Expand Up @@ -1368,16 +1368,30 @@ private bool ChangeViewCore(
return ChangeViewNative(horizontalOffset, verticalOffset, zoomFactor, disableAnimation);
}

#region Scroll indicators visual states (Managed scroll bars only)
#region Scroll indicators visual states (Managed scroll bars only)
private static readonly TimeSpan _indicatorResetDelay = FeatureConfiguration.ScrollViewer.DefaultAutoHideDelay ?? TimeSpan.FromSeconds(4);
private static readonly bool _indicatorResetDisabled = _indicatorResetDelay == TimeSpan.MaxValue;
private DispatcherQueueTimer? _indicatorResetTimer;
private string? _indicatorState;

private void PrepareScrollIndicator() // OnApplyTemplate
{
if (_indicatorResetDisabled)
{
ShowScrollIndicator(PointerDeviceType.Mouse, forced: true);
}
else
{
ResetScrollIndicator(forced: true);
}
}

private static void ShowScrollIndicator(object sender, PointerRoutedEventArgs e) // OnPointerMove
=> (sender as ScrollViewer)?.ShowScrollIndicator(e.Pointer.PointerDeviceType);

private void ShowScrollIndicator(PointerDeviceType type)
private void ShowScrollIndicator(PointerDeviceType type, bool forced = false)
{
if (!ComputedIsVerticalScrollEnabled && !ComputedIsHorizontalScrollEnabled)
if (!forced && !ComputedIsVerticalScrollEnabled && !ComputedIsHorizontalScrollEnabled)
{
return;
}
Expand All @@ -1393,13 +1407,18 @@ private void ShowScrollIndicator(PointerDeviceType type)
_indicatorState = indicatorState;
}

if (_indicatorResetDisabled)
{
return;
}

// Automatically hide the scroll indicator after a delay without any interaction
if (_indicatorResetTimer == null)
{
var weakRef = WeakReferencePool.RentSelfWeakReference(this);
_indicatorResetTimer = new DispatcherQueueTimer
{
Interval = TimeSpan.FromSeconds(4),
Interval = _indicatorResetDelay,
IsRepeating = false
};
_indicatorResetTimer.Tick += (snd, e) => (weakRef.Target as ScrollViewer)?.ResetScrollIndicator();
Expand All @@ -1412,6 +1431,11 @@ private static void ResetScrollIndicator(object sender, RoutedEventArgs _) // On

private void ResetScrollIndicator(bool forced = false)
{
if (_indicatorResetDisabled)
{
return;
}

_indicatorResetTimer?.Stop();

if (!forced && ((_horizontalScrollbar?.IsPointerOver ?? false) || (_verticalScrollbar?.IsPointerOver ?? false)))
Expand Down Expand Up @@ -1447,6 +1471,6 @@ private void HideScrollBarSeparator(object sender, PointerRoutedEventArgs e) //
VisualStateManager.GoToState(this, VisualStates.ScrollBarsSeparator.Collapsed, true);
}
}
#endregion
#endregion
}
}

0 comments on commit a5ec556

Please sign in to comment.