PreviewerControl.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System.Collections.ObjectModel;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Input;
  5. using Avalonia.Media;
  6. using Avalonia.Threading;
  7. namespace QuestPDF.Previewer
  8. {
  9. class PreviewerControl : Control
  10. {
  11. private InteractiveCanvas InteractiveCanvas { get; set; } = new ();
  12. public static readonly StyledProperty<ObservableCollection<DocumentSnapshot.PageSnapshot>> PagesProperty =
  13. AvaloniaProperty.Register<PreviewerControl, ObservableCollection<DocumentSnapshot.PageSnapshot>>(nameof(Pages));
  14. public ObservableCollection<DocumentSnapshot.PageSnapshot>? Pages
  15. {
  16. get => GetValue(PagesProperty);
  17. set => SetValue(PagesProperty, value);
  18. }
  19. public static readonly StyledProperty<float> CurrentScrollProperty = AvaloniaProperty.Register<PreviewerControl, float>(nameof(CurrentScroll));
  20. public float CurrentScroll
  21. {
  22. get => GetValue(CurrentScrollProperty);
  23. set => SetValue(CurrentScrollProperty, value);
  24. }
  25. public static readonly StyledProperty<float> ScrollViewportSizeProperty = AvaloniaProperty.Register<PreviewerControl, float>(nameof(ScrollViewportSize));
  26. public float ScrollViewportSize
  27. {
  28. get => GetValue(ScrollViewportSizeProperty);
  29. set => SetValue(ScrollViewportSizeProperty, value);
  30. }
  31. public PreviewerControl()
  32. {
  33. PagesProperty.Changed.Subscribe(x =>
  34. {
  35. InteractiveCanvas.Pages = x.NewValue.Value;
  36. InvalidateVisual();
  37. });
  38. CurrentScrollProperty.Changed.Subscribe(x =>
  39. {
  40. InteractiveCanvas.ScrollPercentY = x.NewValue.Value;
  41. Dispatcher.UIThread.InvokeAsync(InvalidateVisual).GetTask();
  42. });
  43. ClipToBounds = true;
  44. }
  45. protected override void OnPointerWheelChanged(PointerWheelEventArgs e)
  46. {
  47. base.OnPointerWheelChanged(e);
  48. if ((e.KeyModifiers & KeyModifiers.Control) != 0)
  49. {
  50. var scaleFactor = 1 + e.Delta.Y / 10f;
  51. var point = new Point(Bounds.Center.X, Bounds.Top) - e.GetPosition(this);
  52. InteractiveCanvas.ZoomToPoint((float)point.X, -(float)point.Y, (float)scaleFactor);
  53. }
  54. if (e.KeyModifiers == KeyModifiers.None)
  55. {
  56. var translation = (float)e.Delta.Y * 25;
  57. InteractiveCanvas.TranslateWithCurrentScale(0, -translation);
  58. }
  59. InvalidateVisual();
  60. }
  61. private bool IsMousePressed { get; set; }
  62. private Vector MousePosition { get; set; }
  63. protected override void OnPointerMoved(PointerEventArgs e)
  64. {
  65. base.OnPointerMoved(e);
  66. if (IsMousePressed)
  67. {
  68. var currentPosition = e.GetPosition(this);
  69. var translation = currentPosition - MousePosition;
  70. InteractiveCanvas.TranslateWithCurrentScale((float)translation.X, -(float)translation.Y);
  71. InvalidateVisual();
  72. }
  73. MousePosition = e.GetPosition(this);
  74. }
  75. protected override void OnPointerPressed(PointerPressedEventArgs e)
  76. {
  77. base.OnPointerPressed(e);
  78. IsMousePressed = true;
  79. }
  80. protected override void OnPointerReleased(PointerReleasedEventArgs e)
  81. {
  82. base.OnPointerReleased(e);
  83. IsMousePressed = false;
  84. }
  85. public override void Render(DrawingContext context)
  86. {
  87. CurrentScroll = InteractiveCanvas.ScrollPercentY;
  88. ScrollViewportSize = InteractiveCanvas.ScrollViewportSizeY;
  89. InteractiveCanvas.Bounds = new Rect(0, 0, Bounds.Width, Bounds.Height);
  90. context.Custom(InteractiveCanvas);
  91. base.Render(context);
  92. }
  93. }
  94. }