Files
crystall-punk-14/Content.Client/Fax/UI/FaxBoundUi.cs

105 lines
2.7 KiB
C#
Raw Permalink Normal View History

using System.IO;
using System.Threading.Tasks;
2022-12-11 21:06:11 +03:00
using Content.Shared.Fax;
2023-04-16 23:20:57 -07:00
using JetBrains.Annotations;
2022-12-11 21:06:11 +03:00
using Robust.Client.GameObjects;
using Robust.Client.UserInterface;
2022-12-11 21:06:11 +03:00
namespace Content.Client.Fax.UI;
2023-04-16 23:20:57 -07:00
[UsedImplicitly]
2022-12-11 21:06:11 +03:00
public sealed class FaxBoundUi : BoundUserInterface
{
[Dependency] private readonly IFileDialogManager _fileDialogManager = default!;
2023-07-08 09:02:17 -07:00
[ViewVariables]
2022-12-11 21:06:11 +03:00
private FaxWindow? _window;
2023-04-16 23:20:57 -07:00
private bool _dialogIsOpen = false;
2023-07-08 09:02:17 -07:00
public FaxBoundUi(EntityUid owner, Enum uiKey) : base(owner, uiKey)
2022-12-11 21:06:11 +03:00
{
}
protected override void Open()
{
base.Open();
_window = this.CreateWindow<FaxWindow>();
_window.FileButtonPressed += OnFileButtonPressed;
_window.CopyButtonPressed += OnCopyButtonPressed;
2022-12-11 21:06:11 +03:00
_window.SendButtonPressed += OnSendButtonPressed;
_window.RefreshButtonPressed += OnRefreshButtonPressed;
_window.PeerSelected += OnPeerSelected;
}
private async void OnFileButtonPressed()
{
if (_dialogIsOpen)
return;
_dialogIsOpen = true;
var filters = new FileDialogFilters(new FileDialogFilters.Group("txt"));
await using var file = await _fileDialogManager.OpenFile(filters);
_dialogIsOpen = false;
if (_window == null || _window.Disposed || file == null)
{
return;
}
using var reader = new StreamReader(file);
var firstLine = await reader.ReadLineAsync();
string? label = null;
var content = await reader.ReadToEndAsync();
if (firstLine is { })
{
if (firstLine.StartsWith('#'))
{
label = firstLine[1..].Trim();
}
else
{
content = firstLine + "\n" + content;
}
}
SendMessage(new FaxFileMessage(
label?[..Math.Min(label.Length, FaxFileMessageValidation.MaxLabelSize)],
content[..Math.Min(content.Length, FaxFileMessageValidation.MaxContentSize)],
_window.OfficePaper));
}
2022-12-11 21:06:11 +03:00
private void OnSendButtonPressed()
{
SendMessage(new FaxSendMessage());
}
private void OnCopyButtonPressed()
{
SendMessage(new FaxCopyMessage());
}
2022-12-11 21:06:11 +03:00
private void OnRefreshButtonPressed()
{
SendMessage(new FaxRefreshMessage());
}
private void OnPeerSelected(string address)
{
SendMessage(new FaxDestinationMessage(address));
}
protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);
2023-04-16 23:20:57 -07:00
2022-12-11 21:06:11 +03:00
if (_window == null || state is not FaxUiState cast)
return;
_window.UpdateState(cast);
}
}