ax_tree_update.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2021 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. #include "ui/accessibility/ax_tree_update.h"
  5. #include "ui/accessibility/ax_tree_data.h"
  6. #include "base/strings/string_number_conversions.h"
  7. #include "ui/accessibility/ax_enum_util.h"
  8. namespace ui {
  9. AXTreeUpdate::AXTreeUpdate() = default;
  10. AXTreeUpdate::AXTreeUpdate(const ui::AXTreeUpdate& other) = default;
  11. AXTreeUpdate::~AXTreeUpdate() = default;
  12. std::string AXTreeUpdate::ToString() const {
  13. std::string result;
  14. if (has_tree_data) {
  15. result += "AXTreeUpdate tree data:" + tree_data.ToString() + "\n";
  16. }
  17. if (node_id_to_clear != kInvalidAXNodeID) {
  18. result += "AXTreeUpdate: clear node " +
  19. base::NumberToString(node_id_to_clear) + "\n";
  20. }
  21. if (root_id != kInvalidAXNodeID) {
  22. result += "AXTreeUpdate: root id " + base::NumberToString(root_id) + "\n";
  23. }
  24. if (event_from != ax::mojom::EventFrom::kNone)
  25. result += "event_from=" + std::string(ui::ToString(event_from)) + "\n";
  26. if (event_from_action != ax::mojom::Action::kNone)
  27. result +=
  28. "event_from_action=" + std::string(ui::ToString(event_from_action)) +
  29. "\n";
  30. if (!event_intents.empty()) {
  31. result += "event_intents=[\n";
  32. for (const auto& event_intent : event_intents)
  33. result += " " + event_intent.ToString() + "\n";
  34. result += "]\n";
  35. }
  36. // The challenge here is that we want to indent the nodes being updated
  37. // so that parent/child relationships are clear, but we don't have access
  38. // to the rest of the tree for context, so we have to try to show the
  39. // relative indentation of child nodes in this update relative to their
  40. // parents.
  41. std::map<AXNodeID, int> id_to_indentation;
  42. for (const AXNodeData& node_data : nodes) {
  43. int indent = id_to_indentation[node_data.id];
  44. result += std::string(2 * indent, ' ');
  45. result += node_data.ToString() + "\n";
  46. for (AXNodeID child_id : node_data.child_ids)
  47. id_to_indentation[child_id] = indent + 1;
  48. }
  49. return result;
  50. }
  51. bool TreeUpdatesCanBeMerged(
  52. const AXTreeUpdate& u1,
  53. const AXTreeUpdate& u2) {
  54. if (u2.node_id_to_clear)
  55. return false;
  56. if (u2.has_tree_data && u2.tree_data != u1.tree_data)
  57. return false;
  58. if (u2.root_id != u1.root_id)
  59. return false;
  60. return true;
  61. }
  62. } // namespace ui