ControlExtension.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Content.Client.Guidebook.Controls;
  2. using Robust.Client.UserInterface;
  3. using Robust.Client.UserInterface.Controls;
  4. namespace Content.Client.UserInterface.ControlExtensions;
  5. public static class ControlExtension
  6. {
  7. public static List<T> GetControlOfType<T>(this Control parent) where T : Control
  8. {
  9. return parent.GetControlOfType<T>(typeof(T).Name, false);
  10. }
  11. public static List<T> GetControlOfType<T>(this Control parent, string childType) where T : Control
  12. {
  13. return parent.GetControlOfType<T>(childType, false);
  14. }
  15. public static List<T> GetControlOfType<T>(this Control parent, bool fullTreeSearch) where T : Control
  16. {
  17. return parent.GetControlOfType<T>(typeof(T).Name, fullTreeSearch);
  18. }
  19. public static List<T> GetControlOfType<T>(this Control parent, string childType, bool fullTreeSearch) where T : Control
  20. {
  21. List<T> controlList = new List<T>();
  22. foreach (var child in parent.Children)
  23. {
  24. var isType = child.GetType().Name == childType;
  25. var hasChildren = child.ChildCount > 0;
  26. var searchDeeper = hasChildren && !isType;
  27. if (isType)
  28. {
  29. controlList.Add((T) child);
  30. }
  31. if (fullTreeSearch || searchDeeper)
  32. {
  33. controlList.AddRange(child.GetControlOfType<T>(childType, fullTreeSearch));
  34. }
  35. }
  36. return controlList;
  37. }
  38. public static List<ISearchableControl> GetSearchableControls(this Control parent, bool fullTreeSearch = false)
  39. {
  40. List<ISearchableControl> controlList = new List<ISearchableControl>();
  41. foreach (var child in parent.Children)
  42. {
  43. var hasChildren = child.ChildCount > 0;
  44. var searchDeeper = hasChildren && child is not ISearchableControl;
  45. if (child is ISearchableControl searchableChild)
  46. {
  47. controlList.Add(searchableChild);
  48. }
  49. if (fullTreeSearch || searchDeeper)
  50. {
  51. controlList.AddRange(child.GetSearchableControls(fullTreeSearch));
  52. }
  53. }
  54. return controlList;
  55. }
  56. public static bool ChildrenContainText(this Control parent, string search)
  57. {
  58. var labels = parent.GetControlOfType<Label>();
  59. var richTextLabels = parent.GetControlOfType<RichTextLabel>();
  60. foreach (var label in labels)
  61. {
  62. if (label.Text != null && label.Text.Contains(search, StringComparison.OrdinalIgnoreCase))
  63. {
  64. return true;
  65. }
  66. }
  67. foreach (var label in richTextLabels)
  68. {
  69. var text = label.GetMessage();
  70. if (text != null && text.Contains(search, StringComparison.OrdinalIgnoreCase))
  71. {
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. }