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

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

Issue 2134293002: Adding a ScrollView for IntentPickerBubbleView (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moving from friend to ForTesting() methods Created 4 years, 5 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/intent_picker_bubble_view.h" 5 #include "chrome/browser/ui/views/intent_picker_bubble_view.h"
6 6
7 #include <algorithm>
8
9 #include "base/bind.h" 7 #include "base/bind.h"
10 #include "base/logging.h" 8 #include "base/logging.h"
11 #include "base/strings/string_piece.h" 9 #include "base/strings/string_piece.h"
12 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/ui/browser_finder.h" 11 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/browser/ui/views/frame/browser_view.h" 12 #include "chrome/browser/ui/views/frame/browser_view.h"
15 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" 13 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
16 #include "chrome/grit/generated_resources.h" 14 #include "chrome/grit/generated_resources.h"
17 #include "content/public/browser/navigation_handle.h" 15 #include "content/public/browser/navigation_handle.h"
18 #include "third_party/skia/include/core/SkColor.h" 16 #include "third_party/skia/include/core/SkColor.h"
19 #include "ui/base/l10n/l10n_util.h" 17 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/gfx/canvas.h" 18 #include "ui/gfx/canvas.h"
19 #include "ui/views/border.h"
21 #include "ui/views/controls/button/image_button.h" 20 #include "ui/views/controls/button/image_button.h"
21 #include "ui/views/controls/scroll_view.h"
22 #include "ui/views/controls/scrollbar/overlay_scroll_bar.h"
22 #include "ui/views/layout/box_layout.h" 23 #include "ui/views/layout/box_layout.h"
23 #include "ui/views/layout/grid_layout.h" 24 #include "ui/views/layout/grid_layout.h"
24 #include "ui/views/window/dialog_client_view.h" 25 #include "ui/views/window/dialog_client_view.h"
25 26
27 namespace content {
28 class WebContents;
29 } // namespace content
30
26 namespace { 31 namespace {
27 32
28 // TODO(djacobo): Add a scroll bar and remove kMaxAppResults. 33 // Using |kMaxAppResults| as a measure of how many apps we want to show.
29 // Discriminating app results when the list is longer than |kMaxAppResults| 34 constexpr size_t kMaxAppResults = arc::ArcNavigationThrottle::kMaxAppResults;
30 constexpr size_t kMaxAppResults = 3;
31 // Main components sizes 35 // Main components sizes
32 constexpr int kRowHeight = 40; 36 constexpr int kRowHeight = 40;
33 constexpr int kMaxWidth = 320; 37 constexpr int kMaxWidth = 320;
34 constexpr int kHeaderHeight = 60; 38 constexpr int kHeaderHeight = 60;
35 constexpr int kFooterHeight = 68; 39 constexpr int kFooterHeight = 68;
36 // Inter components padding 40 // Inter components padding
37 constexpr int kButtonSeparation = 8; 41 constexpr int kButtonSeparation = 8;
38 constexpr int kLabelImageSeparation = 12; 42 constexpr int kLabelImageSeparation = 12;
39 43
40 // UI position wrt the Top Container 44 // UI position wrt the Top Container
41 constexpr int kTopContainerMerge = 3; 45 constexpr int kTopContainerMerge = 3;
42 constexpr int kAppTagNoneSelected = -1; 46 constexpr size_t kAppTagNoneSelected = -1;
Luis Héctor Chávez 2016/07/20 22:16:38 size_t is unsigned. Maybe ssize_t?
djacobo_ 2016/07/21 16:04:11 This variable is used together with the enum Close
43 47
44 // Arbitrary negative values to use as tags on the |always_button_| and 48 // Arbitrary negative values to use as tags on the |always_button_| and
45 // |just_once_button_|. These are negative to differentiate from the app's tags 49 // |just_once_button_|. These are negative to differentiate from the app's tags
46 // which are always >= 0. 50 // which are always >= 0.
47 enum class Option : int { ALWAYS = -2, JUST_ONCE }; 51 enum class Option : int { ALWAYS = -2, JUST_ONCE };
48 52
49 } // namespace 53 } // namespace
50 54
51 // static 55 // static
52 void IntentPickerBubbleView::ShowBubble( 56 void IntentPickerBubbleView::ShowBubble(
(...skipping 29 matching lines...) Expand all
82 // new Rectangle. 86 // new Rectangle.
83 delegate->SetAnchorRect( 87 delegate->SetAnchorRect(
84 gfx::Rect(browser_view->GetTopContainerBoundsInScreen().x(), 88 gfx::Rect(browser_view->GetTopContainerBoundsInScreen().x(),
85 browser_view->GetTopContainerBoundsInScreen().y(), 89 browser_view->GetTopContainerBoundsInScreen().y(),
86 browser_view->GetTopContainerBoundsInScreen().width(), 90 browser_view->GetTopContainerBoundsInScreen().width(),
87 browser_view->GetTopContainerBoundsInScreen().height() - 91 browser_view->GetTopContainerBoundsInScreen().height() -
88 kTopContainerMerge)); 92 kTopContainerMerge));
89 widget->Show(); 93 widget->Show();
90 } 94 }
91 95
96 // static
97 std::unique_ptr<IntentPickerBubbleView>
98 IntentPickerBubbleView::CreateBubbleForTesting(
99 const std::vector<NameAndIcon>& app_info,
100 const ThrottleCallback& throttle_cb,
101 content::WebContents* web_contents) {
102 std::unique_ptr<IntentPickerBubbleView> bubble(
103 new IntentPickerBubbleView(app_info, throttle_cb, web_contents));
Yusuke Sato 2016/07/20 21:58:57 auto bubble = base::MakeUnique<...>(...); ? It's
djacobo_ 2016/07/21 16:04:11 by using base::MakeUnique<...>(...); que compiler
Luis Héctor Chávez 2016/07/21 21:55:34 Another option is to make base::MakeUnique<IntentP
104 bubble->Init();
105 return bubble;
106 }
107
92 void IntentPickerBubbleView::Init() { 108 void IntentPickerBubbleView::Init() {
93 SkColor button_text_color = SkColorSetRGB(0x42, 0x85, 0xf4); 109 SkColor button_text_color = SkColorSetRGB(0x42, 0x85, 0xf4);
94 110
95 views::BoxLayout* general_layout = 111 views::BoxLayout* general_layout =
96 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); 112 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0);
97 SetLayoutManager(general_layout); 113 SetLayoutManager(general_layout);
98 114
99 // Create a view for the upper part of the UI, managed by a GridLayout to 115 // Create a view for the upper part of the UI, managed by a GridLayout to
100 // allow precise padding. 116 // allow precise padding.
101 View* header = new View(); 117 View* header = new View();
(...skipping 19 matching lines...) Expand all
121 this, l10n_util::GetStringUTF16(IDS_INTENT_PICKER_BUBBLE_VIEW_ALWAYS)); 137 this, l10n_util::GetStringUTF16(IDS_INTENT_PICKER_BUBBLE_VIEW_ALWAYS));
122 always_button_->SetFocusBehavior(View::FocusBehavior::ALWAYS); 138 always_button_->SetFocusBehavior(View::FocusBehavior::ALWAYS);
123 always_button_->SetFontList(gfx::FontList("Roboto-medium, 13px")); 139 always_button_->SetFontList(gfx::FontList("Roboto-medium, 13px"));
124 always_button_->set_tag(static_cast<int>(Option::ALWAYS)); 140 always_button_->set_tag(static_cast<int>(Option::ALWAYS));
125 always_button_->SetMinSize(gfx::Size(80, 32)); 141 always_button_->SetMinSize(gfx::Size(80, 32));
126 always_button_->SetTextColor(views::Button::STATE_DISABLED, SK_ColorGRAY); 142 always_button_->SetTextColor(views::Button::STATE_DISABLED, SK_ColorGRAY);
127 always_button_->SetTextColor(views::Button::STATE_NORMAL, button_text_color); 143 always_button_->SetTextColor(views::Button::STATE_NORMAL, button_text_color);
128 always_button_->SetTextColor(views::Button::STATE_HOVERED, button_text_color); 144 always_button_->SetTextColor(views::Button::STATE_HOVERED, button_text_color);
129 always_button_->SetHorizontalAlignment(gfx::ALIGN_CENTER); 145 always_button_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
130 always_button_->SetState(views::Button::STATE_DISABLED); 146 always_button_->SetState(views::Button::STATE_DISABLED);
147 // Insets in the format top, left, bottom, right.
148 always_button_->SetBorder(views::Border::CreateEmptyBorder(16, 16, 16, 16));
131 149
132 just_once_button_ = new views::LabelButton( 150 just_once_button_ = new views::LabelButton(
133 this, l10n_util::GetStringUTF16(IDS_INTENT_PICKER_BUBBLE_VIEW_JUST_ONCE)); 151 this, l10n_util::GetStringUTF16(IDS_INTENT_PICKER_BUBBLE_VIEW_JUST_ONCE));
134 just_once_button_->SetFocusBehavior(View::FocusBehavior::ALWAYS); 152 just_once_button_->SetFocusBehavior(View::FocusBehavior::ALWAYS);
135 just_once_button_->SetFontList(gfx::FontList("Roboto-medium, 13px")); 153 just_once_button_->SetFontList(gfx::FontList("Roboto-medium, 13px"));
136 just_once_button_->set_tag(static_cast<int>(Option::JUST_ONCE)); 154 just_once_button_->set_tag(static_cast<int>(Option::JUST_ONCE));
137 just_once_button_->SetMinSize(gfx::Size(80, 32)); 155 just_once_button_->SetMinSize(gfx::Size(80, 32));
138 just_once_button_->SetState(views::Button::STATE_DISABLED); 156 just_once_button_->SetState(views::Button::STATE_DISABLED);
139 just_once_button_->SetTextColor(views::Button::STATE_DISABLED, SK_ColorGRAY); 157 just_once_button_->SetTextColor(views::Button::STATE_DISABLED, SK_ColorGRAY);
140 just_once_button_->SetTextColor(views::Button::STATE_NORMAL, 158 just_once_button_->SetTextColor(views::Button::STATE_NORMAL,
141 button_text_color); 159 button_text_color);
142 just_once_button_->SetTextColor(views::Button::STATE_HOVERED, 160 just_once_button_->SetTextColor(views::Button::STATE_HOVERED,
143 button_text_color); 161 button_text_color);
144 just_once_button_->SetHorizontalAlignment(gfx::ALIGN_CENTER); 162 just_once_button_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
163 just_once_button_->SetBorder(
164 views::Border::CreateEmptyBorder(16, 16, 16, 16));
145 165
146 for (size_t i = 0; i < std::min(app_info_.size(), kMaxAppResults); ++i) { 166 // Adding the |open_with| label to the picker.
167 header_layout->StartRow(0, 0);
168 AddChildView(header);
169
170 // Setting a view and a layout for a ScrollView for the apps' list.
171 views::View* scrollable_view = new views::View();
172 views::BoxLayout* scrollable_layout =
173 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0);
174 scrollable_view->SetLayoutManager(scrollable_layout);
175 for (size_t i = 0; i < app_info_.size(); ++i) {
147 views::LabelButton* tmp_label = new views::LabelButton( 176 views::LabelButton* tmp_label = new views::LabelButton(
148 this, base::UTF8ToUTF16(base::StringPiece(app_info_[i].first))); 177 this, base::UTF8ToUTF16(base::StringPiece(app_info_[i].first)));
149 tmp_label->SetFocusBehavior(View::FocusBehavior::ALWAYS); 178 tmp_label->SetFocusBehavior(View::FocusBehavior::ALWAYS);
150 tmp_label->SetFontList(gfx::FontList("Roboto-regular, 13px")); 179 tmp_label->SetFontList(gfx::FontList("Roboto-regular, 13px"));
151 tmp_label->SetImageLabelSpacing(kLabelImageSeparation); 180 tmp_label->SetImageLabelSpacing(kLabelImageSeparation);
152 tmp_label->SetMinSize(gfx::Size(kMaxWidth, kRowHeight)); 181 tmp_label->SetMinSize(gfx::Size(kMaxWidth, kRowHeight));
153 tmp_label->SetMaxSize(gfx::Size(kMaxWidth, kRowHeight)); 182 tmp_label->SetMaxSize(gfx::Size(kMaxWidth, kRowHeight));
154 tmp_label->set_tag(i); 183 tmp_label->set_tag(i);
155 const gfx::ImageSkia* icon_ = app_info_[i].second.ToImageSkia(); 184 // By default a View set the id = 0 so we need to set values within the
156 tmp_label->SetImage(views::ImageButton::STATE_NORMAL, *icon_); 185 // range [1, app_info_.size()].
157 header_layout->StartRow(0, 0); 186 tmp_label->set_id(i + 1);
Yusuke Sato 2016/07/20 21:58:57 These IDs conflict with the ones in chrome/browser
djacobo_ 2016/07/21 16:04:11 On the most parts of the codebase that I watched t
158 header_layout->AddView(tmp_label); 187 if (!app_info_[i].second.IsEmpty()) {
188 tmp_label->SetImage(views::ImageButton::STATE_NORMAL,
189 *app_info_[i].second.ToImageSkia());
190 }
191 tmp_label->SetBorder(views::Border::CreateEmptyBorder(10, 16, 10, 0));
192 scrollable_view->AddChildView(tmp_label);
159 } 193 }
160 194
161 // Adding the Upper part of the Intent Picker |open_with| label and all the 195 views::ScrollView* scroll_view = new views::ScrollView();
162 // app options to |this|. 196 scroll_view->SetContents(scrollable_view);
163 header_layout->StartRow(0, 0); 197 scroll_view->SetVerticalScrollBar(new views::OverlayScrollBar(false));
164 header_layout->AddPaddingRow(0, 12); 198 // This part gives the scroll a fixed width and height. The height depends on
165 AddChildView(header); 199 // how many app candidates we got and how many we actually want to show.
200 // The added 0.5 on the else block allow us to let the user know there are
201 // more than |kMaxAppResults| apps accessible by scrolling the list.
202 if (app_info_.size() <= kMaxAppResults) {
203 scroll_view->ClipHeightTo(kMaxWidth, app_info_.size() * kRowHeight);
204 } else {
205 scroll_view->ClipHeightTo(kMaxWidth, (kMaxAppResults + 0.5) * kRowHeight);
206 }
207 AddChildView(scroll_view);
166 208
167 // The lower part of the Picker contains only the 2 buttons 209 // The lower part of the Picker contains only the 2 buttons
168 // |just_once_button_| and |always_button_|. 210 // |just_once_button_| and |always_button_|.
169 View* footer = new View(); 211 View* footer = new View();
212 footer->SetBorder(views::Border::CreateEmptyBorder(24, 0, 12, 12));
170 views::BoxLayout* buttons_layout = new views::BoxLayout( 213 views::BoxLayout* buttons_layout = new views::BoxLayout(
171 views::BoxLayout::kHorizontal, 12, 12, kButtonSeparation); 214 views::BoxLayout::kHorizontal, 0, 0, kButtonSeparation);
172 footer->SetLayoutManager(buttons_layout); 215 footer->SetLayoutManager(buttons_layout);
173 216
174 buttons_layout->set_main_axis_alignment( 217 buttons_layout->set_main_axis_alignment(
175 views::BoxLayout::MAIN_AXIS_ALIGNMENT_END); 218 views::BoxLayout::MAIN_AXIS_ALIGNMENT_END);
176 footer->AddChildView(just_once_button_); 219 footer->AddChildView(just_once_button_);
177 footer->AddChildView(always_button_); 220 footer->AddChildView(always_button_);
178 AddChildView(footer); 221 AddChildView(footer);
179 } 222 }
180 223
181 IntentPickerBubbleView::IntentPickerBubbleView( 224 IntentPickerBubbleView::IntentPickerBubbleView(
182 const std::vector<NameAndIcon>& app_info, 225 const std::vector<NameAndIcon>& app_info,
183 ThrottleCallback throttle_cb, 226 ThrottleCallback throttle_cb,
184 content::WebContents* web_contents) 227 content::WebContents* web_contents)
185 : views::BubbleDialogDelegateView(nullptr /* anchor_view */, 228 : views::BubbleDialogDelegateView(nullptr /* anchor_view */,
186 views::BubbleBorder::TOP_CENTER), 229 views::BubbleBorder::TOP_CENTER),
187 WebContentsObserver(web_contents), 230 WebContentsObserver(web_contents),
188 was_callback_run_(false), 231 was_callback_run_(false),
189 throttle_cb_(throttle_cb), 232 throttle_cb_(throttle_cb),
190 selected_app_tag_(kAppTagNoneSelected), 233 selected_app_tag_(kAppTagNoneSelected),
191 always_button_(nullptr), 234 always_button_(nullptr),
192 just_once_button_(nullptr), 235 just_once_button_(nullptr),
193 app_info_(app_info) {} 236 app_info_(app_info) {}
194 237
195 IntentPickerBubbleView::~IntentPickerBubbleView() { 238 IntentPickerBubbleView::~IntentPickerBubbleView() {
196 SetLayoutManager(nullptr); 239 SetLayoutManager(nullptr);
197 } 240 }
198 241
242 size_t IntentPickerBubbleView::GetNumberOfAppLabelsForTesting() {
243 return app_info_.size();
244 }
245
199 // If the widget gets closed without an app being selected we still need to use 246 // If the widget gets closed without an app being selected we still need to use
200 // the callback so the caller can Resume the navigation. 247 // the callback so the caller can Resume the navigation.
201 void IntentPickerBubbleView::OnWidgetDestroying(views::Widget* widget) { 248 void IntentPickerBubbleView::OnWidgetDestroying(views::Widget* widget) {
202 if (!was_callback_run_) { 249 if (!was_callback_run_) {
203 throttle_cb_.Run( 250 throttle_cb_.Run(
204 kAppTagNoneSelected, 251 kAppTagNoneSelected,
205 arc::ArcNavigationThrottle::CloseReason::DIALOG_DEACTIVATED); 252 arc::ArcNavigationThrottle::CloseReason::DIALOG_DEACTIVATED);
206 was_callback_run_ = true; 253 was_callback_run_ = true;
207 } 254 }
208 } 255 }
209 256
210 int IntentPickerBubbleView::GetDialogButtons() const { 257 int IntentPickerBubbleView::GetDialogButtons() const {
211 return ui::DIALOG_BUTTON_NONE; 258 return ui::DIALOG_BUTTON_NONE;
212 } 259 }
213 260
214 void IntentPickerBubbleView::ButtonPressed(views::Button* sender, 261 void IntentPickerBubbleView::ButtonPressed(views::Button* sender,
215 const ui::Event& event) { 262 const ui::Event& event) {
216 switch (sender->tag()) { 263 switch (sender->tag()) {
217 case static_cast<int>(Option::ALWAYS): 264 case static_cast<int>(Option::ALWAYS):
218 throttle_cb_.Run(selected_app_tag_, 265 if (!was_callback_run_) {
219 arc::ArcNavigationThrottle::CloseReason::ALWAYS_PRESSED); 266 throttle_cb_.Run(
220 was_callback_run_ = true; 267 selected_app_tag_,
268 arc::ArcNavigationThrottle::CloseReason::ALWAYS_PRESSED);
269 was_callback_run_ = true;
270 }
221 GetWidget()->Close(); 271 GetWidget()->Close();
222 break; 272 break;
223 case static_cast<int>(Option::JUST_ONCE): 273 case static_cast<int>(Option::JUST_ONCE):
224 throttle_cb_.Run( 274 if (!was_callback_run_) {
225 selected_app_tag_, 275 throttle_cb_.Run(
226 arc::ArcNavigationThrottle::CloseReason::JUST_ONCE_PRESSED); 276 selected_app_tag_,
227 was_callback_run_ = true; 277 arc::ArcNavigationThrottle::CloseReason::JUST_ONCE_PRESSED);
278 was_callback_run_ = true;
279 }
228 GetWidget()->Close(); 280 GetWidget()->Close();
229 break; 281 break;
230 default: 282 default:
231 // TODO(djacobo): Paint the background of the selected button on a
232 // different color, so the user has a clear remainder of his selection.
233 // The user cannot click on the |always_button_| or |just_once_button_| 283 // The user cannot click on the |always_button_| or |just_once_button_|
234 // unless an explicit app has been selected. 284 // unless an explicit app has been selected.
285 if (selected_app_tag_ != kAppTagNoneSelected) {
286 GetViewByID(selected_app_tag_ + 1)
287 ->set_background(
288 views::Background::CreateSolidBackground(SK_ColorWHITE));
289 GetViewByID(selected_app_tag_ + 1)->SchedulePaint();
290 }
235 selected_app_tag_ = sender->tag(); 291 selected_app_tag_ = sender->tag();
292 GetViewByID(selected_app_tag_ + 1)
293 ->set_background(views::Background::CreateSolidBackground(
294 SkColorSetRGB(0xeb, 0xeb, 0xeb)));
236 always_button_->SetState(views::Button::STATE_NORMAL); 295 always_button_->SetState(views::Button::STATE_NORMAL);
237 just_once_button_->SetState(views::Button::STATE_NORMAL); 296 just_once_button_->SetState(views::Button::STATE_NORMAL);
238 } 297 }
239 } 298 }
240 299
241 gfx::Size IntentPickerBubbleView::GetPreferredSize() const { 300 gfx::Size IntentPickerBubbleView::GetPreferredSize() const {
242 gfx::Size ps; 301 gfx::Size ps;
243 ps.set_width(kMaxWidth); 302 ps.set_width(kMaxWidth);
244 ps.set_height((std::min(app_info_.size(), kMaxAppResults)) * kRowHeight + 303 int apps_height = app_info_.size();
245 kHeaderHeight + kFooterHeight); 304 // We are showing |kMaxAppResults| + 0.5 rows at max, the extra 0.5 is used so
305 // the user can notice that more options are available.
306 if (app_info_.size() > kMaxAppResults) {
307 apps_height = (kMaxAppResults + 0.5) * kRowHeight;
308 } else {
309 apps_height *= kRowHeight;
310 }
311 ps.set_height(apps_height + kHeaderHeight + kFooterHeight);
246 return ps; 312 return ps;
247 } 313 }
248 314
249 // If the actual web_contents gets destroyed in the middle of the process we 315 // If the actual web_contents gets destroyed in the middle of the process we
250 // should inform the caller about this error. 316 // should inform the caller about this error.
251 void IntentPickerBubbleView::WebContentsDestroyed() { 317 void IntentPickerBubbleView::WebContentsDestroyed() {
252 if (!was_callback_run_) { 318 if (!was_callback_run_) {
253 throttle_cb_.Run(kAppTagNoneSelected, 319 throttle_cb_.Run(kAppTagNoneSelected,
254 arc::ArcNavigationThrottle::CloseReason::ERROR); 320 arc::ArcNavigationThrottle::CloseReason::ERROR);
255 was_callback_run_ = true; 321 was_callback_run_ = true;
256 } 322 }
257 GetWidget()->Close(); 323 GetWidget()->Close();
258 } 324 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698