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 21 matching lines...) Expand all Loading... |
32 return std::max(0, monitor_bounds.y() - window_bounds.y()) + | 32 return std::max(0, monitor_bounds.y() - window_bounds.y()) + |
33 std::max(0, window_bounds.bottom() - monitor_bounds.bottom()); | 33 std::max(0, window_bounds.bottom() - monitor_bounds.bottom()); |
34 return std::max(0, monitor_bounds.x() - window_bounds.x()) + | 34 return std::max(0, monitor_bounds.x() - window_bounds.x()) + |
35 std::max(0, window_bounds.right() - monitor_bounds.right()); | 35 std::max(0, window_bounds.right() - monitor_bounds.right()); |
36 } | 36 } |
37 | 37 |
38 } // namespace | 38 } // namespace |
39 | 39 |
40 namespace views { | 40 namespace views { |
41 | 41 |
42 BubbleFrameView::BubbleFrameView(const gfx::Insets& margins, | 42 BubbleFrameView::BubbleFrameView(const gfx::Insets& content_margins) |
43 BubbleBorder* border) | 43 : bubble_border_(NULL), |
44 : bubble_border_(border), | 44 content_margins_(content_margins) { |
45 content_margins_(margins) { | |
46 set_border(bubble_border_); | |
47 } | 45 } |
48 | 46 |
49 BubbleFrameView::~BubbleFrameView() {} | 47 BubbleFrameView::~BubbleFrameView() {} |
50 | 48 |
51 gfx::Rect BubbleFrameView::GetBoundsForClientView() const { | 49 gfx::Rect BubbleFrameView::GetBoundsForClientView() const { |
52 gfx::Rect client_bounds = GetLocalBounds(); | 50 gfx::Rect client_bounds = GetLocalBounds(); |
53 client_bounds.Inset(border()->GetInsets()); | 51 client_bounds.Inset(border()->GetInsets()); |
54 client_bounds.Inset(content_margins()); | 52 client_bounds.Inset(content_margins()); |
55 return client_bounds; | 53 return client_bounds; |
56 } | 54 } |
57 | 55 |
58 gfx::Rect BubbleFrameView::GetWindowBoundsForClientBounds( | 56 gfx::Rect BubbleFrameView::GetWindowBoundsForClientBounds( |
59 const gfx::Rect& client_bounds) const { | 57 const gfx::Rect& client_bounds) const { |
60 return const_cast<BubbleFrameView*>(this)->GetUpdatedWindowBounds( | 58 return const_cast<BubbleFrameView*>(this)->GetUpdatedWindowBounds( |
61 gfx::Rect(), client_bounds.size(), false); | 59 gfx::Rect(), client_bounds.size(), false); |
62 } | 60 } |
63 | 61 |
64 int BubbleFrameView::NonClientHitTest(const gfx::Point& point) { | 62 int BubbleFrameView::NonClientHitTest(const gfx::Point& point) { |
65 return GetWidget()->client_view()->NonClientHitTest(point); | 63 return GetWidget()->client_view()->NonClientHitTest(point); |
66 } | 64 } |
67 | 65 |
68 gfx::Size BubbleFrameView::GetPreferredSize() { | 66 gfx::Size BubbleFrameView::GetPreferredSize() { |
69 gfx::Size client_size(GetWidget()->client_view()->GetPreferredSize()); | 67 gfx::Size client_size(GetWidget()->client_view()->GetPreferredSize()); |
70 return GetUpdatedWindowBounds(gfx::Rect(), client_size, false).size(); | 68 return GetUpdatedWindowBounds(gfx::Rect(), client_size, false).size(); |
71 } | 69 } |
72 | 70 |
| 71 void BubbleFrameView::SetBubbleBorder(BubbleBorder* border) { |
| 72 bubble_border_ = border; |
| 73 set_border(bubble_border_); |
| 74 |
| 75 // Update the background, which relies on the border. |
| 76 set_background(new views::BubbleBackground(border)); |
| 77 } |
| 78 |
73 gfx::Rect BubbleFrameView::GetUpdatedWindowBounds(const gfx::Rect& anchor_rect, | 79 gfx::Rect BubbleFrameView::GetUpdatedWindowBounds(const gfx::Rect& anchor_rect, |
74 gfx::Size client_size, | 80 gfx::Size client_size, |
75 bool adjust_if_offscreen) { | 81 bool adjust_if_offscreen) { |
76 // Give the contents a margin. | 82 // Give the contents a margin. |
77 client_size.Enlarge(content_margins_.width(), content_margins_.height()); | 83 client_size.Enlarge(content_margins_.width(), content_margins_.height()); |
78 | 84 |
79 const BubbleBorder::ArrowLocation arrow = bubble_border_->arrow_location(); | 85 const BubbleBorder::ArrowLocation arrow = bubble_border_->arrow_location(); |
80 if (adjust_if_offscreen && BubbleBorder::has_arrow(arrow)) { | 86 if (adjust_if_offscreen && BubbleBorder::has_arrow(arrow)) { |
81 if (!bubble_border_->is_arrow_at_center(arrow)) { | 87 if (!bubble_border_->is_arrow_at_center(arrow)) { |
82 // Try to mirror the anchoring if the bubble does not fit on the screen. | 88 // Try to mirror the anchoring if the bubble does not fit on the screen. |
83 MirrorArrowIfOffScreen(true, anchor_rect, client_size); | 89 MirrorArrowIfOffScreen(true, anchor_rect, client_size); |
84 MirrorArrowIfOffScreen(false, anchor_rect, client_size); | 90 MirrorArrowIfOffScreen(false, anchor_rect, client_size); |
85 } else { | 91 } else { |
86 OffsetArrowIfOffScreen(anchor_rect, client_size); | 92 OffsetArrowIfOffScreen(anchor_rect, client_size); |
87 } | 93 } |
88 } | 94 } |
89 | 95 |
90 // Calculate the bounds with the arrow in its updated location and offset. | 96 // Calculate the bounds with the arrow in its updated location and offset. |
91 return bubble_border_->GetBounds(anchor_rect, client_size); | 97 return bubble_border_->GetBounds(anchor_rect, client_size); |
92 } | 98 } |
93 | 99 |
94 void BubbleFrameView::SetBubbleBorder(BubbleBorder* border) { | |
95 bubble_border_ = border; | |
96 set_border(bubble_border_); | |
97 | |
98 // Update the background, which relies on the border. | |
99 set_background(new views::BubbleBackground(border)); | |
100 } | |
101 | |
102 gfx::Rect BubbleFrameView::GetMonitorBounds(const gfx::Rect& rect) { | 100 gfx::Rect BubbleFrameView::GetMonitorBounds(const gfx::Rect& rect) { |
103 // TODO(scottmg): Native is wrong. http://crbug.com/133312 | 101 // TODO(scottmg): Native is wrong. http://crbug.com/133312 |
104 return gfx::Screen::GetNativeScreen()->GetDisplayNearestPoint( | 102 return gfx::Screen::GetNativeScreen()->GetDisplayNearestPoint( |
105 rect.CenterPoint()).work_area(); | 103 rect.CenterPoint()).work_area(); |
106 } | 104 } |
107 | 105 |
108 void BubbleFrameView::MirrorArrowIfOffScreen( | 106 void BubbleFrameView::MirrorArrowIfOffScreen( |
109 bool vertical, | 107 bool vertical, |
110 const gfx::Rect& anchor_rect, | 108 const gfx::Rect& anchor_rect, |
111 const gfx::Size& client_size) { | 109 const gfx::Size& client_size) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 // |offscreen_adjust|, e.g. positive |offscreen_adjust| means bubble | 159 // |offscreen_adjust|, e.g. positive |offscreen_adjust| means bubble |
162 // window needs to be moved to the right and that means we need to move arrow | 160 // window needs to be moved to the right and that means we need to move arrow |
163 // to the left, and that means negative offset. | 161 // to the left, and that means negative offset. |
164 bubble_border_->set_arrow_offset( | 162 bubble_border_->set_arrow_offset( |
165 bubble_border_->GetArrowOffset(window_bounds.size()) - offscreen_adjust); | 163 bubble_border_->GetArrowOffset(window_bounds.size()) - offscreen_adjust); |
166 if (offscreen_adjust) | 164 if (offscreen_adjust) |
167 SchedulePaint(); | 165 SchedulePaint(); |
168 } | 166 } |
169 | 167 |
170 } // namespace views | 168 } // namespace views |
OLD | NEW |