Files

121 lines
3.9 KiB
C#
Raw Permalink Normal View History

2022-03-21 01:02:53 +01:00
using System.Collections.ObjectModel;
2022-03-23 01:10:15 +01:00
using System.Diagnostics;
2022-03-21 01:02:53 +01:00
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media;
2022-03-23 01:10:15 +01:00
using ReactiveUI;
2022-03-21 01:02:53 +01:00
namespace QuestPDF.Previewer
{
2022-03-21 01:33:33 +01:00
class PreviewerControl : Control
2022-03-21 01:02:53 +01:00
{
2022-03-23 01:10:15 +01:00
private InteractiveCanvas InteractiveCanvas { get; set; } = new ();
public static readonly StyledProperty<ObservableCollection<PreviewPage>> PagesProperty =
2022-03-21 01:16:26 +01:00
AvaloniaProperty.Register<PreviewerControl, ObservableCollection<PreviewPage>>(nameof(Pages));
2022-03-21 01:02:53 +01:00
2022-03-21 01:16:26 +01:00
public ObservableCollection<PreviewPage>? Pages
2022-03-21 01:02:53 +01:00
{
get => GetValue(PagesProperty);
set => SetValue(PagesProperty, value);
}
2022-03-23 01:10:15 +01:00
public static readonly StyledProperty<float> CurrentScrollProperty = AvaloniaProperty.Register<PreviewerControl, float>(nameof(CurrentScroll));
public float CurrentScroll
{
get => GetValue(CurrentScrollProperty);
set => SetValue(CurrentScrollProperty, value);
}
public static readonly StyledProperty<float> ScrollViewportSizeProperty = AvaloniaProperty.Register<PreviewerControl, float>(nameof(ScrollViewportSize));
public float ScrollViewportSize
{
get => GetValue(ScrollViewportSizeProperty);
set => SetValue(ScrollViewportSizeProperty, value);
}
2022-03-21 01:02:53 +01:00
public PreviewerControl()
{
2022-03-24 23:09:09 +01:00
PagesProperty.Changed.Subscribe(x =>
2022-03-21 01:02:53 +01:00
{
2022-03-24 23:09:09 +01:00
InteractiveCanvas.Pages = x.NewValue.Value;
2022-03-21 01:02:53 +01:00
InvalidateVisual();
});
2022-03-24 23:09:09 +01:00
CurrentScrollProperty.Changed.Subscribe(x =>
2022-03-23 01:10:15 +01:00
{
2022-03-24 23:09:09 +01:00
InteractiveCanvas.ScrollPercentY = x.NewValue.Value;
2022-03-23 01:10:15 +01:00
InvalidateVisual();
});
2022-03-21 01:02:53 +01:00
ClipToBounds = true;
}
protected override void OnPointerWheelChanged(PointerWheelEventArgs e)
{
base.OnPointerWheelChanged(e);
if ((e.KeyModifiers & KeyModifiers.Control) != 0)
{
var scaleFactor = 1 + e.Delta.Y / 10f;
2022-03-24 13:35:02 +01:00
var point = new Point(Bounds.Center.X, Bounds.Top) - e.GetPosition(this);
2022-03-21 01:02:53 +01:00
2022-03-24 13:35:02 +01:00
InteractiveCanvas.ZoomToPoint((float)point.X, -(float)point.Y, (float)scaleFactor);
2022-03-21 01:02:53 +01:00
}
if (e.KeyModifiers == KeyModifiers.None)
{
var translation = (float)e.Delta.Y * 25;
2022-03-24 13:35:02 +01:00
InteractiveCanvas.TranslateWithCurrentScale(0, -translation);
2022-03-21 01:02:53 +01:00
}
InvalidateVisual();
}
private bool IsMousePressed { get; set; }
private Vector MousePosition { get; set; }
protected override void OnPointerMoved(PointerEventArgs e)
{
base.OnPointerMoved(e);
if (IsMousePressed)
{
var currentPosition = e.GetPosition(this);
var translation = currentPosition - MousePosition;
2022-03-24 13:35:02 +01:00
InteractiveCanvas.TranslateWithCurrentScale((float)translation.X, -(float)translation.Y);
2022-03-21 01:02:53 +01:00
InvalidateVisual();
}
MousePosition = e.GetPosition(this);
}
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
IsMousePressed = true;
}
protected override void OnPointerReleased(PointerReleasedEventArgs e)
{
base.OnPointerReleased(e);
IsMousePressed = false;
}
public override void Render(DrawingContext context)
{
2022-03-23 01:10:15 +01:00
CurrentScroll = InteractiveCanvas.ScrollPercentY;
ScrollViewportSize = InteractiveCanvas.ScrollViewportSizeY;
2022-03-21 01:02:53 +01:00
InteractiveCanvas.Bounds = new Rect(0, 0, Bounds.Width, Bounds.Height);
context.Custom(InteractiveCanvas);
base.Render(context);
}
}
}