-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
99 lines (86 loc) · 3.2 KB
/
Main.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
using SuchByte.MacroDeck;
using SuchByte.MacroDeck.Plugins;
using dichternebel.YaSB.MacroDeckPlug;
using SuchByte.MacroDeck.GUI.CustomControls;
namespace dichternebel.YaSB
{
public class Main : MacroDeckPlugin
{
public static Main Instance { get; private set; }
public static Model Model { get; private set; }
public ContentSelectorButton Button { get; private set; }
public Main()
{
Instance ??= this;
Button = new ContentSelectorButton
{
BackgroundImageLayout = ImageLayout.Stretch,
BackgroundImage = Properties.Resources.streamerbot_logo_white
};
Model = new Model();
Model.PropertyChanged += Model_PropertyChanged;
}
private void Model_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(Model.IsConnectedToStreamerBot))
{
Button.BackgroundImage = Model.IsConnectedToStreamerBot
? Properties.Resources.streamerbot_logo_white_checked
: Properties.Resources.streamerbot_logo_white_error;
}
}
public override bool CanConfigure => true;
public override void Enable()
{
Actions = new()
{
new YaSBAction()
};
AppDomain.CurrentDomain.ProcessExit += Application_ApplicationExit;
MacroDeck.OnMainWindowLoad += MacroDeck_OnMainWindowLoad;
Model.InstantiateWebSocketClient();
Model.WebSocketClient.StartConnectionAsync().Wait();
}
private void DisplayButton()
{
if (MacroDeck.MainWindow == null) return;
// When closing the main window Button is disposed
if (Button == null || Button.IsDisposed)
{
Button = new ContentSelectorButton
{
BackgroundImageLayout = ImageLayout.Stretch,
BackgroundImage = Model.IsConnectedToStreamerBot
? Properties.Resources.streamerbot_logo_white_checked
: Properties.Resources.streamerbot_logo_white
};
}
if (!MacroDeck.MainWindow.contentButtonPanel.Controls.Contains(Button))
{
MacroDeck.MainWindow.contentButtonPanel.Controls.Add(Button);
Button.Click += ModelButton_Click;
}
}
private void MacroDeck_OnMainWindowLoad(object? sender, EventArgs e)
{
DisplayButton();
}
private void ModelButton_Click(object? sender, EventArgs e)
{
OpenConfigurator();
}
private void Application_ApplicationExit(object? sender, EventArgs e)
{
Model.WebSocketClient.Dispose();
if (Model.IsDeleteVariablesOnExit) Model.DeleteVariables();
}
public override void OpenConfigurator()
{
DisplayButton();
using (var dialogForm = new ConfigurationDialogForm(Model))
{
dialogForm.ShowDialog();
}
}
}
}