1
0

HandLabelerWindow.xaml.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Robust.Client.UserInterface.CustomControls;
  2. using Robust.Client.AutoGenerated;
  3. using Robust.Client.UserInterface.XAML;
  4. namespace Content.Client.Labels.UI
  5. {
  6. [GenerateTypedNameReferences]
  7. public sealed partial class HandLabelerWindow : DefaultWindow
  8. {
  9. public event Action<string>? OnLabelChanged;
  10. /// <summary>
  11. /// Is the user currently entering text into the control?
  12. /// </summary>
  13. private bool _focused;
  14. // TODO LineEdit Make this a bool on the LineEdit control
  15. private string _label = string.Empty;
  16. public HandLabelerWindow()
  17. {
  18. RobustXamlLoader.Load(this);
  19. LabelLineEdit.OnTextChanged += e =>
  20. {
  21. _label = e.Text;
  22. OnLabelChanged?.Invoke(_label);
  23. };
  24. LabelLineEdit.OnFocusEnter += _ => _focused = true;
  25. LabelLineEdit.OnFocusExit += _ =>
  26. {
  27. _focused = false;
  28. LabelLineEdit.Text = _label;
  29. };
  30. }
  31. protected override void Opened()
  32. {
  33. base.Opened();
  34. // Give the editor keyboard focus, since that's the only
  35. // thing the user will want to be doing with this UI
  36. LabelLineEdit.GrabKeyboardFocus();
  37. }
  38. public void SetCurrentLabel(string label)
  39. {
  40. if (label == _label)
  41. return;
  42. _label = label;
  43. if (!_focused)
  44. LabelLineEdit.Text = label;
  45. }
  46. public void SetMaxLabelLength(int maxLength)
  47. {
  48. LabelLineEdit.IsValid = s => s.Length <= maxLength;
  49. }
  50. }
  51. }