| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/corewm/tooltip_aura.h" | 5 #include "ui/views/corewm/tooltip_aura.h" |
| 6 | 6 |
| 7 #include "base/strings/string_split.h" | 7 #include "base/strings/string_split.h" |
| 8 #include "ui/aura/window.h" | 8 #include "ui/aura/window.h" |
| 9 #include "ui/aura/window_tree_host.h" | 9 #include "ui/aura/window_tree_host.h" |
| 10 #include "ui/gfx/screen.h" | 10 #include "ui/gfx/screen.h" |
| 11 #include "ui/gfx/text_elider.h" | 11 #include "ui/gfx/text_elider.h" |
| 12 #include "ui/gfx/text_utils.h" | 12 #include "ui/gfx/text_utils.h" |
| 13 #include "ui/native_theme/native_theme.h" | 13 #include "ui/native_theme/native_theme.h" |
| 14 #include "ui/views/background.h" | 14 #include "ui/views/background.h" |
| 15 #include "ui/views/border.h" | 15 #include "ui/views/border.h" |
| 16 #include "ui/views/widget/widget.h" | 16 #include "ui/views/widget/widget.h" |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 const int kTooltipHorizontalPadding = 3; | |
| 21 | |
| 22 // Max visual tooltip width. If a tooltip is greater than this width, it will | 20 // Max visual tooltip width. If a tooltip is greater than this width, it will |
| 23 // be wrapped. | 21 // be wrapped. |
| 24 const int kTooltipMaxWidthPixels = 400; | 22 const int kTooltipMaxWidthPixels = 400; |
| 25 | 23 |
| 26 const size_t kMaxLines = 10; | 24 const size_t kMaxLines = 10; |
| 27 | 25 |
| 28 // TODO(derat): This padding is needed on Chrome OS devices but seems excessive | |
| 29 // when running the same binary on a Linux workstation; presumably there's a | |
| 30 // difference in font metrics. Rationalize this. | |
| 31 const int kTooltipVerticalPadding = 2; | |
| 32 | |
| 33 // FIXME: get cursor offset from actual cursor size. | 26 // FIXME: get cursor offset from actual cursor size. |
| 34 const int kCursorOffsetX = 10; | 27 const int kCursorOffsetX = 10; |
| 35 const int kCursorOffsetY = 15; | 28 const int kCursorOffsetY = 15; |
| 36 | 29 |
| 37 // Creates a widget of type TYPE_TOOLTIP | 30 // Creates a widget of type TYPE_TOOLTIP |
| 38 views::Widget* CreateTooltipWidget(aura::Window* tooltip_window) { | 31 views::Widget* CreateTooltipWidget(aura::Window* tooltip_window) { |
| 39 views::Widget* widget = new views::Widget; | 32 views::Widget* widget = new views::Widget; |
| 40 views::Widget::InitParams params; | 33 views::Widget::InitParams params; |
| 41 // For aura, since we set the type to TYPE_TOOLTIP, the widget will get | 34 // For aura, since we set the type to TYPE_TOOLTIP, the widget will get |
| 42 // auto-parented to the right container. | 35 // auto-parented to the right container. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 53 | 46 |
| 54 namespace views { | 47 namespace views { |
| 55 namespace corewm { | 48 namespace corewm { |
| 56 | 49 |
| 57 TooltipAura::TooltipAura(gfx::ScreenType screen_type) | 50 TooltipAura::TooltipAura(gfx::ScreenType screen_type) |
| 58 : screen_type_(screen_type), | 51 : screen_type_(screen_type), |
| 59 widget_(NULL), | 52 widget_(NULL), |
| 60 tooltip_window_(NULL) { | 53 tooltip_window_(NULL) { |
| 61 label_.set_owned_by_client(); | 54 label_.set_owned_by_client(); |
| 62 label_.SetMultiLine(true); | 55 label_.SetMultiLine(true); |
| 56 |
| 57 const int kHorizontalPadding = 3; |
| 58 const int kVerticalPadding = 2; |
| 59 label_.SetBorder(Border::CreateEmptyBorder( |
| 60 kVerticalPadding, kHorizontalPadding, |
| 61 kVerticalPadding, kHorizontalPadding)); |
| 63 } | 62 } |
| 64 | 63 |
| 65 TooltipAura::~TooltipAura() { | 64 TooltipAura::~TooltipAura() { |
| 66 DestroyWidget(); | 65 DestroyWidget(); |
| 67 } | 66 } |
| 68 | 67 |
| 69 // static | 68 // static |
| 70 void TooltipAura::TrimTooltipToFit(const gfx::FontList& font_list, | 69 void TooltipAura::TrimTooltipToFit(const gfx::FontList& font_list, |
| 71 int max_width, | 70 int max_width, |
| 72 base::string16* text, | 71 base::string16* text, |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 | 143 |
| 145 int TooltipAura::GetMaxWidth(const gfx::Point& location) const { | 144 int TooltipAura::GetMaxWidth(const gfx::Point& location) const { |
| 146 // TODO(varunjain): implementation duplicated in tooltip_manager_aura. Figure | 145 // TODO(varunjain): implementation duplicated in tooltip_manager_aura. Figure |
| 147 // out a way to merge. | 146 // out a way to merge. |
| 148 gfx::Screen* screen = gfx::Screen::GetScreenByType(screen_type_); | 147 gfx::Screen* screen = gfx::Screen::GetScreenByType(screen_type_); |
| 149 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(location).bounds()); | 148 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(location).bounds()); |
| 150 return (display_bounds.width() + 1) / 2; | 149 return (display_bounds.width() + 1) / 2; |
| 151 } | 150 } |
| 152 | 151 |
| 153 void TooltipAura::SetTooltipBounds(const gfx::Point& mouse_pos, | 152 void TooltipAura::SetTooltipBounds(const gfx::Point& mouse_pos, |
| 154 int tooltip_width, | 153 const gfx::Size& tooltip_size) { |
| 155 int tooltip_height) { | 154 gfx::Rect tooltip_rect(mouse_pos, tooltip_size); |
| 156 gfx::Rect tooltip_rect(mouse_pos.x(), mouse_pos.y(), tooltip_width, | |
| 157 tooltip_height); | |
| 158 | |
| 159 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY); | 155 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY); |
| 160 gfx::Screen* screen = gfx::Screen::GetScreenByType(screen_type_); | 156 gfx::Screen* screen = gfx::Screen::GetScreenByType(screen_type_); |
| 161 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(mouse_pos).bounds()); | 157 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(mouse_pos).bounds()); |
| 162 | 158 |
| 163 // If tooltip is out of bounds on the x axis, we simply shift it | 159 // If tooltip is out of bounds on the x axis, we simply shift it |
| 164 // horizontally by the offset. | 160 // horizontally by the offset. |
| 165 if (tooltip_rect.right() > display_bounds.right()) { | 161 if (tooltip_rect.right() > display_bounds.right()) { |
| 166 int h_offset = tooltip_rect.right() - display_bounds.right(); | 162 int h_offset = tooltip_rect.right() - display_bounds.right(); |
| 167 tooltip_rect.Offset(-h_offset, 0); | 163 tooltip_rect.Offset(-h_offset, 0); |
| 168 } | 164 } |
| 169 | 165 |
| 170 // If tooltip is out of bounds on the y axis, we flip it to appear above the | 166 // If tooltip is out of bounds on the y axis, we flip it to appear above the |
| 171 // mouse cursor instead of below. | 167 // mouse cursor instead of below. |
| 172 if (tooltip_rect.bottom() > display_bounds.bottom()) | 168 if (tooltip_rect.bottom() > display_bounds.bottom()) |
| 173 tooltip_rect.set_y(mouse_pos.y() - tooltip_height); | 169 tooltip_rect.set_y(mouse_pos.y() - tooltip_size.height()); |
| 174 | 170 |
| 175 tooltip_rect.AdjustToFit(display_bounds); | 171 tooltip_rect.AdjustToFit(display_bounds); |
| 176 widget_->SetBounds(tooltip_rect); | 172 widget_->SetBounds(tooltip_rect); |
| 177 } | 173 } |
| 178 | 174 |
| 179 void TooltipAura::DestroyWidget() { | 175 void TooltipAura::DestroyWidget() { |
| 180 if (widget_) { | 176 if (widget_) { |
| 181 widget_->RemoveObserver(this); | 177 widget_->RemoveObserver(this); |
| 182 widget_->Close(); | 178 widget_->Close(); |
| 183 widget_ = NULL; | 179 widget_ = NULL; |
| 184 } | 180 } |
| 185 } | 181 } |
| 186 | 182 |
| 187 void TooltipAura::SetText(aura::Window* window, | 183 void TooltipAura::SetText(aura::Window* window, |
| 188 const base::string16& tooltip_text, | 184 const base::string16& tooltip_text, |
| 189 const gfx::Point& location) { | 185 const gfx::Point& location) { |
| 190 tooltip_window_ = window; | 186 tooltip_window_ = window; |
| 191 int max_width, line_count; | 187 int max_width = 0; |
| 188 int line_count = 0; |
| 192 base::string16 trimmed_text(tooltip_text); | 189 base::string16 trimmed_text(tooltip_text); |
| 193 TrimTooltipToFit(label_.font_list(), GetMaxWidth(location), &trimmed_text, | 190 TrimTooltipToFit(label_.font_list(), GetMaxWidth(location), &trimmed_text, |
| 194 &max_width, &line_count); | 191 &max_width, &line_count); |
| 195 label_.SetText(trimmed_text); | 192 label_.SetText(trimmed_text); |
| 196 | 193 label_.SizeToFit(max_width + label_.GetInsets().width()); |
| 197 int width = max_width + 2 * kTooltipHorizontalPadding; | |
| 198 int height = label_.GetHeightForWidth(max_width) + | |
| 199 2 * kTooltipVerticalPadding; | |
| 200 | 194 |
| 201 if (!widget_) { | 195 if (!widget_) { |
| 202 widget_ = CreateTooltipWidget(tooltip_window_); | 196 widget_ = CreateTooltipWidget(tooltip_window_); |
| 203 widget_->SetContentsView(&label_); | 197 widget_->SetContentsView(&label_); |
| 204 widget_->AddObserver(this); | 198 widget_->AddObserver(this); |
| 205 } | 199 } |
| 206 | 200 |
| 207 SetTooltipBounds(location, width, height); | 201 SetTooltipBounds(location, label_.size()); |
| 208 | 202 |
| 209 ui::NativeTheme* native_theme = widget_->GetNativeTheme(); | 203 ui::NativeTheme* native_theme = widget_->GetNativeTheme(); |
| 210 label_.set_background( | 204 label_.set_background( |
| 211 views::Background::CreateSolidBackground( | 205 views::Background::CreateSolidBackground( |
| 212 native_theme->GetSystemColor( | 206 native_theme->GetSystemColor( |
| 213 ui::NativeTheme::kColorId_TooltipBackground))); | 207 ui::NativeTheme::kColorId_TooltipBackground))); |
| 214 | 208 |
| 215 label_.SetAutoColorReadabilityEnabled(false); | 209 label_.SetAutoColorReadabilityEnabled(false); |
| 216 label_.SetEnabledColor(native_theme->GetSystemColor( | 210 label_.SetEnabledColor(native_theme->GetSystemColor( |
| 217 ui::NativeTheme::kColorId_TooltipText)); | 211 ui::NativeTheme::kColorId_TooltipText)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 235 } | 229 } |
| 236 | 230 |
| 237 void TooltipAura::OnWidgetDestroying(views::Widget* widget) { | 231 void TooltipAura::OnWidgetDestroying(views::Widget* widget) { |
| 238 DCHECK_EQ(widget_, widget); | 232 DCHECK_EQ(widget_, widget); |
| 239 widget_ = NULL; | 233 widget_ = NULL; |
| 240 tooltip_window_ = NULL; | 234 tooltip_window_ = NULL; |
| 241 } | 235 } |
| 242 | 236 |
| 243 } // namespace corewm | 237 } // namespace corewm |
| 244 } // namespace views | 238 } // namespace views |
| OLD | NEW |