mirror of
https://github.com/DeltaV-Station/Delta-v.git
synced 2026-09-29 23:57:29 +00:00
142 lines
4.8 KiB
C#
142 lines
4.8 KiB
C#
using System.Linq;
|
|
using System.Numerics;
|
|
using Content.Shared._DV.CartridgeLoader.Cartridges;
|
|
using Content.Shared.Roles;
|
|
using Robust.Client.AutoGenerated;
|
|
using Robust.Client.UserInterface.Controls;
|
|
using Robust.Client.UserInterface.XAML;
|
|
using Robust.Shared.Prototypes;
|
|
using Robust.Shared.Utility;
|
|
|
|
namespace Content.Client._DV.CartridgeLoader.Cartridges;
|
|
|
|
[GenerateTypedNameReferences]
|
|
public sealed partial class NanoChatLookupView : PanelContainer
|
|
{
|
|
public NanoChatLookupView()
|
|
{
|
|
RobustXamlLoader.Load(this);
|
|
}
|
|
|
|
public event Action<NanoChatRecipient>? OnStartChat;
|
|
|
|
public void UpdateContactList(NanoChatUiState state)
|
|
{
|
|
ContactsList.RemoveAllChildren();
|
|
if (state.Contacts is not { } contacts)
|
|
{
|
|
ContactsList.AddChild(new Label() { Text = Loc.GetString("nano-chat-look-up-no-server") });
|
|
return;
|
|
}
|
|
|
|
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
|
|
var departmentGroups = new Dictionary<DepartmentPrototype, List<NanoChatRecipient>>();
|
|
var noDepartmentContacts = new List<NanoChatRecipient>();
|
|
|
|
foreach (var contact in contacts)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(contact.Department) &&
|
|
prototypeManager.TryIndex<DepartmentPrototype>(contact.Department, out var dept))
|
|
{
|
|
departmentGroups.GetOrNew(dept).Add(contact);
|
|
}
|
|
else
|
|
{
|
|
noDepartmentContacts.Add(contact);
|
|
}
|
|
}
|
|
|
|
var rowIndex = 0;
|
|
foreach (var (department, departmentContacts) in departmentGroups.OrderBy(entry => entry.Key, DepartmentUIComparer.Instance))
|
|
{
|
|
departmentContacts.Sort();
|
|
|
|
// Add department header
|
|
ContactsList.AddChild(new Label
|
|
{
|
|
Text = Loc.GetString(department.Name),
|
|
StyleClasses = { "LabelBig" },
|
|
Margin = new Thickness(0, rowIndex == 0 ? 0 : 8, 0, 2)
|
|
});
|
|
|
|
foreach (var contact in departmentContacts)
|
|
{
|
|
var isEvenRow = rowIndex % 2 == 0;
|
|
var contactControl = new ContactContainer(contact, state, isEvenRow, OnStartChat);
|
|
ContactsList.AddChild(contactControl);
|
|
rowIndex++;
|
|
}
|
|
}
|
|
|
|
// Add contacts without departments at the end
|
|
if (noDepartmentContacts.Count > 0)
|
|
{
|
|
noDepartmentContacts.Sort();
|
|
|
|
if (departmentGroups.Count > 0)
|
|
{
|
|
ContactsList.AddChild(new Label
|
|
{
|
|
Text = Loc.GetString("nano-chat-other-department"),
|
|
StyleClasses = { "LabelBig" },
|
|
Margin = new Thickness(0, 8, 0, 2)
|
|
});
|
|
}
|
|
|
|
foreach (var contact in noDepartmentContacts)
|
|
{
|
|
var isEvenRow = rowIndex % 2 == 0;
|
|
var contactControl = new ContactContainer(contact, state, isEvenRow, OnStartChat);
|
|
ContactsList.AddChild(contactControl);
|
|
rowIndex++;
|
|
}
|
|
}
|
|
}
|
|
|
|
public sealed class ContactContainer : PanelContainer
|
|
{
|
|
public ContactContainer(NanoChatRecipient contact, NanoChatUiState state, bool isEvenRow, Action<NanoChatRecipient>? onStartChat)
|
|
{
|
|
HorizontalExpand = true;
|
|
StyleClasses.Add(isEvenRow ? "PanelBackgroundBaseDark" : "PanelBackgroundLight");
|
|
|
|
var displayName = contact.Name;
|
|
if (!string.IsNullOrWhiteSpace(contact.JobTitle))
|
|
displayName = $"{contact.Name} ({contact.JobTitle})";
|
|
|
|
var nameLabel = new Label()
|
|
{
|
|
Text = displayName,
|
|
HorizontalAlignment = HAlignment.Left,
|
|
HorizontalExpand = true
|
|
};
|
|
var numberLabel = new Label()
|
|
{
|
|
Text = $"#{contact.Number:D4}",
|
|
HorizontalAlignment = HAlignment.Right,
|
|
Margin = new Thickness(0, 0, 36, 0),
|
|
};
|
|
var startChatButton = new Button()
|
|
{
|
|
Text = "+",
|
|
HorizontalAlignment = HAlignment.Right,
|
|
MinSize = new Vector2(32, 32),
|
|
MaxSize = new Vector2(32, 32),
|
|
ToolTip = Loc.GetString("nano-chat-new-chat"),
|
|
};
|
|
startChatButton.AddStyleClass("OpenBoth");
|
|
|
|
if (contact.Number == state.OwnNumber || state.Recipients.ContainsKey(contact.Number) || state.MaxRecipients <= state.Recipients.Count)
|
|
{
|
|
startChatButton.Disabled = true;
|
|
}
|
|
|
|
startChatButton.OnPressed += _ => onStartChat?.Invoke(contact);
|
|
|
|
AddChild(nameLabel);
|
|
AddChild(numberLabel);
|
|
AddChild(startChatButton);
|
|
}
|
|
}
|
|
}
|