-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZoomAndPanControl_UndoRedo.cs
155 lines (138 loc) · 5.93 KB
/
ZoomAndPanControl_UndoRedo.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace ZoomAndPan
{
public partial class ZoomAndPanControl
{
private readonly Stack<UndoRedoStackItem> _undoStack = new Stack<UndoRedoStackItem>();
private readonly Stack<UndoRedoStackItem> _redoStack = new Stack<UndoRedoStackItem>();
private UndoRedoStackItem _viewportZoomCache;
/// <summary>
/// Record the previous zoom level, so that we can return to it.
/// </summary>
public void SaveZoom()
{
_viewportZoomCache = CreateUndoRedoStackItem();
if (_undoStack.Any() && _viewportZoomCache.Equals(_undoStack.Peek())) return;
_undoStack.Push(_viewportZoomCache);
_redoStack.Clear();
_undoZoomCommand?.RaiseCanExecuteChanged();
_redoZoomCommand?.RaiseCanExecuteChanged();
}
/// <summary>
/// Record the last saved zoom level, so that we can return to it if no activity for 750 milliseconds
/// </summary>
public void DelayedSaveZoom750Miliseconds()
{
if (_timer750Miliseconds?.Running != true) _viewportZoomCache = CreateUndoRedoStackItem();
(_timer750Miliseconds ?? (_timer750Miliseconds = new KeepAliveTimer(TimeSpan.FromMilliseconds(740), () =>
{
if (_undoStack.Any() && _viewportZoomCache.Equals(_undoStack.Peek())) return;
_undoStack.Push(_viewportZoomCache);
_redoStack.Clear();
_undoZoomCommand?.RaiseCanExecuteChanged();
_redoZoomCommand?.RaiseCanExecuteChanged();
}))).Nudge();
}
private KeepAliveTimer _timer750Miliseconds;
/// <summary>
/// Record the last saved zoom level, so that we can return to it if no activity for 1550 milliseconds
/// </summary>
public void DelayedSaveZoom1500Miliseconds()
{
if (!_timer1500Miliseconds?.Running != true) _viewportZoomCache = CreateUndoRedoStackItem();
(_timer1500Miliseconds ?? (_timer1500Miliseconds = new KeepAliveTimer(TimeSpan.FromMilliseconds(1500), () =>
{
if (_undoStack.Any() && _viewportZoomCache.Equals(_undoStack.Peek())) return;
_undoStack.Push(_viewportZoomCache);
_redoStack.Clear();
_undoZoomCommand?.RaiseCanExecuteChanged();
_redoZoomCommand?.RaiseCanExecuteChanged();
}))).Nudge();
}
private KeepAliveTimer _timer1500Miliseconds;
private UndoRedoStackItem CreateUndoRedoStackItem()
{
return new UndoRedoStackItem(this.ContentOffsetX, this.ContentOffsetY,
this.ContentViewportWidth, this.ContentViewportHeight, InternalViewportZoom);
}
/// <summary>
/// Jump back to the previous zoom level, saving current zoom to Redo Stack.
/// </summary>
private void UndoZoom()
{
_viewportZoomCache = CreateUndoRedoStackItem();
if (!_undoStack.Any() || !_viewportZoomCache.Equals(_undoStack.Peek()))
_redoStack.Push(_viewportZoomCache);
_viewportZoomCache = _undoStack.Pop();
this.AnimatedZoomTo(_viewportZoomCache.Zoom, _viewportZoomCache.Rect);
SetScrollViewerFocus();
_undoZoomCommand?.RaiseCanExecuteChanged();
_redoZoomCommand?.RaiseCanExecuteChanged();
}
/// <summary>
/// Jump back to the most recent zoom level saved on redo stack.
/// </summary>
private void RedoZoom()
{
_viewportZoomCache = CreateUndoRedoStackItem();
if (!_redoStack.Any() || !_viewportZoomCache.Equals(_redoStack.Peek()))
_undoStack.Push(_viewportZoomCache);
_viewportZoomCache = _redoStack.Pop();
this.AnimatedZoomTo(_viewportZoomCache.Zoom, _viewportZoomCache.Rect);
SetScrollViewerFocus();
_undoZoomCommand?.RaiseCanExecuteChanged();
_redoZoomCommand?.RaiseCanExecuteChanged();
}
private bool CanUndoZoom => _undoStack.Any();
private bool CanRedoZoom => _redoStack.Any();
/// <summary>
/// Command to implement Undo
/// </summary>
public ICommand UndoZoomCommand => _undoZoomCommand ?? (_undoZoomCommand =
new RelayCommand(UndoZoom, () => CanUndoZoom));
private RelayCommand _undoZoomCommand;
/// <summary>
/// Command to implement Redo
/// </summary>
public ICommand RedoZoomCommand => _redoZoomCommand ?? (_redoZoomCommand =
new RelayCommand(RedoZoom, () => CanRedoZoom));
private RelayCommand _redoZoomCommand;
private class UndoRedoStackItem
{
public UndoRedoStackItem(Rect rect, double zoom)
{
Rect = rect;
Zoom = zoom;
}
public UndoRedoStackItem(double offsetX, double offsetY, double width, double height, double zoom)
{
Rect = new Rect(offsetX, offsetY, width, height);
Zoom = zoom;
}
public Rect Rect { get; }
public double Zoom { get; }
public override string ToString()
{
return $"Rectangle {{{Rect.X},{Rect.X}}}, Zoom {Zoom}";
}
public bool Equals(UndoRedoStackItem obj)
{
return Zoom.IsWithinOnePercent(obj.Zoom) && Rect.Equals(obj.Rect);
}
}
private void SetScrollViewerFocus()
{
var scrollViewer = _content.FindParentControl<ScrollViewer>();
if (scrollViewer != null)
{
Keyboard.Focus(scrollViewer);
scrollViewer.Focus();
}
}
}
}