| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/views/desktop_capture/desktop_media_picker_views.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/command_line.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "build/build_config.h" | |
| 14 #include "chrome/browser/media/combined_desktop_media_list.h" | |
| 15 #include "chrome/browser/media/desktop_media_list.h" | |
| 16 #include "chrome/browser/ui/ash/ash_util.h" | |
| 17 #include "chrome/browser/ui/views/desktop_media_picker_views_deprecated.h" | |
| 18 #include "chrome/common/chrome_switches.h" | |
| 19 #include "chrome/grit/chromium_strings.h" | |
| 20 #include "chrome/grit/generated_resources.h" | |
| 21 #include "chrome/grit/google_chrome_strings.h" | |
| 22 #include "components/constrained_window/constrained_window_views.h" | |
| 23 #include "components/web_modal/web_contents_modal_dialog_manager.h" | |
| 24 #include "content/public/browser/browser_thread.h" | |
| 25 #include "content/public/browser/render_frame_host.h" | |
| 26 #include "content/public/browser/web_contents_delegate.h" | |
| 27 #include "extensions/common/switches.h" | |
| 28 #include "grit/components_strings.h" | |
| 29 #include "ui/aura/window_tree_host.h" | |
| 30 #include "ui/base/l10n/l10n_util.h" | |
| 31 #include "ui/events/event_constants.h" | |
| 32 #include "ui/events/keycodes/keyboard_codes.h" | |
| 33 #include "ui/gfx/canvas.h" | |
| 34 #include "ui/native_theme/native_theme.h" | |
| 35 #include "ui/views/background.h" | |
| 36 #include "ui/views/bubble/bubble_frame_view.h" | |
| 37 #include "ui/views/controls/button/checkbox.h" | |
| 38 #include "ui/views/controls/image_view.h" | |
| 39 #include "ui/views/controls/label.h" | |
| 40 #include "ui/views/controls/scroll_view.h" | |
| 41 #include "ui/views/controls/tabbed_pane/tabbed_pane.h" | |
| 42 #include "ui/views/layout/box_layout.h" | |
| 43 #include "ui/views/layout/layout_constants.h" | |
| 44 #include "ui/views/widget/widget.h" | |
| 45 #include "ui/views/window/dialog_client_view.h" | |
| 46 #include "ui/wm/core/shadow_types.h" | |
| 47 | |
| 48 using content::DesktopMediaID; | |
| 49 | |
| 50 namespace { | |
| 51 | |
| 52 const int kThumbnailWidth = 160; | |
| 53 const int kThumbnailHeight = 100; | |
| 54 const int kThumbnailMargin = 10; | |
| 55 const int kLabelHeight = 40; | |
| 56 const int kListItemWidth = kThumbnailMargin * 2 + kThumbnailWidth; | |
| 57 const int kListItemHeight = | |
| 58 kThumbnailMargin * 2 + kThumbnailHeight + kLabelHeight; | |
| 59 const int kListColumns = 3; | |
| 60 const int kTotalListWidth = kListColumns * kListItemWidth; | |
| 61 | |
| 62 const int kDesktopMediaSourceViewGroupId = 1; | |
| 63 | |
| 64 const char kDesktopMediaSourceViewClassName[] = | |
| 65 "DesktopMediaPicker_DesktopMediaSourceView"; | |
| 66 | |
| 67 DesktopMediaID::Id AcceleratedWidgetToDesktopMediaId( | |
| 68 gfx::AcceleratedWidget accelerated_widget) { | |
| 69 #if defined(OS_WIN) | |
| 70 return reinterpret_cast<DesktopMediaID::Id>(accelerated_widget); | |
| 71 #else | |
| 72 return static_cast<DesktopMediaID::Id>(accelerated_widget); | |
| 73 #endif | |
| 74 } | |
| 75 | |
| 76 int GetMediaListViewHeightForRows(size_t rows) { | |
| 77 return kListItemHeight * rows; | |
| 78 } | |
| 79 | |
| 80 } // namespace | |
| 81 | |
| 82 DesktopMediaSourceView::DesktopMediaSourceView(DesktopMediaListView* parent, | |
| 83 DesktopMediaID source_id) | |
| 84 : parent_(parent), | |
| 85 source_id_(source_id), | |
| 86 image_view_(new views::ImageView()), | |
| 87 label_(new views::Label()), | |
| 88 selected_(false) { | |
| 89 AddChildView(image_view_); | |
| 90 AddChildView(label_); | |
| 91 SetFocusBehavior(FocusBehavior::ALWAYS); | |
| 92 } | |
| 93 | |
| 94 DesktopMediaSourceView::~DesktopMediaSourceView() {} | |
| 95 | |
| 96 void DesktopMediaSourceView::SetName(const base::string16& name) { | |
| 97 label_->SetText(name); | |
| 98 } | |
| 99 | |
| 100 void DesktopMediaSourceView::SetThumbnail(const gfx::ImageSkia& thumbnail) { | |
| 101 image_view_->SetImage(thumbnail); | |
| 102 } | |
| 103 | |
| 104 void DesktopMediaSourceView::SetSelected(bool selected) { | |
| 105 if (selected == selected_) | |
| 106 return; | |
| 107 selected_ = selected; | |
| 108 | |
| 109 if (selected) { | |
| 110 // Unselect all other sources. | |
| 111 Views neighbours; | |
| 112 parent()->GetViewsInGroup(GetGroup(), &neighbours); | |
| 113 for (Views::iterator i(neighbours.begin()); i != neighbours.end(); ++i) { | |
| 114 if (*i != this) { | |
| 115 DCHECK_EQ((*i)->GetClassName(), kDesktopMediaSourceViewClassName); | |
| 116 DesktopMediaSourceView* source_view = | |
| 117 static_cast<DesktopMediaSourceView*>(*i); | |
| 118 source_view->SetSelected(false); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 const SkColor bg_color = GetNativeTheme()->GetSystemColor( | |
| 123 ui::NativeTheme::kColorId_FocusedMenuItemBackgroundColor); | |
| 124 set_background(views::Background::CreateSolidBackground(bg_color)); | |
| 125 | |
| 126 parent_->OnSelectionChanged(); | |
| 127 } else { | |
| 128 set_background(NULL); | |
| 129 } | |
| 130 | |
| 131 SchedulePaint(); | |
| 132 } | |
| 133 | |
| 134 const char* DesktopMediaSourceView::GetClassName() const { | |
| 135 return kDesktopMediaSourceViewClassName; | |
| 136 } | |
| 137 | |
| 138 void DesktopMediaSourceView::Layout() { | |
| 139 image_view_->SetBounds(kThumbnailMargin, kThumbnailMargin, kThumbnailWidth, | |
| 140 kThumbnailHeight); | |
| 141 label_->SetBounds(kThumbnailMargin, kThumbnailHeight + kThumbnailMargin, | |
| 142 kThumbnailWidth, kLabelHeight); | |
| 143 } | |
| 144 | |
| 145 views::View* DesktopMediaSourceView::GetSelectedViewForGroup(int group) { | |
| 146 Views neighbours; | |
| 147 parent()->GetViewsInGroup(group, &neighbours); | |
| 148 if (neighbours.empty()) | |
| 149 return NULL; | |
| 150 | |
| 151 for (Views::iterator i(neighbours.begin()); i != neighbours.end(); ++i) { | |
| 152 DCHECK_EQ((*i)->GetClassName(), kDesktopMediaSourceViewClassName); | |
| 153 DesktopMediaSourceView* source_view = | |
| 154 static_cast<DesktopMediaSourceView*>(*i); | |
| 155 if (source_view->selected_) | |
| 156 return source_view; | |
| 157 } | |
| 158 return NULL; | |
| 159 } | |
| 160 | |
| 161 bool DesktopMediaSourceView::IsGroupFocusTraversable() const { | |
| 162 return false; | |
| 163 } | |
| 164 | |
| 165 void DesktopMediaSourceView::OnPaint(gfx::Canvas* canvas) { | |
| 166 View::OnPaint(canvas); | |
| 167 if (HasFocus()) { | |
| 168 gfx::Rect bounds(GetLocalBounds()); | |
| 169 bounds.Inset(kThumbnailMargin / 2, kThumbnailMargin / 2); | |
| 170 canvas->DrawFocusRect(bounds); | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 void DesktopMediaSourceView::OnFocus() { | |
| 175 View::OnFocus(); | |
| 176 SetSelected(true); | |
| 177 ScrollRectToVisible(gfx::Rect(size())); | |
| 178 // We paint differently when focused. | |
| 179 SchedulePaint(); | |
| 180 } | |
| 181 | |
| 182 void DesktopMediaSourceView::OnBlur() { | |
| 183 View::OnBlur(); | |
| 184 // We paint differently when focused. | |
| 185 SchedulePaint(); | |
| 186 } | |
| 187 | |
| 188 bool DesktopMediaSourceView::OnMousePressed(const ui::MouseEvent& event) { | |
| 189 if (event.GetClickCount() == 1) { | |
| 190 RequestFocus(); | |
| 191 } else if (event.GetClickCount() == 2) { | |
| 192 RequestFocus(); | |
| 193 parent_->OnDoubleClick(); | |
| 194 } | |
| 195 return true; | |
| 196 } | |
| 197 | |
| 198 void DesktopMediaSourceView::OnGestureEvent(ui::GestureEvent* event) { | |
| 199 if (event->type() == ui::ET_GESTURE_TAP && | |
| 200 event->details().tap_count() == 2) { | |
| 201 RequestFocus(); | |
| 202 parent_->OnDoubleClick(); | |
| 203 event->SetHandled(); | |
| 204 return; | |
| 205 } | |
| 206 | |
| 207 // Detect tap gesture using ET_GESTURE_TAP_DOWN so the view also gets focused | |
| 208 // on the long tap (when the tap gesture starts). | |
| 209 if (event->type() == ui::ET_GESTURE_TAP_DOWN) { | |
| 210 RequestFocus(); | |
| 211 event->SetHandled(); | |
| 212 } | |
| 213 } | |
| 214 | |
| 215 DesktopMediaListView::DesktopMediaListView( | |
| 216 DesktopMediaPickerDialogView* parent, | |
| 217 std::unique_ptr<DesktopMediaList> media_list) | |
| 218 : parent_(parent), media_list_(std::move(media_list)), weak_factory_(this) { | |
| 219 media_list_->SetThumbnailSize(gfx::Size(kThumbnailWidth, kThumbnailHeight)); | |
| 220 SetFocusBehavior(FocusBehavior::ALWAYS); | |
| 221 } | |
| 222 | |
| 223 DesktopMediaListView::~DesktopMediaListView() {} | |
| 224 | |
| 225 void DesktopMediaListView::StartUpdating(DesktopMediaID dialog_window_id) { | |
| 226 media_list_->SetViewDialogWindowId(dialog_window_id); | |
| 227 media_list_->StartUpdating(this); | |
| 228 } | |
| 229 | |
| 230 void DesktopMediaListView::OnSelectionChanged() { | |
| 231 parent_->OnSelectionChanged(); | |
| 232 } | |
| 233 | |
| 234 void DesktopMediaListView::OnDoubleClick() { | |
| 235 parent_->OnDoubleClick(); | |
| 236 } | |
| 237 | |
| 238 DesktopMediaSourceView* DesktopMediaListView::GetSelection() { | |
| 239 for (int i = 0; i < child_count(); ++i) { | |
| 240 DesktopMediaSourceView* source_view = | |
| 241 static_cast<DesktopMediaSourceView*>(child_at(i)); | |
| 242 DCHECK_EQ(source_view->GetClassName(), kDesktopMediaSourceViewClassName); | |
| 243 if (source_view->is_selected()) | |
| 244 return source_view; | |
| 245 } | |
| 246 return NULL; | |
| 247 } | |
| 248 | |
| 249 gfx::Size DesktopMediaListView::GetPreferredSize() const { | |
| 250 int total_rows = (child_count() + kListColumns - 1) / kListColumns; | |
| 251 return gfx::Size(kTotalListWidth, | |
| 252 GetMediaListViewHeightForRows(total_rows)); | |
| 253 } | |
| 254 | |
| 255 void DesktopMediaListView::Layout() { | |
| 256 int x = 0; | |
| 257 int y = 0; | |
| 258 | |
| 259 for (int i = 0; i < child_count(); ++i) { | |
| 260 if (x + kListItemWidth > kTotalListWidth) { | |
| 261 x = 0; | |
| 262 y += kListItemHeight; | |
| 263 } | |
| 264 | |
| 265 View* source_view = child_at(i); | |
| 266 source_view->SetBounds(x, y, kListItemWidth, kListItemHeight); | |
| 267 | |
| 268 x += kListItemWidth; | |
| 269 } | |
| 270 | |
| 271 y += kListItemHeight; | |
| 272 } | |
| 273 | |
| 274 bool DesktopMediaListView::OnKeyPressed(const ui::KeyEvent& event) { | |
| 275 int position_increment = 0; | |
| 276 switch (event.key_code()) { | |
| 277 case ui::VKEY_UP: | |
| 278 position_increment = -kListColumns; | |
| 279 break; | |
| 280 case ui::VKEY_DOWN: | |
| 281 position_increment = kListColumns; | |
| 282 break; | |
| 283 case ui::VKEY_LEFT: | |
| 284 position_increment = -1; | |
| 285 break; | |
| 286 case ui::VKEY_RIGHT: | |
| 287 position_increment = 1; | |
| 288 break; | |
| 289 default: | |
| 290 return false; | |
| 291 } | |
| 292 | |
| 293 if (position_increment != 0) { | |
| 294 DesktopMediaSourceView* selected = GetSelection(); | |
| 295 DesktopMediaSourceView* new_selected = NULL; | |
| 296 | |
| 297 if (selected) { | |
| 298 int index = GetIndexOf(selected); | |
| 299 int new_index = index + position_increment; | |
| 300 if (new_index >= child_count()) | |
| 301 new_index = child_count() - 1; | |
| 302 else if (new_index < 0) | |
| 303 new_index = 0; | |
| 304 if (index != new_index) { | |
| 305 new_selected = | |
| 306 static_cast<DesktopMediaSourceView*>(child_at(new_index)); | |
| 307 } | |
| 308 } else if (has_children()) { | |
| 309 new_selected = static_cast<DesktopMediaSourceView*>(child_at(0)); | |
| 310 } | |
| 311 | |
| 312 if (new_selected) { | |
| 313 GetFocusManager()->SetFocusedView(new_selected); | |
| 314 } | |
| 315 | |
| 316 return true; | |
| 317 } | |
| 318 | |
| 319 return false; | |
| 320 } | |
| 321 | |
| 322 void DesktopMediaListView::OnSourceAdded(DesktopMediaList* list, int index) { | |
| 323 const DesktopMediaList::Source& source = media_list_->GetSource(index); | |
| 324 DesktopMediaSourceView* source_view = | |
| 325 new DesktopMediaSourceView(this, source.id); | |
| 326 source_view->SetName(source.name); | |
| 327 source_view->SetGroup(kDesktopMediaSourceViewGroupId); | |
| 328 AddChildViewAt(source_view, index); | |
| 329 | |
| 330 PreferredSizeChanged(); | |
| 331 | |
| 332 if (child_count() % kListColumns == 1) | |
| 333 parent_->OnMediaListRowsChanged(); | |
| 334 | |
| 335 // Auto select the first screen. | |
| 336 if (index == 0 && source.id.type == DesktopMediaID::TYPE_SCREEN) | |
| 337 source_view->RequestFocus(); | |
| 338 | |
| 339 std::string autoselect_source = | |
| 340 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 341 switches::kAutoSelectDesktopCaptureSource); | |
| 342 if (!autoselect_source.empty() && | |
| 343 base::ASCIIToUTF16(autoselect_source) == source.name) { | |
| 344 // Select, then accept and close the dialog when we're done adding sources. | |
| 345 source_view->OnFocus(); | |
| 346 content::BrowserThread::PostTask( | |
| 347 content::BrowserThread::UI, FROM_HERE, | |
| 348 base::Bind(&DesktopMediaListView::AcceptSelection, | |
| 349 weak_factory_.GetWeakPtr())); | |
| 350 } | |
| 351 } | |
| 352 | |
| 353 void DesktopMediaListView::OnSourceRemoved(DesktopMediaList* list, int index) { | |
| 354 DesktopMediaSourceView* view = | |
| 355 static_cast<DesktopMediaSourceView*>(child_at(index)); | |
| 356 DCHECK(view); | |
| 357 DCHECK_EQ(view->GetClassName(), kDesktopMediaSourceViewClassName); | |
| 358 bool was_selected = view->is_selected(); | |
| 359 RemoveChildView(view); | |
| 360 delete view; | |
| 361 | |
| 362 if (was_selected) | |
| 363 OnSelectionChanged(); | |
| 364 | |
| 365 PreferredSizeChanged(); | |
| 366 | |
| 367 if (child_count() % kListColumns == 0) | |
| 368 parent_->OnMediaListRowsChanged(); | |
| 369 } | |
| 370 | |
| 371 void DesktopMediaListView::OnSourceMoved(DesktopMediaList* list, | |
| 372 int old_index, | |
| 373 int new_index) { | |
| 374 DesktopMediaSourceView* view = | |
| 375 static_cast<DesktopMediaSourceView*>(child_at(old_index)); | |
| 376 ReorderChildView(view, new_index); | |
| 377 PreferredSizeChanged(); | |
| 378 } | |
| 379 | |
| 380 void DesktopMediaListView::OnSourceNameChanged(DesktopMediaList* list, | |
| 381 int index) { | |
| 382 const DesktopMediaList::Source& source = media_list_->GetSource(index); | |
| 383 DesktopMediaSourceView* source_view = | |
| 384 static_cast<DesktopMediaSourceView*>(child_at(index)); | |
| 385 source_view->SetName(source.name); | |
| 386 } | |
| 387 | |
| 388 void DesktopMediaListView::OnSourceThumbnailChanged(DesktopMediaList* list, | |
| 389 int index) { | |
| 390 const DesktopMediaList::Source& source = media_list_->GetSource(index); | |
| 391 DesktopMediaSourceView* source_view = | |
| 392 static_cast<DesktopMediaSourceView*>(child_at(index)); | |
| 393 source_view->SetThumbnail(source.thumbnail); | |
| 394 } | |
| 395 | |
| 396 void DesktopMediaListView::AcceptSelection() { | |
| 397 OnSelectionChanged(); | |
| 398 OnDoubleClick(); | |
| 399 } | |
| 400 | |
| 401 DesktopMediaPickerDialogView::DesktopMediaPickerDialogView( | |
| 402 content::WebContents* parent_web_contents, | |
| 403 gfx::NativeWindow context, | |
| 404 DesktopMediaPickerViews* parent, | |
| 405 const base::string16& app_name, | |
| 406 const base::string16& target_name, | |
| 407 std::unique_ptr<DesktopMediaList> screen_list, | |
| 408 std::unique_ptr<DesktopMediaList> window_list, | |
| 409 std::unique_ptr<DesktopMediaList> tab_list, | |
| 410 bool request_audio) | |
| 411 : parent_(parent), | |
| 412 description_label_(new views::Label()), | |
| 413 audio_share_checkbox_(nullptr), | |
| 414 pane_(new views::TabbedPane()) { | |
| 415 SetLayoutManager(new views::BoxLayout( | |
| 416 views::BoxLayout::kVertical, views::kButtonHEdgeMarginNew, | |
| 417 views::kPanelVertMargin, views::kLabelToControlVerticalSpacing)); | |
| 418 | |
| 419 description_label_->SetMultiLine(true); | |
| 420 description_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 421 AddChildView(description_label_); | |
| 422 | |
| 423 if (screen_list) { | |
| 424 source_types_.push_back(DesktopMediaID::TYPE_SCREEN); | |
| 425 | |
| 426 views::ScrollView* screen_scroll_view = | |
| 427 views::ScrollView::CreateScrollViewWithBorder(); | |
| 428 list_views_.push_back( | |
| 429 new DesktopMediaListView(this, std::move(screen_list))); | |
| 430 | |
| 431 screen_scroll_view->SetContents(list_views_.back()); | |
| 432 screen_scroll_view->ClipHeightTo(GetMediaListViewHeightForRows(1), | |
| 433 GetMediaListViewHeightForRows(2)); | |
| 434 pane_->AddTab( | |
| 435 l10n_util::GetStringUTF16(IDS_DESKTOP_MEDIA_PICKER_SOURCE_TYPE_SCREEN), | |
| 436 screen_scroll_view); | |
| 437 pane_->set_listener(this); | |
| 438 } | |
| 439 | |
| 440 if (window_list) { | |
| 441 source_types_.push_back(DesktopMediaID::TYPE_WINDOW); | |
| 442 views::ScrollView* window_scroll_view = | |
| 443 views::ScrollView::CreateScrollViewWithBorder(); | |
| 444 list_views_.push_back( | |
| 445 new DesktopMediaListView(this, std::move(window_list))); | |
| 446 | |
| 447 window_scroll_view->SetContents(list_views_.back()); | |
| 448 window_scroll_view->ClipHeightTo(GetMediaListViewHeightForRows(1), | |
| 449 GetMediaListViewHeightForRows(2)); | |
| 450 | |
| 451 pane_->AddTab( | |
| 452 l10n_util::GetStringUTF16(IDS_DESKTOP_MEDIA_PICKER_SOURCE_TYPE_WINDOW), | |
| 453 window_scroll_view); | |
| 454 pane_->set_listener(this); | |
| 455 } | |
| 456 | |
| 457 if (tab_list) { | |
| 458 source_types_.push_back(DesktopMediaID::TYPE_WEB_CONTENTS); | |
| 459 views::ScrollView* tab_scroll_view = | |
| 460 views::ScrollView::CreateScrollViewWithBorder(); | |
| 461 list_views_.push_back(new DesktopMediaListView(this, std::move(tab_list))); | |
| 462 | |
| 463 tab_scroll_view->SetContents(list_views_.back()); | |
| 464 tab_scroll_view->ClipHeightTo(GetMediaListViewHeightForRows(1), | |
| 465 GetMediaListViewHeightForRows(2)); | |
| 466 | |
| 467 pane_->AddTab( | |
| 468 l10n_util::GetStringUTF16(IDS_DESKTOP_MEDIA_PICKER_SOURCE_TYPE_TAB), | |
| 469 tab_scroll_view); | |
| 470 pane_->set_listener(this); | |
| 471 } | |
| 472 | |
| 473 if (app_name == target_name) { | |
| 474 description_label_->SetText( | |
| 475 l10n_util::GetStringFUTF16(IDS_DESKTOP_MEDIA_PICKER_TEXT, app_name)); | |
| 476 } else { | |
| 477 description_label_->SetText(l10n_util::GetStringFUTF16( | |
| 478 IDS_DESKTOP_MEDIA_PICKER_TEXT_DELEGATED, app_name, target_name)); | |
| 479 } | |
| 480 | |
| 481 DCHECK(!source_types_.empty()); | |
| 482 AddChildView(pane_); | |
| 483 | |
| 484 if (request_audio) { | |
| 485 audio_share_checkbox_ = new views::Checkbox( | |
| 486 l10n_util::GetStringUTF16(IDS_DESKTOP_MEDIA_PICKER_AUDIO_SHARE)); | |
| 487 audio_share_checkbox_->SetChecked(true); | |
| 488 } | |
| 489 | |
| 490 // Focus on the first non-null media_list. | |
| 491 SwitchSourceType(0); | |
| 492 | |
| 493 // If |parent_web_contents| is set and it's not a background page then the | |
| 494 // picker will be shown modal to the web contents. Otherwise the picker is | |
| 495 // shown in a separate window. | |
| 496 views::Widget* widget = NULL; | |
| 497 bool modal_dialog = | |
| 498 parent_web_contents && | |
| 499 !parent_web_contents->GetDelegate()->IsNeverVisible(parent_web_contents); | |
| 500 if (modal_dialog) { | |
| 501 widget = | |
| 502 constrained_window::ShowWebModalDialogViews(this, parent_web_contents); | |
| 503 } else { | |
| 504 widget = DialogDelegate::CreateDialogWidget(this, context, NULL); | |
| 505 widget->Show(); | |
| 506 } | |
| 507 | |
| 508 // If the picker is not modal to the calling web contents then it is displayed | |
| 509 // in its own top-level window, so in that case it needs to be filtered out of | |
| 510 // the list of top-level windows available for capture, and to achieve that | |
| 511 // the Id is passed to DesktopMediaList. | |
| 512 DesktopMediaID dialog_window_id; | |
| 513 if (!modal_dialog) { | |
| 514 dialog_window_id = DesktopMediaID::RegisterAuraWindow( | |
| 515 DesktopMediaID::TYPE_WINDOW, widget->GetNativeWindow()); | |
| 516 | |
| 517 bool is_ash_window = false; | |
| 518 #if defined(USE_ASH) | |
| 519 is_ash_window = chrome::IsNativeWindowInAsh(widget->GetNativeWindow()); | |
| 520 #endif | |
| 521 | |
| 522 // Set native window ID if the windows is outside Ash. | |
| 523 if (!is_ash_window) { | |
| 524 dialog_window_id.id = AcceleratedWidgetToDesktopMediaId( | |
| 525 widget->GetNativeWindow()->GetHost()->GetAcceleratedWidget()); | |
| 526 } | |
| 527 } | |
| 528 | |
| 529 for (auto& list_view : list_views_) | |
| 530 list_view->StartUpdating(dialog_window_id); | |
| 531 } | |
| 532 | |
| 533 DesktopMediaPickerDialogView::~DesktopMediaPickerDialogView() {} | |
| 534 | |
| 535 void DesktopMediaPickerDialogView::TabSelectedAt(int index) { | |
| 536 SwitchSourceType(index); | |
| 537 GetDialogClientView()->UpdateDialogButtons(); | |
| 538 } | |
| 539 | |
| 540 void DesktopMediaPickerDialogView::SwitchSourceType(int index) { | |
| 541 // Set whether the checkbox is visible based on the source type. | |
| 542 if (audio_share_checkbox_) { | |
| 543 switch (source_types_[index]) { | |
| 544 case DesktopMediaID::TYPE_SCREEN: | |
| 545 #if defined(USE_CRAS) || defined(OS_WIN) | |
| 546 audio_share_checkbox_->SetVisible(true); | |
| 547 #else | |
| 548 audio_share_checkbox_->SetVisible(false); | |
| 549 #endif | |
| 550 break; | |
| 551 case DesktopMediaID::TYPE_WINDOW: | |
| 552 audio_share_checkbox_->SetVisible(false); | |
| 553 break; | |
| 554 case DesktopMediaID::TYPE_WEB_CONTENTS: | |
| 555 audio_share_checkbox_->SetVisible(true); | |
| 556 break; | |
| 557 case DesktopMediaID::TYPE_NONE: | |
| 558 NOTREACHED(); | |
| 559 break; | |
| 560 } | |
| 561 } | |
| 562 } | |
| 563 | |
| 564 void DesktopMediaPickerDialogView::DetachParent() { | |
| 565 parent_ = NULL; | |
| 566 } | |
| 567 | |
| 568 gfx::Size DesktopMediaPickerDialogView::GetPreferredSize() const { | |
| 569 static const size_t kDialogViewWidth = 600; | |
| 570 return gfx::Size(kDialogViewWidth, GetHeightForWidth(kDialogViewWidth)); | |
| 571 } | |
| 572 | |
| 573 ui::ModalType DesktopMediaPickerDialogView::GetModalType() const { | |
| 574 return ui::MODAL_TYPE_CHILD; | |
| 575 } | |
| 576 | |
| 577 base::string16 DesktopMediaPickerDialogView::GetWindowTitle() const { | |
| 578 return l10n_util::GetStringUTF16(IDS_DESKTOP_MEDIA_PICKER_TITLE); | |
| 579 } | |
| 580 | |
| 581 bool DesktopMediaPickerDialogView::IsDialogButtonEnabled( | |
| 582 ui::DialogButton button) const { | |
| 583 if (button == ui::DIALOG_BUTTON_OK) | |
| 584 return list_views_[pane_->selected_tab_index()]->GetSelection() != NULL; | |
| 585 return true; | |
| 586 } | |
| 587 | |
| 588 views::View* DesktopMediaPickerDialogView::GetInitiallyFocusedView() { | |
| 589 return list_views_[0]; | |
| 590 } | |
| 591 | |
| 592 base::string16 DesktopMediaPickerDialogView::GetDialogButtonLabel( | |
| 593 ui::DialogButton button) const { | |
| 594 return l10n_util::GetStringUTF16(button == ui::DIALOG_BUTTON_OK | |
| 595 ? IDS_DESKTOP_MEDIA_PICKER_SHARE | |
| 596 : IDS_CANCEL); | |
| 597 } | |
| 598 | |
| 599 bool DesktopMediaPickerDialogView::ShouldDefaultButtonBeBlue() const { | |
| 600 return true; | |
| 601 } | |
| 602 | |
| 603 views::View* DesktopMediaPickerDialogView::CreateExtraView() { | |
| 604 return audio_share_checkbox_; | |
| 605 } | |
| 606 | |
| 607 bool DesktopMediaPickerDialogView::Accept() { | |
| 608 DesktopMediaSourceView* selection = | |
| 609 list_views_[pane_->selected_tab_index()]->GetSelection(); | |
| 610 | |
| 611 // Ok button should only be enabled when a source is selected. | |
| 612 DCHECK(selection); | |
| 613 DesktopMediaID source = selection->source_id(); | |
| 614 source.audio_share = audio_share_checkbox_ && | |
| 615 audio_share_checkbox_->visible() && | |
| 616 audio_share_checkbox_->checked(); | |
| 617 | |
| 618 // If the media source is an tab, activate it. | |
| 619 if (source.type == DesktopMediaID::TYPE_WEB_CONTENTS) { | |
| 620 content::WebContents* tab = content::WebContents::FromRenderFrameHost( | |
| 621 content::RenderFrameHost::FromID( | |
| 622 source.web_contents_id.render_process_id, | |
| 623 source.web_contents_id.main_render_frame_id)); | |
| 624 if (tab) | |
| 625 tab->GetDelegate()->ActivateContents(tab); | |
| 626 } | |
| 627 | |
| 628 if (parent_) | |
| 629 parent_->NotifyDialogResult(source); | |
| 630 | |
| 631 // Return true to close the window. | |
| 632 return true; | |
| 633 } | |
| 634 | |
| 635 void DesktopMediaPickerDialogView::DeleteDelegate() { | |
| 636 // If the dialog is being closed then notify the parent about it. | |
| 637 if (parent_) | |
| 638 parent_->NotifyDialogResult(DesktopMediaID()); | |
| 639 delete this; | |
| 640 } | |
| 641 | |
| 642 void DesktopMediaPickerDialogView::OnSelectionChanged() { | |
| 643 GetDialogClientView()->UpdateDialogButtons(); | |
| 644 } | |
| 645 | |
| 646 void DesktopMediaPickerDialogView::OnDoubleClick() { | |
| 647 // This will call Accept() and close the dialog. | |
| 648 GetDialogClientView()->AcceptWindow(); | |
| 649 } | |
| 650 | |
| 651 void DesktopMediaPickerDialogView::OnMediaListRowsChanged() { | |
| 652 gfx::Rect widget_bound = GetWidget()->GetWindowBoundsInScreen(); | |
| 653 | |
| 654 int new_height = widget_bound.height() - pane_->height() + | |
| 655 pane_->GetPreferredSize().height(); | |
| 656 | |
| 657 GetWidget()->CenterWindow(gfx::Size(widget_bound.width(), new_height)); | |
| 658 } | |
| 659 | |
| 660 DesktopMediaListView* DesktopMediaPickerDialogView::GetMediaListViewForTesting() | |
| 661 const { | |
| 662 return list_views_[pane_->selected_tab_index()]; | |
| 663 } | |
| 664 | |
| 665 DesktopMediaSourceView* | |
| 666 DesktopMediaPickerDialogView::GetMediaSourceViewForTesting(int index) const { | |
| 667 if (list_views_[pane_->selected_tab_index()]->child_count() <= index) | |
| 668 return NULL; | |
| 669 | |
| 670 return reinterpret_cast<DesktopMediaSourceView*>( | |
| 671 list_views_[pane_->selected_tab_index()]->child_at(index)); | |
| 672 } | |
| 673 | |
| 674 views::Checkbox* DesktopMediaPickerDialogView::GetCheckboxForTesting() const { | |
| 675 return audio_share_checkbox_; | |
| 676 } | |
| 677 | |
| 678 int DesktopMediaPickerDialogView::GetIndexOfSourceTypeForTesting( | |
| 679 DesktopMediaID::Type source_type) const { | |
| 680 for (size_t i = 0; i < source_types_.size(); i++) { | |
| 681 if (source_types_[i] == source_type) | |
| 682 return i; | |
| 683 } | |
| 684 return -1; | |
| 685 } | |
| 686 | |
| 687 views::TabbedPane* DesktopMediaPickerDialogView::GetPaneForTesting() const { | |
| 688 return pane_; | |
| 689 } | |
| 690 | |
| 691 DesktopMediaPickerViews::DesktopMediaPickerViews() : dialog_(NULL) {} | |
| 692 | |
| 693 DesktopMediaPickerViews::~DesktopMediaPickerViews() { | |
| 694 if (dialog_) { | |
| 695 dialog_->DetachParent(); | |
| 696 dialog_->GetWidget()->Close(); | |
| 697 } | |
| 698 } | |
| 699 | |
| 700 void DesktopMediaPickerViews::Show( | |
| 701 content::WebContents* web_contents, | |
| 702 gfx::NativeWindow context, | |
| 703 gfx::NativeWindow parent, | |
| 704 const base::string16& app_name, | |
| 705 const base::string16& target_name, | |
| 706 std::unique_ptr<DesktopMediaList> screen_list, | |
| 707 std::unique_ptr<DesktopMediaList> window_list, | |
| 708 std::unique_ptr<DesktopMediaList> tab_list, | |
| 709 bool request_audio, | |
| 710 const DoneCallback& done_callback) { | |
| 711 callback_ = done_callback; | |
| 712 dialog_ = new DesktopMediaPickerDialogView( | |
| 713 web_contents, context, this, app_name, target_name, | |
| 714 std::move(screen_list), std::move(window_list), std::move(tab_list), | |
| 715 request_audio); | |
| 716 } | |
| 717 | |
| 718 void DesktopMediaPickerViews::NotifyDialogResult(DesktopMediaID source) { | |
| 719 // Once this method is called the |dialog_| will close and destroy itself. | |
| 720 dialog_->DetachParent(); | |
| 721 dialog_ = NULL; | |
| 722 | |
| 723 DCHECK(!callback_.is_null()); | |
| 724 | |
| 725 // Notify the |callback_| asynchronously because it may need to destroy | |
| 726 // DesktopMediaPicker. | |
| 727 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 728 base::Bind(callback_, source)); | |
| 729 callback_.Reset(); | |
| 730 } | |
| 731 | |
| 732 // static | |
| 733 std::unique_ptr<DesktopMediaPicker> DesktopMediaPicker::Create() { | |
| 734 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 735 extensions::switches::kDisableDesktopCapturePickerOldUI)) { | |
| 736 return std::unique_ptr<DesktopMediaPicker>( | |
| 737 new deprecated::DesktopMediaPickerViews()); | |
| 738 } | |
| 739 return std::unique_ptr<DesktopMediaPicker>(new DesktopMediaPickerViews()); | |
| 740 } | |
| OLD | NEW |