-
Notifications
You must be signed in to change notification settings - Fork 5
/
DesktopLyrics.cs
267 lines (243 loc) · 10.1 KB
/
DesktopLyrics.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Timers;
using System.Windows.Forms;
using Newtonsoft.Json;
using Exception = System.Exception;
using Timer = System.Timers.Timer;
namespace MusicBeePlugin
{
[SuppressMessage("ReSharper", "UnusedMember.Global")]
// ReSharper disable once ClassNeverInstantiated.Global
public partial class Plugin
{
private const long UpdateIntervalMs = 100L;
private const string SettingsFileName = "desktopLyrics.json";
private const string SettingsFileName2 = "desktopLyrics_Window.set";
// MB related
private MusicBeeApiInterface _mbApiInterface;
private readonly PluginInfo _about = new PluginInfo();
private string SettingsPath => Path.Combine(_mbApiInterface.Setting_GetPersistentStoragePath(), SettingsFileName);
private string SettingsPath2 => Path.Combine(_mbApiInterface.Setting_GetPersistentStoragePath(), SettingsFileName2);
// Customed
private volatile SettingsObj _settings;
private volatile FrmLyrics _frmLyrics;
private Timer _timer;
private LyricsController _lyricsCtrl;
private readonly object _lock = new object();
public PluginInfo Initialise(IntPtr apiInterfacePtr)
{
var versions = Assembly.GetExecutingAssembly().GetName().Version.ToString().Split('.');
_mbApiInterface = new MusicBeeApiInterface();
_mbApiInterface.Initialise(apiInterfacePtr);
_about.PluginInfoVersion = PluginInfoVersion;
_about.Name = "Desktop Lyrics";
_about.Description = "Display lyrics on your desktop!";
_about.Author = "Charlie Jiang";
_about.TargetApplication = ""; // current only applies to artwork, lyrics or instant messenger name that appears in the provider drop down selector or target Instant Messenger
_about.Type = PluginType.General;
_about.VersionMajor = short.Parse(versions[0]); // your plugin version
_about.VersionMinor = short.Parse(versions[1]);
_about.Revision = short.Parse(versions[2]);
_about.MinInterfaceVersion = MinInterfaceVersion;
_about.MinApiRevision = MinApiRevision;
_about.ReceiveNotifications = ReceiveNotificationFlags.PlayerEvents;
_about.ConfigurationPanelHeight = 0; // height in pixels that musicbee should reserve in a panel for config settings. When set, a handle to an empty panel will be passed to the Configure function
_lyricsCtrl = new LyricsController(_mbApiInterface);
return _about;
}
// ReSharper disable once UnusedParameter.Global
public bool Configure(IntPtr panelHandle)
{
var settingsForm = new FrmSettings(_settings);
settingsForm.ShowDialog();
SaveSettings(_settings);
LyricParser.PreserveSlash = _settings.PreserveSlash;
_lyricsCtrl.NextLineWhenNoTranslation = _settings.NextLineWhenNoTranslation;
_frmLyrics?.Invoke(new Action(() =>
{
_frmLyrics.UpdateFromSettings(_settings);
}));
return true;
}
private void SaveSettings(SettingsObj settings)
{
File.WriteAllText(SettingsPath, JsonConvert.SerializeObject(settings));
}
public void SaveSettings()
{
// Still, we've saved all settings already.
}
// MusicBee is closing the plugin (plugin is being disabled by user or MusicBee is shutting down)
// ReSharper disable once UnusedParameter.Global
public void Close(PluginCloseReason reason)
{
_timer.Stop();
_frmLyrics?.Invoke(new Action(() =>
{
_frmLyrics?.Hide();
_frmLyrics = null;
}));
SaveSettings(_settings);
}
public void Uninstall()
{
if (File.Exists(SettingsPath)) File.Delete(SettingsPath);
if (File.Exists(SettingsPath2)) File.Delete(SettingsPath2);
}
// ReSharper disable once UnusedParameter.Global
public void ReceiveNotification(string sourceFileUrl, NotificationType type)
{
// perform some action depending on the notification type
// ReSharper disable once SwitchStatementMissingSomeCases
switch (type)
{
case NotificationType.PluginStartup:
// while (!Debugger.IsAttached) System.Threading.Thread.Sleep(1);
try
{
try
{
_settings = JsonConvert.DeserializeObject<SettingsObj>(File.ReadAllText(SettingsPath));
}
catch (Exception)
{
_settings = SettingsObj.GenerateDefault();
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
StartupMenuItem();
if (!_settings.HideOnStartup)
StartupForm();
if (_timer != null && _timer.Enabled) _timer.Enabled = false;
_timer = new Timer(UpdateIntervalMs) {AutoReset = false};
_timer.Elapsed += TimerTick;
_timer.Start();
}
catch (Exception e)
{
_mbApiInterface.MB_Trace(e.ToString());
}
break;
case NotificationType.PlayStateChanged:
UpdatePlayState(_mbApiInterface.Player_GetPlayState());
break;
case NotificationType.NowPlayingLyricsReady:
try
{
UpdateLyrics(force: true);
}
catch (Exception e)
{
_mbApiInterface.MB_Trace(e.ToString());
}
break;
case NotificationType.TagsChanged:
try
{
UpdateLyrics(force: true);
}
catch (Exception e)
{
_mbApiInterface.MB_Trace(e.ToString());
}
break;
}
}
// Change form according to play state
private void UpdatePlayState(PlayState state)
{
if (!_settings.AutoHide) return;
if (_frmLyrics == null) return;
switch (state)
{
case PlayState.Stopped:
_frmLyrics.Visible = false;
break;
case PlayState.Playing:
_frmLyrics.Visible = true;
break;
}
}
private void StartupMenuItem()
{
// MenuItem will only be returned before we construct the handler lambda expression, so we have to use a "slot"
// (which will be filled after MB_AddMenuItem returns) to hold the menuItem to be referred in the closure.
var menuItemSlot = new ToolStripMenuItem[] { null };
var menuItem = (ToolStripMenuItem) _mbApiInterface.MB_AddMenuItem(
"mnuView/Desktop Lyrics", "Toggle Desktop Lyrics visibility.",
ToggleLyrics);
menuItemSlot[0] = menuItem;
menuItem.Checked = !_settings.HideOnStartup;
return;
void ToggleLyrics(object sender, EventArgs args)
{
var menuItem2 = menuItemSlot[0];
if (menuItem2 == null) return; // BUG: multithread race condition
menuItem2.Checked = !menuItem2.Checked;
if (!menuItem2.Checked)
_frmLyrics?.Dispose();
else
StartupForm();
_settings.HideOnStartup = !menuItem2.Checked;
SaveSettings(_settings);
}
}
private void StartupForm()
{
_frmLyrics?.Dispose();
var f = (Form)Control.FromHandle(_mbApiInterface.MB_GetWindowHandle());
f.Invoke(new Action(() =>
{
_frmLyrics = new FrmLyrics(_settings);
_frmLyrics.Show();
try
{
UpdateLyrics(force: true);
}
catch (Exception e)
{
_mbApiInterface.MB_Trace(e.ToString());
}
}));
}
private void TimerTick(object sender, ElapsedEventArgs args)
{
Debug.Assert(sender is Timer);
try
{
UpdateLyrics();
}
catch (Exception e)
{
_mbApiInterface.MB_Trace(e.ToString());
}
((Timer) sender).Start();
}
private string _line1, _line2;
private void UpdateLyrics(bool force = false)
{
lock (_lock)
{
if (_frmLyrics == null) return;
var entry = _lyricsCtrl.UpdateLyrics(!_settings.HideWhenUnavailable);
if (entry == null && _line1 != "")
{
_line1 = "";
_frmLyrics.Clear();
return;
}
if (entry == null) return;
if (!force && entry.LyricLine1 == _line1 && entry.LyricLine2 == _line2) return;
_frmLyrics.BeginInvoke(new Action<string, string>((line1, line2) => _frmLyrics.UpdateLyrics(line1, line2)), entry.LyricLine1, entry.LyricLine2);
_line1 = entry.LyricLine1;
_line2 = entry.LyricLine2;
}
}
public string[] GetProviders() { return null; }
}
}