You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.3 KiB
66 lines
2.3 KiB
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using MultiTerm.Core.Types;
|
|
using MultiTerm.Protocols;
|
|
using MultiTerm.Protocols.Types;
|
|
|
|
namespace MultiTerm.Core.ViewModel;
|
|
|
|
public abstract partial class TerminalViewModel : ObservableObject, ITerminalViewModel
|
|
{
|
|
public abstract string Title { get; }
|
|
public abstract TerminalViewType ViewType { get; }
|
|
public ProtocolType ProtocolType { get; set; }
|
|
public ICommunicationProtocol? CommunicationProtocol { get; set; }
|
|
|
|
public event EventHandler? ClosingEvent;
|
|
|
|
private IProtocolSettingsViewModel? protocolSettings;
|
|
|
|
public IProtocolSettingsViewModel? ProtocolSettings
|
|
{
|
|
get { return protocolSettings; }
|
|
set
|
|
{
|
|
protocolSettings = value;
|
|
// register event handler for connection request from viewmodel
|
|
if(value != null)
|
|
{
|
|
protocolSettings!.ConnectRequested += OnViewModelRequestedConnect; ; ;
|
|
protocolSettings!.DisconnectRequested += OnViewModelRequestedDisconnect;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Method to override if any closing actions are required.
|
|
/// Closing can be cancelled using the return value.
|
|
/// </summary>
|
|
/// <returns>true if closing is allowed. false if it shall be cancelled.</returns>
|
|
protected virtual bool ClosingActions() { return true; }
|
|
|
|
[RelayCommand]
|
|
public void CloseRequest()
|
|
{
|
|
if (this.ClosingActions() == true)
|
|
{
|
|
ClosingEvent?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
private void OnViewModelRequestedConnect(object? sender, ConnectionRequestEventArgs e)
|
|
{
|
|
// guard uninitialized CommunicationProtocol
|
|
if (CommunicationProtocol == null) { throw new Exception($"To call '{nameof(OnViewModelRequestedConnect)}()', CommunicationProtocol must not be null!"); }
|
|
|
|
e.Success = this.CommunicationProtocol.Connect(e.Settings);
|
|
}
|
|
|
|
private void OnViewModelRequestedDisconnect(object? sender, EventArgs e)
|
|
{
|
|
// guard uninitialized CommunicationProtocol
|
|
if (CommunicationProtocol == null) { throw new Exception($"To call '{nameof(OnViewModelRequestedConnect)}()', CommunicationProtocol must not be null!"); }
|
|
|
|
this.CommunicationProtocol.Disconnect();
|
|
}
|
|
}
|
|
|