using System.Linq; using Content.Shared.Administration.Notes; using Robust.Client.AutoGenerated; using Robust.Client.UserInterface; using Robust.Client.UserInterface.XAML; using static Robust.Client.UserInterface.Controls.LineEdit; namespace Content.Client.Administration.UI.Notes; [GenerateTypedNameReferences] public sealed partial class AdminNotesControl : Control { [Dependency] private readonly IUserInterfaceManager _ui = default!; public event Action? OnNoteChanged; public event Action? OnNewNoteEntered; public event Action? OnNoteDeleted; private AdminNotesLinePopup? _popup; public AdminNotesControl() { RobustXamlLoader.Load(this); IoCManager.InjectDependencies(this); NewNote.OnTextEntered += NewNoteEntered; } private Dictionary Inputs { get; } = new(); private bool CanCreate { get; set; } private bool CanDelete { get; set; } private bool CanEdit { get; set; } private void NewNoteEntered(LineEditEventArgs args) { if (string.IsNullOrWhiteSpace(args.Text)) { return; } NewNote.Clear(); OnNewNoteEntered?.Invoke(args.Text); } private void NoteSubmitted(AdminNotesLine input) { var text = input.EditText.Trim(); if (input.OriginalMessage == text) { return; } OnNoteChanged?.Invoke(input.Id, text); } private bool NoteRightClicked(AdminNotesLine line) { ClosePopup(); _popup = new AdminNotesLinePopup(line.Note, CanDelete, CanEdit); _popup.OnEditPressed += noteId => { if (!Inputs.TryGetValue(noteId, out var input)) { return; } input.SetEditable(true); }; _popup.OnDeletePressed += noteId => OnNoteDeleted?.Invoke(noteId); var box = UIBox2.FromDimensions(_ui.MousePositionScaled.Position, (1, 1)); _popup.Open(box); return true; } private void ClosePopup() { _popup?.Close(); _popup = null; } public void SetNotes(Dictionary notes) { foreach (var (id, input) in Inputs) { if (!notes.ContainsKey(id)) { Notes.RemoveChild(input); Inputs.Remove(id); } } foreach (var note in notes.Values.OrderBy(note => note.Id)) { if (Inputs.TryGetValue(note.Id, out var input)) { input.UpdateNote(note); continue; } input = new AdminNotesLine(note); input.OnSubmitted += NoteSubmitted; input.OnRightClicked += NoteRightClicked; Notes.AddChild(input); Inputs[note.Id] = input; } } public void SetPermissions(bool create, bool delete, bool edit) { CanCreate = create; CanDelete = delete; CanEdit = edit; NewNoteLabel.Visible = create; NewNote.Visible = create; } protected override void Dispose(bool disposing) { base.Dispose(disposing); if (!disposing) { return; } foreach (var input in Inputs.Values) { input.OnSubmitted -= NoteSubmitted; } Inputs.Clear(); NewNote.OnTextEntered -= NewNoteEntered; if (_popup != null) { _ui.PopupRoot.RemoveChild(_popup); } OnNoteChanged = null; OnNewNoteEntered = null; OnNoteDeleted = null; } }