Skip to content

Commit

Permalink
fix(scrollviewer): Was throwing exception when <ScrollViewerPresenter…
Browse files Browse the repository at this point in the history
… /> were absent from Control Template. Now simply behave as a ContentControl as UWP.
  • Loading branch information
carldebilly committed Oct 29, 2020
1 parent 40a5787 commit 4013b29
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/Uno.UI/UI/Xaml/Controls/ScrollViewer/ScrollViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#pragma warning disable CS0067
#endif

#nullable enable

using System;
using System.Collections.Generic;
using Uno.Disposables;
Expand Down Expand Up @@ -571,7 +573,7 @@ public double HorizontalOffset
private readonly SerialDisposable _sizeChangedSubscription = new SerialDisposable();

#pragma warning disable 649 // unused member for Unit tests
private IScrollContentPresenter _presenter;
private IScrollContentPresenter? _presenter;
#pragma warning restore 649 // unused member for Unit tests

/// <summary>
Expand Down Expand Up @@ -754,7 +756,7 @@ private static bool ComputeIsScrollEnabled(double scrollable, ScrollBarVisibilit
&& visibility != ScrollBarVisibility.Disabled
&& mode != ScrollMode.Disabled;

private static ScrollBarVisibility ComputeNativeScrollBarVisibility(ScrollBarVisibility visibility, ScrollMode mode, ScrollBar managedScrollbar)
private static ScrollBarVisibility ComputeNativeScrollBarVisibility(ScrollBarVisibility visibility, ScrollMode mode, ScrollBar? managedScrollbar)
=> mode switch
{
ScrollMode.Disabled => ScrollBarVisibility.Disabled,
Expand Down Expand Up @@ -784,17 +786,18 @@ protected override void OnApplyTemplate()


base.OnApplyTemplate();
_isTemplateApplied = true;

var scpTemplatePart = GetTemplateChild(Parts.WinUI3.Scroller) ?? GetTemplateChild(Parts.Uwp.ScrollContentPresenter);
_presenter = scpTemplatePart as IScrollContentPresenter;

_isTemplateApplied = _presenter != null;

// Load new template
_verticalScrollbar = null;
_isVerticalScrollBarMaterialized = false;
_horizontalScrollbar = null;
_isHorizontalScrollBarMaterialized = false;

var scpTemplatePart = GetTemplateChild(Parts.WinUI3.Scroller) ?? GetTemplateChild(Parts.Uwp.ScrollContentPresenter);
_presenter = scpTemplatePart as IScrollContentPresenter;

#if __IOS__ || __MACOS__ || __ANDROID__
if (scpTemplatePart is ScrollContentPresenter scp)
{
Expand All @@ -806,11 +809,6 @@ protected override void OnApplyTemplate()
}
#endif

if (_presenter == null)
{
throw new InvalidOperationException("The template part ScrollContentPresenter could not be found or is not a ScrollContentPresenter");
}

// We update the scrollability properties here in order to make sure to set the right scrollbar visibility
// on the _presenter as soon as possible
UpdateComputedVerticalScrollability(invalidate: false);
Expand Down Expand Up @@ -878,7 +876,10 @@ private void ApplyScrollContentPresenterContent()
}

// Then explicitly propagate the Content to the _presenter
_presenter.Content = Content as View;
if (_presenter != null)
{
_presenter.Content = Content as View;
}

// Propagate the ScrollViewer's own templated parent, instead of
// the scrollviewer itself (through ScrollContentPresenter)
Expand All @@ -903,7 +904,7 @@ void OnElementSizeChanged(object sender, SizeChangedEventArgs args)
=> UpdateDimensionProperties();
}

internal protected override void OnTemplatedParentChanged(DependencyPropertyChangedEventArgs e)
protected internal override void OnTemplatedParentChanged(DependencyPropertyChangedEventArgs e)
{
base.OnTemplatedParentChanged(e);

Expand All @@ -929,8 +930,8 @@ private void ClearContentTemplatedParent(object oldContent)

#region Managed scroll bars support
private bool _isTemplateApplied;
private ScrollBar _verticalScrollbar;
private ScrollBar _horizontalScrollbar;
private ScrollBar? _verticalScrollbar;
private ScrollBar? _horizontalScrollbar;
private bool _isVerticalScrollBarMaterialized;
private bool _isHorizontalScrollBarMaterialized;

Expand Down Expand Up @@ -1175,7 +1176,7 @@ public bool ChangeView(double? horizontalOffset, double? verticalOffset, float?
partial void ChangeViewZoom(float zoomFactor, bool disableAnimation);

#region Scroll indicators visual states (Managed scroll bars only)
private DispatcherQueueTimer _indicatorResetTimer;
private DispatcherQueueTimer? _indicatorResetTimer;
private string _indicatorState;

private static void ShowScrollIndicator(object sender, PointerRoutedEventArgs e) // OnPointerMove
Expand Down

0 comments on commit 4013b29

Please sign in to comment.