-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationExtensions.cs
More file actions
133 lines (114 loc) · 5.51 KB
/
Copy pathApplicationExtensions.cs
File metadata and controls
133 lines (114 loc) · 5.51 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
using System.Collections.Concurrent;
using System.Drawing;
using Terminal.Gui.App;
namespace TerminalGui.Extensions.Extensions;
/// <summary>
/// Extension methods for <see cref="IApplication" />.
/// </summary>
public static class ApplicationExtensions
{
extension(IApplication application)
{
#region Session Stack
/// <summary>
/// The number of active sessions on the <see cref="IApplication.SessionStack" />.
/// </summary>
public int SessionCount => application.SessionStack?.Count ?? 0;
/// <summary>
/// Whether the <see cref="IApplication.SessionStack" /> has any active sessions.
/// </summary>
public bool HasActiveSessions => application.SessionStack is { IsEmpty: false };
/// <summary>
/// Peeks at the top of the <see cref="IApplication.SessionStack" /> without removing it.
/// </summary>
/// <returns>The current <see cref="SessionToken" />, or <see langword="null" /> if the stack is empty.</returns>
public SessionToken? GetCurrentSession()
{
if (application.SessionStack is { } stack && stack.TryPeek(out SessionToken? token))
{
return token;
}
return null;
}
/// <summary>
/// Finds a <see cref="SessionToken" /> on the <see cref="IApplication.SessionStack" /> whose
/// <see cref="SessionToken.Runnable" /> matches the specified <paramref name="runnable" />.
/// </summary>
/// <param name="runnable">The <see cref="IRunnable" /> to search for.</param>
/// <returns>The matching <see cref="SessionToken" />, or <see langword="null" /> if not found.</returns>
public SessionToken? FindSession(IRunnable runnable)
{
ArgumentNullException.ThrowIfNull(runnable);
if (application.SessionStack is not { } stack)
{
return null;
}
foreach (SessionToken token in stack)
{
if (ReferenceEquals(token.Runnable, runnable))
{
return token;
}
}
return null;
}
#endregion
#region Events
/// <summary>
/// Subscribes to the <see cref="IApplication.InitializedChanged" /> event,
/// raised after <see cref="IApplication.Init" /> or <see cref="IDisposable.Dispose" /> has been called.
/// </summary>
/// <param name="callback">The callback to invoke with the <see cref="EventArgs{T}" /> containing the initialization state.</param>
/// <returns>The <see cref="IApplication" /> instance.</returns>
public IApplication OnInitializedChanged(Action<EventArgs<bool>> callback)
{
application.InitializedChanged += (_, e) => callback(e);
return application;
}
/// <summary>
/// Subscribes to the <see cref="IApplication.Iteration" /> event,
/// raised on each iteration of the main loop.
/// </summary>
/// <param name="callback">The callback to invoke with the <see cref="EventArgs{T}" /> containing the <see cref="IApplication" />.</param>
/// <returns>The <see cref="IApplication" /> instance.</returns>
public IApplication OnIteration(Action<EventArgs<IApplication>> callback)
{
application.Iteration += (_, e) => callback(e);
return application;
}
/// <summary>
/// Subscribes to the <see cref="IApplication.SessionBegun" /> event,
/// raised when <see cref="IApplication.Begin" /> has created a new <see cref="SessionToken" />.
/// </summary>
/// <param name="callback">The callback to invoke with the <see cref="SessionTokenEventArgs" /> containing the new session token.</param>
/// <returns>The <see cref="IApplication" /> instance.</returns>
public IApplication OnSessionBegun(Action<SessionTokenEventArgs> callback)
{
application.SessionBegun += (_, e) => callback(e);
return application;
}
/// <summary>
/// Subscribes to the <see cref="IApplication.SessionEnded" /> event,
/// raised when <see cref="IApplication.End" /> was called and the session is stopping.
/// </summary>
/// <param name="callback">The callback to invoke with the <see cref="SessionTokenEventArgs" /> containing the ended session token.</param>
/// <returns>The <see cref="IApplication" /> instance.</returns>
public IApplication OnSessionEnded(Action<SessionTokenEventArgs> callback)
{
application.SessionEnded += (_, e) => callback(e);
return application;
}
/// <summary>
/// Subscribes to the <see cref="IApplication.ScreenChanged" /> event,
/// raised when the terminal's size has changed.
/// </summary>
/// <param name="callback">The callback to invoke with the <see cref="EventArgs{T}" /> containing the new screen <see cref="Rectangle" />.</param>
/// <returns>The <see cref="IApplication" /> instance.</returns>
public IApplication OnScreenChanged(Action<EventArgs<Rectangle>> callback)
{
application.ScreenChanged += (_, e) => callback(e);
return application;
}
#endregion
}
}