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 "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 views::ImageButton; |
31 | 31 using views::Label; |
32 typedef ConstrainedWindowFrameSimple::HeaderViews HeaderViews; | 32 using views::View; |
Peter Kasting
2012/10/16 17:28:59
Nit: These are perfectly legal, but I normally avo
Ben Goodger (Google)
2012/10/16 18:07:56
+1. I'd prefer not to do this if you're prefixing
please use gerrit instead
2012/10/16 19:12:37
Removed. Done.
please use gerrit instead
2012/10/16 19:12:37
Roger that. Done.
| |
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 | 33 |
90 ConstrainedWindowFrameSimple::ConstrainedWindowFrameSimple( | 34 ConstrainedWindowFrameSimple::ConstrainedWindowFrameSimple( |
91 ConstrainedWindowViews* container) | 35 ConstrainedWindowViews* container) |
92 : container_(container) { | 36 : container_(container), |
37 title_label_(new Label(container->widget_delegate()->GetWindowTitle())), | |
38 ALLOW_THIS_IN_INITIALIZER_LIST(close_button_(new ImageButton(this))), | |
39 min_width_(0) { | |
93 container_->set_frame_type(views::Widget::FRAME_TYPE_FORCE_CUSTOM); | 40 container_->set_frame_type(views::Widget::FRAME_TYPE_FORCE_CUSTOM); |
94 | 41 |
95 layout_ = new HeaderLayout(); | 42 views::GridLayout* layout = new views::GridLayout(this); |
96 SetLayoutManager(layout_); | 43 const int kHeaderTopPadding = std::min( |
44 ConstrainedWindowConstants::kCloseButtonPadding, | |
45 ConstrainedWindowConstants::kTitleTopPadding); | |
46 layout->SetInsets(kHeaderTopPadding, | |
47 ConstrainedWindowConstants::kHorizontalPadding, | |
48 0, | |
49 ConstrainedWindowConstants::kCloseButtonPadding); | |
50 SetLayoutManager(layout); | |
51 views::ColumnSet* cs = layout->AddColumnSet(0); | |
52 cs->AddColumn(views::GridLayout::FILL, views::GridLayout::LEADING, 1, | |
53 views::GridLayout::USE_PREF, 0, 0); // Title. | |
54 cs->AddPaddingColumn(0, ConstrainedWindowConstants::kCloseButtonPadding); | |
55 cs->AddColumn(views::GridLayout::TRAILING, views::GridLayout::LEADING, 0, | |
56 views::GridLayout::USE_PREF, 0, 0); // Close Button. | |
97 | 57 |
98 SetHeaderView(CreateDefaultHeaderView()); | 58 layout->StartRow(0, 0); |
59 | |
60 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
61 title_label_->SetFont(rb.GetFont( | |
62 ConstrainedWindowConstants::kTitleFontStyle)); | |
63 title_label_->SetHorizontalAlignment(Label::ALIGN_LEFT); | |
64 title_label_->SetEnabledColor(ConstrainedWindow::GetTextColor()); | |
65 title_label_->set_border(views::Border::CreateEmptyBorder( | |
66 ConstrainedWindowConstants::kTitleTopPadding - kHeaderTopPadding, | |
67 0, 0, 0)); | |
68 layout->AddView(title_label_); | |
69 | |
70 close_button_->SetImage(views::CustomButton::BS_NORMAL, | |
71 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X)); | |
72 close_button_->SetImage(views::CustomButton::BS_HOT, | |
73 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X_HOVER)); | |
74 close_button_->SetImage(views::CustomButton::BS_PUSHED, | |
75 rb.GetImageSkiaNamed(IDR_SHARED_IMAGES_X_PRESSED)); | |
76 close_button_->set_border(views::Border::CreateEmptyBorder( | |
77 ConstrainedWindowConstants::kCloseButtonPadding - kHeaderTopPadding, | |
78 0, 0, 0)); | |
79 layout->AddView(close_button_); | |
80 | |
81 Layout(); | |
Peter Kasting
2012/10/16 17:28:59
Tiny nit: It seems less error-prone to make this t
Ben Goodger (Google)
2012/10/16 18:07:56
My question is why do you have to explicitly call
please use gerrit instead
2012/10/16 19:12:37
Removed as ben@ suggested. It still works correctl
please use gerrit instead
2012/10/16 19:12:37
Removed. It still works correctly. Done.
| |
82 | |
83 min_width_ = ConstrainedWindowConstants::kHorizontalPadding + | |
Peter Kasting
2012/10/16 17:28:59
Nit: Seems like this doesn't need to be a member,
please use gerrit instead
2012/10/16 19:12:37
Moved the calculation and removed the member. Done
| |
84 close_button_->GetPreferredSize().width() + | |
85 2 * ConstrainedWindowConstants::kCloseButtonPadding; | |
99 | 86 |
100 set_background(views::Background::CreateSolidBackground( | 87 set_background(views::Background::CreateSolidBackground( |
101 ConstrainedWindow::GetBackgroundColor())); | 88 ConstrainedWindow::GetBackgroundColor())); |
102 | 89 |
103 set_border(views::Border::CreateEmptyBorder( | 90 switch (container->chrome_style_client_insets()) { |
Peter Kasting
2012/10/16 17:28:59
Nit: I'd just do "if (container->chrome_style_clie
please use gerrit instead
2012/10/16 19:12:37
Using if instead of switch statement. Done.
| |
104 ConstrainedWindowConstants::kClientTopPadding, | 91 case ConstrainedWindowViews::DEFAULT_INSETS: |
105 ConstrainedWindowConstants::kHorizontalPadding, | 92 set_border(views::Border::CreateEmptyBorder( |
106 ConstrainedWindowConstants::kClientBottomPadding, | 93 ConstrainedWindowConstants::kClientTopPadding + |
107 ConstrainedWindowConstants::kHorizontalPadding)); | 94 std::max(close_button_->GetPreferredSize().height(), |
95 title_label_->GetPreferredSize().height()), | |
96 ConstrainedWindowConstants::kHorizontalPadding, | |
Peter Kasting
2012/10/16 17:28:59
Are the values in this CreateEmptyBorder() call ad
please use gerrit instead
2012/10/16 19:12:37
Added kHeaderTopPadding to the calculation to make
Peter Kasting
2012/10/16 19:20:04
Yeah, that's what I was thinking: presumably NO_IN
| |
97 ConstrainedWindowConstants::kClientBottomPadding, | |
98 ConstrainedWindowConstants::kHorizontalPadding)); | |
99 break; | |
100 case ConstrainedWindowViews::NO_INSETS: | |
101 break; | |
102 default: | |
103 NOTREACHED(); | |
104 } | |
108 } | 105 } |
109 | 106 |
110 ConstrainedWindowFrameSimple::~ConstrainedWindowFrameSimple() { | 107 ConstrainedWindowFrameSimple::~ConstrainedWindowFrameSimple() { |
111 } | 108 } |
112 | 109 |
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 { | 110 gfx::Rect ConstrainedWindowFrameSimple::GetBoundsForClientView() const { |
161 gfx::Rect bounds(GetContentsBounds()); | 111 return GetContentsBounds(); |
162 if (header_views_->header) | |
163 bounds.Inset(0, header_views_->header->GetPreferredSize().height(), 0, 0); | |
164 return bounds; | |
165 } | 112 } |
166 | 113 |
167 gfx::Rect ConstrainedWindowFrameSimple::GetWindowBoundsForClientBounds( | 114 gfx::Rect ConstrainedWindowFrameSimple::GetWindowBoundsForClientBounds( |
168 const gfx::Rect& client_bounds) const { | 115 const gfx::Rect& client_bounds) const { |
169 gfx::Rect bounds(client_bounds); | 116 gfx::Rect bounds(client_bounds); |
170 bounds.Inset(-GetInsets()); | 117 bounds.Inset(-GetInsets()); |
171 if (header_views_->header) | 118 bounds.set_width(std::max( |
172 bounds.Inset(0, -header_views_->header->GetPreferredSize().height(), 0, 0); | 119 bounds.width(), |
120 min_width_ + title_label_->GetPreferredSize().width())); | |
173 return bounds; | 121 return bounds; |
174 } | 122 } |
175 | 123 |
176 int ConstrainedWindowFrameSimple::NonClientHitTest(const gfx::Point& point) { | 124 int ConstrainedWindowFrameSimple::NonClientHitTest(const gfx::Point& point) { |
177 if (!bounds().Contains(point)) | 125 if (!bounds().Contains(point)) |
178 return HTNOWHERE; | 126 return HTNOWHERE; |
179 return HTCLIENT; | 127 return HTCLIENT; |
180 } | 128 } |
181 | 129 |
182 void ConstrainedWindowFrameSimple::GetWindowMask(const gfx::Size& size, | 130 void ConstrainedWindowFrameSimple::GetWindowMask(const gfx::Size& size, |
(...skipping 14 matching lines...) Expand all Loading... | |
197 window_mask->addRoundRect(rect, radii); | 145 window_mask->addRoundRect(rect, radii); |
198 } | 146 } |
199 | 147 |
200 void ConstrainedWindowFrameSimple::ResetWindowControls() { | 148 void ConstrainedWindowFrameSimple::ResetWindowControls() { |
201 } | 149 } |
202 | 150 |
203 void ConstrainedWindowFrameSimple::UpdateWindowIcon() { | 151 void ConstrainedWindowFrameSimple::UpdateWindowIcon() { |
204 } | 152 } |
205 | 153 |
206 void ConstrainedWindowFrameSimple::UpdateWindowTitle() { | 154 void ConstrainedWindowFrameSimple::UpdateWindowTitle() { |
207 if (!header_views_->title_label) | 155 title_label_->SetText(container_->widget_delegate()->GetWindowTitle()); |
208 return; | |
209 | |
210 string16 text = container_->widget_delegate()->GetWindowTitle(); | |
211 header_views_->title_label->SetText(text); | |
212 } | 156 } |
213 | 157 |
214 gfx::Size ConstrainedWindowFrameSimple::GetPreferredSize() { | 158 gfx::Size ConstrainedWindowFrameSimple::GetPreferredSize() { |
215 return container_->non_client_view()->GetWindowBoundsForClientBounds( | 159 return GetWindowBoundsForClientBounds( |
216 gfx::Rect(container_->client_view()->GetPreferredSize())).size(); | 160 gfx::Rect(container_->client_view()->GetPreferredSize())).size(); |
217 } | 161 } |
218 | 162 |
219 void ConstrainedWindowFrameSimple::ButtonPressed(views::Button* sender, | 163 void ConstrainedWindowFrameSimple::ButtonPressed(views::Button* sender, |
220 const ui::Event& event) { | 164 const ui::Event& event) { |
221 if (header_views_->close_button && sender == header_views_->close_button) | 165 if (sender == close_button_) |
222 sender->GetWidget()->Close(); | 166 sender->GetWidget()->Close(); |
223 } | 167 } |
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 } | |
OLD | NEW |