Chromium Code Reviews| 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 | |
| 26 namespace { | |
| 27 | |
| 28 SkColor kTooltipBackground = 0xBFFFFFDD; | |
| 29 SkColor kTooltipBorder = 0xFF000000; | |
| 30 int kTooltipBorderWidth = 1; | |
| 31 int kTooltipTimeoutMs = 500; | |
| 32 | |
| 33 // FIXME: get cursor offset from actual cursor size. | |
| 34 int kCursorOffsetX = 10; | |
| 35 int kCursorOffsetY = 15; | |
| 36 | |
| 37 // Maximum number of characters we allow in a tooltip. | |
| 38 const size_t kMaxTooltipLength = 1024; | |
| 39 | |
| 40 // Maximum number of lines we allow in the tooltip. | |
| 41 const size_t kMaxLines = 6; | |
| 42 | |
| 43 gfx::Font GetDefaultFont() { | |
| 44 return ui::ResourceBundle::GetSharedInstance().GetFont( | |
| 45 ui::ResourceBundle::BaseFont); | |
| 46 } | |
| 47 | |
| 48 // Returns the maximum width of the tooltip. |x| and |y| give the location | |
| 49 // the tooltip is to be displayed on in screen coordinates. | |
| 50 int GetMaxWidth(int x, int y) { | |
| 51 gfx::Rect monitor_bounds = | |
| 52 gfx::Screen::GetMonitorAreaNearestPoint(gfx::Point(x, y)); | |
| 53 return monitor_bounds.width() == 0 ? 800 : (monitor_bounds.width() + 1) / 2; | |
|
Ben Goodger (Google)
2011/11/30 21:54:19
does this happen? (return 0)?
varunjain
2011/11/30 22:42:07
I am not sure when this could happen. This code is
| |
| 54 } | |
| 55 | |
| 56 // Trims the tooltip to fit, setting |text| to the clipped result, | |
| 57 // |max_width| to the width (in pixels) of the clipped text and |line_count| | |
| 58 // to the number of lines of text in the tooltip. |x| and |y| give the | |
| 59 // location of the tooltip in screen coordinates. | |
| 60 void TrimTooltipToFit(string16* text, | |
| 61 int* max_width, | |
| 62 int* line_count, | |
| 63 int x, | |
| 64 int y) { | |
| 65 *max_width = 0; | |
| 66 *line_count = 0; | |
| 67 | |
| 68 // Clamp the tooltip length to kMaxTooltipLength so that we don't | |
| 69 // accidentally DOS the user with a mega tooltip. | |
| 70 if (text->length() > kMaxTooltipLength) | |
| 71 *text = text->substr(0, kMaxTooltipLength); | |
| 72 | |
| 73 // Determine the available width for the tooltip. | |
| 74 int available_width = GetMaxWidth(x, y); | |
| 75 | |
| 76 // Split the string into at most kMaxLines lines. | |
| 77 std::vector<string16> lines; | |
| 78 base::SplitString(*text, '\n', &lines); | |
| 79 if (lines.size() > kMaxLines) | |
| 80 lines.resize(kMaxLines); | |
| 81 *line_count = static_cast<int>(lines.size()); | |
| 82 | |
| 83 // Format each line to fit. | |
| 84 gfx::Font font = GetDefaultFont(); | |
| 85 string16 result; | |
| 86 for (std::vector<string16>::iterator i = lines.begin(); i != lines.end(); | |
| 87 ++i) { | |
| 88 string16 elided_text = ui::ElideText(*i, font, available_width, false); | |
| 89 *max_width = std::max(*max_width, font.GetStringWidth(elided_text)); | |
| 90 if (!result.empty()) | |
| 91 result.push_back('\n'); | |
| 92 result.append(elided_text); | |
| 93 } | |
| 94 *text = result; | |
| 95 } | |
| 96 | |
| 97 // Creates a widget of type TYPE_TOOLTIP | |
| 98 views::Widget* CreateTooltip() { | |
| 99 views::Widget* widget = new views::Widget; | |
| 100 views::Widget::InitParams params; | |
| 101 // For aura, since we set the type to TOOLTIP_TYPE, the widget will get | |
| 102 // auto-parented to the MenuAndTooltipsContainer. | |
| 103 params.type = views::Widget::InitParams::TYPE_TOOLTIP; | |
| 104 params.keep_on_top = true; | |
| 105 params.accept_events = false; | |
| 106 params.transparent = true; | |
| 107 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
| 108 widget->Init(params); | |
| 109 widget->SetOpacity(0xFF); | |
|
Ben Goodger (Google)
2011/11/30 21:54:19
I believe this will be the default opacity, so you
varunjain
2011/11/30 22:42:07
Done.
| |
| 110 return widget; | |
| 111 } | |
| 112 | |
| 113 } // namespace | |
| 114 | |
| 115 namespace aura_shell { | |
| 116 | |
| 117 //////////////////////////////////////////////////////////////////////////////// | |
| 118 // ShellTooltipManager public: | |
| 119 | |
| 120 ShellTooltipManager::ShellTooltipManager() : aura::EventFilter(NULL), | |
| 121 tooltip_window_(NULL) { | |
| 122 tooltip_label_.set_background( | |
| 123 views::Background::CreateSolidBackground(kTooltipBackground)); | |
| 124 tooltip_label_.set_border( | |
| 125 views::Border::CreateSolidBorder(kTooltipBorderWidth, kTooltipBorder)); | |
| 126 tooltip_widget_.reset(CreateTooltip()); | |
| 127 tooltip_widget_->SetContentsView(&tooltip_label_); | |
| 128 tooltip_widget_->Activate(); | |
| 129 tooltip_widget_->SetAlwaysOnTop(true); | |
|
Ben Goodger (Google)
2011/11/30 21:54:19
you shouldn't need this. the stacking controller s
varunjain
2011/11/30 22:42:07
Done.
| |
| 130 tooltip_timer_.Start(FROM_HERE, | |
| 131 base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs), | |
| 132 this, &ShellTooltipManager::TooltipTimerFired); | |
| 133 } | |
| 134 | |
| 135 ShellTooltipManager::~ShellTooltipManager() { | |
| 136 if (tooltip_window_) | |
| 137 tooltip_window_->RemoveObserver(this); | |
| 138 tooltip_widget_->Close(); | |
| 139 } | |
| 140 | |
| 141 void ShellTooltipManager::UpdateTooltip(aura::Window* target) { | |
| 142 // If tooltip is visible, we may want to hide it. If it is not, we are ok. | |
| 143 if (tooltip_window_ == target && tooltip_widget_->IsVisible()) | |
| 144 UpdateIfRequired(); | |
| 145 } | |
| 146 | |
| 147 bool ShellTooltipManager::PreHandleKeyEvent(aura::Window* target, | |
| 148 aura::KeyEvent* event) { | |
| 149 return false; | |
| 150 } | |
| 151 | |
| 152 bool ShellTooltipManager::PreHandleMouseEvent(aura::Window* target, | |
| 153 aura::MouseEvent* event) { | |
| 154 switch (event->type()) { | |
| 155 case ui::ET_MOUSE_MOVED: | |
| 156 if (tooltip_window_ != target) { | |
| 157 if (tooltip_window_) | |
| 158 tooltip_window_->RemoveObserver(this); | |
| 159 tooltip_window_ = target; | |
| 160 tooltip_window_->AddObserver(this); | |
| 161 } | |
| 162 curr_mouse_loc_ = event->location(); | |
| 163 if (tooltip_timer_.IsRunning()) | |
| 164 tooltip_timer_.Reset(); | |
| 165 | |
| 166 if (tooltip_widget_->IsVisible()) | |
| 167 UpdateIfRequired(); | |
| 168 break; | |
| 169 case ui::ET_MOUSE_PRESSED: | |
| 170 case ui::ET_MOUSE_RELEASED: | |
| 171 case ui::ET_MOUSE_DRAGGED: | |
| 172 case ui::ET_MOUSEWHEEL: | |
| 173 // Hide the tooltip for click, release, drag, wheel events. | |
| 174 if (tooltip_widget_->IsVisible()) | |
| 175 tooltip_widget_->Hide(); | |
| 176 break; | |
| 177 default: | |
| 178 break; | |
| 179 } | |
| 180 return false; | |
| 181 } | |
| 182 | |
| 183 ui::TouchStatus ShellTooltipManager::PreHandleTouchEvent( | |
| 184 aura::Window* target, | |
| 185 aura::TouchEvent* event) { | |
| 186 return ui::TOUCH_STATUS_UNKNOWN; | |
| 187 } | |
| 188 | |
| 189 void ShellTooltipManager::OnWindowDestroyed(aura::Window* window) { | |
| 190 if (tooltip_window_ == window) { | |
| 191 tooltip_window_->RemoveObserver(this); | |
| 192 tooltip_window_ = NULL; | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 void ShellTooltipManager::TooltipTimerFired() { | |
| 197 UpdateIfRequired(); | |
| 198 } | |
| 199 | |
| 200 void ShellTooltipManager::UpdateIfRequired() { | |
| 201 string16 tooltip_text; | |
| 202 if (tooltip_window_) { | |
| 203 void* property = tooltip_window_->GetProperty(aura::kTooltipTextKey); | |
| 204 if (property) | |
| 205 tooltip_text = *static_cast<string16*>(property); | |
| 206 } | |
| 207 | |
| 208 if (tooltip_text_ != tooltip_text) { | |
| 209 tooltip_text_ = tooltip_text; | |
| 210 if (tooltip_text_.empty()) { | |
| 211 tooltip_widget_->Hide(); | |
| 212 } else { | |
| 213 int max_width, line_count; | |
|
Ben Goodger (Google)
2011/11/30 21:54:19
by doing the Tooltip class thing I mentioned in th
varunjain
2011/11/30 22:42:07
Done.
| |
| 214 string16 tooltip_text(tooltip_text_); | |
| 215 gfx::Point widget_loc = curr_mouse_loc_; | |
| 216 widget_loc = widget_loc.Add(tooltip_window_->GetScreenBounds().origin()); | |
| 217 TrimTooltipToFit(&tooltip_text, &max_width, &line_count, | |
| 218 widget_loc.x(), widget_loc.y()); | |
| 219 tooltip_label_.SetText(tooltip_text); | |
| 220 | |
| 221 SetTooltipBounds(widget_loc, max_width + 2 * kTooltipBorderWidth, | |
| 222 tooltip_label_.GetPreferredSize().height()); | |
| 223 tooltip_widget_->Show(); | |
| 224 } | |
| 225 } | |
| 226 } | |
| 227 | |
| 228 void ShellTooltipManager::SetTooltipBounds(gfx::Point mouse_pos, | |
| 229 int tooltip_width, | |
| 230 int tooltip_height) { | |
| 231 gfx::Rect tooltip_rect(mouse_pos.x(), mouse_pos.y(), tooltip_width, | |
| 232 tooltip_height); | |
| 233 | |
| 234 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY); | |
| 235 gfx::Rect monitor_bounds = | |
| 236 gfx::Screen::GetMonitorAreaNearestPoint(tooltip_rect.origin()); | |
| 237 tooltip_widget_->SetBounds(tooltip_rect.AdjustToFit(monitor_bounds)); | |
| 238 } | |
| 239 | |
| 240 } // namespace aura_shell | |
| OLD | NEW |