diff --git a/samples/Uno.Toolkit.Samples/Uno.Toolkit.Samples.Shared/Content/Controls/ZoomContentControlSamplePage.xaml.cs b/samples/Uno.Toolkit.Samples/Uno.Toolkit.Samples.Shared/Content/Controls/ZoomContentControlSamplePage.xaml.cs index f1f3f494f..5b66d1313 100644 --- a/samples/Uno.Toolkit.Samples/Uno.Toolkit.Samples.Shared/Content/Controls/ZoomContentControlSamplePage.xaml.cs +++ b/samples/Uno.Toolkit.Samples/Uno.Toolkit.Samples.Shared/Content/Controls/ZoomContentControlSamplePage.xaml.cs @@ -72,6 +72,6 @@ private void OnZoomOutClick(object sender, RoutedEventArgs e) private void OnResetClick(object sender, RoutedEventArgs e) { - zoomControl.Initialize(); + zoomControl.ResetViewport(); } } diff --git a/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.Properties.cs b/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.Properties.cs index 59d7f6f51..f6eb8862a 100644 --- a/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.Properties.cs +++ b/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.Properties.cs @@ -52,7 +52,6 @@ public double HorizontalOffset } #endregion - #region DependencyProperty: HorizontalScrollValue /// Identifies the HorizontalScrollValue dependency property. @@ -70,7 +69,6 @@ public double HorizontalScrollValue } #endregion - #region DependencyProperty: HorizontalMinScroll /// Identifies the HorizontalMinScroll dependency property. @@ -88,7 +86,6 @@ public double HorizontalMinScroll } #endregion - #region DependencyProperty: HorizontalMaxScroll /// Identifies the HorizontalMaxScroll dependency property. @@ -106,7 +103,6 @@ public double HorizontalMaxScroll } #endregion - #region DependencyProperty: HorizontalZoomCenter /// Identifies the HorizontalZoomCenter dependency property. @@ -142,7 +138,6 @@ public double VerticalOffset } #endregion - #region DependencyProperty: VerticalMaxScroll /// Identifies the VerticalMaxScroll dependency property. @@ -160,7 +155,6 @@ public double VerticalMaxScroll } #endregion - #region DependencyProperty: VerticalMinScroll /// Identifies the VerticalMinScroll dependency property. @@ -178,7 +172,6 @@ public double VerticalMinScroll } #endregion - #region DependencyProperty: VerticalZoomCenter /// Identifies the VerticalZoomCenter dependency property. @@ -197,42 +190,6 @@ public double VerticalZoomCenter #endregion - #region DependencyProperty: ViewportWidth - - /// Identifies the ViewportWidth dependency property. - public static DependencyProperty ViewportWidthProperty { get; } = DependencyProperty.Register( - nameof(ViewportWidth), - typeof(double), - typeof(ZoomContentControl), - new PropertyMetadata(default(double))); - - /// Gets or sets the width of the viewport. - public double ViewportWidth - { - get => (double)GetValue(ViewportWidthProperty); - set => SetValue(ViewportWidthProperty, value); - } - - #endregion - - #region DependencyProperty: ViewportHeight - - /// Identifies the ViewportHeight dependency property. - public static DependencyProperty ViewportHeightProperty { get; } = DependencyProperty.Register( - nameof(ViewportHeight), - typeof(double), - typeof(ZoomContentControl), - new PropertyMetadata(default(double))); - - /// Gets or sets the height of the viewport. - public double ViewportHeight - { - get => (double)GetValue(ViewportHeightProperty); - set => SetValue(ViewportHeightProperty, value); - } - - #endregion - #region DependencyProperty: ZoomLevel /// Identifies the ZoomLevel dependency property. @@ -250,7 +207,6 @@ public double ZoomLevel } #endregion - #region DependencyProperty: MinZoomLevel /// Identifies the MinZoomLevel dependency property. @@ -268,7 +224,6 @@ public double MinZoomLevel } #endregion - #region DependencyProperty: MaxZoomLevel /// Identifies the MaxZoomLevel dependency property. @@ -304,7 +259,6 @@ public double PanWheelRatio } #endregion - #region DependencyProperty: ScaleWheelRatio /// Identifies the ScaleWheelRatio dependency property. @@ -323,6 +277,41 @@ public double ScaleWheelRatio #endregion + #region DependencyProperty: ViewportWidth + + /// Identifies the ViewportWidth dependency property. + public static DependencyProperty ViewportWidthProperty { get; } = DependencyProperty.Register( + nameof(ViewportWidth), + typeof(double), + typeof(ZoomContentControl), + new PropertyMetadata(default(double))); + + /// Gets or sets the width of the viewport. + public double ViewportWidth + { + get => (double)GetValue(ViewportWidthProperty); + set => SetValue(ViewportWidthProperty, value); + } + + #endregion + #region DependencyProperty: ViewportHeight + + /// Identifies the ViewportHeight dependency property. + public static DependencyProperty ViewportHeightProperty { get; } = DependencyProperty.Register( + nameof(ViewportHeight), + typeof(double), + typeof(ZoomContentControl), + new PropertyMetadata(default(double))); + + /// Gets or sets the height of the viewport. + public double ViewportHeight + { + get => (double)GetValue(ViewportHeightProperty); + set => SetValue(ViewportHeightProperty, value); + } + + #endregion + #region DependencyProperty: IsActive /// Identifies the IsActive dependency property. @@ -340,7 +329,6 @@ public bool IsActive } #endregion - #region DependencyProperty: IsZoomAllowed /// Identifies the IsZoomAllowed dependency property. @@ -358,7 +346,6 @@ public bool IsZoomAllowed } #endregion - #region DependencyProperty: IsHorizontalScrollBarVisible /// Identifies the IsHorizontalScrollBarVisible dependency property. @@ -376,7 +363,6 @@ public bool IsHorizontalScrollBarVisible } #endregion - #region DependencyProperty: IsPanAllowed /// Identifies the IsPanAllowed dependency property. @@ -394,7 +380,6 @@ public bool IsPanAllowed } #endregion - #region DependencyProperty: IsVerticalScrollBarVisible /// Identifies the IsVerticalScrollBarVisible dependency property. @@ -412,7 +397,6 @@ public bool IsVerticalScrollBarVisible } #endregion - #region DependencyProperty: AutoZoomToCanvasOnSizeChanged /// Identifies the AutoZoomToCanvasOnSizeChanged dependency property. @@ -430,7 +414,6 @@ public bool AutoZoomToCanvasOnSizeChanged } #endregion - #region DependencyProperty: AdditionalMargin /// Identifies the AdditionalMargin dependency property. @@ -448,7 +431,6 @@ public Thickness AdditionalMargin } #endregion - #region DependencyProperty: ContentBoundsVisibility /// Identifies the ContentBoundsVisibility dependency property. diff --git a/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.cs b/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.cs index 58a3a4c5b..c656c4fc6 100644 --- a/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.cs +++ b/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.cs @@ -49,6 +49,9 @@ private static class TemplateParts public const string VerticalScrollBar = "PART_ScrollV"; } + // Events + public event EventHandler? RenderedContentUpdated; + // Fields private ContentPresenter? _presenter; private ScrollBar? _scrollV; @@ -61,7 +64,17 @@ private static class TemplateParts private uint _capturedPointerId; private Point _referencePosition; - // Constructor + // Properties + public Size AvailableSize + { + get + { + var vOffset = (AdditionalMargin.Top + AdditionalMargin.Bottom); + var hOffset = (AdditionalMargin.Left + AdditionalMargin.Right); + return new Size(ActualWidth - hOffset, ActualHeight - vOffset); + } + } + public ZoomContentControl() { DefaultStyleKey = typeof(ZoomContentControl); @@ -73,21 +86,43 @@ public ZoomContentControl() PointerWheelChanged += OnPointerWheelChanged; } - // Events - public event EventHandler? RenderedContentUpdated; - - // Properties - public Size AvailableSize + protected override void OnApplyTemplate() { - get + base.OnApplyTemplate(); + + T FindTemplatePart(string name) where T : class => + (GetTemplateChild(name) ?? throw new Exception($"Expected template part not found: {name}")) + as T ?? throw new Exception($"Expected template part '{name}' to be of type: {typeof(T)}"); + + _presenter = FindTemplatePart(TemplateParts.Presenter); + _scrollV = FindTemplatePart(TemplateParts.VerticalScrollBar); + _scrollH = FindTemplatePart(TemplateParts.HorizontalScrollBar); + + ResetOffset(); + ResetZoom(); + + if (_presenter?.Content is FrameworkElement { } fe) { - var vOffset = (AdditionalMargin.Top + AdditionalMargin.Bottom); - var hOffset = (AdditionalMargin.Left + AdditionalMargin.Right); - return new Size(ActualWidth - hOffset, ActualHeight - vOffset); + fe.LayoutUpdated += (s, e) => + { + ViewportWidth = fe.ActualWidth; + ViewportHeight = fe.ActualHeight; + + UpdateScrollLimits(); + }; + } + + if (_scrollV is not null) + { + _scrollV.Scroll += ScrollV_Scroll; + } + + if (_scrollH is not null) + { + _scrollH.Scroll += ScrollH_Scroll; } } - // Methods private async Task RaiseRenderedContentUpdated() { await Task.Yield(); @@ -155,11 +190,6 @@ private bool CanMoveIn((bool Horizontal, bool Vertical) _movementDirection) return canMove; } - private bool CanScrollUp() => !ContentBoundsVisibility.HasFlag(BoundsVisibilityFlag.Top); - private bool CanScrollDown() => !ContentBoundsVisibility.HasFlag(BoundsVisibilityFlag.Bottom); - private bool CanScrollLeft() => !ContentBoundsVisibility.HasFlag(BoundsVisibilityFlag.Left); - private bool CanScrollRight() => !ContentBoundsVisibility.HasFlag(BoundsVisibilityFlag.Right); - private async void UpdateHorizontalScrollBarValue() { HorizontalScrollValue = -1 * HorizontalOffset; @@ -203,26 +233,6 @@ private void CoerceZoomLevel() ZoomLevel = Math.Clamp(ZoomLevel, MinZoomLevel, MaxZoomLevel); } - // Template handling - protected override void OnApplyTemplate() - { - base.OnApplyTemplate(); - - T FindTemplatePart(string name) where T : class => - (GetTemplateChild(name) ?? throw new Exception($"Expected template part not found: {name}")) - as T ?? throw new Exception($"Expected template part '{name}' to be of type: {typeof(T)}"); - - _presenter = FindTemplatePart(TemplateParts.Presenter); - _scrollV = FindTemplatePart(TemplateParts.VerticalScrollBar); - _scrollH = FindTemplatePart(TemplateParts.HorizontalScrollBar); - - ResetOffset(); - ResetZoom(); - - RegisterToControlEvents(); - } - - // Event handlers private void OnLoaded(object sender, RoutedEventArgs e) { CenterContent(); @@ -237,30 +247,6 @@ private void OnSizeChanged(object sender, SizeChangedEventArgs args) } } - private void RegisterToControlEvents() - { - if (_presenter?.Content is FrameworkElement { } fe) - { - fe.LayoutUpdated += (s, e) => - { - ViewportWidth = fe.ActualWidth; - ViewportHeight = fe.ActualHeight; - - UpdateScrollLimits(); - }; - } - - if (_scrollV is not null) - { - _scrollV.Scroll += ScrollV_Scroll; - } - - if (_scrollH is not null) - { - _scrollH.Scroll += ScrollH_Scroll; - } - } - private void ScrollV_Scroll(object sender, ScrollEventArgs e) { if ((_previousVerticalScrollValue > e.NewValue && !CanScrollUp()) || @@ -285,7 +271,6 @@ private void ScrollH_Scroll(object sender, ScrollEventArgs e) _previousHorizontalScrollValue = e.NewValue; } - // Additional private methods for pointer handling private void OnPointerPressed(object sender, PointerRoutedEventArgs e) { if (!IsAllowedToWork) return; @@ -409,8 +394,7 @@ private void TryUpdateOffsets(double deltaX, double deltaY) } } - // Public methods - public void Initialize() + public void ResetViewport() { ResetZoom(); ResetOffset(); @@ -418,6 +402,7 @@ public void Initialize() } internal void ResetZoom() => ZoomLevel = 1; + private void ResetOffset() { HorizontalOffset = AdditionalMargin.Left; @@ -451,7 +436,12 @@ public void FitToCanvas() } } - // Static helper + // Helper + private bool CanScrollUp() => !ContentBoundsVisibility.HasFlag(BoundsVisibilityFlag.Top); + private bool CanScrollDown() => !ContentBoundsVisibility.HasFlag(BoundsVisibilityFlag.Bottom); + private bool CanScrollLeft() => !ContentBoundsVisibility.HasFlag(BoundsVisibilityFlag.Left); + private bool CanScrollRight() => !ContentBoundsVisibility.HasFlag(BoundsVisibilityFlag.Right); + private static Matrix GetPositionMatrix(FrameworkElement element, FrameworkElement rootElement) => ((MatrixTransform)element.TransformToVisual(rootElement)).Matrix; }