diff --git a/src/Uno.Toolkit.UI/Controls/ZoomContentControl/BoundsVisibilityFlag.cs b/src/Uno.Toolkit.UI/Controls/ZoomContentControl/BoundsVisibilityFlag.cs index 6e480c5a6..c5985e6de 100644 --- a/src/Uno.Toolkit.UI/Controls/ZoomContentControl/BoundsVisibilityFlag.cs +++ b/src/Uno.Toolkit.UI/Controls/ZoomContentControl/BoundsVisibilityFlag.cs @@ -1,15 +1,15 @@ -using System; +//using System; -namespace Uno.Toolkit.UI; +//namespace Uno.Toolkit.UI; -[Flags] -public enum BoundsVisibilityFlag -{ - Left = 1 << 0, - Top = 1 << 1, - Right = 1 << 2, - Bottom = 1 << 3, +//[Flags] +//public enum BoundsVisibilityFlag +//{ +// Left = 1 << 0, +// Top = 1 << 1, +// Right = 1 << 2, +// Bottom = 1 << 3, - None = 0, - All = Left | Top | Right | Bottom, -} +// None = 0, +// All = Left | Top | Right | Bottom, +//} diff --git a/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.Properties.cs b/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.Properties.cs index d1139dc02..79746a6e2 100644 --- a/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.Properties.cs +++ b/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.Properties.cs @@ -400,7 +400,7 @@ public bool AutoZoomToCanvasOnSizeChanged nameof(AdditionalMargin), typeof(Thickness), typeof(ZoomContentControl), - new PropertyMetadata(Thickness.Empty, OnAdditionalMarginChanged)); + new PropertyMetadata(new Thickness(0), OnAdditionalMarginChanged)); /// Gets or sets additional margins around the content. public Thickness AdditionalMargin @@ -410,23 +410,23 @@ public Thickness AdditionalMargin } #endregion - #region DependencyProperty: ContentBoundsVisibility - - /// Identifies the ContentBoundsVisibility dependency property. - public static DependencyProperty ContentBoundsVisibilityProperty { get; } = DependencyProperty.Register( - nameof(ContentBoundsVisibility), - typeof(BoundsVisibilityFlag), - typeof(ZoomContentControl), - new PropertyMetadata(BoundsVisibilityFlag.None)); - - /// Gets or sets the visibility data for the content bounds. - public BoundsVisibilityFlag ContentBoundsVisibility - { - get => (BoundsVisibilityFlag)GetValue(ContentBoundsVisibilityProperty); - private set => SetValue(ContentBoundsVisibilityProperty, value); - } - - #endregion + //#region DependencyProperty: ContentBoundsVisibility + + ///// Identifies the ContentBoundsVisibility dependency property. + //public static DependencyProperty ContentBoundsVisibilityProperty { get; } = DependencyProperty.Register( + // nameof(ContentBoundsVisibility), + // typeof(BoundsVisibilityFlag), + // typeof(ZoomContentControl), + // new PropertyMetadata(BoundsVisibilityFlag.None)); + + ///// Gets or sets the visibility data for the content bounds. + //public BoundsVisibilityFlag ContentBoundsVisibility + //{ + // get => (BoundsVisibilityFlag)GetValue(ContentBoundsVisibilityProperty); + // private set => SetValue(ContentBoundsVisibilityProperty, value); + //} + + //#endregion private static void OnHorizontalScrollValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) => ((ZoomContentControl)sender).OnHorizontalScrollValueChanged(); private static void OnVerticalScrollValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) => ((ZoomContentControl)sender).OnVerticalScrollValueChanged(); diff --git a/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.cs b/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.cs index c11f759e8..f6b13f43e 100644 --- a/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.cs +++ b/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.cs @@ -31,11 +31,10 @@ using Windows.Devices.Input; #endif -using static Uno.Toolkit.UI.BoundsVisibilityFlag; - namespace Uno.Toolkit.UI; [TemplatePart(Name = TemplateParts.RootGrid, Type = typeof(Grid))] +[TemplatePart(Name = TemplateParts.Canvas, Type = typeof(Canvas))] [TemplatePart(Name = TemplateParts.Presenter, Type = typeof(ContentPresenter))] [TemplatePart(Name = TemplateParts.VerticalScrollBar, Type = typeof(ScrollBar))] [TemplatePart(Name = TemplateParts.HorizontalScrollBar, Type = typeof(ScrollBar))] @@ -45,6 +44,7 @@ public partial class ZoomContentControl : ContentControl private static class TemplateParts { public const string RootGrid = "PART_RootGrid"; + public const string Canvas = "PART_Canvas"; public const string Presenter = "PART_Presenter"; public const string HorizontalScrollBar = "PART_ScrollH"; public const string VerticalScrollBar = "PART_ScrollV"; @@ -52,7 +52,8 @@ private static class TemplateParts } public event EventHandler? RenderedContentUpdated; - + + private Canvas? _canvas; private ContentPresenter? _presenter; private ScrollBar? _scrollV; private ScrollBar? _scrollH; @@ -60,20 +61,10 @@ private static class TemplateParts private Point _lastPosition = new Point(0, 0); private (bool Horizontal, bool Vertical) _movementDirection = (false, false); - private bool IsAllowedToWork => (IsEnabled && IsActive && _presenter is not null); + private bool IsAllowedToWork => (IsLoaded && IsActive && _presenter is not null); private uint _capturedPointerId; private Point _referencePosition; - 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); @@ -98,13 +89,14 @@ T FindTemplatePart(string name) where T : class => _scrollV = FindTemplatePart(TemplateParts.VerticalScrollBar); _scrollH = FindTemplatePart(TemplateParts.HorizontalScrollBar); _translation = FindTemplatePart(TemplateParts.TranslateTransform); + _canvas = FindTemplatePart(TemplateParts.Canvas); ResetOffset(); ResetZoom(); if (_presenter?.Content is FrameworkElement { } fe) { - fe.LayoutUpdated += (s, e) => + fe.SizeChanged += (s, e) => { ViewportWidth = fe.ActualWidth; ViewportHeight = fe.ActualHeight; @@ -119,12 +111,12 @@ private async Task RaiseRenderedContentUpdated() await Task.Yield(); RenderedContentUpdated?.Invoke(this, EventArgs.Empty); } - + private void OnHorizontalScrollValueChanged() { UpdateTranslation(); } - + private void OnVerticalScrollValueChanged() { UpdateTranslation(); @@ -146,40 +138,48 @@ private async void OnZoomLevelChanged() private void UpdateContentBoundsVisibility() { - if (_presenter?.Content is FrameworkElement fe) - { - var m = GetPositionMatrix(fe, this); - - var flags = None; - if (m.OffsetX >= 0) flags |= BoundsVisibilityFlag.Left; - if (m.OffsetY >= 0) flags |= BoundsVisibilityFlag.Top; - if (ActualWidth >= (fe.ActualWidth * ZoomLevel) + m.OffsetX) flags |= BoundsVisibilityFlag.Right; - if (ActualHeight >= (fe.ActualHeight * ZoomLevel) + m.OffsetY) flags |= BoundsVisibilityFlag.Bottom; - - ContentBoundsVisibility = flags; - } + //if (_presenter?.Content is FrameworkElement fe) + //{ + // var m = GetPositionMatrix(fe, this); + + // var flags = None; + // if (m.OffsetX >= 0) flags |= BoundsVisibilityFlag.Left; + // if (m.OffsetY >= 0) flags |= BoundsVisibilityFlag.Top; + // if (ActualWidth >= (fe.ActualWidth * ZoomLevel) + m.OffsetX) flags |= BoundsVisibilityFlag.Right; + // if (ActualHeight >= (fe.ActualHeight * ZoomLevel) + m.OffsetY) flags |= BoundsVisibilityFlag.Bottom; + + // ContentBoundsVisibility = flags; + //} + this._capturedPointerId.ToString(); } private void UpdateScrollVisibility() { - IsHorizontalScrollBarVisible = !ContentBoundsVisibility.HasFlag(BoundsVisibilityFlag.Left | BoundsVisibilityFlag.Right); - IsVerticalScrollBarVisible = !ContentBoundsVisibility.HasFlag(BoundsVisibilityFlag.Top | BoundsVisibilityFlag.Bottom); + if (_canvas is not null) + { + IsHorizontalScrollBarVisible = _canvas.ActualWidth < ((ViewportWidth + AdditionalMargin.Left + AdditionalMargin.Right) * ZoomLevel); + IsVerticalScrollBarVisible = _canvas.ActualHeight < ((ViewportHeight + AdditionalMargin.Top + AdditionalMargin.Bottom) * ZoomLevel); + } } private bool CanMoveIn((bool Horizontal, bool Vertical) _movementDirection) { - if (ContentBoundsVisibility.HasFlag(All)) - { - return false; - } + //if (ContentBoundsVisibility.HasFlag(All)) + //{ + // return false; + //} + + //var canMove = false; + //canMove |= CanScrollLeft() && _movementDirection.Horizontal is true; + //canMove |= CanScrollRight() && _movementDirection.Horizontal is false; + //canMove |= CanScrollUp() && _movementDirection.Vertical is true; + //canMove |= CanScrollDown() && _movementDirection.Vertical is false; + + //return canMove; - var canMove = false; - canMove |= CanScrollLeft() && _movementDirection.Horizontal is true; - canMove |= CanScrollRight() && _movementDirection.Horizontal is false; - canMove |= CanScrollUp() && _movementDirection.Vertical is true; - canMove |= CanScrollDown() && _movementDirection.Vertical is false; + this._capturedPointerId.ToString(); - return canMove; + return false; } private void IsActiveChanged() @@ -275,7 +275,7 @@ private void OnPointerReleased(object sender, PointerRoutedEventArgs e) private void OnPointerMoved(object sender, PointerRoutedEventArgs e) { - if (!(IsAllowedToWork && _capturedPointerId > 0 && IsPanAllowed)) return; + if (!IsAllowedToWork || _capturedPointerId == 0 || !IsPanAllowed) return; var currentPosition = e.GetCurrentPoint(this).Position; _movementDirection = (currentPosition.X > _lastPosition.X, currentPosition.Y > _lastPosition.Y); @@ -348,16 +348,18 @@ private double GetPanDelta(PointerPointProperties pointerProperties) private void TryUpdateOffsets(double deltaX, double deltaY) // todo: review scroll values { - if ((deltaX > 0 && CanScrollLeft()) || - (deltaX < 0 && CanScrollRight())) + //if ((deltaX > 0 && CanScrollLeft()) || + // (deltaX < 0 && CanScrollRight())) + if (DateTime.Now.Month == 13) { var offset = HorizontalScrollValue + deltaX; var max = HorizontalMaxScroll * ZoomLevel; HorizontalScrollValue = Math.Clamp(offset, 0, max); } - if ((deltaY > 0 && CanScrollUp()) || - (deltaY < 0 && CanScrollDown())) + //if ((deltaY > 0 && CanScrollUp()) || + // (deltaY < 0 && CanScrollDown())) + if (DateTime.Now.Month == 13) { var offset = VerticalScrollValue + deltaY; var max = VerticalMaxScroll * ZoomLevel; @@ -376,7 +378,7 @@ public void ResetViewport() private void ResetOffset() { - HorizontalScrollValue =0; + HorizontalScrollValue = 0; VerticalScrollValue = 0; } @@ -384,9 +386,11 @@ public void CenterContent() { if (IsActive && _presenter?.Content is FrameworkElement { } content) { + var width = ActualWidth - (AdditionalMargin.Left + AdditionalMargin.Right); + var height = ActualHeight - (AdditionalMargin.Top + AdditionalMargin.Bottom); - HorizontalScrollValue = ((AvailableSize.Width - (content.ActualWidth * ZoomLevel)) / 2) + AdditionalMargin.Left; - VerticalScrollValue = ((AvailableSize.Height - (content.ActualHeight * ZoomLevel)) / 2) + AdditionalMargin.Top; + HorizontalScrollValue = ((width - (content.ActualWidth * ZoomLevel)) / 2) + AdditionalMargin.Left; + VerticalScrollValue = ((height - (content.ActualHeight * ZoomLevel)) / 2) + AdditionalMargin.Top; } } @@ -403,10 +407,10 @@ public void FitToCanvas() } // 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 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; diff --git a/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.xaml b/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.xaml index 081a66605..6444ef4b3 100644 --- a/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.xaml +++ b/src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.xaml @@ -25,7 +25,7 @@ Grid.Column="0">