You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
7.3 KiB

  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Diagnostics;
  21. using System.Text;
  22. using System.Windows;
  23. using System.Windows.Controls;
  24. using System.Windows.Controls.Primitives;
  25. using System.Windows.Data;
  26. using System.Windows.Documents;
  27. using System.Windows.Input;
  28. using System.Windows.Media;
  29. namespace ICSharpCode.ILSpy.Controls
  30. {
  31. public class ZoomScrollViewer : ScrollViewer
  32. {
  33. static ZoomScrollViewer()
  34. {
  35. DefaultStyleKeyProperty.OverrideMetadata(typeof(ZoomScrollViewer),
  36. new FrameworkPropertyMetadata(typeof(ZoomScrollViewer)));
  37. }
  38. public static readonly DependencyProperty CurrentZoomProperty =
  39. DependencyProperty.Register("CurrentZoom", typeof(double), typeof(ZoomScrollViewer),
  40. new FrameworkPropertyMetadata(1.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, CalculateZoomButtonCollapsed, CoerceZoom));
  41. public double CurrentZoom {
  42. get { return (double)GetValue(CurrentZoomProperty); }
  43. set { SetValue(CurrentZoomProperty, value); }
  44. }
  45. static object CoerceZoom(DependencyObject d, object baseValue)
  46. {
  47. var zoom = (double)baseValue;
  48. ZoomScrollViewer sv = (ZoomScrollViewer)d;
  49. return Math.Max(sv.MinimumZoom, Math.Min(sv.MaximumZoom, zoom));
  50. }
  51. public static readonly DependencyProperty MinimumZoomProperty =
  52. DependencyProperty.Register("MinimumZoom", typeof(double), typeof(ZoomScrollViewer),
  53. new FrameworkPropertyMetadata(0.2));
  54. public double MinimumZoom {
  55. get { return (double)GetValue(MinimumZoomProperty); }
  56. set { SetValue(MinimumZoomProperty, value); }
  57. }
  58. public static readonly DependencyProperty MaximumZoomProperty =
  59. DependencyProperty.Register("MaximumZoom", typeof(double), typeof(ZoomScrollViewer),
  60. new FrameworkPropertyMetadata(5.0));
  61. public double MaximumZoom {
  62. get { return (double)GetValue(MaximumZoomProperty); }
  63. set { SetValue(MaximumZoomProperty, value); }
  64. }
  65. public static readonly DependencyProperty MouseWheelZoomProperty =
  66. DependencyProperty.Register("MouseWheelZoom", typeof(bool), typeof(ZoomScrollViewer),
  67. new FrameworkPropertyMetadata(true));
  68. public bool MouseWheelZoom {
  69. get { return (bool)GetValue(MouseWheelZoomProperty); }
  70. set { SetValue(MouseWheelZoomProperty, value); }
  71. }
  72. public static readonly DependencyProperty AlwaysShowZoomButtonsProperty =
  73. DependencyProperty.Register("AlwaysShowZoomButtons", typeof(bool), typeof(ZoomScrollViewer),
  74. new FrameworkPropertyMetadata(false, CalculateZoomButtonCollapsed));
  75. public bool AlwaysShowZoomButtons {
  76. get { return (bool)GetValue(AlwaysShowZoomButtonsProperty); }
  77. set { SetValue(AlwaysShowZoomButtonsProperty, value); }
  78. }
  79. static readonly DependencyPropertyKey ComputedZoomButtonCollapsedPropertyKey =
  80. DependencyProperty.RegisterReadOnly("ComputedZoomButtonCollapsed", typeof(bool), typeof(ZoomScrollViewer),
  81. new FrameworkPropertyMetadata(true));
  82. public static readonly DependencyProperty ComputedZoomButtonCollapsedProperty = ComputedZoomButtonCollapsedPropertyKey.DependencyProperty;
  83. public bool ComputedZoomButtonCollapsed {
  84. get { return (bool)GetValue(ComputedZoomButtonCollapsedProperty); }
  85. private set { SetValue(ComputedZoomButtonCollapsedPropertyKey, value); }
  86. }
  87. static void CalculateZoomButtonCollapsed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  88. {
  89. ZoomScrollViewer z = d as ZoomScrollViewer;
  90. if (z != null)
  91. z.ComputedZoomButtonCollapsed = (z.AlwaysShowZoomButtons == false) && (z.CurrentZoom == 1.0);
  92. }
  93. protected override void OnPreviewMouseWheel(MouseWheelEventArgs e)
  94. {
  95. if (!e.Handled && Keyboard.Modifiers == ModifierKeys.Control && MouseWheelZoom)
  96. {
  97. double oldZoom = CurrentZoom;
  98. double newZoom = RoundToOneIfClose(CurrentZoom * Math.Pow(1.001, e.Delta));
  99. newZoom = Math.Max(this.MinimumZoom, Math.Min(this.MaximumZoom, newZoom));
  100. // adjust scroll position so that mouse stays over the same virtual coordinate
  101. ContentPresenter presenter = Template.FindName("PART_Presenter", this) as ContentPresenter;
  102. Vector relMousePos;
  103. if (presenter != null)
  104. {
  105. Point mousePos = e.GetPosition(presenter);
  106. relMousePos = new Vector(mousePos.X / presenter.ActualWidth, mousePos.Y / presenter.ActualHeight);
  107. }
  108. else
  109. {
  110. relMousePos = new Vector(0.5, 0.5);
  111. }
  112. Point scrollOffset = new Point(this.HorizontalOffset, this.VerticalOffset);
  113. Vector oldHalfViewport = new Vector(this.ViewportWidth / 2, this.ViewportHeight / 2);
  114. Vector newHalfViewport = oldHalfViewport / newZoom * oldZoom;
  115. Point oldCenter = scrollOffset + oldHalfViewport;
  116. Point virtualMousePos = scrollOffset + new Vector(relMousePos.X * this.ViewportWidth, relMousePos.Y * this.ViewportHeight);
  117. // As newCenter, we want to choose a point between oldCenter and virtualMousePos. The more we zoom in, the closer
  118. // to virtualMousePos. We'll create the line x = oldCenter + lambda * (virtualMousePos-oldCenter).
  119. // On this line, we need to choose lambda between -1 and 1:
  120. // -1 = zoomed out completely
  121. // 0 = zoom unchanged
  122. // +1 = zoomed in completely
  123. // But the zoom factor (newZoom/oldZoom) we have is in the range [0,+Infinity].
  124. // Basically, I just played around until I found a function that maps this to [-1,1] and works well.
  125. // "f" is squared because otherwise the mouse simply stays over virtualMousePos, but I wanted virtualMousePos
  126. // to move towards the middle -> squaring f causes lambda to be closer to 1, giving virtualMousePos more weight
  127. // then oldCenter.
  128. double f = Math.Min(newZoom, oldZoom) / Math.Max(newZoom, oldZoom);
  129. double lambda = 1 - f * f;
  130. if (oldZoom > newZoom)
  131. lambda = -lambda;
  132. Debug.Print("old: " + oldZoom + ", new: " + newZoom);
  133. Point newCenter = oldCenter + lambda * (virtualMousePos - oldCenter);
  134. scrollOffset = newCenter - newHalfViewport;
  135. SetCurrentValue(CurrentZoomProperty, newZoom);
  136. this.ScrollToHorizontalOffset(scrollOffset.X);
  137. this.ScrollToVerticalOffset(scrollOffset.Y);
  138. e.Handled = true;
  139. }
  140. base.OnPreviewMouseWheel(e);
  141. }
  142. internal static double RoundToOneIfClose(double val)
  143. {
  144. if (Math.Abs(val - 1.0) < 0.001)
  145. return 1.0;
  146. else
  147. return val;
  148. }
  149. }
  150. }