-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationNavigationExtensions.cs
More file actions
59 lines (48 loc) · 2.61 KB
/
Copy pathApplicationNavigationExtensions.cs
File metadata and controls
59 lines (48 loc) · 2.61 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
using Terminal.Gui.App;
using Terminal.Gui.ViewBase;
using Terminal.Gui.Views;
using TerminalGui.Extensions.Extensions.ViewExtensions;
namespace TerminalGui.Extensions.Extensions;
public static class ApplicationNavigationExtensions
{
extension(ApplicationNavigation navigation)
{
public ApplicationNavigation OnFocusChanged(Action<EventArgs> callback)
{
navigation.FocusedChanged += (_, args) => callback(args);
return navigation;
}
#region Navigation
/// <inheritdoc cref="NavigatesTo{TRunnableTo}(ApplicationNavigation, View, TRunnableTo, bool)"
/// path="/param[@name='targetView']" />
/// <param name="runnableToFactory">An factory returning the runnable instance to navigate to.</param>
public View NavigatesTo<TRunnableTo>(View targetView, Func<TRunnableTo> runnableToFactory, bool closeCurrent = false)
where TRunnableTo : Runnable => navigation.SetupNavigation(
targetView,
closeCurrent,
() => navigation.App?.Run(runnableToFactory())); // Invoke factory each time
/// <param name="targetView">The runnable instance whose Accepting event will trigger navigation to the new runnable.</param>
/// <param name="runnableTo">The runnable instance to navigate to.</param>
public View NavigatesTo<TRunnableTo>(View targetView, TRunnableTo runnableTo, bool closeCurrent = false)
where TRunnableTo : Runnable => navigation.SetupNavigation(targetView, closeCurrent, () => navigation.App?.Run(runnableTo));
/// <inheritdoc cref="NavigatesTo{TRunnableTo}(ApplicationNavigation, View, TRunnableTo, bool)"
/// path="/param[@name='targetView']" />
public View NavigatesTo<TRunnableTo>(View targetView, bool closeCurrent = false)
where TRunnableTo : Runnable, new() => navigation.SetupNavigation(targetView, closeCurrent, () => navigation.App?.Run<TRunnableTo>());
/// <inheritdoc cref="NavigatesTo{TRunnableTo}(ApplicationNavigation, View, TRunnableTo, bool)"
/// path="/param[@name='targetView']" />
internal View SetupNavigation(View targetView, bool closeCurrent, Action runAction)
{
ArgumentNullException.ThrowIfNull(navigation.App, nameof(navigation.App));
targetView.OnActivating(_ => {
if (closeCurrent)
{
navigation.App.RequestStop();
}
runAction();
});
return targetView;
}
#endregion
}
}