Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NanoChat QoL And Bug Fixes #1460

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<DefaultWindow xmlns="https://spacestation14.io"
Title="{Loc nano-chat-new-title}"
Title="{Loc nano-chat-edit-title}"
MinSize="300 200">
<PanelContainer StyleClasses="AngleRect">
<BoxContainer Orientation="Vertical" Margin="4">
Expand Down Expand Up @@ -42,8 +42,8 @@
Text="{Loc nano-chat-cancel}"
StyleClasses="OpenRight"
MinSize="80 0" />
<Button Name="CreateButton"
Text="{Loc nano-chat-create}"
<Button Name="ConfirmButton"
Text="{Loc nano-chat-confirm}"
StyleClasses="OpenLeft"
MinSize="80 0"
Disabled="True" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Linq;
using Content.Client.DeltaV.NanoChat;
using Content.Shared.Access.Components;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
Expand All @@ -9,9 +9,14 @@ namespace Content.Client.DeltaV.CartridgeLoader.Cartridges;
[GenerateTypedNameReferences]
public sealed partial class EditChatPopup : DefaultWindow
{
private const int MaxInputLength = 16;
private const int MaxNumberLength = 4;

// Used to see if the user input is different from the original data
// to check if the user can submit
private string _originalNumber = "";
private string _originalName = "";
private string _originalJob = "";

public event Action<uint, string, string?>? OnContactEdited;

public EditChatPopup()
Expand All @@ -23,22 +28,23 @@ public EditChatPopup()

// Button handlers
CancelButton.OnPressed += _ => Close();
CreateButton.OnPressed += _ => EditChat();
ConfirmButton.OnPressed += _ => EditChat();

// Input validation
NameInput.OnTextChanged += _ => ValidateInputs();

NameInput.OnTextChanged += args =>
{
if (args.Text.Length > MaxInputLength)
NameInput.Text = args.Text[..MaxInputLength];
if (args.Text.Length > IdCardConsoleComponent.MaxFullNameLength)
NameInput.Text = args.Text[..IdCardConsoleComponent.MaxFullNameLength];
ValidateInputs();
};

JobInput.OnTextChanged += args =>
{
if (args.Text.Length > MaxInputLength)
JobInput.Text = args.Text[..MaxInputLength];
if (args.Text.Length > IdCardConsoleComponent.MaxJobTitleLength)
JobInput.Text = args.Text[..IdCardConsoleComponent.MaxJobTitleLength];
ValidateInputs();
};

NumberInput.OnTextChanged += args =>
Expand All @@ -58,10 +64,15 @@ public EditChatPopup()
private void ValidateInputs()
{
var isValid = !string.IsNullOrWhiteSpace(NumberInput.Text) &&
!string.IsNullOrWhiteSpace(NameInput.Text) &&
uint.TryParse(NumberInput.Text, out _);

CreateButton.Disabled = !isValid;
!string.IsNullOrWhiteSpace(NameInput.Text) &&
NumberInput.Text.Length == MaxNumberLength &&
uint.TryParse(NumberInput.Text, out _) &&
// Only valid if there are any changes
(NumberInput.Text != _originalNumber ||
NameInput.Text != _originalName ||
JobInput.Text != _originalJob);

ConfirmButton.Disabled = !isValid;
}

private void EditChat()
Expand All @@ -83,7 +94,21 @@ public void ClearInputs()
ValidateInputs();
}

public void SetNumberInput(string newNumber) => NumberInput.Text = newNumber;
public void SetNameInput(string newName) => NameInput.Text = newName;
public void SetJobInput(string newJob) => JobInput.Text = newJob;
public void SetNumberInput(string newNumber)
{
NumberInput.Text = newNumber.PadLeft(MaxNumberLength, '0');
_originalNumber = newNumber;
}

public void SetNameInput(string newName)
{
NameInput.Text = newName;
_originalName = newName;
}

public void SetJobInput(string newJob)
{
JobInput.Text = newJob;
_originalJob = newJob;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace Content.Client.DeltaV.CartridgeLoader.Cartridges;
[GenerateTypedNameReferences]
public sealed partial class NanoChatEntry : BoxContainer
{
private const int MaxNameLength = 14;
private const int MaxJobLength = 20;

public event Action<uint>? OnPressed;
private uint _number;
private Action<EventArgs>? _pressHandler;
Expand All @@ -29,11 +32,19 @@ public void SetRecipient(NanoChatRecipient recipient, uint number, bool isSelect
_pressHandler = _ => OnPressed?.Invoke(_number);
ChatButton.OnPressed += _pressHandler;

NameLabel.Text = recipient.Name;
JobLabel.Text = recipient.JobTitle ?? "";
NameLabel.Text = Truncate(recipient.Name, MaxNameLength);
JobLabel.Text = Truncate(recipient.JobTitle ?? "", MaxJobLength);
JobLabel.Visible = !string.IsNullOrEmpty(recipient.JobTitle);
UnreadIndicator.Visible = recipient.HasUnread;

ChatButton.ModulateSelfOverride = isSelected ? NanoChatMessageBubble.OwnMessageColor : null;
}

/// <summary>
/// Truncates a string to a maximum length.
/// </summary>
private static string Truncate(string text, int maxLength) =>
text.Length <= maxLength
? text
: text[..(maxLength - 3)] + "...";
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@
VerticalAlignment="Center"
Margin="0 0 8 0" />
<Button Name="EditChatButton"
Text="E"
MaxSize="32 32"
Visible="False"
StyleClasses="OpenBoth"
Margin="0 0 4 0"
ToolTip="{Loc nano-chat-edit}">
<TextureRect StyleClasses="ButtonSquare"
TexturePath="/Textures/Interface/VerbIcons/edit.svg.png"
Stretch="KeepAspectCentered"
MinSize="18 18" />
</Button>
<Button Name="DeleteChatButton"
MaxSize="32 32"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public sealed partial class NanoChatUiFragment : BoxContainer
{
[Dependency] private readonly IGameTiming _timing = default!;

private const int MaxMessageLength = 256;

private readonly NewChatPopup _newChatPopup;
private readonly EditChatPopup _editChatPopup;
private uint? _currentChat;
Expand Down Expand Up @@ -50,8 +48,7 @@ private void SetupEventHandlers()

_editChatPopup.OnContactEdited += (number, name, job) =>
{
DeleteCurrentChat();
ActionSendUiMessage?.Invoke(NanoChatUiMessageType.NewChat, number, name, job);
ActionSendUiMessage?.Invoke(NanoChatUiMessageType.EditChat, number, name, job);
};

NewChatButton.OnPressed += _ =>
Expand All @@ -71,18 +68,18 @@ private void SetupEventHandlers()
{
var length = args.Text.Length;
var isValid = !string.IsNullOrWhiteSpace(args.Text) &&
length <= MaxMessageLength &&
length <= NanoChatMessage.MaxContentLength &&
(_currentChat != null || _pendingChat != null);

SendButton.Disabled = !isValid;

// Show character count when over limit
CharacterCount.Visible = length > MaxMessageLength;
if (length > MaxMessageLength)
CharacterCount.Visible = length > NanoChatMessage.MaxContentLength;
if (length > NanoChatMessage.MaxContentLength)
{
CharacterCount.Text = Loc.GetString("nano-chat-message-too-long",
("current", length),
("max", MaxMessageLength));
("max", NanoChatMessage.MaxContentLength));
CharacterCount.StyleClasses.Add("LabelDanger");
}
};
Expand All @@ -100,6 +97,12 @@ private void SendMessage()
return;

var messageContent = MessageInput.Text;
if (!string.IsNullOrWhiteSpace(messageContent))
{
messageContent = messageContent.Trim();
if (messageContent.Length > NanoChatMessage.MaxContentLength)
messageContent = messageContent[..NanoChatMessage.MaxContentLength];
}

// Add predicted message
var predictedMessage = new NanoChatMessage(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using Content.Shared.Access.Components;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
Expand All @@ -8,7 +9,6 @@ namespace Content.Client.DeltaV.CartridgeLoader.Cartridges;
[GenerateTypedNameReferences]
public sealed partial class NewChatPopup : DefaultWindow
{
private const int MaxInputLength = 16;
private const int MaxNumberLength = 4; // i hardcoded it to be 4 so suffer

public event Action<uint, string, string?>? OnChatCreated;
Expand Down Expand Up @@ -44,22 +44,23 @@ public NewChatPopup()

NameInput.OnTextChanged += args =>
{
if (args.Text.Length > MaxInputLength)
NameInput.Text = args.Text[..MaxInputLength];
if (args.Text.Length > IdCardConsoleComponent.MaxFullNameLength)
NameInput.Text = args.Text[..IdCardConsoleComponent.MaxFullNameLength];
ValidateInputs();
};

JobInput.OnTextChanged += args =>
{
if (args.Text.Length > MaxInputLength)
JobInput.Text = args.Text[..MaxInputLength];
if (args.Text.Length > IdCardConsoleComponent.MaxJobTitleLength)
JobInput.Text = args.Text[..IdCardConsoleComponent.MaxJobTitleLength];
};
}

private void ValidateInputs()
{
var isValid = !string.IsNullOrWhiteSpace(NumberInput.Text) &&
!string.IsNullOrWhiteSpace(NameInput.Text) &&
NumberInput.Text.Length == MaxNumberLength &&
uint.TryParse(NumberInput.Text, out _);

CreateButton.Disabled = !isValid;
Expand Down
Loading
Loading