UI is now updated through MPD events and not every second + misc fixes

This commit is contained in:
2021-08-31 01:30:00 +02:00
parent 1b44c64ec5
commit 72d2c5993d
6 changed files with 211 additions and 113 deletions

View File

@ -1,7 +1,8 @@
using Hardcodet.Wpf.TaskbarNotification;
using System;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Threading;
using Hardcodet.Wpf.TaskbarNotification;
namespace unison
{
@ -9,28 +10,39 @@ namespace unison
{
private readonly Process _snapcast = new();
public bool Started { get; private set; }
private string _snapcastPath;
public SnapcastHandler()
{
// wip: this will have to be moved after the mpd connection, later on
_snapcastPath = Properties.Settings.Default.snapcast_path;
if (Properties.Settings.Default.snapcast_startup)
Start();
}
private void UpdateSystray()
public void OnConnectionChanged(object sender, EventArgs e)
{
if (Properties.Settings.Default.snapcast_startup)
{
var mpd = (MPDHandler)Application.Current.Properties["mpd"];
if (mpd._connected)
Start();
}
}
public void UpdateInterface()
{
TaskbarIcon Systray = (TaskbarIcon)Application.Current.Properties["systray"];
SystrayViewModel DataContext = Systray.DataContext as SystrayViewModel;
DataContext.OnPropertyChanged("SnapcastText");
Application.Current.Dispatcher.Invoke(() =>
{
MainWindow MainWin = (MainWindow)Application.Current.MainWindow;
MainWin.OnSnapcastChanged();
}, DispatcherPriority.ContextIdle);
}
public void Start()
{
if (!Started)
{
_snapcast.StartInfo.FileName = _snapcastPath + @"\snapclient.exe";
_snapcast.StartInfo.FileName = Properties.Settings.Default.snapcast_path + @"\snapclient.exe";
_snapcast.StartInfo.Arguments = $"--host {Properties.Settings.Default.mpd_host}";
_snapcast.StartInfo.CreateNoWindow = true;
try
@ -39,19 +51,19 @@ namespace unison
}
catch (Exception err)
{
MessageBox.Show($"[Snapcast error]\nInvalid path: {err.Message}\n\nCurrent path: {_snapcastPath}\nYou can reset it in the settings if needed.",
MessageBox.Show($"[Snapcast error]\nInvalid path: {err.Message}\n\nCurrent path: {Properties.Settings.Default.snapcast_path}\nYou can reset it in the settings if needed.",
"unison", MessageBoxButton.OK, MessageBoxImage.Error);
Trace.WriteLine(err.Message);
return;
}
Started = true;
UpdateSystray();
UpdateInterface();
}
else
{
_snapcast.Kill();
Started = false;
UpdateSystray();
UpdateInterface();
}
}
@ -61,7 +73,7 @@ namespace unison
{
_snapcast.Kill();
Started = false;
UpdateSystray();
UpdateInterface();
}
}
}