overlay_agent_views.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 COMPONENTS_UI_DEVTOOLS_VIEWS_OVERLAY_AGENT_VIEWS_H_
  5. #define COMPONENTS_UI_DEVTOOLS_VIEWS_OVERLAY_AGENT_VIEWS_H_
  6. #include "base/gtest_prod_util.h"
  7. #include "components/ui_devtools/overlay.h"
  8. #include "components/ui_devtools/overlay_agent.h"
  9. #include "components/ui_devtools/views/dom_agent_views.h"
  10. #include "ui/compositor/layer.h"
  11. #include "ui/compositor/layer_delegate.h"
  12. #include "ui/events/event.h"
  13. #include "ui/events/event_handler.h"
  14. #include "ui/gfx/native_widget_types.h"
  15. namespace gfx {
  16. class RenderText;
  17. }
  18. namespace ui_devtools {
  19. enum HighlightRectsConfiguration {
  20. NO_DRAW,
  21. R1_CONTAINS_R2,
  22. R1_HORIZONTAL_FULL_LEFT_R2,
  23. R1_TOP_FULL_LEFT_R2,
  24. R1_BOTTOM_FULL_LEFT_R2,
  25. R1_TOP_PARTIAL_LEFT_R2,
  26. R1_BOTTOM_PARTIAL_LEFT_R2,
  27. R1_INTERSECTS_R2
  28. };
  29. enum RectSide { TOP_SIDE, LEFT_SIDE, RIGHT_SIDE, BOTTOM_SIDE };
  30. class OverlayAgentViews : public OverlayAgent,
  31. public ui::EventHandler,
  32. public ui::LayerDelegate {
  33. public:
  34. OverlayAgentViews(const OverlayAgentViews&) = delete;
  35. OverlayAgentViews& operator=(const OverlayAgentViews&) = delete;
  36. ~OverlayAgentViews() override;
  37. // Creates a platform-specific instance.
  38. static std::unique_ptr<OverlayAgentViews> Create(DOMAgent* dom_agent);
  39. int pinned_id() const { return pinned_id_; }
  40. void SetPinnedNodeId(int pinned_id);
  41. // Overlay::Backend:
  42. protocol::Response setInspectMode(
  43. const protocol::String& in_mode,
  44. protocol::Maybe<protocol::Overlay::HighlightConfig> in_highlightConfig)
  45. override;
  46. protocol::Response highlightNode(
  47. std::unique_ptr<protocol::Overlay::HighlightConfig> highlight_config,
  48. protocol::Maybe<int> node_id) override;
  49. protocol::Response hideHighlight() override;
  50. HighlightRectsConfiguration highlight_rect_config() const {
  51. return highlight_rect_config_;
  52. }
  53. // Return the id of the UI element located at |event|'s root location.
  54. // The function first searches for the targeted window, then the targeted
  55. // widget (if one exists), then the targeted view (if one exists). Return 0 if
  56. // no valid target is found.
  57. virtual int FindElementIdTargetedByPoint(ui::LocatedEvent* event) const = 0;
  58. protected:
  59. OverlayAgentViews(DOMAgent* dom_agent);
  60. private:
  61. FRIEND_TEST_ALL_PREFIXES(OverlayAgentTest,
  62. MouseEventsGenerateFEEventsInInspectMode);
  63. FRIEND_TEST_ALL_PREFIXES(OverlayAgentTest, HighlightRects);
  64. FRIEND_TEST_ALL_PREFIXES(OverlayAgentTest, HighlightNonexistentNode);
  65. FRIEND_TEST_ALL_PREFIXES(OverlayAgentTest, HighlightWidget);
  66. #if defined(USE_AURA)
  67. FRIEND_TEST_ALL_PREFIXES(OverlayAgentTest, HighlightWindow);
  68. FRIEND_TEST_ALL_PREFIXES(OverlayAgentTest, HighlightEmptyOrInvisibleWindow);
  69. #endif
  70. // Start handling events intended for inspectable elements.
  71. virtual void InstallPreTargetHandler() = 0;
  72. // Stop handling events intended for inspectable elements.
  73. virtual void RemovePreTargetHandler() = 0;
  74. protocol::Response HighlightNode(int node_id, bool show_size = false);
  75. // Returns true when there is any visible element to highlight.
  76. bool UpdateHighlight(
  77. const std::pair<gfx::NativeWindow, gfx::Rect>& window_and_screen_bounds);
  78. // Shows the distances between the nodes identified by |pinned_id| and
  79. // |element_id| in the highlight overlay.
  80. void ShowDistancesInHighlightOverlay(int pinned_id, int element_id);
  81. // ui:EventHandler:
  82. void OnMouseEvent(ui::MouseEvent* event) override;
  83. void OnKeyEvent(ui::KeyEvent* event) override;
  84. // ui::LayerDelegate:
  85. void OnPaintLayer(const ui::PaintContext& context) override;
  86. void OnDeviceScaleFactorChanged(float old_device_scale_factor,
  87. float new_device_scale_factor) override {}
  88. ui::Layer* layer_for_highlighting() { return layer_for_highlighting_.get(); }
  89. std::unique_ptr<gfx::RenderText> render_text_;
  90. bool show_size_on_canvas_ = false;
  91. HighlightRectsConfiguration highlight_rect_config_;
  92. bool is_swap_ = false;
  93. // The layer used to paint highlights, and its offset from the screen origin.
  94. std::unique_ptr<ui::Layer> layer_for_highlighting_;
  95. gfx::Vector2d layer_for_highlighting_screen_offset_;
  96. // Hovered and pinned element bounds in screen coordinates; empty if none.
  97. gfx::Rect hovered_rect_;
  98. gfx::Rect pinned_rect_;
  99. int pinned_id_ = 0;
  100. };
  101. } // namespace ui_devtools
  102. #endif // COMPONENTS_UI_DEVTOOLS_VIEWS_OVERLAY_AGENT_VIEWS_H_