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