Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(286)

Side by Side Diff: ui/views/bubble/bubble_frame_view.cc

Issue 8870003: Merge BorderContentsView into BubbleFrameView; simplify. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove allow_bubble_offscreen, simplify and update tests. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/views/bubble/bubble_frame_view.h ('k') | ui/views/bubble/bubble_frame_view_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/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/views/bubble/border_contents_view.h" 9 #include "ui/gfx/screen.h"
10 #include "ui/views/bubble/bubble_border.h" 10 #include "ui/views/bubble/bubble_border.h"
11 #include "ui/views/layout/fill_layout.h"
12 #include "ui/views/widget/widget.h" 11 #include "ui/views/widget/widget.h"
13 #include "ui/views/window/client_view.h" 12 #include "ui/views/window/client_view.h"
14 13
14 static const int kMargin = 6;
15
16 namespace {
17
18 // Get the |vertical| or horizontal screen overflow of the |window_bounds|.
19 int GetOffScreenLength(const gfx::Rect& monitor_bounds,
20 const gfx::Rect& window_bounds,
21 bool vertical) {
22 if (monitor_bounds.IsEmpty() || monitor_bounds.Contains(window_bounds))
23 return 0;
24
25 // window_bounds
26 // +-------------------------------+
27 // | top |
28 // | +----------------+ |
29 // | left | monitor_bounds | right |
30 // | +----------------+ |
31 // | bottom |
32 // +-------------------------------+
33 if (vertical)
34 return std::max(0, monitor_bounds.y() - window_bounds.y()) +
35 std::max(0, window_bounds.bottom() - monitor_bounds.bottom());
36 return std::max(0, monitor_bounds.x() - window_bounds.x()) +
37 std::max(0, window_bounds.right() - monitor_bounds.right());
38 }
39
40 } // namespace
41
15 namespace views { 42 namespace views {
16 43
17 BubbleFrameView::BubbleFrameView(BubbleBorder::ArrowLocation location, 44 BubbleFrameView::BubbleFrameView(BubbleBorder::ArrowLocation arrow_location,
18 const gfx::Size& client_size, 45 SkColor color)
19 SkColor color, 46 : bubble_border_(NULL),
20 bool allow_bubble_offscreen) 47 content_margins_(kMargin, kMargin, kMargin, kMargin) {
21 : border_contents_(new BorderContentsView()), 48 if (base::i18n::IsRTL())
22 location_(location), 49 arrow_location = BubbleBorder::horizontal_mirror(arrow_location);
23 allow_bubble_offscreen_(allow_bubble_offscreen) { 50 // TODO(alicet): Expose the shadow option in BorderContentsView when we make
24 border_contents_->Init(); 51 // the fullscreen exit bubble use the new bubble code.
25 bubble_border()->set_arrow_location(location_); 52 bubble_border_ = new BubbleBorder(arrow_location, BubbleBorder::NO_SHADOW);
53 set_border(bubble_border_);
54 set_background(new BubbleBackground(bubble_border_));
26 bubble_border()->set_background_color(color); 55 bubble_border()->set_background_color(color);
27 SetLayoutManager(new views::FillLayout());
28 AddChildView(border_contents_);
29 gfx::Rect windows_bounds =
30 GetWindowBoundsForAnchorAndClientSize(gfx::Rect(), client_size);
31 border_contents_->SetBoundsRect(
32 gfx::Rect(gfx::Point(), windows_bounds.size()));
33 SetBoundsRect(windows_bounds);
34 } 56 }
35 57
36 BubbleFrameView::~BubbleFrameView() {} 58 BubbleFrameView::~BubbleFrameView() {}
37 59
38 gfx::Rect BubbleFrameView::GetBoundsForClientView() const { 60 gfx::Rect BubbleFrameView::GetBoundsForClientView() const {
39 gfx::Insets margin; 61 gfx::Insets margin;
40 bubble_border()->GetInsets(&margin); 62 bubble_border()->GetInsets(&margin);
41 margin += border_contents_->content_margins(); 63 margin += content_margins();
42 return gfx::Rect(margin.left(), 64 return gfx::Rect(margin.left(), margin.top(),
43 margin.top(),
44 std::max(width() - margin.width(), 0), 65 std::max(width() - margin.width(), 0),
45 std::max(height() - margin.height(), 0)); 66 std::max(height() - margin.height(), 0));
46 } 67 }
47 68
48 gfx::Rect BubbleFrameView::GetWindowBoundsForClientBounds( 69 gfx::Rect BubbleFrameView::GetWindowBoundsForClientBounds(
49 const gfx::Rect& client_bounds) const { 70 const gfx::Rect& client_bounds) const {
50 // The |client_bounds| origin is the bubble arrow anchor point. 71 return const_cast<BubbleFrameView*>(this)->GetUpdatedWindowBounds(
51 gfx::Rect anchor(client_bounds.origin(), gfx::Size()); 72 gfx::Rect(), client_bounds.size(), false);
alicet1 2011/12/14 21:48:54 why do this one and the one on line 81 try_mirrori
msw 2011/12/14 22:31:09 Because they are using dummy anchor rect values |g
52 // The |client_bounds| size is the bubble client view size.
53 return GetWindowBoundsForAnchorAndClientSize(anchor, client_bounds.size());
54 }
55
56 gfx::Rect BubbleFrameView::GetWindowBoundsForAnchorAndClientSize(
57 const gfx::Rect& anchor,
58 const gfx::Size& client_size) const {
59 gfx::Rect content_bounds;
60 gfx::Rect window_bounds;
61 border_contents_->SizeAndGetBounds(anchor,
62 location_,
63 allow_bubble_offscreen_,
64 client_size,
65 &content_bounds,
66 &window_bounds);
67 return window_bounds;
68 } 73 }
69 74
70 int BubbleFrameView::NonClientHitTest(const gfx::Point& point) { 75 int BubbleFrameView::NonClientHitTest(const gfx::Point& point) {
71 return GetWidget()->client_view()->NonClientHitTest(point); 76 return GetWidget()->client_view()->NonClientHitTest(point);
72 } 77 }
73 78
74 gfx::Size BubbleFrameView::GetPreferredSize() { 79 gfx::Size BubbleFrameView::GetPreferredSize() {
75 Widget* widget = GetWidget(); 80 gfx::Size client_size(GetWidget()->client_view()->GetPreferredSize());
76 gfx::Rect rect(gfx::Point(), widget->client_view()->GetPreferredSize()); 81 return GetUpdatedWindowBounds(gfx::Rect(), client_size, false).size();
77 return widget->non_client_view()->GetWindowBoundsForClientBounds(rect).size();
78 } 82 }
79 83
80 BubbleBorder* BubbleFrameView::bubble_border() const { 84 gfx::Rect BubbleFrameView::GetUpdatedWindowBounds(const gfx::Rect& anchor_rect,
81 return static_cast<BubbleBorder*>(border_contents_->border()); 85 gfx::Size client_size,
86 bool try_mirroring_arrow) {
87 // Give the contents a margin.
88 client_size.Enlarge(content_margins_.width(), content_margins_.height());
89
90 if (try_mirroring_arrow) {
91 // Try to mirror the anchoring if the bubble does not fit on the screen.
92 MirrorArrowIfOffScreen(true, anchor_rect, client_size);
93 MirrorArrowIfOffScreen(false, anchor_rect, client_size);
94 }
95
96 // Calculate the bounds with the arrow in its updated location.
97 return bubble_border_->GetBounds(anchor_rect, client_size);
98 }
99
100 gfx::Rect BubbleFrameView::GetMonitorBounds(const gfx::Rect& rect) {
101 return gfx::Screen::GetMonitorWorkAreaNearestPoint(rect.CenterPoint());
102 }
103
104 void BubbleFrameView::MirrorArrowIfOffScreen(
105 bool vertical,
106 const gfx::Rect& anchor_rect,
107 const gfx::Size& client_size) {
108 // Check if the bounds don't fit on screen.
109 gfx::Rect monitor_rect(GetMonitorBounds(anchor_rect));
110 gfx::Rect window_bounds(bubble_border_->GetBounds(anchor_rect, client_size));
111 if (GetOffScreenLength(monitor_rect, window_bounds, vertical) > 0) {
112 BubbleBorder::ArrowLocation arrow = bubble_border()->arrow_location();
113 // Mirror the arrow and get the new bounds.
114 bubble_border_->set_arrow_location(
115 vertical ? BubbleBorder::vertical_mirror(arrow) :
116 BubbleBorder::horizontal_mirror(arrow));
117 gfx::Rect mirror_bounds =
118 bubble_border_->GetBounds(anchor_rect, client_size);
119 // Restore the original arrow if mirroring doesn't show more of the bubble.
120 if (GetOffScreenLength(monitor_rect, mirror_bounds, vertical) >=
121 GetOffScreenLength(monitor_rect, window_bounds, vertical)) {
122 bubble_border_->set_arrow_location(arrow);
123 }
124 }
82 } 125 }
83 126
84 } // namespace views 127 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/bubble/bubble_frame_view.h ('k') | ui/views/bubble/bubble_frame_view_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698