| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bubble/bubble_frame_view.h" | 5 #include "ui/views/bubble/bubble_frame_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "ui/gfx/screen.h" | 9 #include "ui/gfx/screen.h" |
| 10 #include "ui/views/bubble/bubble_border.h" | 10 #include "ui/views/bubble/bubble_border.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 return GetWidget()->client_view()->NonClientHitTest(point); | 67 return GetWidget()->client_view()->NonClientHitTest(point); |
| 68 } | 68 } |
| 69 | 69 |
| 70 gfx::Size BubbleFrameView::GetPreferredSize() { | 70 gfx::Size BubbleFrameView::GetPreferredSize() { |
| 71 gfx::Size client_size(GetWidget()->client_view()->GetPreferredSize()); | 71 gfx::Size client_size(GetWidget()->client_view()->GetPreferredSize()); |
| 72 return GetUpdatedWindowBounds(gfx::Rect(), client_size, false).size(); | 72 return GetUpdatedWindowBounds(gfx::Rect(), client_size, false).size(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 gfx::Rect BubbleFrameView::GetUpdatedWindowBounds(const gfx::Rect& anchor_rect, | 75 gfx::Rect BubbleFrameView::GetUpdatedWindowBounds(const gfx::Rect& anchor_rect, |
| 76 gfx::Size client_size, | 76 gfx::Size client_size, |
| 77 bool try_mirroring_arrow) { | 77 bool try_fit_bubble) { |
| 78 // Give the contents a margin. | 78 // Give the contents a margin. |
| 79 client_size.Enlarge(content_margins_.width(), content_margins_.height()); | 79 client_size.Enlarge(content_margins_.width(), content_margins_.height()); |
| 80 | 80 |
| 81 if (try_mirroring_arrow) { | 81 if (try_fit_bubble && |
| 82 // Try to mirror the anchoring if the bubble does not fit on the screen. | 82 BubbleBorder::has_arrow(bubble_border_->arrow_location())) { |
| 83 MirrorArrowIfOffScreen(true, anchor_rect, client_size); | 83 if (!bubble_border_->center_arrow()) { |
| 84 MirrorArrowIfOffScreen(false, anchor_rect, client_size); | 84 // Try to mirror the anchoring if the bubble does not fit on the screen. |
| 85 MirrorArrowIfOffScreen(true, anchor_rect, client_size); |
| 86 MirrorArrowIfOffScreen(false, anchor_rect, client_size); |
| 87 } |
| 88 |
| 89 OffsetArrowIfOffScreen(anchor_rect, client_size); |
| 85 } | 90 } |
| 86 | 91 |
| 87 // Calculate the bounds with the arrow in its updated location. | 92 // Calculate the bounds with the arrow in its updated location and offset. |
| 88 return bubble_border_->GetBounds(anchor_rect, client_size); | 93 return bubble_border_->GetBounds(anchor_rect, client_size); |
| 89 } | 94 } |
| 90 | 95 |
| 91 void BubbleFrameView::SetBubbleBorder(BubbleBorder* border) { | 96 void BubbleFrameView::SetBubbleBorder(BubbleBorder* border) { |
| 92 bubble_border_ = border; | 97 bubble_border_ = border; |
| 93 set_border(bubble_border_); | 98 set_border(bubble_border_); |
| 94 | 99 |
| 95 // Update the background, which relies on the border. | 100 // Update the background, which relies on the border. |
| 96 set_background(new views::BubbleBackground(border)); | 101 set_background(new views::BubbleBackground(border)); |
| 97 } | 102 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 117 bubble_border_->GetBounds(anchor_rect, client_size); | 122 bubble_border_->GetBounds(anchor_rect, client_size); |
| 118 // Restore the original arrow if mirroring doesn't show more of the bubble. | 123 // Restore the original arrow if mirroring doesn't show more of the bubble. |
| 119 if (GetOffScreenLength(monitor_rect, mirror_bounds, vertical) >= | 124 if (GetOffScreenLength(monitor_rect, mirror_bounds, vertical) >= |
| 120 GetOffScreenLength(monitor_rect, window_bounds, vertical)) | 125 GetOffScreenLength(monitor_rect, window_bounds, vertical)) |
| 121 bubble_border_->set_arrow_location(arrow); | 126 bubble_border_->set_arrow_location(arrow); |
| 122 else | 127 else |
| 123 SchedulePaint(); | 128 SchedulePaint(); |
| 124 } | 129 } |
| 125 } | 130 } |
| 126 | 131 |
| 132 void BubbleFrameView::OffsetArrowIfOffScreen(const gfx::Rect& anchor_rect, |
| 133 const gfx::Size& client_size) { |
| 134 BubbleBorder::ArrowLocation arrow = bubble_border()->arrow_location(); |
| 135 DCHECK(BubbleBorder::has_arrow(arrow)); |
| 136 |
| 137 // Get desired bubble bounds without adjustment. |
| 138 const int old_adjust = bubble_border_->relative_arrow_offset(); |
| 139 bubble_border_->set_relative_arrow_offset(0); |
| 140 gfx::Rect window_bounds(bubble_border_->GetBounds(anchor_rect, client_size)); |
| 141 |
| 142 gfx::Rect monitor_rect(GetMonitorBounds(anchor_rect)); |
| 143 if (monitor_rect.IsEmpty() || monitor_rect.Contains(window_bounds)) |
| 144 return; |
| 145 |
| 146 // Calculate off screen adjustment |
| 147 const bool is_horizontal = BubbleBorder::is_arrow_on_horizontal(arrow); |
| 148 int offscreen_adjust = 0; |
| 149 if (is_horizontal) { |
| 150 if (window_bounds.x() < monitor_rect.x()) |
| 151 offscreen_adjust = monitor_rect.x() - window_bounds.x(); |
| 152 else if (window_bounds.right() > monitor_rect.right()) |
| 153 offscreen_adjust = monitor_rect.right() - window_bounds.right(); |
| 154 } else { |
| 155 if (window_bounds.y() < monitor_rect.y()) |
| 156 offscreen_adjust = monitor_rect.y() - window_bounds.y(); |
| 157 else if (window_bounds.bottom() > monitor_rect.bottom()) |
| 158 offscreen_adjust = monitor_rect.bottom() - window_bounds.bottom(); |
| 159 } |
| 160 |
| 161 if ((is_horizontal && BubbleBorder::is_arrow_on_left(arrow)) || |
| 162 (!is_horizontal && BubbleBorder::is_arrow_on_top(arrow))) { |
| 163 // For left and top arrow, arrow moves closer to starting edge for positive |
| 164 // adjustment (i.e. negative arrow offset adjustment). Similarly, negative |
| 165 // adjustment (bubble right edge offscreen) means arrow should move further |
| 166 // from starting edge (i.e. positive arrow offset adjustment). |
| 167 offscreen_adjust = -offscreen_adjust; |
| 168 } |
| 169 |
| 170 bubble_border_->set_relative_arrow_offset(offscreen_adjust); |
| 171 if (old_adjust != offscreen_adjust) |
| 172 SchedulePaint(); |
| 173 } |
| 174 |
| 127 } // namespace views | 175 } // namespace views |
| OLD | NEW |