unison/Views/MainWindow.xaml.cs

340 lines
12 KiB
C#
Raw Normal View History

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Windows.Interop;
2021-08-27 17:49:57 +00:00
using System.Windows.Input;
2021-08-30 23:53:52 +00:00
using System.Windows.Controls.Primitives;
2022-11-13 14:58:30 +00:00
using System.Diagnostics;
2022-11-14 22:53:36 +00:00
using System.Data;
namespace unison
{
2021-09-01 00:35:37 +00:00
public partial class MainWindow : Window
{
2021-09-01 00:35:37 +00:00
private readonly Settings _settingsWin;
2021-10-01 00:24:18 +00:00
private readonly Radios _radiosWin;
2022-11-13 14:58:30 +00:00
private readonly Shuffle _shuffleWin;
2021-09-01 00:35:37 +00:00
private readonly DispatcherTimer _timer;
private readonly MPDHandler _mpd;
2021-08-30 23:53:52 +00:00
2022-11-14 22:53:36 +00:00
public Settings GetSettings() => _settingsWin;
public MainWindow()
{
InitHwnd();
InitializeComponent();
DefaultState(true);
WindowState = WindowState.Minimized;
2021-09-01 00:35:37 +00:00
_settingsWin = new Settings();
2021-10-01 00:24:18 +00:00
_radiosWin = new Radios();
2022-11-13 14:58:30 +00:00
_shuffleWin = new Shuffle();
2021-09-01 00:35:37 +00:00
_timer = new DispatcherTimer();
_mpd = (MPDHandler)Application.Current.Properties["mpd"];
2021-08-16 00:13:47 +00:00
2021-09-01 00:35:37 +00:00
_timer.Interval = TimeSpan.FromSeconds(0.5);
_timer.Tick += Timer_Tick;
_timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
2021-09-01 00:35:37 +00:00
if (_mpd.GetCurrentSong() == null)
return;
2021-09-01 00:35:37 +00:00
CurrentTime.Text = FormatSeconds(_mpd.GetCurrentTime());
TimeSlider.Value = _mpd.GetCurrentTime() / _mpd.GetCurrentSong().Time * 100;
}
2022-11-14 22:53:36 +00:00
public void UpdateStats()
{
_mpd.QueryStats();
_settingsWin.UpdateStats();
}
public void OnConnectionChanged(object sender, EventArgs e)
{
2021-09-01 00:35:37 +00:00
if (_mpd.IsConnected())
{
2022-11-14 22:53:36 +00:00
UpdateStats();
2022-11-13 14:58:30 +00:00
ConnectionOkIcon.Visibility = Visibility.Visible;
ConnectionFailIcon.Visibility = Visibility.Collapsed;
Snapcast.IsEnabled = true;
if (_radiosWin.IsConnected())
Radio.IsEnabled = true;
2022-11-13 14:58:30 +00:00
_shuffleWin.Initialize();
}
else
{
_timer.Stop();
DefaultState(true);
ConnectionOkIcon.Visibility = Visibility.Collapsed;
ConnectionFailIcon.Visibility = Visibility.Visible;
Snapcast.IsEnabled = false;
Radio.IsEnabled = false;
}
_settingsWin.UpdateConnectionStatus();
Connection.Text = $"{Properties.Settings.Default.mpd_host}:{Properties.Settings.Default.mpd_port}";
}
2022-11-13 14:58:30 +00:00
public async void OnSongChanged(object sender, EventArgs e)
{
2021-09-01 00:35:37 +00:00
if (_mpd.GetCurrentSong() == null)
return;
if (_mpd.GetCurrentSong().HasTitle && _mpd.GetCurrentSong().Title.Length > 0)
2021-09-01 00:35:37 +00:00
SongTitle.Text = _mpd.GetCurrentSong().Title;
else if (_mpd.GetCurrentSong().HasName && _mpd.GetCurrentSong().Name.Length > 0)
2021-09-01 00:35:37 +00:00
SongTitle.Text = _mpd.GetCurrentSong().Name;
2021-10-03 11:54:54 +00:00
else if (_mpd.GetCurrentSong().Path != null)
{
int start = _mpd.GetCurrentSong().Path.LastIndexOf("/") + 1;
int end = _mpd.GetCurrentSong().Path.LastIndexOf(".");
2021-10-03 11:54:54 +00:00
if (start > 0 && end > 0 && end > start)
SongTitle.Text = _mpd.GetCurrentSong().Path.Substring(start, end - start);
}
2021-09-01 00:35:37 +00:00
SongTitle.ToolTip = _mpd.GetCurrentSong().Path;
SongArtist.Text = _mpd.GetCurrentSong().Artist;
SongAlbum.Text = _mpd.GetCurrentSong().Album;
2021-09-01 00:35:37 +00:00
if (_mpd.GetCurrentSong().Date != null)
2022-04-07 11:41:18 +00:00
SongAlbum.Text += $" ({_mpd.GetCurrentSong().Date.Split("-")[0]})";
SongGenre.Text = _mpd.GetCurrentSong().Genre;
SongFormat.Text = _mpd.GetCurrentSong().Path.Substring(_mpd.GetCurrentSong().Path.LastIndexOf(".") + 1);
if (SongGenre.Text.Length == 0 || SongFormat.Text.Length == 0)
SongInfoDash.Text = "";
else
SongInfoDash.Text = " ";
TimeSlider.IsEnabled = true;
2021-09-01 00:35:37 +00:00
if (_mpd.GetCurrentSong().Time == -1)
{
CurrentTime.Text = "";
EndTime.Text = "";
_timer.Stop();
TimeSlider.Value = 50;
TimeSlider.IsEnabled = false;
}
else
{
if (!_timer.IsEnabled)
_timer.Start();
EndTime.Text = FormatSeconds(_mpd.GetCurrentSong().Time);
}
2022-11-13 14:58:30 +00:00
Trace.WriteLine("Song changed called!");
if (_shuffleWin.GetContinuous())
{
_mpd.CanPrevNext = false;
await _shuffleWin.HandleContinuous();
_mpd.CanPrevNext = true;
}
}
public void OnStatusChanged(object sender, EventArgs e)
{
2021-09-01 00:35:37 +00:00
if (_mpd.GetStatus() == null)
return;
2021-09-01 00:35:37 +00:00
if (VolumeSlider.Value != _mpd.GetStatus().Volume)
{
2021-09-01 00:35:37 +00:00
VolumeSlider.Value = _mpd.GetStatus().Volume;
VolumeSlider.ToolTip = _mpd.GetStatus().Volume;
}
UpdateButton(ref BorderRandom, _mpd.GetStatus().Random);
UpdateButton(ref BorderRepeat, _mpd.GetStatus().Repeat);
UpdateButton(ref BorderSingle, _mpd.GetStatus().Single);
UpdateButton(ref BorderConsume, _mpd.GetStatus().Consume);
2021-09-01 00:35:37 +00:00
if (_mpd.IsPlaying())
PlayPause.Text = (string)Application.Current.FindResource("playButton");
2021-08-16 00:13:47 +00:00
else
{
PlayPause.Text = (string)Application.Current.FindResource("pauseButton");
if (_mpd.GetStatus().State == MpcNET.MpdState.Stop)
{
DefaultState();
}
}
}
private void DefaultState(bool LostConnection = false)
{
SongTitle.Text = "";
SongArtist.Text = "";
SongAlbum.Text = "";
SongGenre.Text = "";
SongInfoDash.Text = "";
SongFormat.Text = "";
CurrentTime.Text = "";
EndTime.Text = "";
PlayPause.Text = (string)Application.Current.FindResource("pauseButton");
TimeSlider.Value = 50;
TimeSlider.IsEnabled = false;
2022-04-18 21:37:49 +00:00
NoCover.Visibility = Visibility.Collapsed;
Cover.Visibility = Visibility.Collapsed;
2022-04-18 21:37:49 +00:00
RadioCover.Visibility = Visibility.Collapsed;
if (LostConnection)
{
ConnectionOkIcon.Visibility = Visibility.Collapsed;
ConnectionFailIcon.Visibility = Visibility.Visible;
}
Connection.Text = $"{Properties.Settings.Default.mpd_host}:{Properties.Settings.Default.mpd_port}";
}
public void OnCoverChanged(object sender, EventArgs e)
{
2022-04-18 21:37:49 +00:00
NoCover.Visibility = Visibility.Collapsed;
Cover.Visibility = Visibility.Collapsed;
RadioCover.Visibility = Visibility.Collapsed;
if (_mpd.GetCurrentSong().Time == -1)
RadioCover.Visibility = Visibility.Visible;
else if (_mpd.GetCover() == null)
NoCover.Visibility = Visibility.Visible;
2021-09-01 00:35:37 +00:00
else if (Cover.Source != _mpd.GetCover())
{
2021-09-01 00:35:37 +00:00
Cover.Source = _mpd.GetCover();
Cover.Visibility = Visibility.Visible;
}
}
public void OnSnapcastChanged()
{
SnapcastHandler snapcast = (SnapcastHandler)Application.Current.Properties["snapcast"];
2021-09-01 00:35:37 +00:00
if (snapcast.HasStarted)
SnapcastText.Text = unison.Resources.Resources.StopSnapcast;
else
SnapcastText.Text = unison.Resources.Resources.StartSnapcast;
}
2021-09-01 00:35:37 +00:00
public void UpdateButton(ref Border border, bool b)
{
border.Style = b ? (Style)Resources["SelectedButton"] : (Style)Resources["UnselectedButton"];
}
public string FormatSeconds(int time)
{
TimeSpan timespan = TimeSpan.FromSeconds(time);
return timespan.ToString(@"mm\:ss");
}
public string FormatSeconds(double time)
{
TimeSpan timespan = TimeSpan.FromSeconds(time);
return timespan.ToString(@"mm\:ss");
}
public void Pause_Clicked(object sender, RoutedEventArgs e) => _mpd.PlayPause();
public void Previous_Clicked(object sender, RoutedEventArgs e) => _mpd.Prev();
public void Next_Clicked(object sender, RoutedEventArgs e) => _mpd.Next();
public void Random_Clicked(object sender, RoutedEventArgs e) => _mpd.Random();
public void Repeat_Clicked(object sender, RoutedEventArgs e) => _mpd.Repeat();
public void Single_Clicked(object sender, RoutedEventArgs e) => _mpd.Single();
public void Consume_Clicked(object sender, RoutedEventArgs e) => _mpd.Consume();
public void ChangeVolume(int value) => _mpd.SetVolume(value);
2021-08-08 22:51:06 +00:00
public void Snapcast_Clicked(object sender, RoutedEventArgs e)
{
SnapcastHandler snapcast = (SnapcastHandler)Application.Current.Properties["snapcast"];
2021-09-01 00:35:37 +00:00
snapcast.LaunchOrExit();
}
2021-10-03 11:54:54 +00:00
public void Radios_Clicked(object sender, RoutedEventArgs e)
2021-10-01 00:24:18 +00:00
{
_radiosWin.Show();
_radiosWin.Activate();
if (_radiosWin.WindowState == WindowState.Minimized)
_radiosWin.WindowState = WindowState.Normal;
}
2022-11-13 14:58:30 +00:00
public void Shuffle_Clicked(object sender, RoutedEventArgs e)
{
_shuffleWin.Show();
_shuffleWin.Activate();
if (_shuffleWin.WindowState == WindowState.Minimized)
_shuffleWin.WindowState = WindowState.Normal;
}
public void Settings_Clicked(object sender, RoutedEventArgs e)
{
2021-09-01 00:35:37 +00:00
_settingsWin.Show();
_settingsWin.Activate();
2021-09-01 00:35:37 +00:00
if (_settingsWin.WindowState == WindowState.Minimized)
_settingsWin.WindowState = WindowState.Normal;
}
2021-08-08 22:51:06 +00:00
2021-08-30 23:53:52 +00:00
private void TimeSlider_DragStarted(object sender, DragStartedEventArgs e)
{
2021-09-01 00:35:37 +00:00
_timer.Stop();
2021-08-30 23:53:52 +00:00
}
2021-08-27 17:49:57 +00:00
private void TimeSlider_DragCompleted(object sender, MouseButtonEventArgs e)
{
Slider slider = (Slider)sender;
double SongPercentage = slider.Value;
2021-09-01 00:35:37 +00:00
double SongTime = _mpd.GetCurrentSong().Time;
2021-08-27 17:49:57 +00:00
double SeekTime = SongPercentage / 100 * SongTime;
2021-09-01 00:35:37 +00:00
_mpd.SetTime(SeekTime);
_timer.Start();
2021-08-27 17:49:57 +00:00
}
private void VolumeSlider_DragCompleted(object sender, MouseButtonEventArgs e)
{
Slider slider = (Slider)sender;
2021-09-01 00:35:37 +00:00
_mpd.SetVolume((int)slider.Value);
slider.ToolTip = (int)slider.Value;
2021-08-27 17:49:57 +00:00
}
2022-11-06 14:44:53 +00:00
public void UpdateUpdateStatus(string version)
{
_settingsWin.UpdateUpdateStatus(version);
}
2021-08-08 22:51:06 +00:00
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HotkeyHandler hk = (HotkeyHandler)Application.Current.Properties["hotkeys"];
hk.Activate(this);
2021-08-08 22:51:06 +00:00
}
2021-10-04 18:01:50 +00:00
private void MouseDownClipboard(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
string CopyText = SongTitle.Text + " - " + SongArtist.Text + "\n";
CopyText += SongAlbum.Text + "\n";
CopyText += SongTitle.ToolTip;
Clipboard.SetText(CopyText);
}
}
public void InitHwnd()
2021-08-08 22:51:06 +00:00
{
WindowInteropHelper helper = new(this);
helper.EnsureHandle();
2021-08-08 22:51:06 +00:00
}
2021-09-01 00:35:37 +00:00
private void Window_Closing(object sender, CancelEventArgs e)
{
2021-09-01 00:35:37 +00:00
e.Cancel = true;
WindowState = WindowState.Minimized;
Hide();
}
}
}