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

Side by Side Diff: chrome/browser/ui/views/constrained_window_frame_simple.cc

Issue 11044020: Make Web Intents picker in Views conform to latest mocks (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Address comments Created 8 years, 2 months 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
OLDNEW
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 "chrome/browser/ui/views/constrained_window_frame_simple.h" 5 #include "chrome/browser/ui/views/constrained_window_frame_simple.h"
6 6
7 #include "chrome/browser/ui/constrained_window.h" 7 #include "chrome/browser/ui/constrained_window.h"
8 #include "chrome/browser/ui/constrained_window_constants.h" 8 #include "chrome/browser/ui/constrained_window_constants.h"
9 #include "chrome/browser/ui/views/constrained_window_views.h" 9 #include "chrome/browser/ui/views/constrained_window_views.h"
10 #include "grit/ui_resources.h" 10 #include "grit/ui_resources.h"
11 #include "grit/chromium_strings.h" 11 #include "grit/chromium_strings.h"
12 #include "grit/generated_resources.h" 12 #include "grit/generated_resources.h"
13 #include "grit/google_chrome_strings.h" 13 #include "grit/google_chrome_strings.h"
14 #include "grit/shared_resources.h" 14 #include "grit/shared_resources.h"
15 #include "grit/theme_resources.h" 15 #include "grit/theme_resources.h"
16 #include "ui/base/hit_test.h" 16 #include "ui/base/hit_test.h"
17 #include "ui/base/resource/resource_bundle.h" 17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/gfx/canvas.h" 18 #include "ui/gfx/canvas.h"
19 #include "ui/gfx/rect.h" 19 #include "ui/gfx/rect.h"
20 #include "ui/gfx/path.h" 20 #include "ui/gfx/path.h"
21 #include "ui/views/background.h" 21 #include "ui/views/background.h"
22 #include "ui/views/controls/label.h" 22 #include "ui/views/controls/label.h"
23 #include "ui/views/controls/button/image_button.h" 23 #include "ui/views/controls/button/image_button.h"
24 #include "ui/views/layout/grid_layout.h" 24 #include "ui/views/layout/grid_layout.h"
25 #include "ui/views/layout/layout_manager.h" 25 #include "ui/views/layout/layout_manager.h"
26 #include "ui/views/layout/layout_constants.h" 26 #include "ui/views/layout/layout_constants.h"
27 #include "ui/views/widget/widget.h" 27 #include "ui/views/widget/widget.h"
28 #include "ui/views/widget/widget_delegate.h" 28 #include "ui/views/widget/widget_delegate.h"
29 29
30 namespace { 30 using namespace views;
Peter Kasting 2012/10/16 02:26:53 This is banned by the Google style guide.
please use gerrit instead 2012/10/16 16:29:38 Changed to explicit statements: using views::Imag
31
32 typedef ConstrainedWindowFrameSimple::HeaderViews HeaderViews;
33
34 // A layout manager that lays out the header view with proper padding,
35 // and sized to the widget's client view.
36 class HeaderLayout : public views::LayoutManager {
37 public:
38 explicit HeaderLayout() {}
39 virtual ~HeaderLayout() {}
40
41 // Overridden from LayoutManager
42 virtual void Layout(views::View* host);
43 virtual gfx::Size GetPreferredSize(views::View* host);
44
45 DISALLOW_COPY_AND_ASSIGN(HeaderLayout);
46 };
47
48 void HeaderLayout::Layout(views::View* host) {
49 if (!host->has_children())
50 return;
51
52 int top_padding = ConstrainedWindowConstants::kCloseButtonPadding;
53 int left_padding = ConstrainedWindowConstants::kHorizontalPadding;
54 int right_padding = ConstrainedWindowConstants::kCloseButtonPadding;
55
56 views::View* header = host->child_at(0);
57 gfx::Size preferred_size = GetPreferredSize(host);
58 int width = preferred_size.width() - left_padding - right_padding;
59 int height = preferred_size.height() - top_padding;
60
61 header->SetBounds(left_padding, top_padding, width, height);
62 }
63
64 gfx::Size HeaderLayout::GetPreferredSize(views::View* host) {
65 int top_padding = ConstrainedWindowConstants::kCloseButtonPadding;
66 int left_padding = ConstrainedWindowConstants::kHorizontalPadding;
67 int right_padding = ConstrainedWindowConstants::kCloseButtonPadding;
68
69 views::View* header = host->child_at(0);
70 gfx::Size header_size = header ? header->GetPreferredSize() : gfx::Size();
71 int width = std::max(host->GetPreferredSize().width(),
72 left_padding + header_size.width() + right_padding);
73 int height = header_size.height() + top_padding;
74
75 return gfx::Size(width, height);
76 }
77
78 } // namespace
79
80 ConstrainedWindowFrameSimple::HeaderViews::HeaderViews(
81 views::View* header,
82 views::Label* title_label,
83 views::Button* close_button)
84 : header(header),
85 title_label(title_label),
86 close_button(close_button) {
87 DCHECK(header);
88 }
89 31
90 ConstrainedWindowFrameSimple::ConstrainedWindowFrameSimple( 32 ConstrainedWindowFrameSimple::ConstrainedWindowFrameSimple(
91 ConstrainedWindowViews* container) 33 ConstrainedWindowViews* container)
92 : container_(container) { 34 : container_(container),
93 container_->set_frame_type(views::Widget::FRAME_TYPE_FORCE_CUSTOM); 35 title_label_(new Label(container->widget_delegate()->GetWindowTitle())),
36 ALLOW_THIS_IN_INITIALIZER_LIST(close_button_(new ImageButton(this))),
37 header_(new View()) {
38 const int kHeaderTopPadding = ConstrainedWindowConstants::kCloseButtonPadding;
Peter Kasting 2012/10/16 02:26:53 Nit: It'd be technically more correct to do: co
please use gerrit instead 2012/10/16 16:29:38 Done.
39 const int kHeaderLeftPadding = ConstrainedWindowConstants::kHorizontalPadding;
40 const int kHeaderRightPadding =
41 ConstrainedWindowConstants::kCloseButtonPadding;
Peter Kasting 2012/10/16 02:26:53 Nit: I'd eliminate these two constants and just in
please use gerrit instead 2012/10/16 16:29:38 Done.
94 42
95 layout_ = new HeaderLayout(); 43 // Need to remove header padding from the title padding to achieve the desired
96 SetLayoutManager(layout_); 44 // result.
45 const int kTitleTopPadding = ConstrainedWindowConstants::kTitleTopPadding -
46 kHeaderTopPadding;
Peter Kasting 2012/10/16 02:26:53 Nit: I'd remove the comment and inline this calcul
please use gerrit instead 2012/10/16 16:29:38 Done.
97 47
98 SetHeaderView(CreateDefaultHeaderView()); 48 container_->set_frame_type(Widget::FRAME_TYPE_FORCE_CUSTOM);
49 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
99 50
100 set_background(views::Background::CreateSolidBackground( 51 title_label_->SetHorizontalAlignment(Label::ALIGN_LEFT);
52 title_label_->SetFont(rb.GetFont(
53 ConstrainedWindowConstants::kTitleFontStyle));
54 title_label_->SetEnabledColor(ConstrainedWindow::GetTextColor());
55 title_label_->set_border(Border::CreateEmptyBorder(
56 kTitleTopPadding, 0, 0, 0));
57
58 close_button_->SetImage(CustomButton::BS_NORMAL,
59 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X));
60 close_button_->SetImage(CustomButton::BS_HOT,
61 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X_HOVER));
62 close_button_->SetImage(CustomButton::BS_PUSHED,
63 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X_PRESSED));
64
65 GridLayout* header_layout = new GridLayout(header_);
66 header_layout->SetInsets(kHeaderTopPadding, kHeaderLeftPadding, 0,
67 kHeaderRightPadding);
68 header_->SetLayoutManager(header_layout);
69 ColumnSet* cs = header_layout->AddColumnSet(0);
70 cs->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1,
71 GridLayout::USE_PREF, 0, 0); // Title.
72 cs->AddPaddingColumn(0, ConstrainedWindowConstants::kCloseButtonPadding);
73 cs->AddColumn(GridLayout::TRAILING, GridLayout::LEADING, 0,
74 GridLayout::USE_PREF, 0, 0); // Close Button.
75 header_layout->StartRow(0, 0);
76 header_layout->AddView(title_label_);
77 header_layout->AddView(close_button_);
78
79 GridLayout* layout = new GridLayout(this);
Peter Kasting 2012/10/16 02:26:53 What does this GridLayout do for us? It seems lik
please use gerrit instead 2012/10/16 16:29:38 Done. This dramatically simplifies the code.
80 SetLayoutManager(layout);
81 layout->AddColumnSet(0)->AddColumn(
82 GridLayout::FILL, GridLayout::LEADING, 1,
83 GridLayout::USE_PREF, 0, 0);
84 layout->StartRow(0, 0);
85 layout->AddView(header_);
86 Layout();
87
88 set_background(Background::CreateSolidBackground(
101 ConstrainedWindow::GetBackgroundColor())); 89 ConstrainedWindow::GetBackgroundColor()));
102
103 set_border(views::Border::CreateEmptyBorder(
104 ConstrainedWindowConstants::kClientTopPadding,
105 ConstrainedWindowConstants::kHorizontalPadding,
106 ConstrainedWindowConstants::kClientBottomPadding,
107 ConstrainedWindowConstants::kHorizontalPadding));
108 } 90 }
109 91
110 ConstrainedWindowFrameSimple::~ConstrainedWindowFrameSimple() { 92 ConstrainedWindowFrameSimple::~ConstrainedWindowFrameSimple() {
111 } 93 }
112 94
113 void ConstrainedWindowFrameSimple::SetHeaderView(HeaderViews* header_views)
114 {
115 RemoveAllChildViews(true);
116
117 header_views_.reset(header_views);
118
119 AddChildView(header_views_->header);
120 }
121
122 HeaderViews* ConstrainedWindowFrameSimple::CreateDefaultHeaderView() {
123 const int kTitleTopPadding = ConstrainedWindowConstants::kTitleTopPadding -
124 ConstrainedWindowConstants::kCloseButtonPadding;
125 const int kTitleLeftPadding = 0;
126 const int kTitleBottomPadding = 0;
127 const int kTitleRightPadding = 0;
128
129 views::View* header_view = new views::View;
130
131 views::GridLayout* grid_layout = new views::GridLayout(header_view);
132 header_view->SetLayoutManager(grid_layout);
133
134 views::ColumnSet* header_cs = grid_layout->AddColumnSet(0);
135 header_cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0,
136 views::GridLayout::USE_PREF, 0, 0); // Title.
137 header_cs->AddPaddingColumn(1, views::kUnrelatedControlHorizontalSpacing);
138 header_cs->AddColumn(views::GridLayout::TRAILING, views::GridLayout::LEADING,
139 0, views::GridLayout::USE_PREF, 0, 0); // Close Button.
140
141 // Header row.
142 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
143 grid_layout->StartRow(0, 0);
144
145 views::Label* title_label = new views::Label();
146 title_label->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
147 title_label->SetFont(rb.GetFont(ConstrainedWindowConstants::kTitleFontStyle));
148 title_label->SetEnabledColor(ConstrainedWindow::GetTextColor());
149 title_label->SetText(container_->widget_delegate()->GetWindowTitle());
150 title_label->set_border(views::Border::CreateEmptyBorder(kTitleTopPadding,
151 kTitleLeftPadding, kTitleBottomPadding, kTitleRightPadding));
152 grid_layout->AddView(title_label);
153
154 views::Button* close_button = CreateCloseButton();
155 grid_layout->AddView(close_button);
156
157 return new HeaderViews(header_view, title_label, close_button);
158 }
159
160 gfx::Rect ConstrainedWindowFrameSimple::GetBoundsForClientView() const { 95 gfx::Rect ConstrainedWindowFrameSimple::GetBoundsForClientView() const {
161 gfx::Rect bounds(GetContentsBounds()); 96 gfx::Rect bounds(GetContentsBounds());
162 if (header_views_->header)
163 bounds.Inset(0, header_views_->header->GetPreferredSize().height(), 0, 0);
164 return bounds; 97 return bounds;
165 } 98 }
166 99
167 gfx::Rect ConstrainedWindowFrameSimple::GetWindowBoundsForClientBounds( 100 gfx::Rect ConstrainedWindowFrameSimple::GetWindowBoundsForClientBounds(
168 const gfx::Rect& client_bounds) const { 101 const gfx::Rect& client_bounds) const {
169 gfx::Rect bounds(client_bounds); 102 gfx::Rect bounds(client_bounds);
170 bounds.Inset(-GetInsets()); 103 bounds.Inset(-GetInsets());
171 if (header_views_->header) 104 bounds.set_width(std::max(bounds.width(),
172 bounds.Inset(0, -header_views_->header->GetPreferredSize().height(), 0, 0); 105 header_->GetPreferredSize().width()));
173 return bounds; 106 return bounds;
174 } 107 }
175 108
176 int ConstrainedWindowFrameSimple::NonClientHitTest(const gfx::Point& point) { 109 int ConstrainedWindowFrameSimple::NonClientHitTest(const gfx::Point& point) {
177 if (!bounds().Contains(point)) 110 if (!bounds().Contains(point))
178 return HTNOWHERE; 111 return HTNOWHERE;
179 return HTCLIENT; 112 return HTCLIENT;
180 } 113 }
181 114
182 void ConstrainedWindowFrameSimple::GetWindowMask(const gfx::Size& size, 115 void ConstrainedWindowFrameSimple::GetWindowMask(const gfx::Size& size,
(...skipping 14 matching lines...) Expand all
197 window_mask->addRoundRect(rect, radii); 130 window_mask->addRoundRect(rect, radii);
198 } 131 }
199 132
200 void ConstrainedWindowFrameSimple::ResetWindowControls() { 133 void ConstrainedWindowFrameSimple::ResetWindowControls() {
201 } 134 }
202 135
203 void ConstrainedWindowFrameSimple::UpdateWindowIcon() { 136 void ConstrainedWindowFrameSimple::UpdateWindowIcon() {
204 } 137 }
205 138
206 void ConstrainedWindowFrameSimple::UpdateWindowTitle() { 139 void ConstrainedWindowFrameSimple::UpdateWindowTitle() {
207 if (!header_views_->title_label) 140 if (!title_label_)
208 return; 141 return;
209 142
210 string16 text = container_->widget_delegate()->GetWindowTitle(); 143 string16 text = container_->widget_delegate()->GetWindowTitle();
211 header_views_->title_label->SetText(text); 144 title_label_->SetText(text);
145 }
146
147 void ConstrainedWindowFrameSimple::OnBoundsChanged(
148 const gfx::Rect& previous_bounds) {
149 gfx::Rect header_bounds(header_->bounds());
150 header_bounds.set_width(std::max(header_->GetPreferredSize().width(),
151 bounds().width()));
152 header_->SetBounds(header_bounds.x(), header_bounds.y(),
153 header_bounds.width(), header_bounds.height());
154 NonClientFrameView::OnBoundsChanged(previous_bounds);
212 } 155 }
213 156
214 gfx::Size ConstrainedWindowFrameSimple::GetPreferredSize() { 157 gfx::Size ConstrainedWindowFrameSimple::GetPreferredSize() {
215 return container_->non_client_view()->GetWindowBoundsForClientBounds( 158 return GetWindowBoundsForClientBounds(
216 gfx::Rect(container_->client_view()->GetPreferredSize())).size(); 159 gfx::Rect(container_->client_view()->GetPreferredSize())).size();
217 } 160 }
218 161
219 void ConstrainedWindowFrameSimple::ButtonPressed(views::Button* sender, 162 void ConstrainedWindowFrameSimple::ButtonPressed(Button* sender,
220 const ui::Event& event) { 163 const ui::Event& event) {
221 if (header_views_->close_button && sender == header_views_->close_button) 164 if (sender == close_button_)
222 sender->GetWidget()->Close(); 165 sender->GetWidget()->Close();
223 } 166 }
224
225 views::ImageButton* ConstrainedWindowFrameSimple::CreateCloseButton() {
226 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
227 views::ImageButton* close_button = new views::ImageButton(this);
228 close_button->SetImage(views::CustomButton::BS_NORMAL,
229 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X));
230 close_button->SetImage(views::CustomButton::BS_HOT,
231 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X_HOVER));
232 close_button->SetImage(views::CustomButton::BS_PUSHED,
233 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X_PRESSED));
234 return close_button;
235 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698