Skip to content

Commit

Permalink
wip: removed BoundVisibilityFlag
Browse files Browse the repository at this point in the history
  • Loading branch information
vatsashah45 committed Oct 22, 2024
1 parent ff4ee69 commit 185d83e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -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,
//}
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public bool AutoZoomToCanvasOnSizeChanged
nameof(AdditionalMargin),
typeof(Thickness),
typeof(ZoomContentControl),
new PropertyMetadata(Thickness.Empty, OnAdditionalMarginChanged));
new PropertyMetadata(new Thickness(0), OnAdditionalMarginChanged));

/// <summary>Gets or sets additional margins around the content.</summary>
public Thickness AdditionalMargin
Expand All @@ -410,23 +410,23 @@ public Thickness AdditionalMargin
}

#endregion
#region DependencyProperty: ContentBoundsVisibility

/// <summary>Identifies the ContentBoundsVisibility dependency property.</summary>
public static DependencyProperty ContentBoundsVisibilityProperty { get; } = DependencyProperty.Register(
nameof(ContentBoundsVisibility),
typeof(BoundsVisibilityFlag),
typeof(ZoomContentControl),
new PropertyMetadata(BoundsVisibilityFlag.None));

/// <summary>Gets or sets the visibility data for the content bounds.</summary>
public BoundsVisibilityFlag ContentBoundsVisibility
{
get => (BoundsVisibilityFlag)GetValue(ContentBoundsVisibilityProperty);
private set => SetValue(ContentBoundsVisibilityProperty, value);
}

#endregion
//#region DependencyProperty: ContentBoundsVisibility

///// <summary>Identifies the ContentBoundsVisibility dependency property.</summary>
//public static DependencyProperty ContentBoundsVisibilityProperty { get; } = DependencyProperty.Register(
// nameof(ContentBoundsVisibility),
// typeof(BoundsVisibilityFlag),
// typeof(ZoomContentControl),
// new PropertyMetadata(BoundsVisibilityFlag.None));

///// <summary>Gets or sets the visibility data for the content bounds.</summary>
//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();
Expand Down
110 changes: 57 additions & 53 deletions src/Uno.Toolkit.UI/Controls/ZoomContentControl/ZoomContentControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand All @@ -45,35 +44,27 @@ 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";
public const string TranslateTransform = "PART_TranslateTransform";
}

public event EventHandler<EventArgs>? RenderedContentUpdated;


private Canvas? _canvas;
private ContentPresenter? _presenter;
private ScrollBar? _scrollV;
private ScrollBar? _scrollH;
private TranslateTransform? _translation;

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);
Expand All @@ -98,13 +89,14 @@ T FindTemplatePart<T>(string name) where T : class =>
_scrollV = FindTemplatePart<ScrollBar>(TemplateParts.VerticalScrollBar);
_scrollH = FindTemplatePart<ScrollBar>(TemplateParts.HorizontalScrollBar);
_translation = FindTemplatePart<TranslateTransform>(TemplateParts.TranslateTransform);
_canvas = FindTemplatePart<Canvas>(TemplateParts.Canvas);

ResetOffset();
ResetZoom();

if (_presenter?.Content is FrameworkElement { } fe)
{
fe.LayoutUpdated += (s, e) =>
fe.SizeChanged += (s, e) =>
{
ViewportWidth = fe.ActualWidth;
ViewportHeight = fe.ActualHeight;
Expand All @@ -119,12 +111,12 @@ private async Task RaiseRenderedContentUpdated()
await Task.Yield();
RenderedContentUpdated?.Invoke(this, EventArgs.Empty);
}

private void OnHorizontalScrollValueChanged()
{
UpdateTranslation();
}

private void OnVerticalScrollValueChanged()
{
UpdateTranslation();
Expand All @@ -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()
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -376,17 +378,19 @@ public void ResetViewport()

private void ResetOffset()
{
HorizontalScrollValue =0;
HorizontalScrollValue = 0;
VerticalScrollValue = 0;
}

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;
}
}

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
Grid.Column="0">
<ContentPresenter x:Name="PART_Presenter"
Margin="0"
Padding="0"
Padding="{TemplateBinding AdditionalMargin}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
Expand Down

0 comments on commit 185d83e

Please sign in to comment.