-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathNavigationTrack.cs
More file actions
236 lines (190 loc) · 7.05 KB
/
NavigationTrack.cs
File metadata and controls
236 lines (190 loc) · 7.05 KB
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
// Copyright (c) The University of Dundee 2018-2019
// This file is part of the Research Data Management Platform (RDMP).
// RDMP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
// RDMP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Linq;
namespace Rdmp.UI;
/// <summary>
/// Handles tracking which where the user navigates to for Forward / Backward purposes.
/// </summary>
public class NavigationTrack<T>
{
private Stack<T> _navigationStack = new();
private Stack<T> _forward = new();
private const int MaxHistory = 10;
private bool _suspended;
/// <summary>
/// Called when changes are detected, includes Clear, Append etc. Does not include <see cref="Prune"/> which is often called as part of internal operations.
/// </summary>
public event EventHandler Changed;
/// <summary>
/// The last T navigated to or null if no T are alive / pushed
/// </summary>
public T Current
{
get
{
while (_navigationStack.Count != 0)
{
var p = _navigationStack.Peek();
if (_isAlive(p))
return p;
_navigationStack.Pop();
}
return default;
}
}
/// <summary>
/// Creates a new Forward/Backward navigation stack supporting pruning.
/// </summary>
/// <param name="aliveDelegate">Delegate for pruning dead items from the stack (e.g. closed forms). Return true if your <typeparamref name="T"/> should survive filter.</param>
/// <param name="activate">Delegate to execute when Forward/Backward happens (e.g. bring to focus).</param>
public NavigationTrack(Func<T, bool> aliveDelegate, Action<T> activate)
{
_isAlive = aliveDelegate;
_activate = activate;
}
/// <summary>
/// Removes all dead objects in the history (forward and backwards). This is based on the alive delegate used to construct the <see cref="NavigationTrack{T}"/>
/// </summary>
public void Prune()
{
_navigationStack = new Stack<T>(_navigationStack.ToArray().Take(MaxHistory + 1).Reverse().Where(_isAlive));
_forward = new Stack<T>(_forward.ToArray().AsEnumerable().Reverse().Where(_isAlive));
}
/// <summary>
/// Calls <see cref="Back(bool)"/> <paramref name="i"/> times. If this results in a valid <see cref="Current"/> then the activate delegate will be triggered
/// </summary>
/// <param name="i"></param>
/// <param name="show"></param>
/// <returns></returns>
public T Back(int i, bool show)
{
T toShow = default;
while (i-- > 0)
toShow = Back(false);
if (toShow != null && show)
Activate(toShow);
return toShow;
}
/// <summary>
/// Activates the given <typeparamref name="T"/> without adding it to the history stack
/// </summary>
/// <param name="toShow"></param>
private void Activate(T toShow)
{
if (toShow == null)
return;
var sBefore = _suspended;
_suspended = true;
try
{
_activate(toShow);
}
finally
{
_suspended = sBefore;
}
}
/// <summary>
/// Returns and optionally Activates the last entry in the history. This changes the location history
/// </summary>
/// <param name="show">True to launch the activation delegate</param>
/// <returns></returns>
public T Back(bool show)
{
Prune();
if (Current == null)
return default;
var pop = _navigationStack.Pop();
var newHead = Current;
if (newHead == null)
{
_navigationStack.Push(pop);
return default;
}
_forward.Push(pop);
if (show)
Activate(newHead);
Changed?.Invoke(this, EventArgs.Empty);
return newHead;
}
/// <summary>
/// Returns and optionally Activates the next object in the history. This changes the location history
///
/// <para>Does nothing if you have not already gone <see cref="Back(bool)"/></para>
/// </summary>
/// <param name="show"></param>
/// <returns></returns>
public T Forward(bool show)
{
Prune();
if (_forward.Count == 0)
return default;
var r = _forward.Pop();
if (r != null && show)
Activate(r);
_navigationStack.Push(r);
Changed?.Invoke(this, EventArgs.Empty);
return r;
}
/// <summary>
/// Returns true if there is a history that can be navigated back to
/// </summary>
/// <returns></returns>
public bool CanBack() => GetHistory(1).Any();
/// <summary>
/// Returns true if the current state is an exploration of the past history and therefore the user can navigate Forwards again
/// </summary>
/// <returns></returns>
public bool CanForward()
{
Prune();
return _forward.Count > 0;
}
/// <summary>
/// Returns x history objects that <see cref="Back(bool)"/> would go to if called. This does not affect the state of the history. Result does not include <see cref="Current"/>
/// </summary>
/// <returns></returns>
public T[] GetHistory(int maxToReturn)
{
Prune();
return _navigationStack.ToArray().Skip(1).Take(maxToReturn).ToArray();
}
private Func<T, bool> _isAlive;
private readonly Action<T> _activate;
/// <summary>
/// Records that the user has made a new navigation to a fresh page. This will invalidate any Forward history
/// </summary>
/// <param name="newHead"></param>
public void Append(T newHead)
{
//don't push the newHead if we are suspended
if (_suspended || newHead == null)
return;
//don't push the newHead if it is already the head
if (_navigationStack.Count != 0 && Equals(_navigationStack.Peek(), newHead))
return;
//don't allow going forward after a novel forward
_forward.Clear();
_navigationStack.Push(newHead);
Changed?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Changes the behaviour of <see cref="Append"/> to do nothing, use this if you want to activate something or load a layout without populating the history / affecting the current history.
/// </summary>
public void Suspend()
{
_suspended = true;
}
/// <summary>
/// Ends the suspended state created by <see cref="Suspend"/>
/// </summary>
public void Resume()
{
_suspended = false;
}
}