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