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.
5 #include "ui/views/widget/tooltip_manager_aura.h"
7 #include "base/logging.h"
8 #include "ui/aura/client/screen_position_client.h"
9 #include "ui/aura/window_event_dispatcher.h"
10 #include "ui/aura/window_tree_host.h"
11 #include "ui/base/resource/resource_bundle.h"
12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/gfx/screen.h"
14 #include "ui/views/widget/widget.h"
15 #include "ui/wm/public/tooltip_client.h"
19 ////////////////////////////////////////////////////////////////////////////////
20 // TooltipManagerAura public:
22 TooltipManagerAura::TooltipManagerAura(Widget
* widget
) : widget_(widget
) {
23 aura::client::SetTooltipText(GetWindow(), &tooltip_text_
);
26 TooltipManagerAura::~TooltipManagerAura() {
27 aura::client::SetTooltipText(GetWindow(), NULL
);
31 const gfx::FontList
& TooltipManagerAura::GetDefaultFontList() {
32 return ui::ResourceBundle::GetSharedInstance().GetFontList(
33 ui::ResourceBundle::BaseFont
);
37 void TooltipManagerAura::UpdateTooltipManagerForCapture(Widget
* source
) {
38 if (!source
->HasCapture())
41 aura::Window
* root_window
= source
->GetNativeView()->GetRootWindow();
45 gfx::Point
screen_loc(
46 root_window
->GetHost()->dispatcher()->GetLastMouseLocationInRoot());
47 aura::client::ScreenPositionClient
* screen_position_client
=
48 aura::client::GetScreenPositionClient(root_window
);
49 if (!screen_position_client
)
51 screen_position_client
->ConvertPointToScreen(root_window
, &screen_loc
);
52 gfx::Screen
* screen
= gfx::Screen::GetScreenFor(root_window
);
53 aura::Window
* target
= screen
->GetWindowAtScreenPoint(screen_loc
);
56 gfx::Point
target_loc(screen_loc
);
57 screen_position_client
=
58 aura::client::GetScreenPositionClient(target
->GetRootWindow());
59 if (!screen_position_client
)
61 screen_position_client
->ConvertPointFromScreen(target
, &target_loc
);
62 target
= target
->GetEventHandlerForPoint(target_loc
);
64 Widget
* target_widget
= Widget::GetWidgetForNativeView(target
);
65 if (target_widget
== source
)
69 if (target_widget
->GetTooltipManager())
70 target_widget
->GetTooltipManager()->UpdateTooltip();
73 target
= target
->parent();
77 ////////////////////////////////////////////////////////////////////////////////
78 // TooltipManagerAura, TooltipManager implementation:
80 const gfx::FontList
& TooltipManagerAura::GetFontList() const {
81 return GetDefaultFontList();
84 int TooltipManagerAura::GetMaxWidth(const gfx::Point
& point
,
85 aura::Window
* context
) const {
86 return aura::client::GetTooltipClient(context
->GetRootWindow())->
87 GetMaxWidth(point
, context
);
90 void TooltipManagerAura::UpdateTooltip() {
91 aura::Window
* root_window
= GetWindow()->GetRootWindow();
92 if (aura::client::GetTooltipClient(root_window
)) {
93 gfx::Point view_point
=
94 root_window
->GetHost()->dispatcher()->GetLastMouseLocationInRoot();
95 aura::Window::ConvertPointToTarget(root_window
, GetWindow(), &view_point
);
96 View
* view
= GetViewUnderPoint(view_point
);
97 UpdateTooltipForTarget(view
, view_point
, root_window
);
101 void TooltipManagerAura::TooltipTextChanged(View
* view
) {
102 aura::Window
* root_window
= GetWindow()->GetRootWindow();
103 if (aura::client::GetTooltipClient(root_window
)) {
104 gfx::Point view_point
=
105 root_window
->GetHost()->dispatcher()->GetLastMouseLocationInRoot();
106 aura::Window::ConvertPointToTarget(root_window
, GetWindow(), &view_point
);
107 View
* target
= GetViewUnderPoint(view_point
);
110 UpdateTooltipForTarget(view
, view_point
, root_window
);
114 View
* TooltipManagerAura::GetViewUnderPoint(const gfx::Point
& point
) {
115 View
* root_view
= widget_
->GetRootView();
117 return root_view
->GetTooltipHandlerForPoint(point
);
121 void TooltipManagerAura::UpdateTooltipForTarget(View
* target
,
122 const gfx::Point
& point
,
123 aura::Window
* root_window
) {
125 gfx::Point view_point
= point
;
126 View::ConvertPointFromWidget(target
, &view_point
);
127 base::string16 new_tooltip_text
;
128 if (!target
->GetTooltipText(view_point
, &new_tooltip_text
))
129 tooltip_text_
.clear();
131 tooltip_text_
= new_tooltip_text
;
133 tooltip_text_
.clear();
136 aura::client::SetTooltipId(GetWindow(), target
);
138 aura::client::GetTooltipClient(root_window
)->UpdateTooltip(GetWindow());
141 aura::Window
* TooltipManagerAura::GetWindow() {
142 return widget_
->GetNativeView();
145 } // namespace views.