-
Notifications
You must be signed in to change notification settings - Fork 71
/
TitleBarTabsApplicationContext.cs
72 lines (64 loc) · 2.28 KB
/
TitleBarTabsApplicationContext.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
using System.Collections.Generic;
using System.Windows.Forms;
namespace EasyTabs
{
/// <summary>
/// Application context to use when starting a <see cref="TitleBarTabs" /> application via <see cref="Application.Run(ApplicationContext)" />. Used to
/// track open windows so that the entire application doesn't quit when the first-opened window is closed.
/// </summary>
public class TitleBarTabsApplicationContext : ApplicationContext
{
/// <summary>List of all opened windows.</summary>
protected List<TitleBarTabs> _openWindows = new List<TitleBarTabs>();
/// <summary>List of all opened windows.</summary>
public List<TitleBarTabs> OpenWindows
{
get
{
return _openWindows;
}
}
/// <summary>Constructor; takes the initial window to display and, if it's not closing, opens it and shows it.</summary>
/// <param name="initialFormInstance">Initial window to display.</param>
public void Start(TitleBarTabs initialFormInstance)
{
if (initialFormInstance.IsClosing)
{
ExitThread();
}
else
{
OpenWindow(initialFormInstance);
initialFormInstance.Show();
}
}
/// <summary>
/// Adds <paramref name="window" /> to <see cref="_openWindows" /> and attaches event handlers to its <see cref="Form.FormClosed" /> event to keep track
/// of it.
/// </summary>
/// <param name="window">Window that we're opening.</param>
public void OpenWindow(TitleBarTabs window)
{
if (!_openWindows.Contains(window))
{
window.ApplicationContext = this;
_openWindows.Add(window);
window.FormClosed += window_FormClosed;
}
}
/// <summary>
/// Handler method that's called when an item in <see cref="_openWindows" /> has its <see cref="Form.FormClosed" /> event invoked. Removes the window
/// from <see cref="_openWindows" /> and, if there are no more windows open, calls <see cref="ApplicationContext.ExitThread" />.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with the event.</param>
protected void window_FormClosed(object sender, FormClosedEventArgs e)
{
_openWindows.Remove((TitleBarTabs) sender);
if (_openWindows.Count == 0)
{
ExitThread();
}
}
}
}