ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ui / views / corewm / tooltip_aura.cc
blobc0d369149afb1082c4b273ac6b8db79d2b9970f4
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 set_owned_by_client();
59 render_text_->SetMultiline(true);
60 ResetDisplayRect();
63 ~TooltipView() override {}
65 // views:View:
66 void OnPaint(gfx::Canvas* canvas) override {
67 OnPaintBackground(canvas);
68 render_text_->SetDisplayRect(gfx::Rect(size()));
69 render_text_->Draw(canvas);
70 OnPaintBorder(canvas);
73 gfx::Size GetPreferredSize() const override {
74 return render_text_->GetStringSize();
77 const char* GetClassName() const override {
78 return "TooltipView";
81 void SetText(const base::string16& text) {
82 render_text_->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);
83 render_text_->SetText(text);
86 void SetForegroundColor(SkColor color) {
87 render_text_->SetColor(color);
90 void SetMaxWidth(int width) {
91 max_width_ = width;
92 ResetDisplayRect();
95 private:
96 void ResetDisplayRect() {
97 render_text_->SetDisplayRect(gfx::Rect(0, 0, max_width_, 100000));
100 scoped_ptr<gfx::RenderText> render_text_;
101 int max_width_;
103 DISALLOW_COPY_AND_ASSIGN(TooltipView);
106 TooltipAura::TooltipAura()
107 : tooltip_view_(new TooltipView),
108 widget_(NULL),
109 tooltip_window_(NULL) {
111 const int kHorizontalPadding = 3;
112 const int kVerticalPadding = 2;
113 tooltip_view_->SetBorder(Border::CreateEmptyBorder(
114 kVerticalPadding, kHorizontalPadding,
115 kVerticalPadding, kHorizontalPadding));
118 TooltipAura::~TooltipAura() {
119 DestroyWidget();
122 void TooltipAura::SetTooltipBounds(const gfx::Point& mouse_pos,
123 const gfx::Size& tooltip_size) {
124 gfx::Rect tooltip_rect(mouse_pos, tooltip_size);
125 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY);
126 gfx::Screen* screen = gfx::Screen::GetScreenFor(tooltip_window_);
127 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(mouse_pos).bounds());
129 // If tooltip is out of bounds on the x axis, we simply shift it
130 // horizontally by the offset.
131 if (tooltip_rect.right() > display_bounds.right()) {
132 int h_offset = tooltip_rect.right() - display_bounds.right();
133 tooltip_rect.Offset(-h_offset, 0);
136 // If tooltip is out of bounds on the y axis, we flip it to appear above the
137 // mouse cursor instead of below.
138 if (tooltip_rect.bottom() > display_bounds.bottom())
139 tooltip_rect.set_y(mouse_pos.y() - tooltip_size.height());
141 tooltip_rect.AdjustToFit(display_bounds);
142 widget_->SetBounds(tooltip_rect);
145 void TooltipAura::DestroyWidget() {
146 if (widget_) {
147 widget_->RemoveObserver(this);
148 widget_->Close();
149 widget_ = NULL;
153 int TooltipAura::GetMaxWidth(const gfx::Point& location,
154 aura::Window* context) const {
155 gfx::Screen* screen = gfx::Screen::GetScreenFor(context);
156 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(location).bounds());
157 return std::min(kTooltipMaxWidthPixels, (display_bounds.width() + 1) / 2);
160 void TooltipAura::SetText(aura::Window* window,
161 const base::string16& tooltip_text,
162 const gfx::Point& location) {
163 tooltip_window_ = window;
164 tooltip_view_->SetMaxWidth(GetMaxWidth(location, window));
165 tooltip_view_->SetText(tooltip_text);
167 if (!widget_) {
168 widget_ = CreateTooltipWidget(tooltip_window_);
169 widget_->SetContentsView(tooltip_view_.get());
170 widget_->AddObserver(this);
173 SetTooltipBounds(location, tooltip_view_->GetPreferredSize());
175 ui::NativeTheme* native_theme = widget_->GetNativeTheme();
176 tooltip_view_->set_background(
177 views::Background::CreateSolidBackground(
178 native_theme->GetSystemColor(
179 ui::NativeTheme::kColorId_TooltipBackground)));
180 tooltip_view_->SetForegroundColor(native_theme->GetSystemColor(
181 ui::NativeTheme::kColorId_TooltipText));
184 void TooltipAura::Show() {
185 if (widget_) {
186 widget_->Show();
187 widget_->StackAtTop();
191 void TooltipAura::Hide() {
192 tooltip_window_ = NULL;
193 if (widget_)
194 widget_->Hide();
197 bool TooltipAura::IsVisible() {
198 return widget_ && widget_->IsVisible();
201 void TooltipAura::OnWidgetDestroying(views::Widget* widget) {
202 DCHECK_EQ(widget_, widget);
203 widget_ = NULL;
204 tooltip_window_ = NULL;
207 } // namespace corewm
208 } // namespace views