OLD | NEW |
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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/string_piece.h" | 9 #include "base/strings/string_piece.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/ui/browser_finder.h" | 11 #include "chrome/browser/ui/browser_finder.h" |
12 #include "chrome/browser/ui/views/frame/browser_view.h" | 12 #include "chrome/browser/ui/views/frame/browser_view.h" |
13 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" | 13 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" |
14 #include "chrome/grit/generated_resources.h" | 14 #include "chrome/grit/generated_resources.h" |
15 #include "content/public/browser/navigation_handle.h" | 15 #include "content/public/browser/navigation_handle.h" |
16 #include "third_party/skia/include/core/SkColor.h" | 16 #include "third_party/skia/include/core/SkColor.h" |
17 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
18 #include "ui/base/material_design/material_design_controller.h" | |
19 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" |
20 #include "ui/views/animation/ink_drop_host_view.h" | |
21 #include "ui/views/border.h" | 19 #include "ui/views/border.h" |
22 #include "ui/views/controls/button/image_button.h" | 20 #include "ui/views/controls/button/image_button.h" |
23 #include "ui/views/controls/scroll_view.h" | 21 #include "ui/views/controls/scroll_view.h" |
24 #include "ui/views/controls/scrollbar/overlay_scroll_bar.h" | 22 #include "ui/views/controls/scrollbar/overlay_scroll_bar.h" |
25 #include "ui/views/layout/box_layout.h" | 23 #include "ui/views/layout/box_layout.h" |
26 #include "ui/views/layout/grid_layout.h" | 24 #include "ui/views/layout/grid_layout.h" |
27 #include "ui/views/window/dialog_client_view.h" | 25 #include "ui/views/window/dialog_client_view.h" |
28 | 26 |
29 namespace { | 27 namespace { |
30 | 28 |
31 // Using |kMaxAppResults| as a measure of how many apps we want to show. | 29 // Using |kMaxAppResults| as a measure of how many apps we want to show. |
32 constexpr size_t kMaxAppResults = arc::ArcNavigationThrottle::kMaxAppResults; | 30 constexpr size_t kMaxAppResults = arc::ArcNavigationThrottle::kMaxAppResults; |
33 // Main components sizes | 31 // Main components sizes |
34 constexpr int kDialogDelegateInsets = 16; | |
35 constexpr int kRowHeight = 40; | 32 constexpr int kRowHeight = 40; |
36 constexpr int kMaxWidth = 320; | 33 constexpr int kMaxWidth = 320; |
| 34 constexpr int kHeaderHeight = 60; |
| 35 constexpr int kFooterHeight = 68; |
| 36 // Inter components padding |
| 37 constexpr int kButtonSeparation = 8; |
| 38 constexpr int kLabelImageSeparation = 12; |
37 | 39 |
38 // UI position wrt the Top Container | 40 // UI position wrt the Top Container |
39 constexpr int kTopContainerMerge = 3; | 41 constexpr int kTopContainerMerge = 3; |
| 42 constexpr size_t kAppTagNoneSelected = static_cast<size_t>(-1); |
40 | 43 |
41 constexpr char kInvalidPackageName[] = ""; | 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 |
| 46 // which are always >= 0. |
| 47 enum class Option : int { ALWAYS = -2, JUST_ONCE }; |
42 | 48 |
43 } // namespace | 49 } // namespace |
44 | 50 |
45 // IntentPickerLabelButton | |
46 | |
47 // A button that represents a candidate intent handler. | |
48 class IntentPickerLabelButton : public views::LabelButton { | |
49 public: | |
50 IntentPickerLabelButton(views::ButtonListener* listener, | |
51 gfx::Image* icon, | |
52 const std::string& package_name, | |
53 const std::string& activity_name) | |
54 : LabelButton(listener, | |
55 base::UTF8ToUTF16(base::StringPiece(activity_name))), | |
56 package_name_(package_name) { | |
57 SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
58 SetFocusBehavior(View::FocusBehavior::ALWAYS); | |
59 SetMinSize(gfx::Size(kMaxWidth, kRowHeight)); | |
60 SetInkDropMode(InkDropMode::ON); | |
61 if (!icon->IsEmpty()) | |
62 SetImage(views::ImageButton::STATE_NORMAL, *icon->ToImageSkia()); | |
63 SetBorder(views::Border::CreateEmptyBorder(10, 16, 10, 0)); | |
64 } | |
65 | |
66 SkColor GetInkDropBaseColor() const override { return SK_ColorBLACK; } | |
67 | |
68 void MarkAsUnselected(const ui::Event* event) { | |
69 AnimateInkDrop(views::InkDropState::DEACTIVATED, | |
70 ui::LocatedEvent::FromIfValid(event)); | |
71 } | |
72 | |
73 void MarkAsSelected(const ui::Event* event) { | |
74 AnimateInkDrop(views::InkDropState::ACTIVATED, | |
75 ui::LocatedEvent::FromIfValid(event)); | |
76 } | |
77 | |
78 views::InkDropState GetTargetInkDropState() { | |
79 return ink_drop()->GetTargetInkDropState(); | |
80 } | |
81 | |
82 private: | |
83 std::string package_name_; | |
84 | |
85 DISALLOW_COPY_AND_ASSIGN(IntentPickerLabelButton); | |
86 }; | |
87 | |
88 // static | 51 // static |
89 void IntentPickerBubbleView::ShowBubble( | 52 void IntentPickerBubbleView::ShowBubble( |
90 content::WebContents* web_contents, | 53 content::WebContents* web_contents, |
91 const std::vector<AppInfo>& app_info, | 54 const std::vector<NameAndIcon>& app_info, |
92 const IntentPickerResponse& intent_picker_cb) { | 55 const ThrottleCallback& throttle_cb) { |
93 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | 56 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
94 if (!browser || !BrowserView::GetBrowserViewForBrowser(browser)) { | 57 if (!browser) { |
95 intent_picker_cb.Run(kInvalidPackageName, | 58 throttle_cb.Run(kAppTagNoneSelected, |
96 arc::ArcNavigationThrottle::CloseReason::ERROR); | 59 arc::ArcNavigationThrottle::CloseReason::ERROR); |
97 return; | 60 return; |
98 } | 61 } |
99 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); | 62 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); |
| 63 if (!browser_view) { |
| 64 throttle_cb.Run(kAppTagNoneSelected, |
| 65 arc::ArcNavigationThrottle::CloseReason::ERROR); |
| 66 return; |
| 67 } |
| 68 |
100 IntentPickerBubbleView* delegate = | 69 IntentPickerBubbleView* delegate = |
101 new IntentPickerBubbleView(app_info, intent_picker_cb, web_contents); | 70 new IntentPickerBubbleView(app_info, throttle_cb, web_contents); |
102 // TODO(djacobo): Remove the left and right insets when | 71 delegate->set_margins(gfx::Insets()); |
103 // http://crbug.com/656662 gets fixed. | |
104 // Add a 1-pixel extra boundary left and right when using secondary UI. | |
105 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) | |
106 delegate->set_margins(gfx::Insets(16, 1, 0, 1)); | |
107 else | |
108 delegate->set_margins(gfx::Insets(16, 0, 0, 0)); | |
109 delegate->set_parent_window(browser_view->GetNativeWindow()); | 72 delegate->set_parent_window(browser_view->GetNativeWindow()); |
110 views::Widget* widget = | 73 views::Widget* widget = |
111 views::BubbleDialogDelegateView::CreateBubble(delegate); | 74 views::BubbleDialogDelegateView::CreateBubble(delegate); |
112 | 75 |
113 delegate->SetArrowPaintType(views::BubbleBorder::PAINT_NONE); | 76 delegate->SetArrowPaintType(views::BubbleBorder::PAINT_NONE); |
114 delegate->SetAlignment(views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE); | 77 delegate->SetAlignment(views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE); |
115 | 78 |
116 // Using the TopContainerBoundsInScreen Rect to specify an anchor for the the | 79 // Using the TopContainerBoundsInScreen Rect to specify an anchor for the the |
117 // UI. Rect allow us to set the coordinates(x,y), the width and height for the | 80 // UI. Rect allow us to set the coordinates(x,y), the width and height for the |
118 // new Rectangle. | 81 // new Rectangle. |
119 delegate->SetAnchorRect( | 82 delegate->SetAnchorRect( |
120 gfx::Rect(browser_view->GetTopContainerBoundsInScreen().x(), | 83 gfx::Rect(browser_view->GetTopContainerBoundsInScreen().x(), |
121 browser_view->GetTopContainerBoundsInScreen().y(), | 84 browser_view->GetTopContainerBoundsInScreen().y(), |
122 browser_view->GetTopContainerBoundsInScreen().width(), | 85 browser_view->GetTopContainerBoundsInScreen().width(), |
123 browser_view->GetTopContainerBoundsInScreen().height() - | 86 browser_view->GetTopContainerBoundsInScreen().height() - |
124 kTopContainerMerge)); | 87 kTopContainerMerge)); |
125 delegate->GetDialogClientView()->set_button_row_insets( | |
126 gfx::Insets(kDialogDelegateInsets)); | |
127 delegate->GetDialogClientView()->Layout(); | |
128 delegate->GetIntentPickerLabelButtonAt(0)->MarkAsSelected(nullptr); | |
129 widget->Show(); | 88 widget->Show(); |
130 } | 89 } |
131 | 90 |
132 // static | 91 // static |
133 std::unique_ptr<IntentPickerBubbleView> | 92 std::unique_ptr<IntentPickerBubbleView> |
134 IntentPickerBubbleView::CreateBubbleView( | 93 IntentPickerBubbleView::CreateBubbleView( |
135 const std::vector<AppInfo>& app_info, | 94 const std::vector<NameAndIcon>& app_info, |
136 const IntentPickerResponse& intent_picker_cb, | 95 const ThrottleCallback& throttle_cb, |
137 content::WebContents* web_contents) { | 96 content::WebContents* web_contents) { |
138 std::unique_ptr<IntentPickerBubbleView> bubble( | 97 std::unique_ptr<IntentPickerBubbleView> bubble( |
139 new IntentPickerBubbleView(app_info, intent_picker_cb, web_contents)); | 98 new IntentPickerBubbleView(app_info, throttle_cb, web_contents)); |
140 bubble->Init(); | 99 bubble->Init(); |
141 return bubble; | 100 return bubble; |
142 } | 101 } |
143 | 102 |
144 bool IntentPickerBubbleView::Accept() { | 103 void IntentPickerBubbleView::Init() { |
145 RunCallback(app_info_[selected_app_tag_].package_name, | 104 SkColor button_text_color = SkColorSetRGB(0x42, 0x85, 0xf4); |
146 arc::ArcNavigationThrottle::CloseReason::JUST_ONCE_PRESSED); | |
147 return true; | |
148 } | |
149 | 105 |
150 bool IntentPickerBubbleView::Cancel() { | |
151 RunCallback(app_info_[selected_app_tag_].package_name, | |
152 arc::ArcNavigationThrottle::CloseReason::ALWAYS_PRESSED); | |
153 return true; | |
154 } | |
155 | |
156 bool IntentPickerBubbleView::Close() { | |
157 // Whenever closing the bubble without pressing |Just once| or |Always| we | |
158 // need to report back that the user didn't select anything. | |
159 RunCallback(kInvalidPackageName, | |
160 arc::ArcNavigationThrottle::CloseReason::DIALOG_DEACTIVATED); | |
161 return true; | |
162 } | |
163 | |
164 void IntentPickerBubbleView::Init() { | |
165 views::BoxLayout* general_layout = | 106 views::BoxLayout* general_layout = |
166 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); | 107 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); |
167 SetLayoutManager(general_layout); | 108 SetLayoutManager(general_layout); |
168 | 109 |
| 110 // Create a view for the upper part of the UI, managed by a GridLayout to |
| 111 // allow precise padding. |
| 112 View* header = new View(); |
| 113 views::GridLayout* header_layout = new views::GridLayout(header); |
| 114 header->SetLayoutManager(header_layout); |
| 115 views::Label* open_with = new views::Label( |
| 116 l10n_util::GetStringUTF16(IDS_INTENT_PICKER_BUBBLE_VIEW_OPEN_WITH)); |
| 117 open_with->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 118 open_with->SetFontList(gfx::FontList("Roboto-medium, 15px")); |
| 119 |
| 120 views::ColumnSet* column_set = header_layout->AddColumnSet(0); |
| 121 column_set->AddPaddingColumn(0, 16); |
| 122 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, |
| 123 views::GridLayout::FIXED, kMaxWidth, kMaxWidth); |
| 124 column_set->AddPaddingColumn(0, 16); |
| 125 // Tell the GridLayout where to start, then proceed to place the views. |
| 126 header_layout->AddPaddingRow(0.0, 21); |
| 127 header_layout->StartRow(0, 0); |
| 128 header_layout->AddView(open_with); |
| 129 header_layout->AddPaddingRow(0.0, 24); |
| 130 |
| 131 always_button_ = new views::LabelButton( |
| 132 this, l10n_util::GetStringUTF16(IDS_INTENT_PICKER_BUBBLE_VIEW_ALWAYS)); |
| 133 always_button_->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 134 always_button_->SetFontList(gfx::FontList("Roboto-medium, 13px")); |
| 135 always_button_->set_tag(static_cast<int>(Option::ALWAYS)); |
| 136 always_button_->SetMinSize(gfx::Size(80, 32)); |
| 137 always_button_->SetTextColor(views::Button::STATE_DISABLED, SK_ColorGRAY); |
| 138 always_button_->SetTextColor(views::Button::STATE_NORMAL, button_text_color); |
| 139 always_button_->SetTextColor(views::Button::STATE_HOVERED, button_text_color); |
| 140 always_button_->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 141 always_button_->SetState(views::Button::STATE_DISABLED); |
| 142 always_button_->SetBorder(views::Border::CreateEmptyBorder(gfx::Insets(16))); |
| 143 |
| 144 just_once_button_ = new views::LabelButton( |
| 145 this, l10n_util::GetStringUTF16(IDS_INTENT_PICKER_BUBBLE_VIEW_JUST_ONCE)); |
| 146 just_once_button_->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 147 just_once_button_->SetFontList(gfx::FontList("Roboto-medium, 13px")); |
| 148 just_once_button_->set_tag(static_cast<int>(Option::JUST_ONCE)); |
| 149 just_once_button_->SetMinSize(gfx::Size(80, 32)); |
| 150 just_once_button_->SetState(views::Button::STATE_DISABLED); |
| 151 just_once_button_->SetTextColor(views::Button::STATE_DISABLED, SK_ColorGRAY); |
| 152 just_once_button_->SetTextColor(views::Button::STATE_NORMAL, |
| 153 button_text_color); |
| 154 just_once_button_->SetTextColor(views::Button::STATE_HOVERED, |
| 155 button_text_color); |
| 156 just_once_button_->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 157 just_once_button_->SetBorder( |
| 158 views::Border::CreateEmptyBorder(gfx::Insets(16))); |
| 159 |
| 160 header_layout->StartRow(0, 0); |
| 161 AddChildView(header); |
| 162 |
169 // Creates a view to hold the views for each app. | 163 // Creates a view to hold the views for each app. |
170 views::View* scrollable_view = new views::View(); | 164 views::View* scrollable_view = new views::View(); |
171 views::BoxLayout* scrollable_layout = | 165 views::BoxLayout* scrollable_layout = |
172 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); | 166 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); |
173 scrollable_view->SetLayoutManager(scrollable_layout); | 167 scrollable_view->SetLayoutManager(scrollable_layout); |
174 for (size_t i = 0; i < app_info_.size(); ++i) { | 168 for (size_t i = 0; i < app_info_.size(); ++i) { |
175 IntentPickerLabelButton* app_button = new IntentPickerLabelButton( | 169 views::LabelButton* tmp_label = new views::LabelButton( |
176 this, &app_info_[i].icon, app_info_[i].package_name, | 170 this, base::UTF8ToUTF16(base::StringPiece(app_info_[i].first))); |
177 app_info_[i].activity_name); | 171 tmp_label->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
178 app_button->set_tag(i); | 172 tmp_label->SetFontList(gfx::FontList("Roboto-regular, 13px")); |
179 scrollable_view->AddChildViewAt(app_button, i); | 173 tmp_label->SetImageLabelSpacing(kLabelImageSeparation); |
| 174 tmp_label->SetMinSize(gfx::Size(kMaxWidth, kRowHeight)); |
| 175 tmp_label->SetMaxSize(gfx::Size(kMaxWidth, kRowHeight)); |
| 176 tmp_label->set_tag(i); |
| 177 if (!app_info_[i].second.IsEmpty()) { |
| 178 tmp_label->SetImage(views::ImageButton::STATE_NORMAL, |
| 179 *app_info_[i].second.ToImageSkia()); |
| 180 } |
| 181 tmp_label->SetBorder(views::Border::CreateEmptyBorder(10, 16, 10, 0)); |
| 182 scrollable_view->AddChildViewAt(tmp_label, i); |
180 } | 183 } |
181 | 184 |
182 scroll_view_ = new views::ScrollView(); | 185 scroll_view_ = new views::ScrollView(); |
183 scroll_view_->EnableViewPortLayer(); | |
184 scroll_view_->SetContents(scrollable_view); | 186 scroll_view_->SetContents(scrollable_view); |
185 // Setting a customized ScrollBar which is shown only when the mouse pointer | 187 // Setting a customized ScrollBar which is shown only when the mouse pointer |
186 // is inside the ScrollView. | 188 // is inside the ScrollView. |
187 scroll_view_->SetVerticalScrollBar(new views::OverlayScrollBar(false)); | 189 scroll_view_->SetVerticalScrollBar(new views::OverlayScrollBar(false)); |
188 // This part gives the scroll a fixed width and height. The height depends on | 190 // This part gives the scroll a fixed width and height. The height depends on |
189 // how many app candidates we got and how many we actually want to show. | 191 // how many app candidates we got and how many we actually want to show. |
190 // The added 0.5 on the else block allow us to let the user know there are | 192 // The added 0.5 on the else block allow us to let the user know there are |
191 // more than |kMaxAppResults| apps accessible by scrolling the list. | 193 // more than |kMaxAppResults| apps accessible by scrolling the list. |
192 if (app_info_.size() <= kMaxAppResults) { | 194 if (app_info_.size() <= kMaxAppResults) { |
193 scroll_view_->ClipHeightTo(kRowHeight, app_info_.size() * kRowHeight); | 195 scroll_view_->ClipHeightTo(kRowHeight, app_info_.size() * kRowHeight); |
194 } else { | 196 } else { |
195 scroll_view_->ClipHeightTo(kRowHeight, (kMaxAppResults + 0.5) * kRowHeight); | 197 scroll_view_->ClipHeightTo(kRowHeight, (kMaxAppResults + 0.5) * kRowHeight); |
196 } | 198 } |
197 AddChildView(scroll_view_); | 199 AddChildView(scroll_view_); |
198 } | |
199 | 200 |
200 base::string16 IntentPickerBubbleView::GetWindowTitle() const { | 201 // The lower part of the Picker contains only the 2 buttons |
201 return l10n_util::GetStringUTF16(IDS_INTENT_PICKER_BUBBLE_VIEW_OPEN_WITH); | 202 // |just_once_button_| and |always_button_|. |
202 } | 203 View* footer = new View(); |
| 204 footer->SetBorder(views::Border::CreateEmptyBorder(24, 0, 12, 12)); |
| 205 views::BoxLayout* buttons_layout = new views::BoxLayout( |
| 206 views::BoxLayout::kHorizontal, 0, 0, kButtonSeparation); |
| 207 footer->SetLayoutManager(buttons_layout); |
203 | 208 |
204 base::string16 IntentPickerBubbleView::GetDialogButtonLabel( | 209 buttons_layout->set_main_axis_alignment( |
205 ui::DialogButton button) const { | 210 views::BoxLayout::MAIN_AXIS_ALIGNMENT_END); |
206 return l10n_util::GetStringUTF16(button == ui::DIALOG_BUTTON_OK | 211 footer->AddChildView(just_once_button_); |
207 ? IDS_INTENT_PICKER_BUBBLE_VIEW_JUST_ONCE | 212 footer->AddChildView(always_button_); |
208 : IDS_INTENT_PICKER_BUBBLE_VIEW_ALWAYS); | 213 AddChildView(footer); |
209 } | 214 } |
210 | 215 |
211 IntentPickerBubbleView::IntentPickerBubbleView( | 216 IntentPickerBubbleView::IntentPickerBubbleView( |
212 const std::vector<AppInfo>& app_info, | 217 const std::vector<NameAndIcon>& app_info, |
213 IntentPickerResponse intent_picker_cb, | 218 ThrottleCallback throttle_cb, |
214 content::WebContents* web_contents) | 219 content::WebContents* web_contents) |
215 : views::BubbleDialogDelegateView(nullptr /* anchor_view */, | 220 : views::BubbleDialogDelegateView(nullptr /* anchor_view */, |
216 views::BubbleBorder::TOP_CENTER), | 221 views::BubbleBorder::TOP_CENTER), |
217 WebContentsObserver(web_contents), | 222 WebContentsObserver(web_contents), |
218 intent_picker_cb_(intent_picker_cb), | 223 was_callback_run_(false), |
219 selected_app_tag_(0), | 224 throttle_cb_(throttle_cb), |
| 225 selected_app_tag_(kAppTagNoneSelected), |
| 226 always_button_(nullptr), |
| 227 just_once_button_(nullptr), |
220 scroll_view_(nullptr), | 228 scroll_view_(nullptr), |
221 app_info_(app_info) {} | 229 app_info_(app_info) {} |
222 | 230 |
223 IntentPickerBubbleView::~IntentPickerBubbleView() { | 231 IntentPickerBubbleView::~IntentPickerBubbleView() { |
224 SetLayoutManager(nullptr); | 232 SetLayoutManager(nullptr); |
225 } | 233 } |
226 | 234 |
227 // If the widget gets closed without an app being selected we still need to use | 235 // If the widget gets closed without an app being selected we still need to use |
228 // the callback so the caller can Resume the navigation. | 236 // the callback so the caller can Resume the navigation. |
229 void IntentPickerBubbleView::OnWidgetDestroying(views::Widget* widget) { | 237 void IntentPickerBubbleView::OnWidgetDestroying(views::Widget* widget) { |
230 RunCallback(kInvalidPackageName, | 238 if (!was_callback_run_) { |
231 arc::ArcNavigationThrottle::CloseReason::DIALOG_DEACTIVATED); | 239 was_callback_run_ = true; |
| 240 throttle_cb_.Run( |
| 241 kAppTagNoneSelected, |
| 242 arc::ArcNavigationThrottle::CloseReason::DIALOG_DEACTIVATED); |
| 243 } |
| 244 } |
| 245 |
| 246 int IntentPickerBubbleView::GetDialogButtons() const { |
| 247 return ui::DIALOG_BUTTON_NONE; |
232 } | 248 } |
233 | 249 |
234 void IntentPickerBubbleView::ButtonPressed(views::Button* sender, | 250 void IntentPickerBubbleView::ButtonPressed(views::Button* sender, |
235 const ui::Event& event) { | 251 const ui::Event& event) { |
236 // The selected app must be a value in the range [0, app_info_.size()-1]. | 252 switch (sender->tag()) { |
237 DCHECK_LT(static_cast<size_t>(sender->tag()), app_info_.size()); | 253 case static_cast<int>(Option::ALWAYS): |
238 GetIntentPickerLabelButtonAt(selected_app_tag_)->MarkAsUnselected(&event); | 254 was_callback_run_ = true; |
239 | 255 throttle_cb_.Run(selected_app_tag_, |
240 selected_app_tag_ = sender->tag(); | 256 arc::ArcNavigationThrottle::CloseReason::ALWAYS_PRESSED); |
241 GetIntentPickerLabelButtonAt(selected_app_tag_)->MarkAsSelected(&event); | 257 GetWidget()->Close(); |
| 258 break; |
| 259 case static_cast<int>(Option::JUST_ONCE): |
| 260 was_callback_run_ = true; |
| 261 throttle_cb_.Run( |
| 262 selected_app_tag_, |
| 263 arc::ArcNavigationThrottle::CloseReason::JUST_ONCE_PRESSED); |
| 264 GetWidget()->Close(); |
| 265 break; |
| 266 default: |
| 267 // The selected app must be a value in the range [0, app_info_.size()-1]. |
| 268 DCHECK(static_cast<size_t>(sender->tag()) < app_info_.size()); |
| 269 // The user cannot click on the |always_button_| or |just_once_button_| |
| 270 // unless an explicit app has been selected. |
| 271 if (selected_app_tag_ != kAppTagNoneSelected) |
| 272 SetLabelButtonBackgroundColor(selected_app_tag_, SK_ColorWHITE); |
| 273 selected_app_tag_ = sender->tag(); |
| 274 SetLabelButtonBackgroundColor(selected_app_tag_, |
| 275 SkColorSetRGB(0xeb, 0xeb, 0xeb)); |
| 276 always_button_->SetState(views::Button::STATE_NORMAL); |
| 277 just_once_button_->SetState(views::Button::STATE_NORMAL); |
| 278 } |
242 } | 279 } |
243 | 280 |
244 gfx::Size IntentPickerBubbleView::GetPreferredSize() const { | 281 gfx::Size IntentPickerBubbleView::GetPreferredSize() const { |
245 gfx::Size ps; | 282 gfx::Size ps; |
246 ps.set_width(kMaxWidth); | 283 ps.set_width(kMaxWidth); |
247 int apps_height = app_info_.size(); | 284 int apps_height = app_info_.size(); |
248 // We are showing |kMaxAppResults| + 0.5 rows at max, the extra 0.5 is used so | 285 // We are showing |kMaxAppResults| + 0.5 rows at max, the extra 0.5 is used so |
249 // the user can notice that more options are available. | 286 // the user can notice that more options are available. |
250 if (app_info_.size() > kMaxAppResults) { | 287 if (app_info_.size() > kMaxAppResults) { |
251 apps_height = (kMaxAppResults + 0.5) * kRowHeight; | 288 apps_height = (kMaxAppResults + 0.5) * kRowHeight; |
252 } else { | 289 } else { |
253 apps_height *= kRowHeight; | 290 apps_height *= kRowHeight; |
254 } | 291 } |
255 ps.set_height(apps_height + kDialogDelegateInsets); | 292 ps.set_height(apps_height + kHeaderHeight + kFooterHeight); |
256 return ps; | 293 return ps; |
257 } | 294 } |
258 | 295 |
259 // If the actual web_contents gets destroyed in the middle of the process we | 296 // If the actual web_contents gets destroyed in the middle of the process we |
260 // should inform the caller about this error. | 297 // should inform the caller about this error. |
261 void IntentPickerBubbleView::WebContentsDestroyed() { | 298 void IntentPickerBubbleView::WebContentsDestroyed() { |
| 299 if (!was_callback_run_) { |
| 300 was_callback_run_ = true; |
| 301 throttle_cb_.Run(kAppTagNoneSelected, |
| 302 arc::ArcNavigationThrottle::CloseReason::ERROR); |
| 303 } |
262 GetWidget()->Close(); | 304 GetWidget()->Close(); |
263 } | 305 } |
264 | 306 |
265 IntentPickerLabelButton* IntentPickerBubbleView::GetIntentPickerLabelButtonAt( | 307 views::LabelButton* IntentPickerBubbleView::GetLabelButtonAt(size_t index) { |
266 size_t index) { | |
267 views::View* temp_contents = scroll_view_->contents(); | 308 views::View* temp_contents = scroll_view_->contents(); |
268 return static_cast<IntentPickerLabelButton*>(temp_contents->child_at(index)); | 309 return static_cast<views::LabelButton*>(temp_contents->child_at(index)); |
269 } | 310 } |
270 | 311 |
271 void IntentPickerBubbleView::RunCallback( | 312 void IntentPickerBubbleView::SetLabelButtonBackgroundColor(size_t index, |
272 std::string package, | 313 SkColor color) { |
273 arc::ArcNavigationThrottle::CloseReason close_reason) { | 314 views::LabelButton* temp_lb = GetLabelButtonAt(index); |
274 if (!intent_picker_cb_.is_null()) { | 315 temp_lb->set_background(views::Background::CreateSolidBackground(color)); |
275 // We must ensure |intent_picker_cb_| is only Run() once, this is why we | 316 temp_lb->SchedulePaint(); |
276 // have a temporary |callback| helper, so we can set the original callback | |
277 // to null and still report back to whoever started the UI. | |
278 auto callback = intent_picker_cb_; | |
279 intent_picker_cb_.Reset(); | |
280 callback.Run(package, close_reason); | |
281 } | |
282 } | 317 } |
283 | |
284 gfx::ImageSkia IntentPickerBubbleView::GetAppImageForTesting(size_t index) { | |
285 return GetIntentPickerLabelButtonAt(index)->GetImage( | |
286 views::Button::ButtonState::STATE_NORMAL); | |
287 } | |
288 | |
289 views::InkDropState IntentPickerBubbleView::GetInkDropStateForTesting( | |
290 size_t index) { | |
291 return GetIntentPickerLabelButtonAt(index)->GetTargetInkDropState(); | |
292 } | |
293 | |
294 void IntentPickerBubbleView::PressButtonForTesting(size_t index, | |
295 const ui::Event& event) { | |
296 views::Button* button = | |
297 static_cast<views::Button*>(GetIntentPickerLabelButtonAt(index)); | |
298 ButtonPressed(button, event); | |
299 } | |
OLD | NEW |