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

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: Using base::Unretained with base::Bind() 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
26 namespace { 27 namespace {
27 28
28 // TODO(djacobo): Add a scroll bar and remove kMaxAppResults. 29 // Using |kMaxAppResults| as a measure of how many apps we want to show.
29 // Discriminating app results when the list is longer than |kMaxAppResults| 30 constexpr size_t kMaxAppResults = arc::ArcNavigationThrottle::kMaxAppResults;
30 constexpr size_t kMaxAppResults = 3;
31 // Main components sizes 31 // Main components sizes
32 constexpr int kRowHeight = 40; 32 constexpr int kRowHeight = 40;
33 constexpr int kMaxWidth = 320; 33 constexpr int kMaxWidth = 320;
34 constexpr int kHeaderHeight = 60; 34 constexpr int kHeaderHeight = 60;
35 constexpr int kFooterHeight = 68; 35 constexpr int kFooterHeight = 68;
36 // Inter components padding 36 // Inter components padding
37 constexpr int kButtonSeparation = 8; 37 constexpr int kButtonSeparation = 8;
38 constexpr int kLabelImageSeparation = 12; 38 constexpr int kLabelImageSeparation = 12;
39 39
40 // UI position wrt the Top Container 40 // UI position wrt the Top Container
41 constexpr int kTopContainerMerge = 3; 41 constexpr int kTopContainerMerge = 3;
42 constexpr int kAppTagNoneSelected = -1; 42 constexpr size_t kAppTagNoneSelected = static_cast<size_t>(-1);
43 43
44 // Arbitrary negative values to use as tags on the |always_button_| and 44 // 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 45 // |just_once_button_|. These are negative to differentiate from the app's tags
46 // which are always >= 0. 46 // which are always >= 0.
47 enum class Option : int { ALWAYS = -2, JUST_ONCE }; 47 enum class Option : int { ALWAYS = -2, JUST_ONCE };
48 48
49 } // namespace 49 } // namespace
50 50
51 // static 51 // static
52 void IntentPickerBubbleView::ShowBubble( 52 void IntentPickerBubbleView::ShowBubble(
(...skipping 29 matching lines...) Expand all
82 // new Rectangle. 82 // new Rectangle.
83 delegate->SetAnchorRect( 83 delegate->SetAnchorRect(
84 gfx::Rect(browser_view->GetTopContainerBoundsInScreen().x(), 84 gfx::Rect(browser_view->GetTopContainerBoundsInScreen().x(),
85 browser_view->GetTopContainerBoundsInScreen().y(), 85 browser_view->GetTopContainerBoundsInScreen().y(),
86 browser_view->GetTopContainerBoundsInScreen().width(), 86 browser_view->GetTopContainerBoundsInScreen().width(),
87 browser_view->GetTopContainerBoundsInScreen().height() - 87 browser_view->GetTopContainerBoundsInScreen().height() -
88 kTopContainerMerge)); 88 kTopContainerMerge));
89 widget->Show(); 89 widget->Show();
90 } 90 }
91 91
92 // static
93 std::unique_ptr<IntentPickerBubbleView>
94 IntentPickerBubbleView::CreateBubbleView(
95 const std::vector<NameAndIcon>& app_info,
96 const ThrottleCallback& throttle_cb,
97 content::WebContents* web_contents) {
98 std::unique_ptr<IntentPickerBubbleView> bubble(
99 new IntentPickerBubbleView(app_info, throttle_cb, web_contents));
100 bubble->Init();
101 return bubble;
102 }
103
92 void IntentPickerBubbleView::Init() { 104 void IntentPickerBubbleView::Init() {
93 SkColor button_text_color = SkColorSetRGB(0x42, 0x85, 0xf4); 105 SkColor button_text_color = SkColorSetRGB(0x42, 0x85, 0xf4);
94 106
95 views::BoxLayout* general_layout = 107 views::BoxLayout* general_layout =
96 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); 108 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0);
97 SetLayoutManager(general_layout); 109 SetLayoutManager(general_layout);
98 110
99 // Create a view for the upper part of the UI, managed by a GridLayout to 111 // Create a view for the upper part of the UI, managed by a GridLayout to
100 // allow precise padding. 112 // allow precise padding.
101 View* header = new View(); 113 View* header = new View();
(...skipping 19 matching lines...) Expand all
121 this, l10n_util::GetStringUTF16(IDS_INTENT_PICKER_BUBBLE_VIEW_ALWAYS)); 133 this, l10n_util::GetStringUTF16(IDS_INTENT_PICKER_BUBBLE_VIEW_ALWAYS));
122 always_button_->SetFocusBehavior(View::FocusBehavior::ALWAYS); 134 always_button_->SetFocusBehavior(View::FocusBehavior::ALWAYS);
123 always_button_->SetFontList(gfx::FontList("Roboto-medium, 13px")); 135 always_button_->SetFontList(gfx::FontList("Roboto-medium, 13px"));
124 always_button_->set_tag(static_cast<int>(Option::ALWAYS)); 136 always_button_->set_tag(static_cast<int>(Option::ALWAYS));
125 always_button_->SetMinSize(gfx::Size(80, 32)); 137 always_button_->SetMinSize(gfx::Size(80, 32));
126 always_button_->SetTextColor(views::Button::STATE_DISABLED, SK_ColorGRAY); 138 always_button_->SetTextColor(views::Button::STATE_DISABLED, SK_ColorGRAY);
127 always_button_->SetTextColor(views::Button::STATE_NORMAL, button_text_color); 139 always_button_->SetTextColor(views::Button::STATE_NORMAL, button_text_color);
128 always_button_->SetTextColor(views::Button::STATE_HOVERED, button_text_color); 140 always_button_->SetTextColor(views::Button::STATE_HOVERED, button_text_color);
129 always_button_->SetHorizontalAlignment(gfx::ALIGN_CENTER); 141 always_button_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
130 always_button_->SetState(views::Button::STATE_DISABLED); 142 always_button_->SetState(views::Button::STATE_DISABLED);
143 always_button_->SetBorder(views::Border::CreateEmptyBorder(gfx::Insets(16)));
131 144
132 just_once_button_ = new views::LabelButton( 145 just_once_button_ = new views::LabelButton(
133 this, l10n_util::GetStringUTF16(IDS_INTENT_PICKER_BUBBLE_VIEW_JUST_ONCE)); 146 this, l10n_util::GetStringUTF16(IDS_INTENT_PICKER_BUBBLE_VIEW_JUST_ONCE));
134 just_once_button_->SetFocusBehavior(View::FocusBehavior::ALWAYS); 147 just_once_button_->SetFocusBehavior(View::FocusBehavior::ALWAYS);
135 just_once_button_->SetFontList(gfx::FontList("Roboto-medium, 13px")); 148 just_once_button_->SetFontList(gfx::FontList("Roboto-medium, 13px"));
136 just_once_button_->set_tag(static_cast<int>(Option::JUST_ONCE)); 149 just_once_button_->set_tag(static_cast<int>(Option::JUST_ONCE));
137 just_once_button_->SetMinSize(gfx::Size(80, 32)); 150 just_once_button_->SetMinSize(gfx::Size(80, 32));
138 just_once_button_->SetState(views::Button::STATE_DISABLED); 151 just_once_button_->SetState(views::Button::STATE_DISABLED);
139 just_once_button_->SetTextColor(views::Button::STATE_DISABLED, SK_ColorGRAY); 152 just_once_button_->SetTextColor(views::Button::STATE_DISABLED, SK_ColorGRAY);
140 just_once_button_->SetTextColor(views::Button::STATE_NORMAL, 153 just_once_button_->SetTextColor(views::Button::STATE_NORMAL,
141 button_text_color); 154 button_text_color);
142 just_once_button_->SetTextColor(views::Button::STATE_HOVERED, 155 just_once_button_->SetTextColor(views::Button::STATE_HOVERED,
143 button_text_color); 156 button_text_color);
144 just_once_button_->SetHorizontalAlignment(gfx::ALIGN_CENTER); 157 just_once_button_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
158 just_once_button_->SetBorder(
159 views::Border::CreateEmptyBorder(gfx::Insets(16)));
145 160
146 for (size_t i = 0; i < std::min(app_info_.size(), kMaxAppResults); ++i) { 161 header_layout->StartRow(0, 0);
162 AddChildView(header);
163
164 // Creates a view to hold the views for each app.
165 views::View* scrollable_view = new views::View();
166 views::BoxLayout* scrollable_layout =
167 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0);
168 scrollable_view->SetLayoutManager(scrollable_layout);
169 for (size_t i = 0; i < app_info_.size(); ++i) {
147 views::LabelButton* tmp_label = new views::LabelButton( 170 views::LabelButton* tmp_label = new views::LabelButton(
148 this, base::UTF8ToUTF16(base::StringPiece(app_info_[i].first))); 171 this, base::UTF8ToUTF16(base::StringPiece(app_info_[i].first)));
149 tmp_label->SetFocusBehavior(View::FocusBehavior::ALWAYS); 172 tmp_label->SetFocusBehavior(View::FocusBehavior::ALWAYS);
150 tmp_label->SetFontList(gfx::FontList("Roboto-regular, 13px")); 173 tmp_label->SetFontList(gfx::FontList("Roboto-regular, 13px"));
151 tmp_label->SetImageLabelSpacing(kLabelImageSeparation); 174 tmp_label->SetImageLabelSpacing(kLabelImageSeparation);
152 tmp_label->SetMinSize(gfx::Size(kMaxWidth, kRowHeight)); 175 tmp_label->SetMinSize(gfx::Size(kMaxWidth, kRowHeight));
153 tmp_label->SetMaxSize(gfx::Size(kMaxWidth, kRowHeight)); 176 tmp_label->SetMaxSize(gfx::Size(kMaxWidth, kRowHeight));
154 tmp_label->set_tag(i); 177 tmp_label->set_tag(i);
155 const gfx::ImageSkia* icon_ = app_info_[i].second.ToImageSkia(); 178 if (!app_info_[i].second.IsEmpty()) {
156 tmp_label->SetImage(views::ImageButton::STATE_NORMAL, *icon_); 179 tmp_label->SetImage(views::ImageButton::STATE_NORMAL,
157 header_layout->StartRow(0, 0); 180 *app_info_[i].second.ToImageSkia());
158 header_layout->AddView(tmp_label); 181 }
182 tmp_label->SetBorder(views::Border::CreateEmptyBorder(10, 16, 10, 0));
183 scrollable_view->AddChildViewAt(tmp_label, i);
159 } 184 }
160 185
161 // Adding the Upper part of the Intent Picker |open_with| label and all the 186 scroll_view_ = new views::ScrollView();
162 // app options to |this|. 187 scroll_view_->SetContents(scrollable_view);
163 header_layout->StartRow(0, 0); 188 // Setting a customized ScrollBar which is shown only when the mouse pointer
164 header_layout->AddPaddingRow(0, 12); 189 // is inside the ScrollView.
165 AddChildView(header); 190 scroll_view_->SetVerticalScrollBar(new views::OverlayScrollBar(false));
191 // This part gives the scroll a fixed width and height. The height depends on
192 // how many app candidates we got and how many we actually want to show.
193 // The added 0.5 on the else block allow us to let the user know there are
194 // more than |kMaxAppResults| apps accessible by scrolling the list.
195 if (app_info_.size() <= kMaxAppResults) {
196 scroll_view_->ClipHeightTo(kRowHeight, app_info_.size() * kRowHeight);
197 } else {
198 scroll_view_->ClipHeightTo(kRowHeight, (kMaxAppResults + 0.5) * kRowHeight);
199 }
200 AddChildView(scroll_view_);
166 201
167 // The lower part of the Picker contains only the 2 buttons 202 // The lower part of the Picker contains only the 2 buttons
168 // |just_once_button_| and |always_button_|. 203 // |just_once_button_| and |always_button_|.
169 View* footer = new View(); 204 View* footer = new View();
205 footer->SetBorder(views::Border::CreateEmptyBorder(24, 0, 12, 12));
170 views::BoxLayout* buttons_layout = new views::BoxLayout( 206 views::BoxLayout* buttons_layout = new views::BoxLayout(
171 views::BoxLayout::kHorizontal, 12, 12, kButtonSeparation); 207 views::BoxLayout::kHorizontal, 0, 0, kButtonSeparation);
172 footer->SetLayoutManager(buttons_layout); 208 footer->SetLayoutManager(buttons_layout);
173 209
174 buttons_layout->set_main_axis_alignment( 210 buttons_layout->set_main_axis_alignment(
175 views::BoxLayout::MAIN_AXIS_ALIGNMENT_END); 211 views::BoxLayout::MAIN_AXIS_ALIGNMENT_END);
176 footer->AddChildView(just_once_button_); 212 footer->AddChildView(just_once_button_);
177 footer->AddChildView(always_button_); 213 footer->AddChildView(always_button_);
178 AddChildView(footer); 214 AddChildView(footer);
179 } 215 }
180 216
181 IntentPickerBubbleView::IntentPickerBubbleView( 217 IntentPickerBubbleView::IntentPickerBubbleView(
182 const std::vector<NameAndIcon>& app_info, 218 const std::vector<NameAndIcon>& app_info,
183 ThrottleCallback throttle_cb, 219 ThrottleCallback throttle_cb,
184 content::WebContents* web_contents) 220 content::WebContents* web_contents)
185 : views::BubbleDialogDelegateView(nullptr /* anchor_view */, 221 : views::BubbleDialogDelegateView(nullptr /* anchor_view */,
186 views::BubbleBorder::TOP_CENTER), 222 views::BubbleBorder::TOP_CENTER),
187 WebContentsObserver(web_contents), 223 WebContentsObserver(web_contents),
188 was_callback_run_(false), 224 was_callback_run_(false),
189 throttle_cb_(throttle_cb), 225 throttle_cb_(throttle_cb),
190 selected_app_tag_(kAppTagNoneSelected), 226 selected_app_tag_(kAppTagNoneSelected),
191 always_button_(nullptr), 227 always_button_(nullptr),
192 just_once_button_(nullptr), 228 just_once_button_(nullptr),
229 scroll_view_(nullptr),
193 app_info_(app_info) {} 230 app_info_(app_info) {}
194 231
195 IntentPickerBubbleView::~IntentPickerBubbleView() { 232 IntentPickerBubbleView::~IntentPickerBubbleView() {
196 SetLayoutManager(nullptr); 233 SetLayoutManager(nullptr);
197 } 234 }
198 235
199 // If the widget gets closed without an app being selected we still need to use 236 // 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. 237 // the callback so the caller can Resume the navigation.
201 void IntentPickerBubbleView::OnWidgetDestroying(views::Widget* widget) { 238 void IntentPickerBubbleView::OnWidgetDestroying(views::Widget* widget) {
202 if (!was_callback_run_) { 239 if (!was_callback_run_) {
(...skipping 18 matching lines...) Expand all
221 GetWidget()->Close(); 258 GetWidget()->Close();
222 break; 259 break;
223 case static_cast<int>(Option::JUST_ONCE): 260 case static_cast<int>(Option::JUST_ONCE):
224 throttle_cb_.Run( 261 throttle_cb_.Run(
225 selected_app_tag_, 262 selected_app_tag_,
226 arc::ArcNavigationThrottle::CloseReason::JUST_ONCE_PRESSED); 263 arc::ArcNavigationThrottle::CloseReason::JUST_ONCE_PRESSED);
227 was_callback_run_ = true; 264 was_callback_run_ = true;
228 GetWidget()->Close(); 265 GetWidget()->Close();
229 break; 266 break;
230 default: 267 default:
231 // TODO(djacobo): Paint the background of the selected button on a 268 // Ignore the user's input if |sender->tag()| is not in the valid range.
232 // different color, so the user has a clear remainder of his selection. 269 if (static_cast<size_t>(sender->tag()) >= app_info_.size())
sky 2016/07/25 15:12:19 Under what conditions will this, line 311 and 320
djacobo_ 2016/07/25 19:42:21 Well the basic flow of this UI ensures the |select
sky 2016/07/25 20:20:26 Again, see the style guide discussion. From what I
djacobo_ 2016/07/25 21:29:44 Ok, notice that if I use DCHECKs in here it will b
270 break;
233 // The user cannot click on the |always_button_| or |just_once_button_| 271 // The user cannot click on the |always_button_| or |just_once_button_|
234 // unless an explicit app has been selected. 272 // unless an explicit app has been selected.
273 if (selected_app_tag_ != kAppTagNoneSelected)
274 SetLabelButtonBackgroundColor(selected_app_tag_, SK_ColorWHITE);
235 selected_app_tag_ = sender->tag(); 275 selected_app_tag_ = sender->tag();
276 SetLabelButtonBackgroundColor(selected_app_tag_,
277 SkColorSetRGB(0xeb, 0xeb, 0xeb));
236 always_button_->SetState(views::Button::STATE_NORMAL); 278 always_button_->SetState(views::Button::STATE_NORMAL);
237 just_once_button_->SetState(views::Button::STATE_NORMAL); 279 just_once_button_->SetState(views::Button::STATE_NORMAL);
238 } 280 }
239 } 281 }
240 282
241 gfx::Size IntentPickerBubbleView::GetPreferredSize() const { 283 gfx::Size IntentPickerBubbleView::GetPreferredSize() const {
242 gfx::Size ps; 284 gfx::Size ps;
243 ps.set_width(kMaxWidth); 285 ps.set_width(kMaxWidth);
244 ps.set_height((std::min(app_info_.size(), kMaxAppResults)) * kRowHeight + 286 int apps_height = app_info_.size();
245 kHeaderHeight + kFooterHeight); 287 // We are showing |kMaxAppResults| + 0.5 rows at max, the extra 0.5 is used so
288 // the user can notice that more options are available.
289 if (app_info_.size() > kMaxAppResults) {
290 apps_height = (kMaxAppResults + 0.5) * kRowHeight;
291 } else {
292 apps_height *= kRowHeight;
293 }
294 ps.set_height(apps_height + kHeaderHeight + kFooterHeight);
246 return ps; 295 return ps;
247 } 296 }
248 297
249 // If the actual web_contents gets destroyed in the middle of the process we 298 // If the actual web_contents gets destroyed in the middle of the process we
250 // should inform the caller about this error. 299 // should inform the caller about this error.
251 void IntentPickerBubbleView::WebContentsDestroyed() { 300 void IntentPickerBubbleView::WebContentsDestroyed() {
252 if (!was_callback_run_) { 301 if (!was_callback_run_) {
253 throttle_cb_.Run(kAppTagNoneSelected, 302 throttle_cb_.Run(kAppTagNoneSelected,
254 arc::ArcNavigationThrottle::CloseReason::ERROR); 303 arc::ArcNavigationThrottle::CloseReason::ERROR);
255 was_callback_run_ = true; 304 was_callback_run_ = true;
256 } 305 }
257 GetWidget()->Close(); 306 GetWidget()->Close();
258 } 307 }
308
309 views::LabelButton* IntentPickerBubbleView::GetLabelButtonAt(size_t index) {
310 if (index >= app_info_.size())
311 return nullptr;
312 views::View* temp_contents = scroll_view_->contents();
313 return static_cast<views::LabelButton*>(temp_contents->child_at(index));
314 }
315
316 void IntentPickerBubbleView::SetLabelButtonBackgroundColor(size_t index,
317 SkColor color) {
318 views::LabelButton* temp_lb = GetLabelButtonAt(index);
319 if (temp_lb == nullptr)
320 return;
321 temp_lb->set_background(views::Background::CreateSolidBackground(color));
322 temp_lb->SchedulePaint();
323 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698