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