| 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/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "ui/aura/window.h" | 10 #include "ui/aura/window.h" |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 } | 131 } |
| 132 | 132 |
| 133 TooltipAura::~TooltipAura() { | 133 TooltipAura::~TooltipAura() { |
| 134 DestroyWidget(); | 134 DestroyWidget(); |
| 135 } | 135 } |
| 136 | 136 |
| 137 void TooltipAura::SetTooltipBounds(const gfx::Point& mouse_pos, | 137 void TooltipAura::SetTooltipBounds(const gfx::Point& mouse_pos, |
| 138 const gfx::Size& tooltip_size) { | 138 const gfx::Size& tooltip_size) { |
| 139 gfx::Rect tooltip_rect(mouse_pos, tooltip_size); | 139 gfx::Rect tooltip_rect(mouse_pos, tooltip_size); |
| 140 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY); | 140 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY); |
| 141 gfx::Screen* screen = gfx::Screen::GetScreenFor(tooltip_window_); | 141 gfx::Screen* screen = gfx::Screen::GetScreen(); |
| 142 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(mouse_pos).bounds()); | 142 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(mouse_pos).bounds()); |
| 143 | 143 |
| 144 // If tooltip is out of bounds on the x axis, we simply shift it | 144 // If tooltip is out of bounds on the x axis, we simply shift it |
| 145 // horizontally by the offset. | 145 // horizontally by the offset. |
| 146 if (tooltip_rect.right() > display_bounds.right()) { | 146 if (tooltip_rect.right() > display_bounds.right()) { |
| 147 int h_offset = tooltip_rect.right() - display_bounds.right(); | 147 int h_offset = tooltip_rect.right() - display_bounds.right(); |
| 148 tooltip_rect.Offset(-h_offset, 0); | 148 tooltip_rect.Offset(-h_offset, 0); |
| 149 } | 149 } |
| 150 | 150 |
| 151 // If tooltip is out of bounds on the y axis, we flip it to appear above the | 151 // If tooltip is out of bounds on the y axis, we flip it to appear above the |
| 152 // mouse cursor instead of below. | 152 // mouse cursor instead of below. |
| 153 if (tooltip_rect.bottom() > display_bounds.bottom()) | 153 if (tooltip_rect.bottom() > display_bounds.bottom()) |
| 154 tooltip_rect.set_y(mouse_pos.y() - tooltip_size.height()); | 154 tooltip_rect.set_y(mouse_pos.y() - tooltip_size.height()); |
| 155 | 155 |
| 156 tooltip_rect.AdjustToFit(display_bounds); | 156 tooltip_rect.AdjustToFit(display_bounds); |
| 157 widget_->SetBounds(tooltip_rect); | 157 widget_->SetBounds(tooltip_rect); |
| 158 } | 158 } |
| 159 | 159 |
| 160 void TooltipAura::DestroyWidget() { | 160 void TooltipAura::DestroyWidget() { |
| 161 if (widget_) { | 161 if (widget_) { |
| 162 widget_->RemoveObserver(this); | 162 widget_->RemoveObserver(this); |
| 163 widget_->Close(); | 163 widget_->Close(); |
| 164 widget_ = NULL; | 164 widget_ = NULL; |
| 165 } | 165 } |
| 166 } | 166 } |
| 167 | 167 |
| 168 int TooltipAura::GetMaxWidth(const gfx::Point& location, | 168 int TooltipAura::GetMaxWidth(const gfx::Point& location) const { |
| 169 aura::Window* context) const { | 169 gfx::Screen* screen = gfx::Screen::GetScreen(); |
| 170 gfx::Screen* screen = gfx::Screen::GetScreenFor(context); | |
| 171 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(location).bounds()); | 170 gfx::Rect display_bounds(screen->GetDisplayNearestPoint(location).bounds()); |
| 172 return std::min(kTooltipMaxWidthPixels, (display_bounds.width() + 1) / 2); | 171 return std::min(kTooltipMaxWidthPixels, (display_bounds.width() + 1) / 2); |
| 173 } | 172 } |
| 174 | 173 |
| 175 void TooltipAura::SetText(aura::Window* window, | 174 void TooltipAura::SetText(aura::Window* window, |
| 176 const base::string16& tooltip_text, | 175 const base::string16& tooltip_text, |
| 177 const gfx::Point& location) { | 176 const gfx::Point& location) { |
| 178 tooltip_window_ = window; | 177 tooltip_window_ = window; |
| 179 tooltip_view_->SetMaxWidth(GetMaxWidth(location, window)); | 178 tooltip_view_->SetMaxWidth(GetMaxWidth(location)); |
| 180 tooltip_view_->SetText(tooltip_text); | 179 tooltip_view_->SetText(tooltip_text); |
| 181 | 180 |
| 182 if (!widget_) { | 181 if (!widget_) { |
| 183 widget_ = CreateTooltipWidget(tooltip_window_); | 182 widget_ = CreateTooltipWidget(tooltip_window_); |
| 184 widget_->SetContentsView(tooltip_view_.get()); | 183 widget_->SetContentsView(tooltip_view_.get()); |
| 185 widget_->AddObserver(this); | 184 widget_->AddObserver(this); |
| 186 } | 185 } |
| 187 | 186 |
| 188 SetTooltipBounds(location, tooltip_view_->GetPreferredSize()); | 187 SetTooltipBounds(location, tooltip_view_->GetPreferredSize()); |
| 189 | 188 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 214 } | 213 } |
| 215 | 214 |
| 216 void TooltipAura::OnWidgetDestroying(views::Widget* widget) { | 215 void TooltipAura::OnWidgetDestroying(views::Widget* widget) { |
| 217 DCHECK_EQ(widget_, widget); | 216 DCHECK_EQ(widget_, widget); |
| 218 widget_ = NULL; | 217 widget_ = NULL; |
| 219 tooltip_window_ = NULL; | 218 tooltip_window_ = NULL; |
| 220 } | 219 } |
| 221 | 220 |
| 222 } // namespace corewm | 221 } // namespace corewm |
| 223 } // namespace views | 222 } // namespace views |
| OLD | NEW |