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 | |
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 }; | |
48 | 40 |
49 } // namespace | 41 } // namespace |
50 | 42 |
43 // TODO(djacobo): Look for a better way to implement this, if not possible then | |
44 // use the same dummy here and in the unit_tests. | |
45 // Dummy mouse press event. | |
46 class MousePressedEvent : public ui::Event { | |
sky
2016/10/13 13:35:07
This seems like a total hack, can you describe why
djacobo_
2016/10/14 02:35:32
I modified MarkAsSelected() so we could pass nullp
| |
47 public: | |
48 MousePressedEvent() : Event(ui::ET_MOUSE_PRESSED, base::TimeTicks(), 0) {} | |
49 ~MousePressedEvent() override {} | |
50 }; | |
51 | |
52 // IntentPickerLabelButton | |
53 | |
54 // A button that represents a candidate intent handler. | |
55 class IntentPickerLabelButton : public views::LabelButton { | |
56 public: | |
57 IntentPickerLabelButton(views::ButtonListener* listener, | |
58 gfx::Image* icon, | |
59 const std::string& package_name, | |
60 const std::string& activity_name) | |
61 : LabelButton(listener, | |
62 base::UTF8ToUTF16(base::StringPiece(activity_name))), | |
63 package_name_(package_name) { | |
64 SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
65 SetFocusBehavior(View::FocusBehavior::ALWAYS); | |
66 SetMinSize(gfx::Size(kMaxWidth, kRowHeight)); | |
67 SetInkDropMode(InkDropMode::ON); | |
68 if (!icon->IsEmpty()) { | |
sky
2016/10/13 13:35:07
no {}
djacobo_
2016/10/14 02:35:31
Done.
| |
69 SetImage(views::ImageButton::STATE_NORMAL, *icon->ToImageSkia()); | |
70 } | |
71 SetBorder(views::Border::CreateEmptyBorder(10, 16, 10, 0)); | |
72 } | |
73 | |
74 SkColor GetInkDropBaseColor() const override { return SK_ColorBLACK; } | |
75 | |
76 void MarkAsUnselected(const ui::Event& event) { | |
77 AnimateInkDrop(views::InkDropState::DEACTIVATED, | |
78 ui::LocatedEvent::FromIfValid(&event)); | |
79 } | |
80 | |
81 void MarkAsSelected(const ui::Event& event) { | |
82 AnimateInkDrop(views::InkDropState::ACTIVATED, | |
83 ui::LocatedEvent::FromIfValid(&event)); | |
84 } | |
85 | |
86 views::InkDropState GetTargetInkDropState() { | |
87 return ink_drop()->GetTargetInkDropState(); | |
88 } | |
89 | |
90 private: | |
91 std::string package_name_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(IntentPickerLabelButton); | |
94 }; | |
95 | |
51 // static | 96 // static |
52 void IntentPickerBubbleView::ShowBubble( | 97 void IntentPickerBubbleView::ShowBubble( |
53 content::WebContents* web_contents, | 98 content::WebContents* web_contents, |
54 const std::vector<NameAndIcon>& app_info, | 99 const std::vector<AppInfo>& app_info, |
55 const ThrottleCallback& throttle_cb) { | 100 const IntentPickerResponse& intent_picker_cb) { |
56 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | 101 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
57 if (!browser) { | 102 if (!browser || !BrowserView::GetBrowserViewForBrowser(browser)) { |
58 throttle_cb.Run(kAppTagNoneSelected, | 103 intent_picker_cb.Run("" /* Invalid package name */, |
sky
2016/10/13 13:35:07
"" -> std::string() unless somewhere defines a con
djacobo_
2016/10/14 02:35:31
Done.
| |
59 arc::ArcNavigationThrottle::CloseReason::ERROR); | 104 arc::ArcNavigationThrottle::CloseReason::ERROR); |
60 return; | 105 return; |
61 } | 106 } |
62 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); | 107 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 = | 108 IntentPickerBubbleView* delegate = |
70 new IntentPickerBubbleView(app_info, throttle_cb, web_contents); | 109 new IntentPickerBubbleView(app_info, intent_picker_cb, web_contents); |
71 delegate->set_margins(gfx::Insets()); | 110 delegate->set_margins(gfx::Insets(16, 0, 0, 0)); |
72 delegate->set_parent_window(browser_view->GetNativeWindow()); | 111 delegate->set_parent_window(browser_view->GetNativeWindow()); |
73 views::Widget* widget = | 112 views::Widget* widget = |
74 views::BubbleDialogDelegateView::CreateBubble(delegate); | 113 views::BubbleDialogDelegateView::CreateBubble(delegate); |
75 | 114 |
76 delegate->SetArrowPaintType(views::BubbleBorder::PAINT_NONE); | 115 delegate->SetArrowPaintType(views::BubbleBorder::PAINT_NONE); |
77 delegate->SetAlignment(views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE); | 116 delegate->SetAlignment(views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE); |
78 | 117 |
79 // Using the TopContainerBoundsInScreen Rect to specify an anchor for the the | 118 // 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 | 119 // UI. Rect allow us to set the coordinates(x,y), the width and height for the |
81 // new Rectangle. | 120 // new Rectangle. |
82 delegate->SetAnchorRect( | 121 delegate->SetAnchorRect( |
83 gfx::Rect(browser_view->GetTopContainerBoundsInScreen().x(), | 122 gfx::Rect(browser_view->GetTopContainerBoundsInScreen().x(), |
84 browser_view->GetTopContainerBoundsInScreen().y(), | 123 browser_view->GetTopContainerBoundsInScreen().y(), |
85 browser_view->GetTopContainerBoundsInScreen().width(), | 124 browser_view->GetTopContainerBoundsInScreen().width(), |
86 browser_view->GetTopContainerBoundsInScreen().height() - | 125 browser_view->GetTopContainerBoundsInScreen().height() - |
87 kTopContainerMerge)); | 126 kTopContainerMerge)); |
88 widget->Show(); | 127 widget->Show(); |
sky
2016/10/13 13:35:07
Why do you show first, then change a bunch of stuf
djacobo_
2016/10/14 02:35:31
That was a mistake, for a minute I thought Init()
| |
128 delegate->GetDialogClientView()->set_button_row_insets( | |
129 gfx::Insets(kDialogDelegateInsets)); | |
130 delegate->GetDialogClientView()->Layout(); | |
131 delegate->GetIntentPickerLabelButtonAt(0)->MarkAsSelected( | |
132 MousePressedEvent()); | |
89 } | 133 } |
90 | 134 |
91 // static | 135 // static |
92 std::unique_ptr<IntentPickerBubbleView> | 136 std::unique_ptr<IntentPickerBubbleView> |
93 IntentPickerBubbleView::CreateBubbleView( | 137 IntentPickerBubbleView::CreateBubbleView( |
94 const std::vector<NameAndIcon>& app_info, | 138 const std::vector<AppInfo>& app_info, |
95 const ThrottleCallback& throttle_cb, | 139 const IntentPickerResponse& intent_picker_cb, |
96 content::WebContents* web_contents) { | 140 content::WebContents* web_contents) { |
97 std::unique_ptr<IntentPickerBubbleView> bubble( | 141 std::unique_ptr<IntentPickerBubbleView> bubble( |
98 new IntentPickerBubbleView(app_info, throttle_cb, web_contents)); | 142 new IntentPickerBubbleView(app_info, intent_picker_cb, web_contents)); |
99 bubble->Init(); | 143 bubble->Init(); |
100 return bubble; | 144 return bubble; |
101 } | 145 } |
102 | 146 |
147 bool IntentPickerBubbleView::Accept() { | |
148 if (!intent_picker_cb_.is_null()) { | |
149 auto callback = intent_picker_cb_; | |
sky
2016/10/13 13:35:07
It's worth commenting as to why you do this rather
djacobo_
2016/10/14 02:35:31
Adding a comment to RunCallback(), method that now
| |
150 intent_picker_cb_.Reset(); | |
151 callback.Run(app_info_[selected_app_tag_].package_name, | |
152 arc::ArcNavigationThrottle::CloseReason::JUST_ONCE_PRESSED); | |
153 } | |
154 return true; | |
155 } | |
156 | |
157 bool IntentPickerBubbleView::Cancel() { | |
158 if (!intent_picker_cb_.is_null()) { | |
sky
2016/10/13 13:35:07
This close is nearly the same as the accept/cancel
djacobo_
2016/10/14 02:35:31
Done.
| |
159 auto callback = intent_picker_cb_; | |
160 intent_picker_cb_.Reset(); | |
161 callback.Run(app_info_[selected_app_tag_].package_name, | |
162 arc::ArcNavigationThrottle::CloseReason::ALWAYS_PRESSED); | |
163 } | |
164 return true; | |
165 } | |
166 | |
167 bool IntentPickerBubbleView::Close() { | |
168 // Whenever closing the bubble without pressing |Just once| or |Always| we | |
169 // need to report back that the user didn't select anything. | |
170 if (!intent_picker_cb_.is_null()) { | |
171 auto callback = intent_picker_cb_; | |
172 intent_picker_cb_.Reset(); | |
173 callback.Run("" /* Invalid package name */, | |
sky
2016/10/13 13:35:07
std::string(). But as you have this in a couple of
djacobo_
2016/10/14 02:35:31
Done.
| |
174 arc::ArcNavigationThrottle::CloseReason::DIALOG_DEACTIVATED); | |
175 } | |
176 return true; | |
177 } | |
178 | |
179 int IntentPickerBubbleView::GetDefaultDialogButton() const { | |
180 return ui::DIALOG_BUTTON_OK; | |
181 } | |
182 | |
103 void IntentPickerBubbleView::Init() { | 183 void IntentPickerBubbleView::Init() { |
104 SkColor button_text_color = SkColorSetRGB(0x42, 0x85, 0xf4); | |
105 | |
106 views::BoxLayout* general_layout = | 184 views::BoxLayout* general_layout = |
107 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); | 185 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); |
108 SetLayoutManager(general_layout); | 186 SetLayoutManager(general_layout); |
109 | 187 |
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. | 188 // Creates a view to hold the views for each app. |
164 views::View* scrollable_view = new views::View(); | 189 views::View* scrollable_view = new views::View(); |
165 views::BoxLayout* scrollable_layout = | 190 views::BoxLayout* scrollable_layout = |
166 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); | 191 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); |
167 scrollable_view->SetLayoutManager(scrollable_layout); | 192 scrollable_view->SetLayoutManager(scrollable_layout); |
168 for (size_t i = 0; i < app_info_.size(); ++i) { | 193 for (size_t i = 0; i < app_info_.size(); ++i) { |
169 views::LabelButton* tmp_label = new views::LabelButton( | 194 IntentPickerLabelButton* app_button = new IntentPickerLabelButton( |
170 this, base::UTF8ToUTF16(base::StringPiece(app_info_[i].first))); | 195 this, &app_info_[i].icon, app_info_[i].package_name, |
171 tmp_label->SetFocusBehavior(View::FocusBehavior::ALWAYS); | 196 app_info_[i].activity_name); |
172 tmp_label->SetFontList(gfx::FontList("Roboto-regular, 13px")); | 197 app_button->set_tag(i); |
173 tmp_label->SetImageLabelSpacing(kLabelImageSeparation); | 198 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 } | 199 } |
184 | 200 |
185 scroll_view_ = new views::ScrollView(); | 201 scroll_view_ = new views::ScrollView(); |
186 scroll_view_->SetContents(scrollable_view); | 202 scroll_view_->SetContents(scrollable_view); |
187 // Setting a customized ScrollBar which is shown only when the mouse pointer | 203 // Setting a customized ScrollBar which is shown only when the mouse pointer |
188 // is inside the ScrollView. | 204 // is inside the ScrollView. |
189 scroll_view_->SetVerticalScrollBar(new views::OverlayScrollBar(false)); | 205 scroll_view_->SetVerticalScrollBar(new views::OverlayScrollBar(false)); |
190 // This part gives the scroll a fixed width and height. The height depends on | 206 // 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. | 207 // 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 | 208 // 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. | 209 // more than |kMaxAppResults| apps accessible by scrolling the list. |
194 if (app_info_.size() <= kMaxAppResults) { | 210 if (app_info_.size() <= kMaxAppResults) { |
195 scroll_view_->ClipHeightTo(kRowHeight, app_info_.size() * kRowHeight); | 211 scroll_view_->ClipHeightTo(kRowHeight, app_info_.size() * kRowHeight); |
196 } else { | 212 } else { |
197 scroll_view_->ClipHeightTo(kRowHeight, (kMaxAppResults + 0.5) * kRowHeight); | 213 scroll_view_->ClipHeightTo(kRowHeight, (kMaxAppResults + 0.5) * kRowHeight); |
198 } | 214 } |
199 AddChildView(scroll_view_); | 215 AddChildView(scroll_view_); |
200 | 216 |
201 // The lower part of the Picker contains only the 2 buttons | 217 // TODO(djacobo|bruthig): Investigate why the ScrollView does not clip child |
sky
2016/10/13 13:35:07
Can you clarify what behavior you are seeing that
djacobo_
2016/10/14 02:35:31
The problem I was observing is, suppose you select
bruthig
2016/10/14 14:08:53
It's been a while since I looked at this, but from
sky
2016/10/14 16:08:24
I'm surprised your fix works. I would expect the c
bruthig
2016/10/14 16:49:51
To be clear here, and hopefully helpful :), this w
djacobo_
2016/10/14 22:30:46
So the quick fix would be to reuse the part of the
bruthig
2016/10/17 13:55:42
Sorry for my poor wording, but that is what I mean
djacobo_
2016/10/17 17:11:25
let me give it another shot in the separated bug.
| |
202 // |just_once_button_| and |always_button_|. | 218 // view layers when the Ink Drop is active. |
203 View* footer = new View(); | 219 SetPaintToLayer(true); |
204 footer->SetBorder(views::Border::CreateEmptyBorder(24, 0, 12, 12)); | 220 layer()->SetMasksToBounds(true); |
205 views::BoxLayout* buttons_layout = new views::BoxLayout( | 221 layer()->SetFillsBoundsOpaquely(false); |
206 views::BoxLayout::kHorizontal, 0, 0, kButtonSeparation); | 222 } |
207 footer->SetLayoutManager(buttons_layout); | |
208 | 223 |
209 buttons_layout->set_main_axis_alignment( | 224 base::string16 IntentPickerBubbleView::GetWindowTitle() const { |
210 views::BoxLayout::MAIN_AXIS_ALIGNMENT_END); | 225 return l10n_util::GetStringUTF16(IDS_INTENT_PICKER_BUBBLE_VIEW_OPEN_WITH); |
211 footer->AddChildView(just_once_button_); | 226 } |
212 footer->AddChildView(always_button_); | 227 |
213 AddChildView(footer); | 228 base::string16 IntentPickerBubbleView::GetDialogButtonLabel( |
229 ui::DialogButton button) const { | |
230 return l10n_util::GetStringUTF16(button == ui::DIALOG_BUTTON_OK | |
231 ? IDS_INTENT_PICKER_BUBBLE_VIEW_JUST_ONCE | |
232 : IDS_INTENT_PICKER_BUBBLE_VIEW_ALWAYS); | |
214 } | 233 } |
215 | 234 |
216 IntentPickerBubbleView::IntentPickerBubbleView( | 235 IntentPickerBubbleView::IntentPickerBubbleView( |
217 const std::vector<NameAndIcon>& app_info, | 236 const std::vector<AppInfo>& app_info, |
218 ThrottleCallback throttle_cb, | 237 IntentPickerResponse intent_picker_cb, |
219 content::WebContents* web_contents) | 238 content::WebContents* web_contents) |
220 : views::BubbleDialogDelegateView(nullptr /* anchor_view */, | 239 : views::BubbleDialogDelegateView(nullptr /* anchor_view */, |
221 views::BubbleBorder::TOP_CENTER), | 240 views::BubbleBorder::TOP_CENTER), |
222 WebContentsObserver(web_contents), | 241 WebContentsObserver(web_contents), |
223 was_callback_run_(false), | 242 intent_picker_cb_(intent_picker_cb), |
224 throttle_cb_(throttle_cb), | 243 selected_app_tag_(0), |
225 selected_app_tag_(kAppTagNoneSelected), | |
226 always_button_(nullptr), | |
227 just_once_button_(nullptr), | |
228 scroll_view_(nullptr), | 244 scroll_view_(nullptr), |
229 app_info_(app_info) {} | 245 app_info_(app_info) {} |
230 | 246 |
231 IntentPickerBubbleView::~IntentPickerBubbleView() { | 247 IntentPickerBubbleView::~IntentPickerBubbleView() { |
232 SetLayoutManager(nullptr); | 248 SetLayoutManager(nullptr); |
233 } | 249 } |
234 | 250 |
235 // If the widget gets closed without an app being selected we still need to use | 251 // 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. | 252 // the callback so the caller can Resume the navigation. |
237 void IntentPickerBubbleView::OnWidgetDestroying(views::Widget* widget) { | 253 void IntentPickerBubbleView::OnWidgetDestroying(views::Widget* widget) { |
238 if (!was_callback_run_) { | 254 if (!intent_picker_cb_.is_null()) { |
239 was_callback_run_ = true; | 255 auto callback = intent_picker_cb_; |
240 throttle_cb_.Run( | 256 intent_picker_cb_.Reset(); |
241 kAppTagNoneSelected, | 257 callback.Run("" /* Invalid package name */, |
242 arc::ArcNavigationThrottle::CloseReason::DIALOG_DEACTIVATED); | 258 arc::ArcNavigationThrottle::CloseReason::DIALOG_DEACTIVATED); |
243 } | 259 } |
244 } | 260 } |
245 | 261 |
246 int IntentPickerBubbleView::GetDialogButtons() const { | |
247 return ui::DIALOG_BUTTON_NONE; | |
248 } | |
249 | |
250 void IntentPickerBubbleView::ButtonPressed(views::Button* sender, | 262 void IntentPickerBubbleView::ButtonPressed(views::Button* sender, |
251 const ui::Event& event) { | 263 const ui::Event& event) { |
252 switch (sender->tag()) { | 264 // The selected app must be a value in the range [0, app_info_.size()-1]. |
253 case static_cast<int>(Option::ALWAYS): | 265 DCHECK_LT(static_cast<size_t>(sender->tag()), app_info_.size()); |
254 was_callback_run_ = true; | 266 GetIntentPickerLabelButtonAt(selected_app_tag_)->MarkAsUnselected(event); |
255 throttle_cb_.Run(selected_app_tag_, | 267 |
256 arc::ArcNavigationThrottle::CloseReason::ALWAYS_PRESSED); | 268 selected_app_tag_ = sender->tag(); |
257 GetWidget()->Close(); | 269 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 } | 270 } |
280 | 271 |
281 gfx::Size IntentPickerBubbleView::GetPreferredSize() const { | 272 gfx::Size IntentPickerBubbleView::GetPreferredSize() const { |
282 gfx::Size ps; | 273 gfx::Size ps; |
283 ps.set_width(kMaxWidth); | 274 ps.set_width(kMaxWidth); |
284 int apps_height = app_info_.size(); | 275 int apps_height = app_info_.size(); |
285 // We are showing |kMaxAppResults| + 0.5 rows at max, the extra 0.5 is used so | 276 // 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. | 277 // the user can notice that more options are available. |
287 if (app_info_.size() > kMaxAppResults) { | 278 if (app_info_.size() > kMaxAppResults) { |
288 apps_height = (kMaxAppResults + 0.5) * kRowHeight; | 279 apps_height = (kMaxAppResults + 0.5) * kRowHeight; |
289 } else { | 280 } else { |
290 apps_height *= kRowHeight; | 281 apps_height *= kRowHeight; |
291 } | 282 } |
292 ps.set_height(apps_height + kHeaderHeight + kFooterHeight); | 283 ps.set_height(apps_height + kDialogDelegateInsets); |
293 return ps; | 284 return ps; |
294 } | 285 } |
295 | 286 |
296 // If the actual web_contents gets destroyed in the middle of the process we | 287 // If the actual web_contents gets destroyed in the middle of the process we |
297 // should inform the caller about this error. | 288 // should inform the caller about this error. |
298 void IntentPickerBubbleView::WebContentsDestroyed() { | 289 void IntentPickerBubbleView::WebContentsDestroyed() { |
299 if (!was_callback_run_) { | 290 if (!intent_picker_cb_.is_null()) { |
sky
2016/10/13 13:35:07
Refactor this to the common place too. That said,
djacobo_
2016/10/14 02:35:31
Refactored, but WebContentsDestroyed() is still ne
sky
2016/10/14 16:08:24
Sorry if I wasn't clear. You definitely want the W
djacobo_
2016/10/14 22:30:46
Done.
| |
300 was_callback_run_ = true; | 291 auto callback = intent_picker_cb_; |
301 throttle_cb_.Run(kAppTagNoneSelected, | 292 intent_picker_cb_.Reset(); |
302 arc::ArcNavigationThrottle::CloseReason::ERROR); | 293 callback.Run("" /* Invalid package name */, |
294 arc::ArcNavigationThrottle::CloseReason::ERROR); | |
303 } | 295 } |
304 GetWidget()->Close(); | 296 GetWidget()->Close(); |
305 } | 297 } |
306 | 298 |
307 views::LabelButton* IntentPickerBubbleView::GetLabelButtonAt(size_t index) { | 299 IntentPickerLabelButton* IntentPickerBubbleView::GetIntentPickerLabelButtonAt( |
300 size_t index) { | |
308 views::View* temp_contents = scroll_view_->contents(); | 301 views::View* temp_contents = scroll_view_->contents(); |
309 return static_cast<views::LabelButton*>(temp_contents->child_at(index)); | 302 return static_cast<IntentPickerLabelButton*>(temp_contents->child_at(index)); |
310 } | 303 } |
311 | 304 |
312 void IntentPickerBubbleView::SetLabelButtonBackgroundColor(size_t index, | 305 gfx::ImageSkia IntentPickerBubbleView::GetAppImageForTesting(size_t index) { |
313 SkColor color) { | 306 return GetIntentPickerLabelButtonAt(index)->GetImage( |
314 views::LabelButton* temp_lb = GetLabelButtonAt(index); | 307 views::Button::ButtonState::STATE_NORMAL); |
315 temp_lb->set_background(views::Background::CreateSolidBackground(color)); | |
316 temp_lb->SchedulePaint(); | |
317 } | 308 } |
309 | |
310 views::InkDropState IntentPickerBubbleView::GetInkDropStateForTesting( | |
311 size_t index) { | |
312 return GetIntentPickerLabelButtonAt(index)->GetTargetInkDropState(); | |
313 } | |
314 | |
315 void IntentPickerBubbleView::PressButtonForTesting(size_t index, | |
316 const ui::Event& event) { | |
317 views::Button* button = | |
318 static_cast<views::Button*>(GetIntentPickerLabelButtonAt(index)); | |
319 ButtonPressed(button, event); | |
320 } | |
OLD | NEW |