| 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" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 params.accept_events = false; | 40 params.accept_events = false; |
| 41 widget->Init(params); | 41 widget->Init(params); |
| 42 return widget; | 42 return widget; |
| 43 } | 43 } |
| 44 | 44 |
| 45 } // namespace | 45 } // namespace |
| 46 | 46 |
| 47 namespace views { | 47 namespace views { |
| 48 namespace corewm { | 48 namespace corewm { |
| 49 | 49 |
| 50 TooltipAura::TooltipAura(gfx::ScreenType screen_type) | 50 TooltipAura::TooltipAura() |
| 51 : screen_type_(screen_type), | 51 : widget_(NULL), |
| 52 widget_(NULL), | |
| 53 tooltip_window_(NULL) { | 52 tooltip_window_(NULL) { |
| 54 label_.set_owned_by_client(); | 53 label_.set_owned_by_client(); |
| 55 label_.SetMultiLine(true); | 54 label_.SetMultiLine(true); |
| 56 label_.SetHorizontalAlignment(gfx::ALIGN_TO_HEAD); | 55 label_.SetHorizontalAlignment(gfx::ALIGN_TO_HEAD); |
| 57 | 56 |
| 58 const int kHorizontalPadding = 3; | 57 const int kHorizontalPadding = 3; |
| 59 const int kVerticalPadding = 2; | 58 const int kVerticalPadding = 2; |
| 60 label_.SetBorder(Border::CreateEmptyBorder( | 59 label_.SetBorder(Border::CreateEmptyBorder( |
| 61 kVerticalPadding, kHorizontalPadding, | 60 kVerticalPadding, kHorizontalPadding, |
| 62 kVerticalPadding, kHorizontalPadding)); | 61 kVerticalPadding, kHorizontalPadding)); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 result.append(gfx::ElideText(*l, font_list, available_width, | 134 result.append(gfx::ElideText(*l, font_list, available_width, |
| 136 gfx::ELIDE_TAIL)); | 135 gfx::ELIDE_TAIL)); |
| 137 } else { | 136 } else { |
| 138 *width = std::max(*width, line_width); | 137 *width = std::max(*width, line_width); |
| 139 result.append(*l); | 138 result.append(*l); |
| 140 } | 139 } |
| 141 } | 140 } |
| 142 *text = result; | 141 *text = result; |
| 143 } | 142 } |
| 144 | 143 |
| 145 int TooltipAura::GetMaxWidth(const gfx::Point& location) const { | |
| 146 // TODO(varunjain): implementation duplicated in tooltip_manager_aura. Figure | |
| 147 // out a way to merge. | |
| 148 gfx::Screen* screen = gfx::Screen::GetScreenByType(screen_type_); | |
| 149 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(location).bounds()); | |
| 150 return (display_bounds.width() + 1) / 2; | |
| 151 } | |
| 152 | |
| 153 void TooltipAura::SetTooltipBounds(const gfx::Point& mouse_pos, | 144 void TooltipAura::SetTooltipBounds(const gfx::Point& mouse_pos, |
| 154 const gfx::Size& tooltip_size) { | 145 const gfx::Size& tooltip_size) { |
| 155 gfx::Rect tooltip_rect(mouse_pos, tooltip_size); | 146 gfx::Rect tooltip_rect(mouse_pos, tooltip_size); |
| 156 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY); | 147 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY); |
| 157 gfx::Screen* screen = gfx::Screen::GetScreenByType(screen_type_); | 148 gfx::Screen* screen = gfx::Screen::GetScreenFor(tooltip_window_); |
| 158 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(mouse_pos).bounds()); | 149 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(mouse_pos).bounds()); |
| 159 | 150 |
| 160 // If tooltip is out of bounds on the x axis, we simply shift it | 151 // If tooltip is out of bounds on the x axis, we simply shift it |
| 161 // horizontally by the offset. | 152 // horizontally by the offset. |
| 162 if (tooltip_rect.right() > display_bounds.right()) { | 153 if (tooltip_rect.right() > display_bounds.right()) { |
| 163 int h_offset = tooltip_rect.right() - display_bounds.right(); | 154 int h_offset = tooltip_rect.right() - display_bounds.right(); |
| 164 tooltip_rect.Offset(-h_offset, 0); | 155 tooltip_rect.Offset(-h_offset, 0); |
| 165 } | 156 } |
| 166 | 157 |
| 167 // If tooltip is out of bounds on the y axis, we flip it to appear above the | 158 // If tooltip is out of bounds on the y axis, we flip it to appear above the |
| 168 // mouse cursor instead of below. | 159 // mouse cursor instead of below. |
| 169 if (tooltip_rect.bottom() > display_bounds.bottom()) | 160 if (tooltip_rect.bottom() > display_bounds.bottom()) |
| 170 tooltip_rect.set_y(mouse_pos.y() - tooltip_size.height()); | 161 tooltip_rect.set_y(mouse_pos.y() - tooltip_size.height()); |
| 171 | 162 |
| 172 tooltip_rect.AdjustToFit(display_bounds); | 163 tooltip_rect.AdjustToFit(display_bounds); |
| 173 widget_->SetBounds(tooltip_rect); | 164 widget_->SetBounds(tooltip_rect); |
| 174 } | 165 } |
| 175 | 166 |
| 176 void TooltipAura::DestroyWidget() { | 167 void TooltipAura::DestroyWidget() { |
| 177 if (widget_) { | 168 if (widget_) { |
| 178 widget_->RemoveObserver(this); | 169 widget_->RemoveObserver(this); |
| 179 widget_->Close(); | 170 widget_->Close(); |
| 180 widget_ = NULL; | 171 widget_ = NULL; |
| 181 } | 172 } |
| 182 } | 173 } |
| 183 | 174 |
| 175 int TooltipAura::GetMaxWidth(const gfx::Point& location, |
| 176 aura::Window* context) const { |
| 177 gfx::Screen* screen = gfx::Screen::GetScreenFor(context); |
| 178 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(location).bounds()); |
| 179 return std::min(kTooltipMaxWidthPixels, (display_bounds.width() + 1) / 2); |
| 180 } |
| 181 |
| 184 void TooltipAura::SetText(aura::Window* window, | 182 void TooltipAura::SetText(aura::Window* window, |
| 185 const base::string16& tooltip_text, | 183 const base::string16& tooltip_text, |
| 186 const gfx::Point& location) { | 184 const gfx::Point& location) { |
| 187 tooltip_window_ = window; | 185 tooltip_window_ = window; |
| 188 int max_width = 0; | 186 int max_width = 0; |
| 189 int line_count = 0; | 187 int line_count = 0; |
| 190 base::string16 trimmed_text(tooltip_text); | 188 base::string16 trimmed_text(tooltip_text); |
| 191 TrimTooltipToFit(label_.font_list(), GetMaxWidth(location), &trimmed_text, | 189 TrimTooltipToFit(label_.font_list(), GetMaxWidth(location, window), |
| 192 &max_width, &line_count); | 190 &trimmed_text, &max_width, &line_count); |
| 193 label_.SetText(trimmed_text); | 191 label_.SetText(trimmed_text); |
| 194 | 192 |
| 195 if (!widget_) { | 193 if (!widget_) { |
| 196 widget_ = CreateTooltipWidget(tooltip_window_); | 194 widget_ = CreateTooltipWidget(tooltip_window_); |
| 197 widget_->SetContentsView(&label_); | 195 widget_->SetContentsView(&label_); |
| 198 widget_->AddObserver(this); | 196 widget_->AddObserver(this); |
| 199 } | 197 } |
| 200 | 198 |
| 201 label_.SizeToFit(max_width + label_.GetInsets().width()); | 199 label_.SizeToFit(max_width + label_.GetInsets().width()); |
| 202 SetTooltipBounds(location, label_.size()); | 200 SetTooltipBounds(location, label_.size()); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 230 } | 228 } |
| 231 | 229 |
| 232 void TooltipAura::OnWidgetDestroying(views::Widget* widget) { | 230 void TooltipAura::OnWidgetDestroying(views::Widget* widget) { |
| 233 DCHECK_EQ(widget_, widget); | 231 DCHECK_EQ(widget_, widget); |
| 234 widget_ = NULL; | 232 widget_ = NULL; |
| 235 tooltip_window_ = NULL; | 233 tooltip_window_ = NULL; |
| 236 } | 234 } |
| 237 | 235 |
| 238 } // namespace corewm | 236 } // namespace corewm |
| 239 } // namespace views | 237 } // namespace views |
| OLD | NEW |