Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / views / corewm / tooltip_aura.cc
blob25b6c3ee299031356e179a390c8eb508291fddad
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_->SetWordWrapBehavior(gfx::WRAP_LONG_WORDS);
66 render_text_->SetMultiline(true);
68 ResetDisplayRect();
71 ~TooltipView() override {}
73 // views:View:
74 void OnPaint(gfx::Canvas* canvas) override {
75 OnPaintBackground(canvas);
76 gfx::Size text_size = size();
77 gfx::Insets insets = border()->GetInsets();
78 text_size.Enlarge(-insets.width(), -insets.height());
79 render_text_->SetDisplayRect(gfx::Rect(text_size));
80 canvas->Save();
81 canvas->Translate(gfx::Vector2d(insets.left(), insets.top()));
82 render_text_->Draw(canvas);
83 canvas->Restore();
84 OnPaintBorder(canvas);
87 gfx::Size GetPreferredSize() const override {
88 gfx::Size view_size = render_text_->GetStringSize();
89 gfx::Insets insets = border()->GetInsets();
90 view_size.Enlarge(insets.width(), insets.height());
91 return view_size;
94 const char* GetClassName() const override {
95 return "TooltipView";
98 void SetText(const base::string16& text) {
99 render_text_->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);
100 render_text_->SetText(text);
103 void SetForegroundColor(SkColor color) {
104 render_text_->SetColor(color);
107 void SetMaxWidth(int width) {
108 max_width_ = width;
109 ResetDisplayRect();
112 private:
113 void ResetDisplayRect() {
114 gfx::Insets insets = border()->GetInsets();
115 int max_text_width = max_width_ - insets.width();
116 render_text_->SetDisplayRect(gfx::Rect(0, 0, max_text_width, 100000));
119 scoped_ptr<gfx::RenderText> render_text_;
120 int max_width_;
122 DISALLOW_COPY_AND_ASSIGN(TooltipView);
125 TooltipAura::TooltipAura()
126 : tooltip_view_(new TooltipView),
127 widget_(NULL),
128 tooltip_window_(NULL) {
131 TooltipAura::~TooltipAura() {
132 DestroyWidget();
135 void TooltipAura::SetTooltipBounds(const gfx::Point& mouse_pos,
136 const gfx::Size& tooltip_size) {
137 gfx::Rect tooltip_rect(mouse_pos, tooltip_size);
138 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY);
139 gfx::Screen* screen = gfx::Screen::GetScreenFor(tooltip_window_);
140 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(mouse_pos).bounds());
142 // If tooltip is out of bounds on the x axis, we simply shift it
143 // horizontally by the offset.
144 if (tooltip_rect.right() > display_bounds.right()) {
145 int h_offset = tooltip_rect.right() - display_bounds.right();
146 tooltip_rect.Offset(-h_offset, 0);
149 // If tooltip is out of bounds on the y axis, we flip it to appear above the
150 // mouse cursor instead of below.
151 if (tooltip_rect.bottom() > display_bounds.bottom())
152 tooltip_rect.set_y(mouse_pos.y() - tooltip_size.height());
154 tooltip_rect.AdjustToFit(display_bounds);
155 widget_->SetBounds(tooltip_rect);
158 void TooltipAura::DestroyWidget() {
159 if (widget_) {
160 widget_->RemoveObserver(this);
161 widget_->Close();
162 widget_ = NULL;
166 int TooltipAura::GetMaxWidth(const gfx::Point& location,
167 aura::Window* context) const {
168 gfx::Screen* screen = gfx::Screen::GetScreenFor(context);
169 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(location).bounds());
170 return std::min(kTooltipMaxWidthPixels, (display_bounds.width() + 1) / 2);
173 void TooltipAura::SetText(aura::Window* window,
174 const base::string16& tooltip_text,
175 const gfx::Point& location) {
176 tooltip_window_ = window;
177 tooltip_view_->SetMaxWidth(GetMaxWidth(location, window));
178 tooltip_view_->SetText(tooltip_text);
180 if (!widget_) {
181 widget_ = CreateTooltipWidget(tooltip_window_);
182 widget_->SetContentsView(tooltip_view_.get());
183 widget_->AddObserver(this);
186 SetTooltipBounds(location, tooltip_view_->GetPreferredSize());
188 ui::NativeTheme* native_theme = widget_->GetNativeTheme();
189 tooltip_view_->set_background(
190 views::Background::CreateSolidBackground(
191 native_theme->GetSystemColor(
192 ui::NativeTheme::kColorId_TooltipBackground)));
193 tooltip_view_->SetForegroundColor(native_theme->GetSystemColor(
194 ui::NativeTheme::kColorId_TooltipText));
197 void TooltipAura::Show() {
198 if (widget_) {
199 widget_->Show();
200 widget_->StackAtTop();
204 void TooltipAura::Hide() {
205 tooltip_window_ = NULL;
206 if (widget_)
207 widget_->Hide();
210 bool TooltipAura::IsVisible() {
211 return widget_ && widget_->IsVisible();
214 void TooltipAura::OnWidgetDestroying(views::Widget* widget) {
215 DCHECK_EQ(widget_, widget);
216 widget_ = NULL;
217 tooltip_window_ = NULL;
220 } // namespace corewm
221 } // namespace views