-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMain.cs
More file actions
128 lines (105 loc) · 3.81 KB
/
Main.cs
File metadata and controls
128 lines (105 loc) · 3.81 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
using Develeon64.SpotifyPlugin.Actions;
using Develeon64.SpotifyPlugin.Views;
using SuchByte.MacroDeck;
using SuchByte.MacroDeck.GUI;
using SuchByte.MacroDeck.GUI.CustomControls;
using SuchByte.MacroDeck.Plugins;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Develeon64.SpotifyPlugin.Windows;
using Timer = System.Timers.Timer;
using Develeon64.SpotifyPlugin.Helpers;
using Develeon64.SpotifyPlugin.Managers;
namespace Develeon64.SpotifyPlugin
{
public static class PluginInstance {
public static Main Main { get; set; }
}
public class Main : MacroDeckPlugin {
public override bool CanConfigure => true;
public const uint TimerIntervalMs = 1000 * 2;
private ToolTip _statusToolTip = new ToolTip();
private ContentSelectorButton _statusButton = new ContentSelectorButton();
private MainWindow _mainWindow;
private readonly Timer _timer = new Timer()
{
Interval = TimerIntervalMs,
Enabled = false,
};
public Main () {
PluginInstance.Main ??= this;
_timer.Elapsed += UpdateTimer_Elapsed;
}
public override void Enable () {
PluginLanguageManager.Initialize();
SpotifyHelper.Initialize();
Actions = new List<PluginAction> {
new PauseAction(),
new SkipAction(),
new PreviousAction(),
new LoopAction(),
new ShuffleAction(),
new VolumeAction(),
new PlaylistAction(),
new LibraryActionAction(),
};
MacroDeck.OnMacroDeckLoaded += MacroDeck_OnMacroDeckLoaded;
MacroDeck.OnMainWindowLoad += MacroDeck_OnMainWindowLoad;
SpotifyHelper.ConnectionStateChanged += SpotifyHelper_ConnectionStateChanged;
if (MacroDeck.MainWindow is { IsDisposed: false, IsHandleCreated: true })
MacroDeck_OnMainWindowLoad(MacroDeck.MainWindow, EventArgs.Empty);
SpotifyHelper.Connect(CredentialHelper.GetCredentials()?.AccessToken ?? null);
}
private void MacroDeck_OnMacroDeckLoaded (object sender, EventArgs e)
{
_timer.Enabled = true;
}
private void MacroDeck_OnMainWindowLoad (object sender, EventArgs e) {
_mainWindow = sender as MainWindow;
_statusToolTip = new ToolTip();
_statusButton = new ContentSelectorButton() {
BackgroundImageLayout = ImageLayout.Stretch,
};
_statusButton.Click += StatusButton_Click;
_mainWindow?.contentButtonPanel.Controls.Add(_statusButton);
SpotifyHelper.CheckTokenRefresh();
UpdateStatus();
}
private void SpotifyHelper_ConnectionStateChanged (object sender, EventArgs e) {
UpdateStatus();
}
private void StatusButton_Click (object sender, EventArgs e) {
var spotifyToken = CredentialHelper.GetCredentials()?.AccessToken;
if (spotifyToken == null) {
OpenConfigurator();
return;
}
if (SpotifyHelper.IsConnected)
SpotifyHelper.Disconnect();
else
SpotifyHelper.Connect(spotifyToken);
}
private void UpdateTimer_Elapsed (object sender, EventArgs e)
{
if (!SpotifyHelper.IsConnected) return;
SpotifyHelper.CheckTokenRefresh();
SpotifyHelper.UpdateVars();
UpdateStatus();
}
private void UpdateStatus ()
{
if (_mainWindow == null || _mainWindow.IsDisposed || _statusButton == null ||
_statusButton.IsDisposed) return;
_mainWindow.Invoke(new Action(() => {
_statusButton.BackgroundImage = SpotifyHelper.IsConnected ? Properties.Resources.Spotify_Connected : Properties.Resources.Spotify_Disconnected;
_statusToolTip.SetToolTip(_statusButton, "Spotify " + (SpotifyHelper.IsConnected ? $"Connected ({SpotifyHelper.UserName})" : "Disconnected"));
}));
}
public override void OpenConfigurator ()
{
using var configurator = new PluginConfigurationWindow();
configurator.ShowDialog();
}
}
}