FaxBoundUi.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System.IO;
  2. using System.Threading.Tasks;
  3. using Content.Shared.Fax;
  4. using JetBrains.Annotations;
  5. using Robust.Client.GameObjects;
  6. using Robust.Client.UserInterface;
  7. namespace Content.Client.Fax.UI;
  8. [UsedImplicitly]
  9. public sealed class FaxBoundUi : BoundUserInterface
  10. {
  11. [Dependency] private readonly IFileDialogManager _fileDialogManager = default!;
  12. [ViewVariables]
  13. private FaxWindow? _window;
  14. private bool _dialogIsOpen = false;
  15. public FaxBoundUi(EntityUid owner, Enum uiKey) : base(owner, uiKey)
  16. {
  17. }
  18. protected override void Open()
  19. {
  20. base.Open();
  21. _window = this.CreateWindow<FaxWindow>();
  22. _window.FileButtonPressed += OnFileButtonPressed;
  23. _window.CopyButtonPressed += OnCopyButtonPressed;
  24. _window.SendButtonPressed += OnSendButtonPressed;
  25. _window.RefreshButtonPressed += OnRefreshButtonPressed;
  26. _window.PeerSelected += OnPeerSelected;
  27. }
  28. private async void OnFileButtonPressed()
  29. {
  30. if (_dialogIsOpen)
  31. return;
  32. _dialogIsOpen = true;
  33. var filters = new FileDialogFilters(new FileDialogFilters.Group("txt"));
  34. await using var file = await _fileDialogManager.OpenFile(filters);
  35. _dialogIsOpen = false;
  36. if (_window == null || _window.Disposed || file == null)
  37. {
  38. return;
  39. }
  40. using var reader = new StreamReader(file);
  41. var firstLine = await reader.ReadLineAsync();
  42. string? label = null;
  43. var content = await reader.ReadToEndAsync();
  44. if (firstLine is { })
  45. {
  46. if (firstLine.StartsWith('#'))
  47. {
  48. label = firstLine[1..].Trim();
  49. }
  50. else
  51. {
  52. content = firstLine + "\n" + content;
  53. }
  54. }
  55. SendMessage(new FaxFileMessage(
  56. label?[..Math.Min(label.Length, FaxFileMessageValidation.MaxLabelSize)],
  57. content[..Math.Min(content.Length, FaxFileMessageValidation.MaxContentSize)],
  58. _window.OfficePaper));
  59. }
  60. private void OnSendButtonPressed()
  61. {
  62. SendMessage(new FaxSendMessage());
  63. }
  64. private void OnCopyButtonPressed()
  65. {
  66. SendMessage(new FaxCopyMessage());
  67. }
  68. private void OnRefreshButtonPressed()
  69. {
  70. SendMessage(new FaxRefreshMessage());
  71. }
  72. private void OnPeerSelected(string address)
  73. {
  74. SendMessage(new FaxDestinationMessage(address));
  75. }
  76. protected override void UpdateState(BoundUserInterfaceState state)
  77. {
  78. base.UpdateState(state);
  79. if (_window == null || state is not FaxUiState cast)
  80. return;
  81. _window.UpdateState(cast);
  82. }
  83. }