-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
282 lines (236 loc) · 10.8 KB
/
MainWindow.xaml.cs
File metadata and controls
282 lines (236 loc) · 10.8 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using System;
using System.ComponentModel;
using System.Linq;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Threading;
using Ookii.Dialogs.Wpf;
using QuickPlay.Properties;
using TextBox = System.Windows.Controls.TextBox;
namespace QuickPlay {
public partial class MainWindow : INotifyPropertyChanged {
static NotifyIcon _TrayIcon;
static DispatcherTimer _PollTimer;
string _CurrentlyPlayingFileFormat;
int _TrackMinSeparatorLength;
string _TrackSeparator;
int _ArtistMinSeparatorLength;
string _ArtistSeparator;
Song _LastSong;
public MainWindow() {
DataContext = this;
InitializeComponent();
CurrentlyPlayingFile.SelectedPath = $"{ExecutingLocation()}\\SpotifyNowPlaying.txt";
CurrentTrackFile.SelectedPath = $"{ExecutingLocation()}\\SpotifyNowPlayingTrack.txt";
CurrentTrackFileOverflow.SelectedPath = $"{ExecutingLocation()}\\SpotifyNowPlayingTrackOverflow.txt";
CurrentArtistFile.SelectedPath = $"{ExecutingLocation()}\\SpotifyNowPlayingArtist.txt";
CurrentArtistFileOverflow.SelectedPath = $"{ExecutingLocation()}\\SpotifyNowPlayingArtistOverflow.txt";
_CurrentlyPlayingFileFormat = CurrentlyPlayingFormat.Text;
_TrackMinSeparatorLength = (int)(CurrentTrackSeparatorMinLength.Value ?? 40d);
_TrackSeparator = CurrentTrackSeparator.Text;
_ArtistMinSeparatorLength = (int)(CurrentArtistSeparatorMinLength.Value ?? 40d);
_ArtistSeparator = CurrentArtistSeparator.Text;
CheckUseTrackSeparator.IsChecked = Settings.Default.TrackOverflow;
CheckUseArtistSeparator.IsChecked = Settings.Default.ArtistOverflow;
AssignTrayIcon();
}
public void AssignTrayIcon() {
_TrayIcon = new NotifyIcon {
Icon = Properties.Resources.IconOrange,
Visible = false,
BalloonTipTitle = Title, //Windows XP compatibility?
BalloonTipText = "Show the QuickPlay window for configuration.",
BalloonTipIcon = ToolTipIcon.Info, //Windows XP compatibility?
Text = "Show the QuickPlay window for configuration."
};
_TrayIcon.Click += TrayIcon_DoubleClick;
HideTray();
}
void TrayIcon_DoubleClick(object Sender, EventArgs E) => HideTray();
public void HideTray() {
_TrayIcon.Visible = false;
Show();
Restore(this);
}
public void ShowTray() {
_TrayIcon.Visible = true;
Hide();
}
[DllImport("user32.dll")]
static extern int ShowWindow(IntPtr HWnd, uint Msg);
const uint Sw_Restore = 0x09;
public static void Restore(Window Window) {
// ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault
switch (Window.WindowState) {
case WindowState.Minimized:
ShowWindow(new WindowInteropHelper(Window).Handle, Sw_Restore);
break;
}
}
void MetroWindow_StateChanged(object Sender, EventArgs E) {
Debug.WriteLine("=> " + Visibility + " | TI: " + _TrayIcon.Visible);
Debug.WriteLine("Sender: " + Sender + " | " + E);
switch (_TrayIcon.Visible) {
case true:
HideTray();
break;
case false:
ShowTray();
break;
}
//switch (Visibility) {
// case Visibility.Visible: //Window just minimised
// Debug.WriteLine("Now Visible");
// //ShowTray();
// break;
// case Visibility.Hidden: //Window just appeared
// Debug.WriteLine("Now Hidden");
// //HideTray();
// break;
// case Visibility.Collapsed: //?
// Debug.WriteLine("Now Collapsed");
// break;
// default:
// throw new ArgumentOutOfRangeException();
//}
}
#region File Management
public static DirectoryInfo ExecutingLocation() => ExecutingApplication().Directory;
public static FileInfo ExecutingApplication() => new FileInfo(Assembly.GetExecutingAssembly().Location);
public static FileInfo DefaultSaveLocation(string FileName = "SpotifyNowPlaying.txt") => new FileInfo(ExecutingLocation().FullName + "\\" + FileName);
public static FileInfo GetSaveLocation(string DefaultFileName = "SpotifyNowPlaying.txt") {
VistaSaveFileDialog SaveDialog = new VistaSaveFileDialog {
AddExtension = true,
DefaultExt = ".txt",
FileName = DefaultFileName,
Filter = "Text File (*.txt)|*.txt|Any File (*.*)|*.*",
FilterIndex = 0,
InitialDirectory = ExecutingLocation().FullName,
OverwritePrompt = true,
Title = "Pick a save location",
ValidateNames = true
};
switch (SaveDialog.ShowDialog()) {
case true:
return new FileInfo(SaveDialog.FileName);
}
return DefaultSaveLocation(DefaultFileName);
}
#endregion
#region Timer
public void CreatePollTimer() {
if (_PollTimer != null) {
Debug.WriteLine("Timer is already running!", "Warning");
return;
}
_PollTimer = new DispatcherTimer(DispatcherPriority.Background) { Interval = new TimeSpan((long)(Settings.Default.PollRate * 10000d)) };
_PollTimer.Tick += _PollTimer_Tick;
_PollTimer.Start();
}
void _PollTimer_Tick(object Sender, EventArgs E) => WriteSongFiles();
#endregion
#region Spotify Process Finding
/// <summary>
/// Returns the first found Spotify Process containing a title; or null
/// </summary>
/// <returns></returns>
public static Process GetSpotifyProcess() => Process.GetProcessesByName(Settings.Default.SpotifyProcessName).FirstOrDefault(P => !string.IsNullOrEmpty(P.MainWindowTitle));
public static Song? GetCurrentSong() => Song.GetSpotifySong(GetSpotifyProcess());
#endregion
#region Value Bindings
bool _UseTrackSeparator;
public bool UseTrackSeparator {
get => _UseTrackSeparator;
set {
if (_UseTrackSeparator != value) {
Settings.Default.TrackOverflow = value;
Settings.Default.Save();
_UseTrackSeparator = value;
OnPropertyChanged();
}
}
}
bool _UseArtistSeparator;
public bool UseArtistSeparator {
get => _UseArtistSeparator;
set {
if (_UseArtistSeparator != value) {
Settings.Default.ArtistOverflow = value;
Settings.Default.Save();
_UseArtistSeparator = value;
OnPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string PropertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
#endregion
public void WriteSongFiles() {
Song? LatestSong = GetCurrentSong();
if (!LatestSong.HasValue || _LastSong == LatestSong) { return; }
_LastSong = LatestSong.Value;
(string TrackName, string TrackArtist) = LatestSong.Value.Tuple;
string SaveTrackOv = CurrentTrackFileOverflow.SelectedPath;
string SaveTrack = CurrentTrackFile.SelectedPath;
string SaveArtistOv = CurrentArtistFileOverflow.SelectedPath;
string SaveArtist = CurrentArtistFile.SelectedPath;
string Save = CurrentlyPlayingFile.SelectedPath;
if (UseTrackSeparator && _TrackMinSeparatorLength >= 0 && TrackName.Length >= _TrackMinSeparatorLength) {
File.WriteAllText(SaveTrackOv, TrackName + _TrackSeparator);
File.WriteAllText(SaveTrackOv, string.Empty);
} else {
File.WriteAllText(SaveTrackOv, string.Empty);
File.WriteAllText(SaveTrack, TrackName);
}
if (UseArtistSeparator && _ArtistMinSeparatorLength >= 0 && TrackArtist.Length >= _ArtistMinSeparatorLength) {
File.WriteAllText(SaveArtistOv, TrackArtist + _ArtistSeparator);
File.WriteAllText(SaveArtist, string.Empty);
} else {
File.WriteAllText(SaveArtistOv, string.Empty);
File.WriteAllText(SaveArtist, TrackArtist);
}
File.WriteAllText(Save, _CurrentlyPlayingFileFormat?.Replace("%%track%%", TrackName).Replace("%%artist%%", TrackArtist) ?? string.Empty);
Debug.WriteLine("E");
}
#region XAML Value Updaters
void CurrentlyPlayingFormat_TextInput(object Sender, TextCompositionEventArgs E) {
if (Sender != null && Sender is TextBlock Tb) {
_CurrentlyPlayingFileFormat = Tb.Text;
}
}
void CurrentTrackSeparatorMinLength_ValueChanged(object Sender, RoutedPropertyChangedEventArgs<double?> E) {
if (Sender != null) {
_TrackMinSeparatorLength = (E.NewValue.HasValue ? (int)E.NewValue.Value : -1);
}
}
void CurrentArtistSeparatorMinLength_ValueChanged(object Sender, RoutedPropertyChangedEventArgs<double?> E) {
if (Sender != null) {
_ArtistMinSeparatorLength = (E.NewValue.HasValue ? (int)E.NewValue.Value : -1);
}
}
void CurrentTrackSeparator_TextChanged(object Sender, TextChangedEventArgs E) {
if (Sender != null && Sender is TextBox Tb) {
_TrackSeparator = Tb.Text;
}
}
void CurrentArtistSeparator_TextChanged(object Sender, TextChangedEventArgs E) {
if (Sender != null && Sender is TextBox Tb) {
_ArtistSeparator = Tb.Text;
}
}
#endregion
void StartButton_Click(object Sender, RoutedEventArgs E) {
StartGrid.IsEnabled = false;
GetSpotifyProcess();
CreatePollTimer();
}
}
}