JD2022-TU1/main/tools/modules/JD.Remoting.WPF.Module/View/RemoteStatusBarItemViewModel.cs

110 lines
3.5 KiB
C#

using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Windows.Controls;
using JD.Interfaces.Remoting;
using JD.Remoting.Services;
using JD.Remoting.WPF.Actions;
using JD.Remoting.WPF.Module.Actions;
using JD.Remoting.WPF.Module.Settings;
using Plume.Interfaces.Services;
using Plume.Interfaces.Settings;
using Plume.Interfaces.WPF.Actions;
using Plume.Interfaces.WPF.Notifications;
using Plume.WPF.Commands;
using Plume.WPF.Mvvm;
namespace JD.Remoting.WPF.View
{
public class RemoteStatusBarItemViewModel : BindableBase
{
private readonly IPluginService _pluginService;
private readonly INotificationManager _notificationService;
private bool _isConnected;
private string _tooltip;
public RemoteStatusBarItemViewModel(IServiceManager serviceManager, INotificationManager notificationManager, IActionManager actionManager, ISettingsManager settingsManager)
{
_notificationService = notificationManager;
_pluginService = serviceManager.GetPluginService();
_pluginService.CollectionChanged += PluginServiceOnCollectionChanged;
Update();
var actionService = actionManager;
ContextMenus.Add(ConnectAction.GetMenu());
ContextMenus.Add(actionService.Get<DisconnectAction>().ToMenuItem());
ContextMenus.Add(new Separator());
ContextMenus.Add(actionManager.Get<ManageConnectionsAction>().ToMenuItem());
foreach (var plugin in _pluginService)
{
plugin.ConnectedChanged += PluginOnConnectedChanged;
}
}
private void PluginServiceOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (var plugin in e.NewItems.Cast<IPlugin>())
{
plugin.ConnectedChanged += PluginOnConnectedChanged;
}
}
if (e.OldItems != null)
{
foreach (var plugin in e.OldItems.Cast<IPlugin>())
{
plugin.ConnectedChanged -= PluginOnConnectedChanged;
}
}
Update();
}
private void PluginOnConnectedChanged(object sender, IPlugin e)
{
Update();
}
private void Update()
{
IsConnected = _pluginService.Any() && _pluginService.All(p=>p.IsConnected);
}
public bool IsConnected
{
get => _isConnected;
set
{
SetProperty(ref _isConnected, value);
OnConnectedChange();
}
}
public List<Control> ContextMenus { get; } = new List<Control>();
private void OnConnectedChange()
{
//foreach (var action in Actions)
//{
// var command = action.Command as DelegateCommandBase;
// command?.RaiseCanExecuteChanged();
//}
if (IsConnected)
{
_notificationService.ShowSuccess($"Connected to UAF engine on {_pluginService.EndPointId}");
}
Tooltip = IsConnected ? $"Connected to UAF engine on {_pluginService.EndPointId}" : "Not connected to UAF engine";
}
public string Tooltip
{
get => _tooltip;
set => SetProperty(ref _tooltip, value);
}
}
}