15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/mouse_watcher_view_host.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/screen.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/view.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/widget/widget.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace views {
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)MouseWatcherViewHost::MouseWatcherViewHost(View* view,
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           const gfx::Insets& hot_zone_insets)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : view_(view),
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      hot_zone_insets_(hot_zone_insets) {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)MouseWatcherViewHost::~MouseWatcherViewHost() {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool MouseWatcherViewHost::Contains(
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const gfx::Point& screen_point,
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MouseEventType type) {
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool in_view = IsCursorInViewZone(screen_point);
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!in_view || (type == MOUSE_EXIT && !IsMouseOverWindow()))
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns whether or not the cursor is currently in the view's "zone" which
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// is defined as a slightly larger region than the view.
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool MouseWatcherViewHost::IsCursorInViewZone(const gfx::Point& screen_point) {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Rect bounds = view_->GetLocalBounds();
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Point view_topleft(bounds.origin());
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  View::ConvertPointToScreen(view_, &view_topleft);
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bounds.set_origin(view_topleft);
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bounds.SetRect(view_topleft.x() - hot_zone_insets_.left(),
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 view_topleft.y() - hot_zone_insets_.top(),
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 bounds.width() + hot_zone_insets_.width(),
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 bounds.height() + hot_zone_insets_.height());
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return bounds.Contains(screen_point.x(), screen_point.y());
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if the mouse is over the view's window.
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool MouseWatcherViewHost::IsMouseOverWindow() {
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Widget* widget = view_->GetWidget();
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!widget)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return gfx::Screen::GetScreenFor(widget->GetNativeView())->
53424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      GetWindowUnderCursor() == widget->GetNativeWindow();
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace views
57