ax_enum_util.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_ACCESSIBILITY_AX_ENUM_UTIL_H_
  5. #define UI_ACCESSIBILITY_AX_ENUM_UTIL_H_
  6. #include <map>
  7. #include <string>
  8. #include "base/containers/contains.h"
  9. #include "base/logging.h"
  10. #include "base/no_destructor.h"
  11. #include "base/notreached.h"
  12. #include "ui/accessibility/ax_base_export.h"
  13. #include "ui/accessibility/ax_enums.mojom-forward.h"
  14. namespace ui {
  15. // ax::mojom::Event
  16. AX_BASE_EXPORT const char* ToString(ax::mojom::Event event);
  17. // ax::mojom::Role
  18. AX_BASE_EXPORT const char* ToString(ax::mojom::Role role);
  19. // ax::mojom::State
  20. AX_BASE_EXPORT const char* ToString(ax::mojom::State state);
  21. // ax::mojom::Action
  22. AX_BASE_EXPORT const char* ToString(ax::mojom::Action action);
  23. // ax::mojom::ActionFlags
  24. AX_BASE_EXPORT const char* ToString(ax::mojom::ActionFlags action_flags);
  25. // ax::mojom::DefaultActionVerb
  26. AX_BASE_EXPORT const char* ToString(
  27. ax::mojom::DefaultActionVerb default_action_verb);
  28. // ax::mojom::Mutation
  29. AX_BASE_EXPORT const char* ToString(ax::mojom::Mutation mutation);
  30. // ax::mojom::StringAttribute
  31. AX_BASE_EXPORT const char* ToString(
  32. ax::mojom::StringAttribute string_attribute);
  33. // ax::mojom::IntAttribute
  34. AX_BASE_EXPORT const char* ToString(ax::mojom::IntAttribute int_attribute);
  35. // ax::mojom::FloatAttribute
  36. AX_BASE_EXPORT const char* ToString(ax::mojom::FloatAttribute float_attribute);
  37. // ax::mojom::BoolAttribute
  38. AX_BASE_EXPORT const char* ToString(ax::mojom::BoolAttribute bool_attribute);
  39. // ax::mojom::IntListAttribute
  40. AX_BASE_EXPORT const char* ToString(
  41. ax::mojom::IntListAttribute int_list_attribute);
  42. // ax::mojom::StringListAttribute
  43. AX_BASE_EXPORT const char* ToString(
  44. ax::mojom::StringListAttribute string_list_attribute);
  45. // ax::mojom::ListStyle
  46. AX_BASE_EXPORT const char* ToString(ax::mojom::ListStyle list_style);
  47. // ax::mojom::MarkerType
  48. AX_BASE_EXPORT const char* ToString(ax::mojom::MarkerType marker_type);
  49. // ax::mojom::HighlightType
  50. AX_BASE_EXPORT const char* ToString(ax::mojom::HighlightType highlight_type);
  51. // ax::mojom::MoveDirection
  52. AX_BASE_EXPORT const char* ToString(ax::mojom::MoveDirection move_direction);
  53. // ax::mojom::Command
  54. AX_BASE_EXPORT const char* ToString(ax::mojom::Command command);
  55. // ax::mojom::InputEventType
  56. AX_BASE_EXPORT const char* ToString(ax::mojom::InputEventType input_event_type);
  57. // ax::mojom::TextBoundary
  58. AX_BASE_EXPORT const char* ToString(ax::mojom::TextBoundary text_boundary);
  59. // ax::mojom::TextAlign
  60. AX_BASE_EXPORT const char* ToString(ax::mojom::TextAlign text_align);
  61. // ax::mojom::WritingDirection
  62. AX_BASE_EXPORT const char* ToString(
  63. ax::mojom::WritingDirection writing_direction);
  64. // ax::mojom::TextPosition
  65. AX_BASE_EXPORT const char* ToString(ax::mojom::TextPosition text_position);
  66. // ax::mojom::TextStyle
  67. AX_BASE_EXPORT const char* ToString(ax::mojom::TextStyle text_style);
  68. // ax:mojom::TextDecorationStyle
  69. AX_BASE_EXPORT const char* ToString(
  70. ax::mojom::TextDecorationStyle text_decoration_style);
  71. // ax::mojom::AriaCurrentState
  72. AX_BASE_EXPORT const char* ToString(
  73. ax::mojom::AriaCurrentState aria_current_state);
  74. // ax::mojom::HasPopup
  75. AX_BASE_EXPORT const char* ToString(ax::mojom::HasPopup has_popup);
  76. // ax::mojom::InvalidState
  77. AX_BASE_EXPORT const char* ToString(ax::mojom::InvalidState invalid_state);
  78. // ax::mojom::Restriction
  79. AX_BASE_EXPORT const char* ToString(ax::mojom::Restriction restriction);
  80. // ax::mojom::CheckedState
  81. AX_BASE_EXPORT const char* ToString(ax::mojom::CheckedState checked_state);
  82. // ax::mojom::SortDirection
  83. AX_BASE_EXPORT const char* ToString(ax::mojom::SortDirection sort_direction);
  84. // ax::mojom::NameFrom
  85. AX_BASE_EXPORT const char* ToString(ax::mojom::NameFrom name_from);
  86. // ax::mojom::DescriptionFrom
  87. AX_BASE_EXPORT const char* ToString(
  88. ax::mojom::DescriptionFrom description_from);
  89. // ax::mojom::EventFrom
  90. AX_BASE_EXPORT const char* ToString(ax::mojom::EventFrom event_from);
  91. // ax::mojom::Gesture
  92. AX_BASE_EXPORT const char* ToString(ax::mojom::Gesture gesture);
  93. // ax::mojom::TextAffinity
  94. AX_BASE_EXPORT const char* ToString(ax::mojom::TextAffinity text_affinity);
  95. // ax::mojom::TreeOrder
  96. AX_BASE_EXPORT const char* ToString(ax::mojom::TreeOrder tree_order);
  97. // ax::mojom::ImageAnnotationStatus
  98. AX_BASE_EXPORT const char* ToString(ax::mojom::ImageAnnotationStatus status);
  99. // ax::mojom::Dropeffect
  100. AX_BASE_EXPORT const char* ToString(ax::mojom::Dropeffect dropeffect);
  101. // Convert from the string representation of an enum defined in ax_enums.mojom
  102. // into the enum value. The first time this is called, builds up a map.
  103. // Relies on the existence of ui::ToString(enum).
  104. template <typename T>
  105. T ParseAXEnum(const char* attribute) {
  106. static base::NoDestructor<std::map<std::string, T>> attr_map;
  107. if (attr_map->empty()) {
  108. (*attr_map)[""] = T::kNone;
  109. for (int i = static_cast<int>(T::kMinValue);
  110. i <= static_cast<int>(T::kMaxValue); i++) {
  111. auto attr = static_cast<T>(i);
  112. std::string str = ui::ToString(attr);
  113. if (!base::Contains(*attr_map, str))
  114. (*attr_map)[str] = attr;
  115. }
  116. }
  117. auto iter = attr_map->find(attribute);
  118. if (iter != attr_map->end())
  119. return iter->second;
  120. LOG(ERROR) << "Could not parse: " << attribute;
  121. NOTREACHED();
  122. return T::kNone;
  123. }
  124. } // namespace ui
  125. #endif // UI_ACCESSIBILITY_AX_ENUM_UTIL_H_