Make castv2 performance test work.
[chromium-blink-merge.git] / ui / views / corewm / tooltip_aura.cc
blob1aa44e339cd98a6f2c60c23d7c340eb0d9bb9868
1 // Copyright 2013 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/corewm/tooltip_aura.h"
7 #include "base/strings/string_split.h"
8 #include "base/strings/string_util.h"
9 #include "ui/aura/window.h"
10 #include "ui/aura/window_tree_host.h"
11 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/render_text.h"
13 #include "ui/gfx/screen.h"
14 #include "ui/gfx/text_elider.h"
15 #include "ui/gfx/text_utils.h"
16 #include "ui/native_theme/native_theme.h"
17 #include "ui/views/background.h"
18 #include "ui/views/border.h"
19 #include "ui/views/view.h"
20 #include "ui/views/widget/widget.h"
22 namespace {
24 // Max visual tooltip width. If a tooltip is greater than this width, it will
25 // be wrapped.
26 const int kTooltipMaxWidthPixels = 400;
28 // FIXME: get cursor offset from actual cursor size.
29 const int kCursorOffsetX = 10;
30 const int kCursorOffsetY = 15;
32 // Creates a widget of type TYPE_TOOLTIP
33 views::Widget* CreateTooltipWidget(aura::Window* tooltip_window) {
34 views::Widget* widget = new views::Widget;
35 views::Widget::InitParams params;
36 // For aura, since we set the type to TYPE_TOOLTIP, the widget will get
37 // auto-parented to the right container.
38 params.type = views::Widget::InitParams::TYPE_TOOLTIP;
39 params.context = tooltip_window;
40 DCHECK(params.context);
41 params.keep_on_top = true;
42 params.accept_events = false;
43 widget->Init(params);
44 return widget;
47 } // namespace
49 namespace views {
50 namespace corewm {
52 // TODO(oshima): Consider to use views::Label.
53 class TooltipAura::TooltipView : public views::View {
54 public:
55 TooltipView()
56 : render_text_(gfx::RenderText::CreateInstance()),
57 max_width_(0) {
58 const int kHorizontalPadding = 3;
59 const int kVerticalPadding = 2;
60 SetBorder(Border::CreateEmptyBorder(
61 kVerticalPadding, kHorizontalPadding,
62 kVerticalPadding, kHorizontalPadding));
64 set_owned_by_client();
65 render_text_->SetMultiline(true);
67 ResetDisplayRect();
70 ~TooltipView() override {}
72 // views:View:
73 void OnPaint(gfx::Canvas* canvas) override {
74 OnPaintBackground(canvas);
75 gfx::Size text_size = size();
76 gfx::Insets insets = border()->GetInsets();
77 text_size.Enlarge(-insets.width(), -insets.height());
78 render_text_->SetDisplayRect(gfx::Rect(text_size));
79 canvas->Save();
80 canvas->Translate(gfx::Vector2d(insets.left(), insets.top()));
81 render_text_->Draw(canvas);
82 canvas->Restore();
83 OnPaintBorder(canvas);
86 gfx::Size GetPreferredSize() const override {
87 gfx::Size view_size = render_text_->GetStringSize();
88 gfx::Insets insets = border()->GetInsets();
89 view_size.Enlarge(insets.width(), insets.height());
90 return view_size;
93 const char* GetClassName() const override {
94 return "TooltipView";
97 void SetText(const base::string16& text) {
98 render_text_->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);
99 render_text_->SetText(text);
102 void SetForegroundColor(SkColor color) {
103 render_text_->SetColor(color);
106 void SetMaxWidth(int width) {
107 max_width_ = width;
108 ResetDisplayRect();
111 private:
112 void ResetDisplayRect() {
113 gfx::Insets insets = border()->GetInsets();
114 int max_text_width = max_width_ - insets.width();
115 render_text_->SetDisplayRect(gfx::Rect(0, 0, max_text_width, 100000));
118 scoped_ptr<gfx::RenderText> render_text_;
119 int max_width_;
121 DISALLOW_COPY_AND_ASSIGN(TooltipView);
124 TooltipAura::TooltipAura()
125 : tooltip_view_(new TooltipView),
126 widget_(NULL),
127 tooltip_window_(NULL) {
130 TooltipAura::~TooltipAura() {
131 DestroyWidget();
134 void TooltipAura::SetTooltipBounds(const gfx::Point& mouse_pos,
135 const gfx::Size& tooltip_size) {
136 gfx::Rect tooltip_rect(mouse_pos, tooltip_size);
137 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY);
138 gfx::Screen* screen = gfx::Screen::GetScreenFor(tooltip_window_);
139 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(mouse_pos).bounds());
141 // If tooltip is out of bounds on the x axis, we simply shift it
142 // horizontally by the offset.
143 if (tooltip_rect.right() > display_bounds.right()) {
144 int h_offset = tooltip_rect.right() - display_bounds.right();
145 tooltip_rect.Offset(-h_offset, 0);
148 // If tooltip is out of bounds on the y axis, we flip it to appear above the
149 // mouse cursor instead of below.
150 if (tooltip_rect.bottom() > display_bounds.bottom())
151 tooltip_rect.set_y(mouse_pos.y() - tooltip_size.height());
153 tooltip_rect.AdjustToFit(display_bounds);
154 widget_->SetBounds(tooltip_rect);
157 void TooltipAura::DestroyWidget() {
158 if (widget_) {
159 widget_->RemoveObserver(this);
160 widget_->Close();
161 widget_ = NULL;
165 int TooltipAura::GetMaxWidth(const gfx::Point& location,
166 aura::Window* context) const {
167 gfx::Screen* screen = gfx::Screen::GetScreenFor(context);
168 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(location).bounds());
169 return std::min(kTooltipMaxWidthPixels, (display_bounds.width() + 1) / 2);
172 void TooltipAura::SetText(aura::Window* window,
173 const base::string16& tooltip_text,
174 const gfx::Point& location) {
175 tooltip_window_ = window;
176 tooltip_view_->SetMaxWidth(GetMaxWidth(location, window));
177 tooltip_view_->SetText(tooltip_text);
179 if (!widget_) {
180 widget_ = CreateTooltipWidget(tooltip_window_);
181 widget_->SetContentsView(tooltip_view_.get());
182 widget_->AddObserver(this);
185 SetTooltipBounds(location, tooltip_view_->GetPreferredSize());
187 ui::NativeTheme* native_theme = widget_->GetNativeTheme();
188 tooltip_view_->set_background(
189 views::Background::CreateSolidBackground(
190 native_theme->GetSystemColor(
191 ui::NativeTheme::kColorId_TooltipBackground)));
192 tooltip_view_->SetForegroundColor(native_theme->GetSystemColor(
193 ui::NativeTheme::kColorId_TooltipText));
196 void TooltipAura::Show() {
197 if (widget_) {
198 widget_->Show();
199 widget_->StackAtTop();
203 void TooltipAura::Hide() {
204 tooltip_window_ = NULL;
205 if (widget_)
206 widget_->Hide();
209 bool TooltipAura::IsVisible() {
210 return widget_ && widget_->IsVisible();
213 void TooltipAura::OnWidgetDestroying(views::Widget* widget) {
214 DCHECK_EQ(widget_, widget);
215 widget_ = NULL;
216 tooltip_window_ = NULL;
219 } // namespace corewm
220 } // namespace views