OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "ui/aura_shell/shell_tooltip_manager.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/location.h" |
| 10 #include "base/string_split.h" |
| 11 #include "base/time.h" |
| 12 #include "ui/aura/client/aura_constants.h" |
| 13 #include "ui/aura/event.h" |
| 14 #include "ui/aura/window.h" |
| 15 #include "ui/aura_shell/shell.h" |
| 16 #include "ui/base/resource/resource_bundle.h" |
| 17 #include "ui/base/text/text_elider.h" |
| 18 #include "ui/gfx/font.h" |
| 19 #include "ui/gfx/point.h" |
| 20 #include "ui/gfx/rect.h" |
| 21 #include "ui/gfx/screen.h" |
| 22 #include "ui/views/widget/widget.h" |
| 23 #include "views/background.h" |
| 24 #include "views/border.h" |
| 25 #include "views/controls/label.h" |
| 26 |
| 27 namespace { |
| 28 |
| 29 SkColor kTooltipBackground = 0xFFFFFFCC; |
| 30 SkColor kTooltipBorder = 0xFF000000; |
| 31 int kTooltipBorderWidth = 1; |
| 32 int kTooltipTimeoutMs = 500; |
| 33 |
| 34 // FIXME: get cursor offset from actual cursor size. |
| 35 int kCursorOffsetX = 10; |
| 36 int kCursorOffsetY = 15; |
| 37 |
| 38 // Maximum number of characters we allow in a tooltip. |
| 39 const size_t kMaxTooltipLength = 1024; |
| 40 |
| 41 // Maximum number of lines we allow in the tooltip. |
| 42 const size_t kMaxLines = 6; |
| 43 |
| 44 gfx::Font GetDefaultFont() { |
| 45 return ui::ResourceBundle::GetSharedInstance().GetFont( |
| 46 ui::ResourceBundle::BaseFont); |
| 47 } |
| 48 |
| 49 // Returns the maximum width of the tooltip. |x| and |y| give the location |
| 50 // the tooltip is to be displayed on in screen coordinates. |
| 51 int GetMaxWidth(int x, int y) { |
| 52 gfx::Rect monitor_bounds = |
| 53 gfx::Screen::GetMonitorAreaNearestPoint(gfx::Point(x, y)); |
| 54 return (monitor_bounds.width() + 1) / 2; |
| 55 } |
| 56 |
| 57 // Trims the tooltip to fit, setting |text| to the clipped result, |
| 58 // |max_width| to the width (in pixels) of the clipped text and |line_count| |
| 59 // to the number of lines of text in the tooltip. |x| and |y| give the |
| 60 // location of the tooltip in screen coordinates. |
| 61 void TrimTooltipToFit(string16* text, |
| 62 int* max_width, |
| 63 int* line_count, |
| 64 int x, |
| 65 int y) { |
| 66 *max_width = 0; |
| 67 *line_count = 0; |
| 68 |
| 69 // Clamp the tooltip length to kMaxTooltipLength so that we don't |
| 70 // accidentally DOS the user with a mega tooltip. |
| 71 if (text->length() > kMaxTooltipLength) |
| 72 *text = text->substr(0, kMaxTooltipLength); |
| 73 |
| 74 // Determine the available width for the tooltip. |
| 75 int available_width = GetMaxWidth(x, y); |
| 76 |
| 77 // Split the string into at most kMaxLines lines. |
| 78 std::vector<string16> lines; |
| 79 base::SplitString(*text, '\n', &lines); |
| 80 if (lines.size() > kMaxLines) |
| 81 lines.resize(kMaxLines); |
| 82 *line_count = static_cast<int>(lines.size()); |
| 83 |
| 84 // Format each line to fit. |
| 85 gfx::Font font = GetDefaultFont(); |
| 86 string16 result; |
| 87 for (std::vector<string16>::iterator i = lines.begin(); i != lines.end(); |
| 88 ++i) { |
| 89 string16 elided_text = ui::ElideText(*i, font, available_width, false); |
| 90 *max_width = std::max(*max_width, font.GetStringWidth(elided_text)); |
| 91 if (!result.empty()) |
| 92 result.push_back('\n'); |
| 93 result.append(elided_text); |
| 94 } |
| 95 *text = result; |
| 96 } |
| 97 |
| 98 // Creates a widget of type TYPE_TOOLTIP |
| 99 views::Widget* CreateTooltip() { |
| 100 views::Widget* widget = new views::Widget; |
| 101 views::Widget::InitParams params; |
| 102 // For aura, since we set the type to TOOLTIP_TYPE, the widget will get |
| 103 // auto-parented to the MenuAndTooltipsContainer. |
| 104 params.type = views::Widget::InitParams::TYPE_TOOLTIP; |
| 105 params.keep_on_top = true; |
| 106 params.accept_events = false; |
| 107 params.transparent = true; |
| 108 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 109 widget->Init(params); |
| 110 return widget; |
| 111 } |
| 112 |
| 113 } // namespace |
| 114 |
| 115 namespace aura_shell { |
| 116 |
| 117 // Displays a widget with tooltip using a views::Label. |
| 118 class ShellTooltipManager::Tooltip { |
| 119 public: |
| 120 Tooltip() { |
| 121 label_.set_background( |
| 122 views::Background::CreateSolidBackground(kTooltipBackground)); |
| 123 label_.set_border( |
| 124 views::Border::CreateSolidBorder(kTooltipBorderWidth, kTooltipBorder)); |
| 125 label_.set_parent_owned(false); |
| 126 widget_.reset(CreateTooltip()); |
| 127 widget_->SetContentsView(&label_); |
| 128 widget_->Activate(); |
| 129 } |
| 130 |
| 131 ~Tooltip() { |
| 132 widget_->Close(); |
| 133 } |
| 134 |
| 135 // Updates the text on the tooltip and resizes to fit. |
| 136 void SetText(string16 tooltip_text, gfx::Point location) { |
| 137 int max_width, line_count; |
| 138 TrimTooltipToFit(&tooltip_text, &max_width, &line_count, |
| 139 location.x(), location.y()); |
| 140 label_.SetText(tooltip_text); |
| 141 |
| 142 SetTooltipBounds(location, max_width + 2 * kTooltipBorderWidth, |
| 143 label_.GetPreferredSize().height()); |
| 144 } |
| 145 |
| 146 // Shows the tooltip. |
| 147 void Show() { |
| 148 widget_->Show(); |
| 149 } |
| 150 |
| 151 // Hides the tooltip. |
| 152 void Hide() { |
| 153 widget_->Hide(); |
| 154 } |
| 155 |
| 156 bool IsVisible() { |
| 157 return widget_->IsVisible(); |
| 158 } |
| 159 |
| 160 private: |
| 161 views::Label label_; |
| 162 scoped_ptr<views::Widget> widget_; |
| 163 |
| 164 // Adjusts the bounds given by the arguments to fit inside the desktop |
| 165 // and applies the adjusted bounds to the label_. |
| 166 void SetTooltipBounds(gfx::Point mouse_pos, |
| 167 int tooltip_width, |
| 168 int tooltip_height) { |
| 169 gfx::Rect tooltip_rect(mouse_pos.x(), mouse_pos.y(), tooltip_width, |
| 170 tooltip_height); |
| 171 |
| 172 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY); |
| 173 gfx::Rect monitor_bounds = |
| 174 gfx::Screen::GetMonitorAreaNearestPoint(tooltip_rect.origin()); |
| 175 widget_->SetBounds(tooltip_rect.AdjustToFit(monitor_bounds)); |
| 176 } |
| 177 |
| 178 }; |
| 179 |
| 180 //////////////////////////////////////////////////////////////////////////////// |
| 181 // ShellTooltipManager public: |
| 182 |
| 183 ShellTooltipManager::ShellTooltipManager() : aura::EventFilter(NULL), |
| 184 tooltip_window_(NULL), |
| 185 tooltip_(new Tooltip) { |
| 186 tooltip_timer_.Start(FROM_HERE, |
| 187 base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs), |
| 188 this, &ShellTooltipManager::TooltipTimerFired); |
| 189 } |
| 190 |
| 191 ShellTooltipManager::~ShellTooltipManager() { |
| 192 if (tooltip_window_) |
| 193 tooltip_window_->RemoveObserver(this); |
| 194 } |
| 195 |
| 196 void ShellTooltipManager::UpdateTooltip(aura::Window* target) { |
| 197 // If tooltip is visible, we may want to hide it. If it is not, we are ok. |
| 198 if (tooltip_window_ == target && tooltip_->IsVisible()) |
| 199 UpdateIfRequired(); |
| 200 } |
| 201 |
| 202 bool ShellTooltipManager::PreHandleKeyEvent(aura::Window* target, |
| 203 aura::KeyEvent* event) { |
| 204 return false; |
| 205 } |
| 206 |
| 207 bool ShellTooltipManager::PreHandleMouseEvent(aura::Window* target, |
| 208 aura::MouseEvent* event) { |
| 209 switch (event->type()) { |
| 210 case ui::ET_MOUSE_MOVED: |
| 211 if (tooltip_window_ != target) { |
| 212 if (tooltip_window_) |
| 213 tooltip_window_->RemoveObserver(this); |
| 214 tooltip_window_ = target; |
| 215 tooltip_window_->AddObserver(this); |
| 216 } |
| 217 curr_mouse_loc_ = event->location(); |
| 218 if (tooltip_timer_.IsRunning()) |
| 219 tooltip_timer_.Reset(); |
| 220 |
| 221 if (tooltip_->IsVisible()) |
| 222 UpdateIfRequired(); |
| 223 break; |
| 224 case ui::ET_MOUSE_PRESSED: |
| 225 case ui::ET_MOUSE_RELEASED: |
| 226 case ui::ET_MOUSE_DRAGGED: |
| 227 case ui::ET_MOUSEWHEEL: |
| 228 // Hide the tooltip for click, release, drag, wheel events. |
| 229 if (tooltip_->IsVisible()) |
| 230 tooltip_->Hide(); |
| 231 break; |
| 232 default: |
| 233 break; |
| 234 } |
| 235 return false; |
| 236 } |
| 237 |
| 238 ui::TouchStatus ShellTooltipManager::PreHandleTouchEvent( |
| 239 aura::Window* target, |
| 240 aura::TouchEvent* event) { |
| 241 return ui::TOUCH_STATUS_UNKNOWN; |
| 242 } |
| 243 |
| 244 void ShellTooltipManager::OnWindowDestroyed(aura::Window* window) { |
| 245 if (tooltip_window_ == window) { |
| 246 tooltip_window_->RemoveObserver(this); |
| 247 tooltip_window_ = NULL; |
| 248 } |
| 249 } |
| 250 |
| 251 void ShellTooltipManager::TooltipTimerFired() { |
| 252 UpdateIfRequired(); |
| 253 } |
| 254 |
| 255 void ShellTooltipManager::UpdateIfRequired() { |
| 256 string16 tooltip_text; |
| 257 if (tooltip_window_) { |
| 258 void* property = tooltip_window_->GetProperty(aura::kTooltipTextKey); |
| 259 if (property) |
| 260 tooltip_text = *static_cast<string16*>(property); |
| 261 } |
| 262 |
| 263 if (tooltip_text_ != tooltip_text) { |
| 264 tooltip_text_ = tooltip_text; |
| 265 if (tooltip_text_.empty()) { |
| 266 tooltip_->Hide(); |
| 267 } else { |
| 268 string16 tooltip_text(tooltip_text_); |
| 269 gfx::Point widget_loc = curr_mouse_loc_; |
| 270 widget_loc = widget_loc.Add(tooltip_window_->GetScreenBounds().origin()); |
| 271 tooltip_->SetText(tooltip_text, widget_loc); |
| 272 tooltip_->Show(); |
| 273 } |
| 274 } |
| 275 } |
| 276 |
| 277 } // namespace aura_shell |
OLD | NEW |