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 |
32 constexpr int kRowHeight = 40; | 34 constexpr int kRowHeight = 40; |
33 constexpr int kMaxWidth = 320; | 35 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 | 36 |
40 // UI position wrt the Top Container | 37 // UI position wrt the Top Container |
41 constexpr int kTopContainerMerge = 3; | 38 constexpr int kTopContainerMerge = 3; |
42 constexpr size_t kAppTagNoneSelected = static_cast<size_t>(-1); | 39 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 } // namespace |
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 // IntentPickerLabelButton |
44 | |
45 // This is a convenience class for the buttons on the intent picker so we can | |
46 // manipulate the internal hovering behavior. | |
Evan Stade
2016/09/14 21:53:08
Suggested comment: A button that represents a cand
djacobo_
2016/09/16 01:23:08
Done.
| |
47 class IntentPickerLabelButton : public views::LabelButton { | |
48 public: | |
49 IntentPickerLabelButton(views::ButtonListener* listener, | |
50 const base::string16& title) | |
51 : LabelButton(listener, title) { | |
52 SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
53 SetFocusBehavior(View::FocusBehavior::ALWAYS); | |
54 SetInkDropMode(InkDropMode::ON); | |
55 } | |
56 | |
57 SkColor GetInkDropBaseColor() const override { return SK_ColorBLACK; } | |
58 | |
59 void MarkAsUnselected(const ui::Event& event) { | |
60 AnimateInkDrop(views::InkDropState::DEACTIVATED, | |
61 ui::LocatedEvent::FromIfValid(&event)); | |
62 } | |
63 | |
64 void MarkAsSelected(const ui::Event& event) { | |
65 AnimateInkDrop(views::InkDropState::ACTIVATED, | |
66 ui::LocatedEvent::FromIfValid(&event)); | |
67 } | |
68 | |
69 views::InkDropState GetTargetInkDropState() { | |
70 return ink_drop()->GetTargetInkDropState(); | |
71 } | |
72 | |
73 private: | |
74 DISALLOW_COPY_AND_ASSIGN(IntentPickerLabelButton); | |
75 }; | |
50 | 76 |
51 // static | 77 // static |
52 void IntentPickerBubbleView::ShowBubble( | 78 void IntentPickerBubbleView::ShowBubble( |
53 content::WebContents* web_contents, | 79 content::WebContents* web_contents, |
54 const std::vector<NameAndIcon>& app_info, | 80 const std::vector<NameAndIcon>& app_info, |
55 const ThrottleCallback& throttle_cb) { | 81 const IntentPickerResponse& intent_picker_cb) { |
56 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | 82 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
57 if (!browser) { | 83 if (!browser || !BrowserView::GetBrowserViewForBrowser(browser)) { |
58 throttle_cb.Run(kAppTagNoneSelected, | 84 intent_picker_cb.Run(kAppTagNoneSelected, |
59 arc::ArcNavigationThrottle::CloseReason::ERROR); | 85 arc::ArcNavigationThrottle::CloseReason::ERROR); |
60 return; | 86 return; |
61 } | 87 } |
62 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); | 88 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 = | 89 IntentPickerBubbleView* delegate = |
70 new IntentPickerBubbleView(app_info, throttle_cb, web_contents); | 90 new IntentPickerBubbleView(app_info, intent_picker_cb, web_contents); |
71 delegate->set_margins(gfx::Insets()); | 91 delegate->set_margins(gfx::Insets()); |
72 delegate->set_parent_window(browser_view->GetNativeWindow()); | 92 delegate->set_parent_window(browser_view->GetNativeWindow()); |
73 views::Widget* widget = | 93 views::Widget* widget = |
74 views::BubbleDialogDelegateView::CreateBubble(delegate); | 94 views::BubbleDialogDelegateView::CreateBubble(delegate); |
75 | 95 |
76 delegate->SetArrowPaintType(views::BubbleBorder::PAINT_NONE); | 96 delegate->SetArrowPaintType(views::BubbleBorder::PAINT_NONE); |
77 delegate->SetAlignment(views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE); | 97 delegate->SetAlignment(views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE); |
78 | 98 |
79 // Using the TopContainerBoundsInScreen Rect to specify an anchor for the the | 99 // 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 | 100 // UI. Rect allow us to set the coordinates(x,y), the width and height for the |
81 // new Rectangle. | 101 // new Rectangle. |
82 delegate->SetAnchorRect( | 102 delegate->SetAnchorRect( |
83 gfx::Rect(browser_view->GetTopContainerBoundsInScreen().x(), | 103 gfx::Rect(browser_view->GetTopContainerBoundsInScreen().x(), |
84 browser_view->GetTopContainerBoundsInScreen().y(), | 104 browser_view->GetTopContainerBoundsInScreen().y(), |
85 browser_view->GetTopContainerBoundsInScreen().width(), | 105 browser_view->GetTopContainerBoundsInScreen().width(), |
86 browser_view->GetTopContainerBoundsInScreen().height() - | 106 browser_view->GetTopContainerBoundsInScreen().height() - |
87 kTopContainerMerge)); | 107 kTopContainerMerge)); |
88 widget->Show(); | 108 widget->Show(); |
89 } | 109 } |
90 | 110 |
91 // static | 111 // static |
92 std::unique_ptr<IntentPickerBubbleView> | 112 std::unique_ptr<IntentPickerBubbleView> |
93 IntentPickerBubbleView::CreateBubbleView( | 113 IntentPickerBubbleView::CreateBubbleView( |
94 const std::vector<NameAndIcon>& app_info, | 114 const std::vector<NameAndIcon>& app_info, |
95 const ThrottleCallback& throttle_cb, | 115 const IntentPickerResponse& intent_picker_cb, |
96 content::WebContents* web_contents) { | 116 content::WebContents* web_contents) { |
97 std::unique_ptr<IntentPickerBubbleView> bubble( | 117 std::unique_ptr<IntentPickerBubbleView> bubble( |
98 new IntentPickerBubbleView(app_info, throttle_cb, web_contents)); | 118 new IntentPickerBubbleView(app_info, intent_picker_cb, web_contents)); |
99 bubble->Init(); | 119 bubble->Init(); |
100 return bubble; | 120 return bubble; |
101 } | 121 } |
102 | 122 |
123 bool IntentPickerBubbleView::Accept() { | |
124 if (!intent_picker_cb_.is_null()) { | |
125 auto callback = intent_picker_cb_; | |
126 intent_picker_cb_.Reset(); | |
127 callback.Run(selected_app_tag_, | |
128 arc::ArcNavigationThrottle::CloseReason::JUST_ONCE_PRESSED); | |
129 } | |
130 return true; | |
131 } | |
132 | |
133 bool IntentPickerBubbleView::Cancel() { | |
134 if (!intent_picker_cb_.is_null()) { | |
135 auto callback = intent_picker_cb_; | |
136 intent_picker_cb_.Reset(); | |
137 callback.Run(selected_app_tag_, | |
138 arc::ArcNavigationThrottle::CloseReason::ALWAYS_PRESSED); | |
139 } | |
140 return true; | |
141 } | |
142 | |
143 bool IntentPickerBubbleView::Close() { | |
144 // Whenever closing the bubble without pressing |JUST ONCE| or |ALWAYS| we | |
145 // need to report back that the user didn't select anything. | |
146 if (!intent_picker_cb_.is_null()) { | |
147 auto callback = intent_picker_cb_; | |
148 intent_picker_cb_.Reset(); | |
149 callback.Run(kAppTagNoneSelected, | |
150 arc::ArcNavigationThrottle::CloseReason::DIALOG_DEACTIVATED); | |
151 } | |
152 return true; | |
153 } | |
154 | |
155 int IntentPickerBubbleView::GetDefaultDialogButton() const { | |
156 return ui::DIALOG_BUTTON_OK; | |
157 } | |
158 | |
159 bool IntentPickerBubbleView::IsDialogButtonEnabled( | |
160 ui::DialogButton button) const { | |
161 // Both buttons are enabled only when the user selected one of the apps. | |
162 return selected_app_tag_ != kAppTagNoneSelected; | |
163 } | |
164 | |
103 void IntentPickerBubbleView::Init() { | 165 void IntentPickerBubbleView::Init() { |
104 SkColor button_text_color = SkColorSetRGB(0x42, 0x85, 0xf4); | |
105 | |
106 views::BoxLayout* general_layout = | 166 views::BoxLayout* general_layout = |
107 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); | 167 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); |
108 SetLayoutManager(general_layout); | 168 SetLayoutManager(general_layout); |
109 | 169 |
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. | 170 // Creates a view to hold the views for each app. |
164 views::View* scrollable_view = new views::View(); | 171 views::View* scrollable_view = new views::View(); |
165 views::BoxLayout* scrollable_layout = | 172 views::BoxLayout* scrollable_layout = |
166 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); | 173 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); |
167 scrollable_view->SetLayoutManager(scrollable_layout); | 174 scrollable_view->SetLayoutManager(scrollable_layout); |
168 for (size_t i = 0; i < app_info_.size(); ++i) { | 175 for (size_t i = 0; i < app_info_.size(); ++i) { |
169 views::LabelButton* tmp_label = new views::LabelButton( | 176 IntentPickerLabelButton* app_button = new IntentPickerLabelButton( |
170 this, base::UTF8ToUTF16(base::StringPiece(app_info_[i].first))); | 177 this, base::UTF8ToUTF16(base::StringPiece(app_info_[i].first))); |
171 tmp_label->SetFocusBehavior(View::FocusBehavior::ALWAYS); | 178 app_button->SetMinSize(gfx::Size(kMaxWidth, kRowHeight)); |
172 tmp_label->SetFontList(gfx::FontList("Roboto-regular, 13px")); | 179 app_button->set_tag(i); |
Evan Stade
2016/09/14 21:53:08
seems like it would be more graceful to pass the a
djacobo_
2016/09/16 01:23:08
taking this offline.
| |
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()) { | 180 if (!app_info_[i].second.IsEmpty()) { |
178 tmp_label->SetImage(views::ImageButton::STATE_NORMAL, | 181 app_button->SetImage(views::ImageButton::STATE_NORMAL, |
179 *app_info_[i].second.ToImageSkia()); | 182 *app_info_[i].second.ToImageSkia()); |
180 } | 183 } |
181 tmp_label->SetBorder(views::Border::CreateEmptyBorder(10, 16, 10, 0)); | 184 app_button->SetBorder(views::Border::CreateEmptyBorder(10, 16, 10, 0)); |
Evan Stade
2016/09/14 21:53:08
all of these calls into app_button I think would b
djacobo_
2016/09/16 01:23:08
Most of the stuff was moved to IntentPickerLabelBu
| |
182 scrollable_view->AddChildViewAt(tmp_label, i); | 185 scrollable_view->AddChildViewAt(app_button, i); |
183 } | 186 } |
184 | 187 |
185 scroll_view_ = new views::ScrollView(); | 188 scroll_view_ = new views::ScrollView(); |
186 scroll_view_->SetContents(scrollable_view); | 189 scroll_view_->SetContents(scrollable_view); |
187 // Setting a customized ScrollBar which is shown only when the mouse pointer | 190 // Setting a customized ScrollBar which is shown only when the mouse pointer |
188 // is inside the ScrollView. | 191 // is inside the ScrollView. |
189 scroll_view_->SetVerticalScrollBar(new views::OverlayScrollBar(false)); | 192 scroll_view_->SetVerticalScrollBar(new views::OverlayScrollBar(false)); |
190 // This part gives the scroll a fixed width and height. The height depends on | 193 // 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. | 194 // 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 | 195 // 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. | 196 // more than |kMaxAppResults| apps accessible by scrolling the list. |
194 if (app_info_.size() <= kMaxAppResults) { | 197 if (app_info_.size() <= kMaxAppResults) { |
195 scroll_view_->ClipHeightTo(kRowHeight, app_info_.size() * kRowHeight); | 198 scroll_view_->ClipHeightTo(kRowHeight, app_info_.size() * kRowHeight); |
196 } else { | 199 } else { |
197 scroll_view_->ClipHeightTo(kRowHeight, (kMaxAppResults + 0.5) * kRowHeight); | 200 scroll_view_->ClipHeightTo(kRowHeight, (kMaxAppResults + 0.5) * kRowHeight); |
198 } | 201 } |
199 AddChildView(scroll_view_); | 202 AddChildView(scroll_view_); |
200 | 203 |
201 // The lower part of the Picker contains only the 2 buttons | 204 // TODO(djacobo|bruthig): Investigate why the ScrollView does not clip child |
202 // |just_once_button_| and |always_button_|. | 205 // view layers when the Ink Drop is active. |
203 View* footer = new View(); | 206 SetPaintToLayer(true); |
204 footer->SetBorder(views::Border::CreateEmptyBorder(24, 0, 12, 12)); | 207 layer()->SetMasksToBounds(true); |
205 views::BoxLayout* buttons_layout = new views::BoxLayout( | 208 layer()->SetFillsBoundsOpaquely(false); |
206 views::BoxLayout::kHorizontal, 0, 0, kButtonSeparation); | 209 } |
207 footer->SetLayoutManager(buttons_layout); | |
208 | 210 |
209 buttons_layout->set_main_axis_alignment( | 211 base::string16 IntentPickerBubbleView::GetWindowTitle() const { |
210 views::BoxLayout::MAIN_AXIS_ALIGNMENT_END); | 212 return l10n_util::GetStringUTF16(IDS_INTENT_PICKER_BUBBLE_VIEW_OPEN_WITH); |
211 footer->AddChildView(just_once_button_); | 213 } |
212 footer->AddChildView(always_button_); | 214 |
213 AddChildView(footer); | 215 base::string16 IntentPickerBubbleView::GetDialogButtonLabel( |
216 ui::DialogButton button) const { | |
217 if (button == ui::DIALOG_BUTTON_OK) | |
218 return l10n_util::GetStringUTF16(IDS_INTENT_PICKER_BUBBLE_VIEW_JUST_ONCE); | |
Evan Stade
2016/09/14 21:53:08
nit: ternary
return l10n_util::GetString(button =
djacobo_
2016/09/16 01:23:08
Done.
| |
219 return l10n_util::GetStringUTF16(IDS_INTENT_PICKER_BUBBLE_VIEW_ALWAYS); | |
214 } | 220 } |
215 | 221 |
216 IntentPickerBubbleView::IntentPickerBubbleView( | 222 IntentPickerBubbleView::IntentPickerBubbleView( |
217 const std::vector<NameAndIcon>& app_info, | 223 const std::vector<NameAndIcon>& app_info, |
218 ThrottleCallback throttle_cb, | 224 IntentPickerResponse intent_picker_cb, |
219 content::WebContents* web_contents) | 225 content::WebContents* web_contents) |
220 : views::BubbleDialogDelegateView(nullptr /* anchor_view */, | 226 : views::BubbleDialogDelegateView(nullptr /* anchor_view */, |
221 views::BubbleBorder::TOP_CENTER), | 227 views::BubbleBorder::TOP_CENTER), |
222 WebContentsObserver(web_contents), | 228 WebContentsObserver(web_contents), |
223 was_callback_run_(false), | 229 intent_picker_cb_(intent_picker_cb), |
224 throttle_cb_(throttle_cb), | |
225 selected_app_tag_(kAppTagNoneSelected), | 230 selected_app_tag_(kAppTagNoneSelected), |
226 always_button_(nullptr), | |
227 just_once_button_(nullptr), | |
228 scroll_view_(nullptr), | 231 scroll_view_(nullptr), |
229 app_info_(app_info) {} | 232 app_info_(app_info) {} |
230 | 233 |
231 IntentPickerBubbleView::~IntentPickerBubbleView() { | 234 IntentPickerBubbleView::~IntentPickerBubbleView() { |
232 SetLayoutManager(nullptr); | 235 SetLayoutManager(nullptr); |
233 } | 236 } |
234 | 237 |
235 // If the widget gets closed without an app being selected we still need to use | 238 // 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. | 239 // the callback so the caller can Resume the navigation. |
237 void IntentPickerBubbleView::OnWidgetDestroying(views::Widget* widget) { | 240 void IntentPickerBubbleView::OnWidgetDestroying(views::Widget* widget) { |
238 if (!was_callback_run_) { | 241 if (!intent_picker_cb_.is_null()) { |
239 throttle_cb_.Run( | 242 auto callback = intent_picker_cb_; |
240 kAppTagNoneSelected, | 243 intent_picker_cb_.Reset(); |
241 arc::ArcNavigationThrottle::CloseReason::DIALOG_DEACTIVATED); | 244 callback.Run(kAppTagNoneSelected, |
242 was_callback_run_ = true; | 245 arc::ArcNavigationThrottle::CloseReason::DIALOG_DEACTIVATED); |
243 } | 246 } |
244 } | 247 } |
245 | 248 |
246 int IntentPickerBubbleView::GetDialogButtons() const { | |
247 return ui::DIALOG_BUTTON_NONE; | |
248 } | |
249 | |
250 void IntentPickerBubbleView::ButtonPressed(views::Button* sender, | 249 void IntentPickerBubbleView::ButtonPressed(views::Button* sender, |
251 const ui::Event& event) { | 250 const ui::Event& event) { |
252 switch (sender->tag()) { | 251 // The selected app must be a value in the range [0, app_info_.size()-1]. |
253 case static_cast<int>(Option::ALWAYS): | 252 DCHECK_LT(static_cast<size_t>(sender->tag()), app_info_.size()); |
254 throttle_cb_.Run(selected_app_tag_, | 253 if (selected_app_tag_ != kAppTagNoneSelected) |
255 arc::ArcNavigationThrottle::CloseReason::ALWAYS_PRESSED); | 254 GetIntentPickerLabelButtonAt(selected_app_tag_)->MarkAsUnselected(event); |
256 was_callback_run_ = true; | 255 |
257 GetWidget()->Close(); | 256 selected_app_tag_ = sender->tag(); |
258 break; | 257 GetIntentPickerLabelButtonAt(selected_app_tag_)->MarkAsSelected(event); |
259 case static_cast<int>(Option::JUST_ONCE): | 258 |
260 throttle_cb_.Run( | 259 // This may not have the integrated |Accept| and |Cancel| buttons because of |
261 selected_app_tag_, | 260 // how the class gets created for testing. |
262 arc::ArcNavigationThrottle::CloseReason::JUST_ONCE_PRESSED); | 261 if (GetWidget()) |
263 was_callback_run_ = true; | 262 GetDialogClientView()->UpdateDialogButtons(); |
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 } | 263 } |
280 | 264 |
281 gfx::Size IntentPickerBubbleView::GetPreferredSize() const { | 265 gfx::Size IntentPickerBubbleView::GetPreferredSize() const { |
282 gfx::Size ps; | 266 gfx::Size ps; |
283 ps.set_width(kMaxWidth); | 267 ps.set_width(kMaxWidth); |
284 int apps_height = app_info_.size(); | 268 int apps_height = app_info_.size(); |
285 // We are showing |kMaxAppResults| + 0.5 rows at max, the extra 0.5 is used so | 269 // 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. | 270 // the user can notice that more options are available. |
287 if (app_info_.size() > kMaxAppResults) { | 271 if (app_info_.size() > kMaxAppResults) { |
288 apps_height = (kMaxAppResults + 0.5) * kRowHeight; | 272 apps_height = (kMaxAppResults + 0.5) * kRowHeight; |
289 } else { | 273 } else { |
290 apps_height *= kRowHeight; | 274 apps_height *= kRowHeight; |
291 } | 275 } |
292 ps.set_height(apps_height + kHeaderHeight + kFooterHeight); | 276 ps.set_height(apps_height); |
293 return ps; | 277 return ps; |
294 } | 278 } |
295 | 279 |
296 // If the actual web_contents gets destroyed in the middle of the process we | 280 // If the actual web_contents gets destroyed in the middle of the process we |
297 // should inform the caller about this error. | 281 // should inform the caller about this error. |
298 void IntentPickerBubbleView::WebContentsDestroyed() { | 282 void IntentPickerBubbleView::WebContentsDestroyed() { |
299 if (!was_callback_run_) { | 283 if (!intent_picker_cb_.is_null()) { |
300 throttle_cb_.Run(kAppTagNoneSelected, | 284 auto callback = intent_picker_cb_; |
301 arc::ArcNavigationThrottle::CloseReason::ERROR); | 285 intent_picker_cb_.Reset(); |
302 was_callback_run_ = true; | 286 callback.Run(kAppTagNoneSelected, |
287 arc::ArcNavigationThrottle::CloseReason::ERROR); | |
303 } | 288 } |
304 GetWidget()->Close(); | 289 GetWidget()->Close(); |
305 } | 290 } |
306 | 291 |
307 views::LabelButton* IntentPickerBubbleView::GetLabelButtonAt(size_t index) { | 292 IntentPickerLabelButton* IntentPickerBubbleView::GetIntentPickerLabelButtonAt( |
293 size_t index) { | |
308 views::View* temp_contents = scroll_view_->contents(); | 294 views::View* temp_contents = scroll_view_->contents(); |
309 return static_cast<views::LabelButton*>(temp_contents->child_at(index)); | 295 return static_cast<IntentPickerLabelButton*>(temp_contents->child_at(index)); |
310 } | 296 } |
311 | 297 |
312 void IntentPickerBubbleView::SetLabelButtonBackgroundColor(size_t index, | 298 gfx::ImageSkia IntentPickerBubbleView::GetAppImageForTesting(size_t index) { |
313 SkColor color) { | 299 return GetIntentPickerLabelButtonAt(index)->GetImage( |
314 views::LabelButton* temp_lb = GetLabelButtonAt(index); | 300 views::Button::ButtonState::STATE_NORMAL); |
315 temp_lb->set_background(views::Background::CreateSolidBackground(color)); | |
316 temp_lb->SchedulePaint(); | |
317 } | 301 } |
302 | |
303 views::InkDropState IntentPickerBubbleView::GetInkDropStateForTesting( | |
304 size_t index) { | |
305 return GetIntentPickerLabelButtonAt(index)->GetTargetInkDropState(); | |
306 } | |
307 | |
308 void IntentPickerBubbleView::PressButtonForTesting(size_t index, | |
309 const ui::Event& event) { | |
310 views::Button* button = | |
311 static_cast<views::Button*>(GetIntentPickerLabelButtonAt(index)); | |
312 ButtonPressed(button, event); | |
313 } | |
OLD | NEW |