mouse_watcher_view_host.cc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (c) 2012 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/views/mouse_watcher_view_host.h"
  5. #include "ui/display/screen.h"
  6. #include "ui/views/view.h"
  7. #include "ui/views/widget/widget.h"
  8. namespace views {
  9. MouseWatcherViewHost::MouseWatcherViewHost(View* view,
  10. const gfx::Insets& hot_zone_insets)
  11. : view_(view), hot_zone_insets_(hot_zone_insets) {}
  12. MouseWatcherViewHost::~MouseWatcherViewHost() = default;
  13. bool MouseWatcherViewHost::Contains(const gfx::Point& screen_point,
  14. EventType type) {
  15. bool in_view = IsCursorInViewZone(screen_point);
  16. if (!in_view || (type == EventType::kExit && !IsMouseOverWindow()))
  17. return false;
  18. return true;
  19. }
  20. // Returns whether or not the cursor is currently in the view's "zone" which
  21. // is defined as a slightly larger region than the view.
  22. bool MouseWatcherViewHost::IsCursorInViewZone(const gfx::Point& screen_point) {
  23. gfx::Rect bounds = view_->GetLocalBounds();
  24. gfx::Point view_topleft(bounds.origin());
  25. View::ConvertPointToScreen(view_, &view_topleft);
  26. bounds.set_origin(view_topleft);
  27. bounds.SetRect(view_topleft.x() - hot_zone_insets_.left(),
  28. view_topleft.y() - hot_zone_insets_.top(),
  29. bounds.width() + hot_zone_insets_.width(),
  30. bounds.height() + hot_zone_insets_.height());
  31. return bounds.Contains(screen_point.x(), screen_point.y());
  32. }
  33. // Returns true if the mouse is over the view's window.
  34. bool MouseWatcherViewHost::IsMouseOverWindow() {
  35. Widget* widget = view_->GetWidget();
  36. if (!widget)
  37. return false;
  38. return display::Screen::GetScreen()->IsWindowUnderCursor(
  39. widget->GetNativeWindow());
  40. }
  41. } // namespace views