This repository has been archived by the owner on Feb 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
FormSettings.cs
237 lines (197 loc) · 7.56 KB
/
FormSettings.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
using System;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using System.Threading.Tasks;
namespace ListenMoeClient
{
public partial class FormSettings : Form
{
MainForm mainForm;
AudioPlayer audioPlayer;
public FormSettings(MainForm mainForm, AudioPlayer audioPlayer)
{
InitializeComponent();
this.Icon = Properties.Resources.icon;
this.mainForm = mainForm;
this.audioPlayer = audioPlayer;
LoadAndBindCheckboxSetting(cbCloseToTray, "CloseToTray");
LoadAndBindCheckboxSetting(cbEnableVisualiser, "EnableVisualiser");
LoadAndBindCheckboxSetting(cbHideFromAltTab, "HideFromAltTab");
LoadAndBindCheckboxSetting(cbUpdateAutocheck, "UpdateAutocheck");
LoadAndBindCheckboxSetting(cbThumbnailButton, "ThumbnailButton");
LoadAndBindCheckboxSetting(cbTopmost, "TopMost");
LoadAndBindCheckboxSetting(cbVisualiserBars, "VisualiserBars");
LoadAndBindCheckboxSetting(cbVisualiserFadeEdges, "VisualiserFadeEdges");
LoadAndBindColorSetting(panelVisualiserColor, "CustomVisualiserColor");
LoadAndBindColorSetting(panelBaseColor, "CustomBaseColor");
LoadAndBindColorSetting(panelAccentColor, "CustomAccentColor");
numericUpdateInterval.Value = Settings.Get<int>(Setting.UpdateInterval) / 60;
numericUpdateInterval.ValueChanged += NumericUpdateInterval_ValueChanged;
float scale = Settings.Get<float>(Setting.Scale);
tbResolutionScale.Value = (int)(scale * 10);
lblResolutionScale.Text = scale.ToString("N1");
float visualiserOpacity = Settings.Get<float>(Setting.VisualiserOpacity);
tbVisualiserOpacity.Value = (int)(visualiserOpacity * 255);
lblVisualiserOpacity.Text = visualiserOpacity.ToString("N1");
float opacity = Settings.Get<float>(Setting.FormOpacity);
tbOpacity.Value = (int)(opacity * 255);
lblOpacity.Text = opacity.ToString("N1");
panelNotLoggedIn.Visible = !User.LoggedIn;
panelLoggedIn.Visible = User.LoggedIn;
lblLoginStatus.Text = "Logged in as " + Settings.Get<string>(Setting.Username);
lblLoginStatus.Location = new Point((this.Width / 2) - (lblLoginStatus.Width / 2), lblLoginStatus.Location.Y);
StreamType st = Settings.Get<StreamType>(Setting.StreamType);
rbKpop.Checked = st == StreamType.Kpop;
User.OnLoginComplete += () =>
{
lblLoginStatus.Text = "Logged in as " + Settings.Get<string>(Setting.Username);
lblLoginStatus.Location = new Point((this.Width / 2) - (lblLoginStatus.Width / 2), lblLoginStatus.Location.Y);
txtUsername.Clear();
txtPassword.Clear();
panelNotLoggedIn.Visible = false;
panelTwoFactorAuth.Visible = false;
panelLoggedIn.Visible = true;
panelLoggedIn.BringToFront();
};
User.OnLogout += () =>
{
panelLoggedIn.Visible = false;
panelNotLoggedIn.Visible = true;
panelNotLoggedIn.BringToFront();
};
reloadAudioDevices();
}
private void NumericUpdateInterval_ValueChanged(object sender, EventArgs e)
{
Settings.Set(Setting.UpdateInterval, (int)numericUpdateInterval.Value * 60);
Settings.WriteSettings();
}
private void LoadAndBindCheckboxSetting(CheckBox checkbox, string settingsKey)
{
Setting key = (Setting)Enum.Parse(typeof(Setting), settingsKey);
checkbox.Checked = Settings.Get<bool>(key);
checkbox.CheckStateChanged += (sender, e) =>
{
Settings.Set(key, checkbox.Checked);
Settings.WriteSettings();
mainForm.ReloadSettings();
};
}
private void LoadAndBindColorSetting(Panel panel, string settingsKey)
{
Setting key = (Setting)Enum.Parse(typeof(Setting), settingsKey);
panel.BackColor = Settings.Get<Color>(key);
panel.MouseClick += (sender, e) =>
{
ColorDialog dialog = new ColorDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
Color c = dialog.Color;
panel.BackColor = c;
Settings.Set(key, c);
Settings.Set(Setting.CustomColors, true);
Settings.WriteSettings();
mainForm.ReloadSettings();
}
};
}
private void tbResolutionScale_Scroll(object sender, EventArgs e)
{
//Set new scale
float newScale = tbResolutionScale.Value / 10f;
lblResolutionScale.Text = newScale.ToString("N1");
Settings.Set(Setting.Scale, newScale);
Settings.WriteSettings();
//Reload form scaling
mainForm.ReloadScale();
}
private void FormSettings_FormClosing(object sender, FormClosingEventArgs e) =>
//Should probably use a mutex for this, but oh well
mainForm.SettingsForm = null;
private async void btnLogin_Click(object sender, EventArgs e)
{
btnLogin.Enabled = false;
AuthenticateResponse response = await User.Login(txtUsername.Text, txtPassword.Text);
btnLogin.Enabled = true;
if (response.mfa)
{
panelNotLoggedIn.Visible = false;
panelLoggedIn.Visible = false;
panelTwoFactorAuth.Visible = true;
panelTwoFactorAuth.BringToFront();
txtTwoFactorAuthCode.Focus();
}
}
private void btnLogout_Click(object sender, EventArgs e) => User.Logout();
private void tbVisualiserOpacity_Scroll(object sender, EventArgs e)
{
float newVal = tbVisualiserOpacity.Value / 255f;
lblVisualiserOpacity.Text = newVal.ToString("N1");
Settings.Set(Setting.VisualiserOpacity, newVal);
Settings.WriteSettings();
mainForm.ReloadSettings();
}
private void tbOpacity_Scroll(object sender, EventArgs e)
{
float newVal = tbOpacity.Value / 255f;
lblOpacity.Text = newVal.ToString("N1");
Settings.Set(Setting.FormOpacity, newVal);
Settings.WriteSettings();
mainForm.ReloadSettings();
}
private void reloadAudioDevices()
{
dropdownAudioDevices.DataSource = audioPlayer.GetAudioOutputDevices();
dropdownAudioDevices.SelectedIndex = Math.Max(0, Array.IndexOf(audioPlayer.GetAudioOutputDevices().Select(a => a.DeviceInfo.Guid).ToArray(), audioPlayer.CurrentDeviceGuid));
}
private void cbAudioDevices_SelectionChangeCommitted(object sender, EventArgs e)
{
AudioDevice selected = (AudioDevice)dropdownAudioDevices.SelectedItem;
audioPlayer.SetAudioOutputDevice(selected.DeviceInfo.Guid);
}
private void btnRefreshAudioDevices_Click(object sender, EventArgs e) => reloadAudioDevices();
private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true;
btnLogin.PerformClick();
}
}
private async void btnTwoFactorAuthSubmit_Click(object sender, EventArgs e)
{
btnTwoFactorAuthSubmit.Enabled = false;
bool success = await User.LoginMfa(txtTwoFactorAuthCode.Text);
btnTwoFactorAuthSubmit.Enabled = true;
if (!success)
{
lblIncorrectTwoFactorAuth.Visible = true;
await Task.Delay(2000);
lblIncorrectTwoFactorAuth.Visible = false;
}
}
private void txtTwoFactorAuthCode_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true;
btnTwoFactorAuthSubmit.PerformClick();
}
}
private async Task ReloadStreamType()
{
StreamType current = Settings.Get<StreamType>(Setting.StreamType);
StreamType next = rbJpop.Checked ? StreamType.Jpop : StreamType.Kpop;
if (current != next)
{
Settings.Set(Setting.StreamType, next);
Settings.WriteSettings();
mainForm.ReloadSettings();
await mainForm.ReloadStream();
}
}
private async void rbKpop_CheckedChanged(object sender, EventArgs e) => await ReloadStreamType();
private async void rbJpop_CheckedChanged(object sender, EventArgs e) => await ReloadStreamType();
}
}