-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZoomAndPanControl.cs
777 lines (690 loc) · 34.1 KB
/
ZoomAndPanControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
namespace ZoomAndPan
{
/// <summary>
/// A class that wraps up zooming and panning of it's content.
/// </summary>
public partial class ZoomAndPanControl : ContentControl, IScrollInfo, INotifyPropertyChanged
{
#region Fields
/// <summary>
/// Reference to the underlying content, which is named PART_Content in the template.
/// </summary>
private FrameworkElement _content = null;
/// <summary>
/// The transform that is applied to the content to scale it by 'ViewportZoom'.
/// </summary>
private ScaleTransform _contentZoomTransform = null;
/// <summary>
/// The transform that is applied to the content to offset it by 'ContentOffsetX' and 'ContentOffsetY'.
/// </summary>
private TranslateTransform _contentOffsetTransform = null;
/// <summary>
/// The height of the viewport in content coordinates, clamped to the height of the content.
/// </summary>
private double _constrainedContentViewportHeight = 0.0;
/// <summary>
/// The width of the viewport in content coordinates, clamped to the width of the content.
/// </summary>
private double _constrainedContentViewportWidth = 0.0;
/// <summary>
/// Normally when content offsets changes the content focus is automatically updated.
/// This syncronization is disabled when 'disableContentFocusSync' is set to 'true'.
/// When we are zooming in or out we 'disableContentFocusSync' is set to 'true' because
/// we are zooming in or out relative to the content focus we don't want to update the focus.
/// </summary>
private bool _disableContentFocusSync = false;
/// <summary>
/// Enable the update of the content offset as the content scale changes.
/// This enabled for zooming about a point (google-maps style zooming) and zooming to a rect.
/// </summary>
private bool _enableContentOffsetUpdateFromScale = false;
/// <summary>
/// Used to disable syncronization between IScrollInfo interface and ContentOffsetX/ContentOffsetY.
/// </summary>
private bool _disableScrollOffsetSync = false;
#endregion
#region constructor and overrides
/// <summary>
/// Static constructor to define metadata for the control (and link it to the style in Generic.xaml).
/// </summary>
static ZoomAndPanControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(typeof(ZoomAndPanControl)));
}
/// <summary>
/// Need to update zoom values if size changes, and update ViewportZoom if too low
/// </summary>
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);
if (sizeInfo.NewSize.Width <= 1 || sizeInfo.NewSize.Height <= 1) return;
switch (_currentZoomTypeEnum)
{
case CurrentZoomTypeEnum.Fit:
InternalViewportZoom = ViewportHelpers.FitZoom(sizeInfo.NewSize.Width, sizeInfo.NewSize.Height,
_content?.ActualWidth, _content?.ActualHeight);
break;
case CurrentZoomTypeEnum.Fill:
InternalViewportZoom = ViewportHelpers.FillZoom(sizeInfo.NewSize.Width, sizeInfo.NewSize.Height,
_content?.ActualWidth, _content?.ActualHeight);
break;
}
if (InternalViewportZoom < MinimumZoomClamped) InternalViewportZoom = MinimumZoomClamped;
//
// INotifyPropertyChanged property update
//
OnPropertyChanged(nameof(MinimumZoomClamped));
OnPropertyChanged(nameof(FillZoomValue));
OnPropertyChanged(nameof(FitZoomValue));
}
/// <summary>
/// Called when a template has been applied to the control.
/// </summary>
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_content = this.Template.FindName("PART_Content", this) as FrameworkElement;
if (_content != null)
{
//
// Setup the transform on the content so that we can scale it by 'ViewportZoom'.
//
this._contentZoomTransform = new ScaleTransform(this.InternalViewportZoom, this.InternalViewportZoom);
//
// Setup the transform on the content so that we can translate it by 'ContentOffsetX' and 'ContentOffsetY'.
//
this._contentOffsetTransform = new TranslateTransform();
UpdateTranslationX();
UpdateTranslationY();
//
// Setup a transform group to contain the translation and scale transforms, and then
// assign this to the content's 'RenderTransform'.
//
var transformGroup = new TransformGroup();
transformGroup.Children.Add(this._contentOffsetTransform);
transformGroup.Children.Add(this._contentZoomTransform);
_content.RenderTransform = transformGroup;
ZoomAndPanControl_EventHandlers_OnApplyTemplate();
}
}
/// <summary>
/// Measure the control and it's children.
/// </summary>
protected override Size MeasureOverride(Size constraint)
{
var infiniteSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
var childSize = base.MeasureOverride(infiniteSize);
if (childSize != _unScaledExtent)
{
//
// Use the size of the child as the un-scaled extent content.
//
_unScaledExtent = childSize;
ScrollOwner?.InvalidateScrollInfo();
}
//
// Update the size of the viewport onto the content based on the passed in 'constraint'.
//
UpdateViewportSize(constraint);
var width = constraint.Width;
var height = constraint.Height;
if (double.IsInfinity(width)) width = childSize.Width;
if (double.IsInfinity(height)) height = childSize.Height;
UpdateTranslationX();
UpdateTranslationY();
return new Size(width, height);
}
/// <summary>
/// Arrange the control and it's children.
/// </summary>
protected override Size ArrangeOverride(Size arrangeBounds)
{
var size = base.ArrangeOverride(this.DesiredSize);
if (_content.DesiredSize != _unScaledExtent)
{
//
// Use the size of the child as the un-scaled extent content.
//
_unScaledExtent = _content.DesiredSize;
ScrollOwner?.InvalidateScrollInfo();
}
//
// Update the size of the viewport onto the content based on the passed in 'arrangeBounds'.
//
UpdateViewportSize(arrangeBounds);
return size;
}
#endregion
#region IScrollInfo Data Members
//
// These data members are for the implementation of the IScrollInfo interface.
// This interface works with the ScrollViewer such that when ZoomAndPanControl is
// wrapped (in XAML) with a ScrollViewer the IScrollInfo interface allows the ZoomAndPanControl to
// handle the the scrollbar offsets.
//
// The IScrollInfo properties and member functions are implemented in ZoomAndPanControl_IScrollInfo.cs.
//
// There is a good series of articles showing how to implement IScrollInfo starting here:
// http://blogs.msdn.com/bencon/archive/2006/01/05/509991.aspx
//
/// <summary>
/// Records the unscaled extent of the content.
/// This is calculated during the measure and arrange.
/// </summary>
private Size _unScaledExtent = new Size(0, 0);
/// <summary>
/// Records the size of the viewport (in viewport coordinates) onto the content.
/// This is calculated during the measure and arrange.
/// </summary>
private Size _viewport = new Size(0, 0);
#endregion IScrollInfo Data Members
#region Dependency Property Definitions
//
// Definitions for dependency properties.
//
/// <summary>
/// This allows the same property name be used for direct and indirect access to this control.
/// </summary>
public ZoomAndPanControl ZoomAndPanContent => this;
/// <summary>
/// The duration of the animations (in seconds) started by calling AnimatedZoomTo and the other animation methods.
/// </summary>
public double AnimationDuration
{
get { return (double)GetValue(AnimationDurationProperty); }
set { SetValue(AnimationDurationProperty, value); }
}
public static readonly DependencyProperty AnimationDurationProperty = DependencyProperty.Register("AnimationDuration",
typeof(double), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(0.4));
/// <summary>
/// The duration of the animations (in seconds) started by calling AnimatedZoomTo and the other animation methods.
/// </summary>
public ZoomAndPanInitialPositionEnum ZoomAndPanInitialPosition
{
get { return (ZoomAndPanInitialPositionEnum)GetValue(ZoomAndPanInitialPositionProperty); }
set { SetValue(ZoomAndPanInitialPositionProperty, value); }
}
public static readonly DependencyProperty ZoomAndPanInitialPositionProperty = DependencyProperty.Register("ZoomAndPanInitialPosition",
typeof(ZoomAndPanInitialPositionEnum), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(ZoomAndPanInitialPositionEnum.Default, ZoomAndPanInitialPositionChanged));
private static void ZoomAndPanInitialPositionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var zoomAndPanControl = (ZoomAndPanControl)d;
// zoomAndPanControl.SetZoomAndPanInitialPosition();
}
/// <summary>
/// Get/set the X offset (in content coordinates) of the view on the content.
/// </summary>
public double ContentOffsetX
{
get { return (double)GetValue(ContentOffsetXProperty); }
set { SetValue(ContentOffsetXProperty, value); }
}
public static readonly DependencyProperty ContentOffsetXProperty = DependencyProperty.Register("ContentOffsetX",
typeof(double), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(0.0, ContentOffsetX_PropertyChanged, ContentOffsetX_Coerce));
/// <summary>
/// Get/set the Y offset (in content coordinates) of the view on the content.
/// </summary>
public double ContentOffsetY
{
get { return (double)GetValue(ContentOffsetYProperty); }
set { SetValue(ContentOffsetYProperty, value); }
}
public static readonly DependencyProperty ContentOffsetYProperty = DependencyProperty.Register("ContentOffsetY",
typeof(double), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(0.0, ContentOffsetY_PropertyChanged, ContentOffsetY_Coerce));
/// <summary>
/// Get the viewport height, in content coordinates.
/// </summary>
public double ContentViewportHeight
{
get { return (double)GetValue(ContentViewportHeightProperty); }
set { SetValue(ContentViewportHeightProperty, value); }
}
public static readonly DependencyProperty ContentViewportHeightProperty = DependencyProperty.Register("ContentViewportHeight",
typeof(double), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(0.0));
/// <summary>
/// Get the viewport width, in content coordinates.
/// </summary>
public double ContentViewportWidth
{
get { return (double)GetValue(ContentViewportWidthProperty); }
set { SetValue(ContentViewportWidthProperty, value); }
}
public static readonly DependencyProperty ContentViewportWidthProperty = DependencyProperty.Register("ContentViewportWidth",
typeof(double), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(0.0));
/// <summary>
/// The X coordinate of the content focus, this is the point that we are focusing on when zooming.
/// </summary>
public double ContentZoomFocusX
{
get { return (double)GetValue(ContentZoomFocusXProperty); }
set { SetValue(ContentZoomFocusXProperty, value); }
}
public static readonly DependencyProperty ContentZoomFocusXProperty = DependencyProperty.Register("ContentZoomFocusX",
typeof(double), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(0.0));
/// <summary>
/// The Y coordinate of the content focus, this is the point that we are focusing on when zooming.
/// </summary>
public double ContentZoomFocusY
{
get { return (double)GetValue(ContentZoomFocusYProperty); }
set { SetValue(ContentZoomFocusYProperty, value); }
}
public static readonly DependencyProperty ContentZoomFocusYProperty = DependencyProperty.Register("ContentZoomFocusY",
typeof(double), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(0.0));
/// <summary>
/// Set to 'true' to enable the mouse wheel to scroll the zoom and pan control.
/// This is set to 'false' by default.
/// </summary>
public bool IsMouseWheelScrollingEnabled
{
get { return (bool)GetValue(IsMouseWheelScrollingEnabledProperty); }
set { SetValue(IsMouseWheelScrollingEnabledProperty, value); }
}
public static readonly DependencyProperty IsMouseWheelScrollingEnabledProperty = DependencyProperty.Register("IsMouseWheelScrollingEnabled",
typeof(bool), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(false));
/// <summary>
/// Get/set the maximum value for 'ViewportZoom'.
/// </summary>
public double MaximumZoom
{
get { return (double)GetValue(MaximumZoomProperty); }
set { SetValue(MaximumZoomProperty, value); }
}
public static readonly DependencyProperty MaximumZoomProperty = DependencyProperty.Register("MaximumZoom",
typeof(double), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(10.0, MinimumOrMaximumZoom_PropertyChanged));
/// <summary>
/// Get/set the maximum value for 'ViewportZoom'.
/// </summary>
public MinimumZoomTypeEnum MinimumZoomType
{
get { return (MinimumZoomTypeEnum)GetValue(MinimumZoomTypeProperty); }
set { SetValue(MinimumZoomTypeProperty, value); }
}
public static readonly DependencyProperty MinimumZoomTypeProperty = DependencyProperty.Register("MinimumZoomType",
typeof(MinimumZoomTypeEnum), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(MinimumZoomTypeEnum.MinimumZoom));
/// <summary>
/// Get/set the MinimumZoom value for 'ViewportZoom'.
/// </summary>
public double MinimumZoom
{
get { return (double)GetValue(MinimumZoomProperty); }
set { SetValue(MinimumZoomProperty, value); }
}
public static readonly DependencyProperty MinimumZoomProperty = DependencyProperty.Register("MinimumZoom",
typeof(double), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(0.1, MinimumOrMaximumZoom_PropertyChanged));
/// <summary>
/// Get/set the MinimumZoom value for 'ViewportZoom'.
/// </summary>
public Point? MousePosition
{
get { return (Point?)GetValue(MousePositionProperty); }
set { SetValue(MousePositionProperty, value); }
}
public static readonly DependencyProperty MousePositionProperty = DependencyProperty.Register("MousePosition",
typeof(Point?), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(null, MinimumOrMaximumZoom_PropertyChanged));
/// <summary>
/// This is used for binding a slider to control the zoom. Cannot use the InternalUseAnimations because of all the
/// assumptions in when the this property is changed. THIS IS NOT USED FOR THE ANIMATIONS
/// </summary>
public bool UseAnimations
{
get { return (bool)GetValue(UseAnimationsProperty); }
set { SetValue(UseAnimationsProperty, value); }
}
public static readonly DependencyProperty UseAnimationsProperty = DependencyProperty.Register("UseAnimations",
typeof(bool), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(true));
/// <summary>
/// This is used for binding a slider to control the zoom. Cannot use the InternalViewportZoom because of all the
/// assumptions in when the this property is changed. THIS IS NOT USED FOR THE ANIMATIONS
/// </summary>
public double ViewportZoom
{
get { return (double)GetValue(ViewportZoomProperty); }
set { SetValue(ViewportZoomProperty, value); }
}
public static readonly DependencyProperty ViewportZoomProperty = DependencyProperty.Register("ViewportZoom",
typeof(double), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(1.0, ViewportZoom_PropertyChanged));
/// <summary>
/// The X coordinate of the viewport focus, this is the point in the viewport (in viewport coordinates)
/// that the content focus point is locked to while zooming in.
/// </summary>
public double ViewportZoomFocusX
{
get { return (double)GetValue(ViewportZoomFocusXProperty); }
set { SetValue(ViewportZoomFocusXProperty, value); }
}
public static readonly DependencyProperty ViewportZoomFocusXProperty = DependencyProperty.Register("ViewportZoomFocusX",
typeof(double), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(0.0));
/// <summary>
/// The Y coordinate of the viewport focus, this is the point in the viewport (in viewport coordinates)
/// that the content focus point is locked to while zooming in.
/// </summary>
public double ViewportZoomFocusY
{
get { return (double)GetValue(ViewportZoomFocusYProperty); }
set { SetValue(ViewportZoomFocusYProperty, value); }
}
public static readonly DependencyProperty ViewportZoomFocusYProperty = DependencyProperty.Register("ViewportZoomFocusY",
typeof(double), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(0.0));
#endregion Dependency Property Definitions
#region events
/// <summary>
/// Event raised when the ContentOffsetX property has changed.
/// </summary>
public event EventHandler ContentOffsetXChanged;
/// <summary>
/// Event raised when the ContentOffsetY property has changed.
/// </summary>
public event EventHandler ContentOffsetYChanged;
/// <summary>
/// Event raised when the ViewportZoom property has changed.
/// </summary>
public event EventHandler ContentZoomChanged;
#endregion
#region Event Handlers
/// <summary>
/// This is required for the animations, but has issues if set by something like a slider.
/// </summary>
private double InternalViewportZoom
{
get { return (double)GetValue(InternalViewportZoomProperty); }
set { SetValue(InternalViewportZoomProperty, value); }
}
private static readonly DependencyProperty InternalViewportZoomProperty = DependencyProperty.Register("InternalViewportZoom",
typeof(double), typeof(ZoomAndPanControl), new FrameworkPropertyMetadata(1.0, InternalViewportZoom_PropertyChanged, InternalViewportZoom_Coerce));
/// <summary>
/// Event raised when the 'ViewportZoom' property has changed value.
/// </summary>
private static void InternalViewportZoom_PropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var c = (ZoomAndPanControl)dependencyObject;
if (c._contentZoomTransform != null)
{
//
// Update the content scale transform whenever 'ViewportZoom' changes.
//
c._contentZoomTransform.ScaleX = c.InternalViewportZoom;
c._contentZoomTransform.ScaleY = c.InternalViewportZoom;
}
//
// Update the size of the viewport in content coordinates.
//
c.UpdateContentViewportSize();
if (c._enableContentOffsetUpdateFromScale)
{
try
{
//
// Disable content focus syncronization. We are about to update content offset whilst zooming
// to ensure that the viewport is focused on our desired content focus point. Setting this
// to 'true' stops the automatic update of the content focus when content offset changes.
//
c._disableContentFocusSync = true;
//
// Whilst zooming in or out keep the content offset up-to-date so that the viewport is always
// focused on the content focus point (and also so that the content focus is locked to the
// viewport focus point - this is how the google maps style zooming works).
//
var viewportOffsetX = c.ViewportZoomFocusX - (c.ViewportWidth / 2);
var viewportOffsetY = c.ViewportZoomFocusY - (c.ViewportHeight / 2);
var contentOffsetX = viewportOffsetX / c.InternalViewportZoom;
var contentOffsetY = viewportOffsetY / c.InternalViewportZoom;
c.ContentOffsetX = (c.ContentZoomFocusX - (c.ContentViewportWidth / 2)) - contentOffsetX;
c.ContentOffsetY = (c.ContentZoomFocusY - (c.ContentViewportHeight / 2)) - contentOffsetY;
}
finally
{
c._disableContentFocusSync = false;
}
}
c.ContentZoomChanged?.Invoke(c, EventArgs.Empty);
c.ViewportZoom = c.InternalViewportZoom;
c.OnPropertyChanged(new DependencyPropertyChangedEventArgs(ViewportZoomProperty, c.ViewportZoom, c.InternalViewportZoom));
c.ScrollOwner?.InvalidateScrollInfo();
c.SetCurrentZoomTypeEnum();
c.RaiseCanExecuteChanged();
}
/// <summary>
/// Method called to clamp the 'ViewportZoom' value to its valid range.
/// </summary>
private static object InternalViewportZoom_Coerce(DependencyObject dependencyObject, object baseValue)
{
var c = (ZoomAndPanControl)dependencyObject;
var value = Math.Max((double)baseValue, c.MinimumZoomClamped);
switch (c.MinimumZoomType)
{
case MinimumZoomTypeEnum.FitScreen:
value = Math.Min(Math.Max(value, c.FitZoomValue), c.MaximumZoom);
break;
case MinimumZoomTypeEnum.FillScreen:
value = Math.Min(Math.Max(value, c.FillZoomValue), c.MaximumZoom);
break;
case MinimumZoomTypeEnum.MinimumZoom:
value = Math.Min(Math.Max(value, c.MinimumZoom), c.MaximumZoom);
break;
default:
throw new ArgumentOutOfRangeException();
}
return value;
}
#endregion
#region DependencyProperty Event Handlers
/// <summary>
/// Event raised 'MinimumZoom' or 'MaximumZoom' has changed.
/// </summary>
private static void MinimumOrMaximumZoom_PropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var c = (ZoomAndPanControl)o;
c.InternalViewportZoom = Math.Min(Math.Max(c.InternalViewportZoom, c.MinimumZoomClamped), c.MaximumZoom);
}
/// <summary>
/// Event raised when the 'ContentOffsetX' property has changed value.
/// </summary>
private static void ContentOffsetX_PropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var c = (ZoomAndPanControl)o;
c.UpdateTranslationX();
if (!c._disableContentFocusSync)
//
// Normally want to automatically update content focus when content offset changes.
// Although this is disabled using 'disableContentFocusSync' when content offset changes due to in-progress zooming.
//
c.UpdateContentZoomFocusX();
//
// Raise an event to let users of the control know that the content offset has changed.
//
c.ContentOffsetXChanged?.Invoke(c, EventArgs.Empty);
if (!c._disableScrollOffsetSync)
//
// Notify the owning ScrollViewer that the scrollbar offsets should be updated.
//
c.ScrollOwner?.InvalidateScrollInfo();
}
/// <summary>
/// Method called to clamp the 'ContentOffsetX' value to its valid range.
/// </summary>
private static object ContentOffsetX_Coerce(DependencyObject d, object baseValue)
{
var c = (ZoomAndPanControl)d;
var value = (double)baseValue;
var minOffsetX = 0.0;
var maxOffsetX = Math.Max(0.0, c._unScaledExtent.Width - c._constrainedContentViewportWidth);
value = Math.Min(Math.Max(value, minOffsetX), maxOffsetX);
return value;
}
/// <summary>
/// Event raised when the 'ContentOffsetY' property has changed value.
/// </summary>
private static void ContentOffsetY_PropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var c = (ZoomAndPanControl)o;
c.UpdateTranslationY();
if (!c._disableContentFocusSync)
//
// Normally want to automatically update content focus when content offset changes.
// Although this is disabled using 'disableContentFocusSync' when content offset changes due to in-progress zooming.
//
c.UpdateContentZoomFocusY();
if (!c._disableScrollOffsetSync)
//
// Notify the owning ScrollViewer that the scrollbar offsets should be updated.
//
c.ScrollOwner?.InvalidateScrollInfo();
//
// Raise an event to let users of the control know that the content offset has changed.
//
c.ContentOffsetYChanged?.Invoke(c, EventArgs.Empty);
}
/// <summary>
/// Method called to clamp the 'ContentOffsetY' value to its valid range.
/// </summary>
private static object ContentOffsetY_Coerce(DependencyObject d, object baseValue)
{
var c = (ZoomAndPanControl)d;
var value = (double)baseValue;
var minOffsetY = 0.0;
var maxOffsetY = Math.Max(0.0, c._unScaledExtent.Height - c._constrainedContentViewportHeight);
value = Math.Min(Math.Max(value, minOffsetY), maxOffsetY);
return value;
}
private static void ViewportZoom_PropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var c = (ZoomAndPanControl)dependencyObject;
var newZoom = (double)e.NewValue;
if (c.InternalViewportZoom != newZoom)
{
var centerPoint = new Point(c.ContentOffsetX + (c._constrainedContentViewportWidth / 2), c.ContentOffsetY + (c._constrainedContentViewportHeight / 2));
c.ZoomAboutPoint(newZoom, centerPoint);
}
}
#endregion
/// <summary>
/// Reset the viewport zoom focus to the center of the viewport.
/// </summary>
private void ResetViewportZoomFocus()
{
ViewportZoomFocusX = ViewportWidth / 2;
ViewportZoomFocusY = ViewportHeight / 2;
}
/// <summary>
/// Update the viewport size from the specified size.
/// </summary>
private void UpdateViewportSize(Size newSize)
{
if (_viewport == newSize)
return;
_viewport = newSize;
//
// Update the viewport size in content coordiates.
//
UpdateContentViewportSize();
//
// Initialise the content zoom focus point.
//
UpdateContentZoomFocusX();
UpdateContentZoomFocusY();
//
// Reset the viewport zoom focus to the center of the viewport.
//
ResetViewportZoomFocus();
//
// Update content offset from itself when the size of the viewport changes.
// This ensures that the content offset remains properly clamped to its valid range.
//
this.ContentOffsetX = this.ContentOffsetX;
this.ContentOffsetY = this.ContentOffsetY;
//
// Tell that owning ScrollViewer that scrollbar data has changed.
//
ScrollOwner?.InvalidateScrollInfo();
}
/// <summary>
/// Update the size of the viewport in content coordinates after the viewport size or 'ViewportZoom' has changed.
/// </summary>
private void UpdateContentViewportSize()
{
ContentViewportWidth = ViewportWidth / InternalViewportZoom;
ContentViewportHeight = ViewportHeight / InternalViewportZoom;
_constrainedContentViewportWidth = Math.Min(ContentViewportWidth, _unScaledExtent.Width);
_constrainedContentViewportHeight = Math.Min(ContentViewportHeight, _unScaledExtent.Height);
UpdateTranslationX();
UpdateTranslationY();
}
/// <summary>
/// Update the X coordinate of the translation transformation.
/// </summary>
private void UpdateTranslationX()
{
if (this._contentOffsetTransform != null)
{
var scaledContentWidth = this._unScaledExtent.Width * this.InternalViewportZoom;
if (scaledContentWidth < this.ViewportWidth)
//
// When the content can fit entirely within the viewport, center it.
//
this._contentOffsetTransform.X = (this.ContentViewportWidth - this._unScaledExtent.Width) / 2;
else
this._contentOffsetTransform.X = -this.ContentOffsetX;
}
}
/// <summary>
/// Update the Y coordinate of the translation transformation.
/// </summary>
private void UpdateTranslationY()
{
if (this._contentOffsetTransform != null)
{
var scaledContentHeight = this._unScaledExtent.Height * this.InternalViewportZoom;
if (scaledContentHeight < this.ViewportHeight)
//
// When the content can fit entirely within the viewport, center it.
//
this._contentOffsetTransform.Y = (this.ContentViewportHeight - this._unScaledExtent.Height) / 2;
else
this._contentOffsetTransform.Y = -this.ContentOffsetY;
}
}
/// <summary>
/// Update the X coordinate of the zoom focus point in content coordinates.
/// </summary>
private void UpdateContentZoomFocusX()
{
ContentZoomFocusX = ContentOffsetX + (_constrainedContentViewportWidth / 2);
}
/// <summary>
/// Update the Y coordinate of the zoom focus point in content coordinates.
/// </summary>
private void UpdateContentZoomFocusY()
{
ContentZoomFocusY = ContentOffsetY + (_constrainedContentViewportHeight / 2);
}
public double FitZoomValue => ViewportHelpers.FitZoom(ActualWidth, ActualHeight, _content?.ActualWidth, _content?.ActualHeight);
public double FillZoomValue => ViewportHelpers.FillZoom(ActualWidth, ActualHeight, _content?.ActualWidth, _content?.ActualHeight);
public double MinimumZoomClamped => ((MinimumZoomType == MinimumZoomTypeEnum.FillScreen) ? FillZoomValue
: (MinimumZoomType == MinimumZoomTypeEnum.FitScreen) ? FitZoomValue
: MinimumZoom).ToRealNumber();
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private enum CurrentZoomTypeEnum { Fill, Fit, Other}
private CurrentZoomTypeEnum _currentZoomTypeEnum;
private void SetCurrentZoomTypeEnum()
{
if (ViewportZoom.IsWithinOnePercent(FitZoomValue))
_currentZoomTypeEnum = CurrentZoomTypeEnum.Fit;
else if (ViewportZoom.IsWithinOnePercent(FillZoomValue))
_currentZoomTypeEnum = CurrentZoomTypeEnum.Fill;
else
_currentZoomTypeEnum = CurrentZoomTypeEnum.Other;
}
}
}