Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/aura_shell/shell_tooltip_manager.h" | 5 #include "ui/aura_shell/shell_tooltip_manager.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/command_line.h" | |
| 9 #include "base/location.h" | 10 #include "base/location.h" |
| 10 #include "base/string_split.h" | 11 #include "base/string_split.h" |
| 11 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "ui/aura/aura_switches.h" | |
| 12 #include "ui/aura/client/aura_constants.h" | 14 #include "ui/aura/client/aura_constants.h" |
| 13 #include "ui/aura/event.h" | 15 #include "ui/aura/event.h" |
| 14 #include "ui/aura/window.h" | 16 #include "ui/aura/window.h" |
| 15 #include "ui/aura_shell/shell.h" | 17 #include "ui/aura_shell/shell.h" |
| 16 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
| 17 #include "ui/base/text/text_elider.h" | 19 #include "ui/base/text/text_elider.h" |
| 18 #include "ui/gfx/font.h" | 20 #include "ui/gfx/font.h" |
| 19 #include "ui/gfx/point.h" | 21 #include "ui/gfx/point.h" |
| 20 #include "ui/gfx/rect.h" | 22 #include "ui/gfx/rect.h" |
| 21 #include "ui/gfx/screen.h" | 23 #include "ui/gfx/screen.h" |
| 22 #include "ui/views/background.h" | 24 #include "ui/views/background.h" |
| 23 #include "ui/views/border.h" | 25 #include "ui/views/border.h" |
| 24 #include "ui/views/controls/label.h" | 26 #include "ui/views/controls/label.h" |
| 25 #include "ui/views/widget/widget.h" | 27 #include "ui/views/widget/widget.h" |
| 26 | 28 |
| 27 namespace { | 29 namespace { |
| 28 | 30 |
| 29 SkColor kTooltipBackground = 0xFFFFFFCC; | 31 const SkColor kTooltipBackground = 0xFFFFFFCC; |
| 30 SkColor kTooltipBorder = 0xFF000000; | 32 const SkColor kTooltipBorder = 0xFF646450; |
| 31 int kTooltipBorderWidth = 1; | 33 const int kTooltipBorderWidth = 1; |
| 32 int kTooltipTimeoutMs = 500; | 34 const int kTooltipHorizontalPadding = 2; |
| 35 // TODO(derat): This padding is needed on Chrome OS devices but seems excessive | |
| 36 // when running the same binary on a Linux workstation; presumably there's a | |
| 37 // difference in font metrics. Rationalize this. | |
| 38 const int kTooltipVerticalPadding = 2; | |
| 39 const int kTooltipTimeoutMs = 500; | |
| 33 | 40 |
| 34 // FIXME: get cursor offset from actual cursor size. | 41 // FIXME: get cursor offset from actual cursor size. |
| 35 int kCursorOffsetX = 10; | 42 const int kCursorOffsetX = 10; |
| 36 int kCursorOffsetY = 15; | 43 const int kCursorOffsetY = 15; |
| 37 | 44 |
| 38 // Maximum number of characters we allow in a tooltip. | 45 // Maximum number of characters we allow in a tooltip. |
| 39 const size_t kMaxTooltipLength = 1024; | 46 const size_t kMaxTooltipLength = 1024; |
| 40 | 47 |
| 41 // Maximum number of lines we allow in the tooltip. | 48 // Maximum number of lines we allow in the tooltip. |
| 42 const size_t kMaxLines = 6; | 49 const size_t kMaxLines = 6; |
| 43 | 50 |
| 44 // Trims the tooltip to fit, setting |text| to the clipped result, | 51 // Trims the tooltip to fit, setting |text| to the clipped result, |
| 45 // |max_width| to the width (in pixels) of the clipped text and |line_count| | 52 // |max_width| to the width (in pixels) of the clipped text and |line_count| |
| 46 // to the number of lines of text in the tooltip. |x| and |y| give the | 53 // to the number of lines of text in the tooltip. |x| and |y| give the |
| 47 // location of the tooltip in screen coordinates. | 54 // location of the tooltip in screen coordinates. |
| 48 void TrimTooltipToFit(string16* text, | 55 void TrimTooltipToFit(string16* text, |
| 49 int* max_width, | 56 int* max_width, |
| 50 int* line_count, | 57 int* line_count, |
| 51 int x, | 58 int x, |
| 52 int y) { | 59 int y) { |
| 53 *max_width = 0; | 60 *max_width = 0; |
| 54 *line_count = 0; | 61 *line_count = 0; |
| 55 | 62 |
| 56 // Clamp the tooltip length to kMaxTooltipLength so that we don't | 63 // Clamp the tooltip length to kMaxTooltipLength so that we don't |
| 57 // accidentally DOS the user with a mega tooltip. | 64 // accidentally DOS the user with a mega tooltip. |
| 58 if (text->length() > kMaxTooltipLength) | 65 if (text->length() > kMaxTooltipLength) |
| 59 *text = text->substr(0, kMaxTooltipLength); | 66 *text = text->substr(0, kMaxTooltipLength); |
| 60 | 67 |
| 61 // Determine the available width for the tooltip. | 68 // Determine the available width for the tooltip. |
| 62 int available_width = aura::TooltipClient::GetMaxWidth(x, y); | 69 int available_width = |
| 70 aura::TooltipClient::GetMaxWidth(x, y) - 2 * kTooltipHorizontalPadding; | |
|
varunjain
2011/12/06 19:25:38
I think you can simply add the padding to the widg
Daniel Erat
2011/12/06 21:34:18
Done.
| |
| 71 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAuraNoShadows)) | |
| 72 available_width -= 2 * kTooltipBorderWidth; | |
| 63 | 73 |
| 64 // Split the string into at most kMaxLines lines. | 74 // Split the string into at most kMaxLines lines. |
| 65 std::vector<string16> lines; | 75 std::vector<string16> lines; |
| 66 base::SplitString(*text, '\n', &lines); | 76 base::SplitString(*text, '\n', &lines); |
| 67 if (lines.size() > kMaxLines) | 77 if (lines.size() > kMaxLines) |
| 68 lines.resize(kMaxLines); | 78 lines.resize(kMaxLines); |
| 69 *line_count = static_cast<int>(lines.size()); | 79 *line_count = static_cast<int>(lines.size()); |
| 70 | 80 |
| 71 // Format each line to fit. | 81 // Format each line to fit. |
| 72 gfx::Font font = aura::TooltipClient::GetDefaultFont(); | 82 gfx::Font font = aura::TooltipClient::GetDefaultFont(); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 100 } // namespace | 110 } // namespace |
| 101 | 111 |
| 102 namespace aura_shell { | 112 namespace aura_shell { |
| 103 | 113 |
| 104 // Displays a widget with tooltip using a views::Label. | 114 // Displays a widget with tooltip using a views::Label. |
| 105 class ShellTooltipManager::Tooltip { | 115 class ShellTooltipManager::Tooltip { |
| 106 public: | 116 public: |
| 107 Tooltip() { | 117 Tooltip() { |
| 108 label_.set_background( | 118 label_.set_background( |
| 109 views::Background::CreateSolidBackground(kTooltipBackground)); | 119 views::Background::CreateSolidBackground(kTooltipBackground)); |
| 110 label_.set_border( | 120 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAuraNoShadows)) |
|
Ben Goodger (Google)
2011/12/06 19:27:03
needs braces
Daniel Erat
2011/12/06 21:34:18
Done.
| |
| 111 views::Border::CreateSolidBorder(kTooltipBorderWidth, kTooltipBorder)); | 121 label_.set_border( |
| 122 views::Border::CreateSolidBorder(kTooltipBorderWidth, | |
| 123 kTooltipBorder)); | |
| 112 label_.set_parent_owned(false); | 124 label_.set_parent_owned(false); |
| 113 widget_.reset(CreateTooltip()); | 125 widget_.reset(CreateTooltip()); |
| 114 widget_->SetContentsView(&label_); | 126 widget_->SetContentsView(&label_); |
| 115 widget_->Activate(); | 127 widget_->Activate(); |
| 116 } | 128 } |
| 117 | 129 |
| 118 ~Tooltip() { | 130 ~Tooltip() { |
| 119 widget_->Close(); | 131 widget_->Close(); |
| 120 } | 132 } |
| 121 | 133 |
| 122 // Updates the text on the tooltip and resizes to fit. | 134 // Updates the text on the tooltip and resizes to fit. |
| 123 void SetText(string16 tooltip_text, gfx::Point location) { | 135 void SetText(string16 tooltip_text, gfx::Point location) { |
| 124 int max_width, line_count; | 136 int max_width, line_count; |
| 125 TrimTooltipToFit(&tooltip_text, &max_width, &line_count, | 137 TrimTooltipToFit(&tooltip_text, &max_width, &line_count, |
| 126 location.x(), location.y()); | 138 location.x(), location.y()); |
| 127 label_.SetText(tooltip_text); | 139 label_.SetText(tooltip_text); |
| 128 | 140 |
| 129 SetTooltipBounds(location, max_width + 2 * kTooltipBorderWidth, | 141 int width = max_width + + 2 * kTooltipHorizontalPadding; |
| 130 label_.GetPreferredSize().height()); | 142 int height = label_.GetPreferredSize().height() + |
| 143 2 * kTooltipVerticalPadding; | |
| 144 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAuraNoShadows)) { | |
| 145 width += 2 * kTooltipBorderWidth; | |
| 146 height += 2 * kTooltipBorderWidth; | |
| 147 } | |
| 148 SetTooltipBounds(location, width, height); | |
| 131 } | 149 } |
| 132 | 150 |
| 133 // Shows the tooltip. | 151 // Shows the tooltip. |
| 134 void Show() { | 152 void Show() { |
| 135 widget_->Show(); | 153 widget_->Show(); |
| 136 } | 154 } |
| 137 | 155 |
| 138 // Hides the tooltip. | 156 // Hides the tooltip. |
| 139 void Hide() { | 157 void Hide() { |
| 140 widget_->Hide(); | 158 widget_->Hide(); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 160 gfx::Rect monitor_bounds = | 178 gfx::Rect monitor_bounds = |
| 161 gfx::Screen::GetMonitorAreaNearestPoint(tooltip_rect.origin()); | 179 gfx::Screen::GetMonitorAreaNearestPoint(tooltip_rect.origin()); |
| 162 widget_->SetBounds(tooltip_rect.AdjustToFit(monitor_bounds)); | 180 widget_->SetBounds(tooltip_rect.AdjustToFit(monitor_bounds)); |
| 163 } | 181 } |
| 164 | 182 |
| 165 }; | 183 }; |
| 166 | 184 |
| 167 //////////////////////////////////////////////////////////////////////////////// | 185 //////////////////////////////////////////////////////////////////////////////// |
| 168 // ShellTooltipManager public: | 186 // ShellTooltipManager public: |
| 169 | 187 |
| 170 ShellTooltipManager::ShellTooltipManager() : aura::EventFilter(NULL), | 188 ShellTooltipManager::ShellTooltipManager() |
| 171 tooltip_window_(NULL), | 189 : aura::EventFilter(NULL), |
| 172 tooltip_(new Tooltip) { | 190 tooltip_window_(NULL), |
| 191 tooltip_(new Tooltip) { | |
| 173 tooltip_timer_.Start(FROM_HERE, | 192 tooltip_timer_.Start(FROM_HERE, |
| 174 base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs), | 193 base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs), |
| 175 this, &ShellTooltipManager::TooltipTimerFired); | 194 this, &ShellTooltipManager::TooltipTimerFired); |
| 176 } | 195 } |
| 177 | 196 |
| 178 ShellTooltipManager::~ShellTooltipManager() { | 197 ShellTooltipManager::~ShellTooltipManager() { |
| 179 if (tooltip_window_) | 198 if (tooltip_window_) |
| 180 tooltip_window_->RemoveObserver(this); | 199 tooltip_window_->RemoveObserver(this); |
| 181 } | 200 } |
| 182 | 201 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 } | 291 } |
| 273 | 292 |
| 274 // static | 293 // static |
| 275 int TooltipClient::GetMaxWidth(int x, int y) { | 294 int TooltipClient::GetMaxWidth(int x, int y) { |
| 276 gfx::Rect monitor_bounds = | 295 gfx::Rect monitor_bounds = |
| 277 gfx::Screen::GetMonitorAreaNearestPoint(gfx::Point(x, y)); | 296 gfx::Screen::GetMonitorAreaNearestPoint(gfx::Point(x, y)); |
| 278 return (monitor_bounds.width() + 1) / 2; | 297 return (monitor_bounds.width() + 1) / 2; |
| 279 } | 298 } |
| 280 | 299 |
| 281 } // namespace aura | 300 } // namespace aura |
| OLD | NEW |