| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/media/desktop_media_picker.h" | 5 #include "chrome/browser/media/desktop_media_picker.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "chrome/browser/media/desktop_media_picker_model.h" | 9 #include "chrome/browser/media/desktop_media_list.h" |
| 10 #include "chrome/browser/media/desktop_media_list_observer.h" |
| 10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 11 #include "grit/generated_resources.h" | 12 #include "grit/generated_resources.h" |
| 12 #include "ui/base/l10n/l10n_util.h" | 13 #include "ui/base/l10n/l10n_util.h" |
| 13 #include "ui/events/keycodes/keyboard_codes.h" | 14 #include "ui/events/keycodes/keyboard_codes.h" |
| 14 #include "ui/native_theme/native_theme.h" | 15 #include "ui/native_theme/native_theme.h" |
| 15 #include "ui/views/controls/image_view.h" | 16 #include "ui/views/controls/image_view.h" |
| 16 #include "ui/views/controls/label.h" | 17 #include "ui/views/controls/label.h" |
| 17 #include "ui/views/controls/scroll_view.h" | 18 #include "ui/views/controls/scroll_view.h" |
| 18 #include "ui/views/corewm/shadow_types.h" | 19 #include "ui/views/corewm/shadow_types.h" |
| 19 #include "ui/views/focus_border.h" | 20 #include "ui/views/focus_border.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 | 94 |
| 94 views::ImageView* image_view_; | 95 views::ImageView* image_view_; |
| 95 views::Label* label_; | 96 views::Label* label_; |
| 96 | 97 |
| 97 bool selected_; | 98 bool selected_; |
| 98 | 99 |
| 99 DISALLOW_COPY_AND_ASSIGN(DesktopMediaSourceView); | 100 DISALLOW_COPY_AND_ASSIGN(DesktopMediaSourceView); |
| 100 }; | 101 }; |
| 101 | 102 |
| 102 // View that shows list of desktop media sources available from | 103 // View that shows list of desktop media sources available from |
| 103 // DesktopMediaPickerModel. | 104 // DesktopMediaList. |
| 104 class DesktopMediaListView : public views::View, | 105 class DesktopMediaListView : public views::View, |
| 105 public DesktopMediaPickerModel::Observer { | 106 public DesktopMediaListObserver { |
| 106 public: | 107 public: |
| 107 DesktopMediaListView(DesktopMediaPickerDialogView* parent, | 108 DesktopMediaListView(DesktopMediaPickerDialogView* parent, |
| 108 scoped_ptr<DesktopMediaPickerModel> model); | 109 scoped_ptr<DesktopMediaList> media_list); |
| 109 virtual ~DesktopMediaListView(); | 110 virtual ~DesktopMediaListView(); |
| 110 | 111 |
| 111 void StartUpdating(content::DesktopMediaID::Id dialog_window_id); | 112 void StartUpdating(content::DesktopMediaID::Id dialog_window_id); |
| 112 | 113 |
| 113 // Called by DesktopMediaSourceView when selection has changed. | 114 // Called by DesktopMediaSourceView when selection has changed. |
| 114 void OnSelectionChanged(); | 115 void OnSelectionChanged(); |
| 115 | 116 |
| 116 // Called by DesktopMediaSourceView when a source has been double-clicked. | 117 // Called by DesktopMediaSourceView when a source has been double-clicked. |
| 117 void OnDoubleClick(); | 118 void OnDoubleClick(); |
| 118 | 119 |
| 119 // Returns currently selected source. | 120 // Returns currently selected source. |
| 120 DesktopMediaSourceView* GetSelection(); | 121 DesktopMediaSourceView* GetSelection(); |
| 121 | 122 |
| 122 // views::View overrides. | 123 // views::View overrides. |
| 123 virtual gfx::Size GetPreferredSize() OVERRIDE; | 124 virtual gfx::Size GetPreferredSize() OVERRIDE; |
| 124 virtual void Layout() OVERRIDE; | 125 virtual void Layout() OVERRIDE; |
| 125 virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE; | 126 virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE; |
| 126 | 127 |
| 127 private: | 128 private: |
| 128 // DesktopMediaPickerModel::Observer interface | 129 // DesktopMediaList::Observer interface |
| 129 virtual void OnSourceAdded(int index) OVERRIDE; | 130 virtual void OnSourceAdded(int index) OVERRIDE; |
| 130 virtual void OnSourceRemoved(int index) OVERRIDE; | 131 virtual void OnSourceRemoved(int index) OVERRIDE; |
| 131 virtual void OnSourceNameChanged(int index) OVERRIDE; | 132 virtual void OnSourceNameChanged(int index) OVERRIDE; |
| 132 virtual void OnSourceThumbnailChanged(int index) OVERRIDE; | 133 virtual void OnSourceThumbnailChanged(int index) OVERRIDE; |
| 133 | 134 |
| 134 DesktopMediaPickerDialogView* parent_; | 135 DesktopMediaPickerDialogView* parent_; |
| 135 scoped_ptr<DesktopMediaPickerModel> model_; | 136 scoped_ptr<DesktopMediaList> media_list_; |
| 136 | 137 |
| 137 DISALLOW_COPY_AND_ASSIGN(DesktopMediaListView); | 138 DISALLOW_COPY_AND_ASSIGN(DesktopMediaListView); |
| 138 }; | 139 }; |
| 139 | 140 |
| 140 // Dialog view used for DesktopMediaPickerViews. | 141 // Dialog view used for DesktopMediaPickerViews. |
| 141 class DesktopMediaPickerDialogView : public views::DialogDelegateView { | 142 class DesktopMediaPickerDialogView : public views::DialogDelegateView { |
| 142 public: | 143 public: |
| 143 DesktopMediaPickerDialogView(gfx::NativeWindow context, | 144 DesktopMediaPickerDialogView(gfx::NativeWindow context, |
| 144 gfx::NativeWindow parent_window, | 145 gfx::NativeWindow parent_window, |
| 145 DesktopMediaPickerViews* parent, | 146 DesktopMediaPickerViews* parent, |
| 146 const string16& app_name, | 147 const string16& app_name, |
| 147 scoped_ptr<DesktopMediaPickerModel> model); | 148 scoped_ptr<DesktopMediaList> media_list); |
| 148 virtual ~DesktopMediaPickerDialogView(); | 149 virtual ~DesktopMediaPickerDialogView(); |
| 149 | 150 |
| 150 // Called by parent (DesktopMediaPickerViews) when it's destroyed. | 151 // Called by parent (DesktopMediaPickerViews) when it's destroyed. |
| 151 void DetachParent(); | 152 void DetachParent(); |
| 152 | 153 |
| 153 // Called by DesktopMediaListView. | 154 // Called by DesktopMediaListView. |
| 154 void OnSelectionChanged(); | 155 void OnSelectionChanged(); |
| 155 void OnDoubleClick(); | 156 void OnDoubleClick(); |
| 156 | 157 |
| 157 // views::View overrides. | 158 // views::View overrides. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 180 public: | 181 public: |
| 181 DesktopMediaPickerViews(); | 182 DesktopMediaPickerViews(); |
| 182 virtual ~DesktopMediaPickerViews(); | 183 virtual ~DesktopMediaPickerViews(); |
| 183 | 184 |
| 184 void NotifyDialogResult(DesktopMediaID source); | 185 void NotifyDialogResult(DesktopMediaID source); |
| 185 | 186 |
| 186 // DesktopMediaPicker overrides. | 187 // DesktopMediaPicker overrides. |
| 187 virtual void Show(gfx::NativeWindow context, | 188 virtual void Show(gfx::NativeWindow context, |
| 188 gfx::NativeWindow parent, | 189 gfx::NativeWindow parent, |
| 189 const string16& app_name, | 190 const string16& app_name, |
| 190 scoped_ptr<DesktopMediaPickerModel> model, | 191 scoped_ptr<DesktopMediaList> media_list, |
| 191 const DoneCallback& done_callback) OVERRIDE; | 192 const DoneCallback& done_callback) OVERRIDE; |
| 192 | 193 |
| 193 private: | 194 private: |
| 194 DoneCallback callback_; | 195 DoneCallback callback_; |
| 195 | 196 |
| 196 // The |dialog_| is owned by the corresponding views::Widget instance. | 197 // The |dialog_| is owned by the corresponding views::Widget instance. |
| 197 // When DesktopMediaPickerViews is destroyed the |dialog_| is destroyed | 198 // When DesktopMediaPickerViews is destroyed the |dialog_| is destroyed |
| 198 // asynchronously by closing the widget. | 199 // asynchronously by closing the widget. |
| 199 DesktopMediaPickerDialogView* dialog_; | 200 DesktopMediaPickerDialogView* dialog_; |
| 200 | 201 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 RequestFocus(); | 301 RequestFocus(); |
| 301 } else if (event.GetClickCount() == 2) { | 302 } else if (event.GetClickCount() == 2) { |
| 302 RequestFocus(); | 303 RequestFocus(); |
| 303 parent_->OnDoubleClick(); | 304 parent_->OnDoubleClick(); |
| 304 } | 305 } |
| 305 return true; | 306 return true; |
| 306 } | 307 } |
| 307 | 308 |
| 308 DesktopMediaListView::DesktopMediaListView( | 309 DesktopMediaListView::DesktopMediaListView( |
| 309 DesktopMediaPickerDialogView* parent, | 310 DesktopMediaPickerDialogView* parent, |
| 310 scoped_ptr<DesktopMediaPickerModel> model) | 311 scoped_ptr<DesktopMediaList> media_list) |
| 311 : parent_(parent), | 312 : parent_(parent), |
| 312 model_(model.Pass()) { | 313 media_list_(media_list.Pass()) { |
| 313 model_->SetThumbnailSize(gfx::Size(kThumbnailWidth, kThumbnailHeight)); | 314 media_list_->SetThumbnailSize(gfx::Size(kThumbnailWidth, kThumbnailHeight)); |
| 314 } | 315 } |
| 315 | 316 |
| 316 DesktopMediaListView::~DesktopMediaListView() {} | 317 DesktopMediaListView::~DesktopMediaListView() {} |
| 317 | 318 |
| 318 void DesktopMediaListView::StartUpdating( | 319 void DesktopMediaListView::StartUpdating( |
| 319 content::DesktopMediaID::Id dialog_window_id) { | 320 content::DesktopMediaID::Id dialog_window_id) { |
| 320 model_->SetViewDialogWindowId(dialog_window_id); | 321 media_list_->SetViewDialogWindowId(dialog_window_id); |
| 321 model_->StartUpdating(this); | 322 media_list_->StartUpdating(this); |
| 322 } | 323 } |
| 323 | 324 |
| 324 void DesktopMediaListView::OnSelectionChanged() { | 325 void DesktopMediaListView::OnSelectionChanged() { |
| 325 parent_->OnSelectionChanged(); | 326 parent_->OnSelectionChanged(); |
| 326 } | 327 } |
| 327 | 328 |
| 328 void DesktopMediaListView::OnDoubleClick() { | 329 void DesktopMediaListView::OnDoubleClick() { |
| 329 parent_->OnDoubleClick(); | 330 parent_->OnDoubleClick(); |
| 330 } | 331 } |
| 331 | 332 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 GetFocusManager()->SetFocusedView(new_selected); | 408 GetFocusManager()->SetFocusedView(new_selected); |
| 408 } | 409 } |
| 409 | 410 |
| 410 return true; | 411 return true; |
| 411 } | 412 } |
| 412 | 413 |
| 413 return false; | 414 return false; |
| 414 } | 415 } |
| 415 | 416 |
| 416 void DesktopMediaListView::OnSourceAdded(int index) { | 417 void DesktopMediaListView::OnSourceAdded(int index) { |
| 417 const DesktopMediaPickerModel::Source& source = model_->source(index); | 418 const DesktopMediaList::Source& source = media_list_->GetSource(index); |
| 418 DesktopMediaSourceView* source_view = | 419 DesktopMediaSourceView* source_view = |
| 419 new DesktopMediaSourceView(this, source.id); | 420 new DesktopMediaSourceView(this, source.id); |
| 420 source_view->SetName(source.name); | 421 source_view->SetName(source.name); |
| 421 source_view->SetGroup(kDesktopMediaSourceViewGroupId); | 422 source_view->SetGroup(kDesktopMediaSourceViewGroupId); |
| 422 AddChildViewAt(source_view, index); | 423 AddChildViewAt(source_view, index); |
| 423 | 424 |
| 424 PreferredSizeChanged(); | 425 PreferredSizeChanged(); |
| 425 } | 426 } |
| 426 | 427 |
| 427 void DesktopMediaListView::OnSourceRemoved(int index) { | 428 void DesktopMediaListView::OnSourceRemoved(int index) { |
| 428 DesktopMediaSourceView* view = | 429 DesktopMediaSourceView* view = |
| 429 static_cast<DesktopMediaSourceView*>(child_at(index)); | 430 static_cast<DesktopMediaSourceView*>(child_at(index)); |
| 430 DCHECK(view); | 431 DCHECK(view); |
| 431 DCHECK_EQ(view->GetClassName(), kDesktopMediaSourceViewClassName); | 432 DCHECK_EQ(view->GetClassName(), kDesktopMediaSourceViewClassName); |
| 432 bool was_selected = view->is_selected(); | 433 bool was_selected = view->is_selected(); |
| 433 RemoveChildView(view); | 434 RemoveChildView(view); |
| 434 delete view; | 435 delete view; |
| 435 | 436 |
| 436 if (was_selected) | 437 if (was_selected) |
| 437 OnSelectionChanged(); | 438 OnSelectionChanged(); |
| 438 | 439 |
| 439 PreferredSizeChanged(); | 440 PreferredSizeChanged(); |
| 440 } | 441 } |
| 441 | 442 |
| 442 void DesktopMediaListView::OnSourceNameChanged(int index) { | 443 void DesktopMediaListView::OnSourceNameChanged(int index) { |
| 443 const DesktopMediaPickerModel::Source& source = model_->source(index); | 444 const DesktopMediaList::Source& source = media_list_->GetSource(index); |
| 444 DesktopMediaSourceView* source_view = | 445 DesktopMediaSourceView* source_view = |
| 445 static_cast<DesktopMediaSourceView*>(child_at(index)); | 446 static_cast<DesktopMediaSourceView*>(child_at(index)); |
| 446 source_view->SetName(source.name); | 447 source_view->SetName(source.name); |
| 447 } | 448 } |
| 448 | 449 |
| 449 void DesktopMediaListView::OnSourceThumbnailChanged(int index) { | 450 void DesktopMediaListView::OnSourceThumbnailChanged(int index) { |
| 450 const DesktopMediaPickerModel::Source& source = model_->source(index); | 451 const DesktopMediaList::Source& source = media_list_->GetSource(index); |
| 451 DesktopMediaSourceView* source_view = | 452 DesktopMediaSourceView* source_view = |
| 452 static_cast<DesktopMediaSourceView*>(child_at(index)); | 453 static_cast<DesktopMediaSourceView*>(child_at(index)); |
| 453 source_view->SetThumbnail(source.thumbnail); | 454 source_view->SetThumbnail(source.thumbnail); |
| 454 } | 455 } |
| 455 | 456 |
| 456 DesktopMediaPickerDialogView::DesktopMediaPickerDialogView( | 457 DesktopMediaPickerDialogView::DesktopMediaPickerDialogView( |
| 457 gfx::NativeWindow context, | 458 gfx::NativeWindow context, |
| 458 gfx::NativeWindow parent_window, | 459 gfx::NativeWindow parent_window, |
| 459 DesktopMediaPickerViews* parent, | 460 DesktopMediaPickerViews* parent, |
| 460 const string16& app_name, | 461 const string16& app_name, |
| 461 scoped_ptr<DesktopMediaPickerModel> model) | 462 scoped_ptr<DesktopMediaList> media_list) |
| 462 : parent_(parent), | 463 : parent_(parent), |
| 463 app_name_(app_name), | 464 app_name_(app_name), |
| 464 label_(new views::Label()), | 465 label_(new views::Label()), |
| 465 scroll_view_(views::ScrollView::CreateScrollViewWithBorder()), | 466 scroll_view_(views::ScrollView::CreateScrollViewWithBorder()), |
| 466 list_view_(new DesktopMediaListView(this, model.Pass())) { | 467 list_view_(new DesktopMediaListView(this, media_list.Pass())) { |
| 467 label_->SetText( | 468 label_->SetText( |
| 468 l10n_util::GetStringFUTF16(IDS_DESKTOP_MEDIA_PICKER_TEXT, app_name_)); | 469 l10n_util::GetStringFUTF16(IDS_DESKTOP_MEDIA_PICKER_TEXT, app_name_)); |
| 469 label_->SetMultiLine(true); | 470 label_->SetMultiLine(true); |
| 470 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 471 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 471 AddChildView(label_); | 472 AddChildView(label_); |
| 472 | 473 |
| 473 scroll_view_->SetContents(list_view_); | 474 scroll_view_->SetContents(list_view_); |
| 474 AddChildView(scroll_view_); | 475 AddChildView(scroll_view_); |
| 475 | 476 |
| 476 DialogDelegate::CreateDialogWidget(this, context, parent_window); | 477 DialogDelegate::CreateDialogWidget(this, context, parent_window); |
| 477 | 478 |
| 478 // DesktopMediaPickerModel needs to know the ID of the picker window which | 479 // DesktopMediaList needs to know the ID of the picker window which |
| 479 // matches the ID it gets from the OS. Depending on the OS and configuration | 480 // matches the ID it gets from the OS. Depending on the OS and configuration |
| 480 // we get this ID differently. | 481 // we get this ID differently. |
| 481 // | 482 // |
| 482 // TODO(sergeyu): Update this code when Ash-specific window capturer is | 483 // TODO(sergeyu): Update this code when Ash-specific window capturer is |
| 483 // implemented. Currently this code will always get native windows ID | 484 // implemented. Currently this code will always get native windows ID |
| 484 // which is not what we need in Ash. http://crbug.com/295102 | 485 // which is not what we need in Ash. http://crbug.com/295102 |
| 485 content::DesktopMediaID::Id dialog_window_id; | 486 content::DesktopMediaID::Id dialog_window_id; |
| 486 | 487 |
| 487 #if defined(OS_WIN) | 488 #if defined(OS_WIN) |
| 488 dialog_window_id = reinterpret_cast<content::DesktopMediaID::Id>( | 489 dialog_window_id = reinterpret_cast<content::DesktopMediaID::Id>( |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 576 DesktopMediaPickerViews::~DesktopMediaPickerViews() { | 577 DesktopMediaPickerViews::~DesktopMediaPickerViews() { |
| 577 if (dialog_) { | 578 if (dialog_) { |
| 578 dialog_->DetachParent(); | 579 dialog_->DetachParent(); |
| 579 dialog_->GetWidget()->Close(); | 580 dialog_->GetWidget()->Close(); |
| 580 } | 581 } |
| 581 } | 582 } |
| 582 | 583 |
| 583 void DesktopMediaPickerViews::Show(gfx::NativeWindow context, | 584 void DesktopMediaPickerViews::Show(gfx::NativeWindow context, |
| 584 gfx::NativeWindow parent, | 585 gfx::NativeWindow parent, |
| 585 const string16& app_name, | 586 const string16& app_name, |
| 586 scoped_ptr<DesktopMediaPickerModel> model, | 587 scoped_ptr<DesktopMediaList> media_list, |
| 587 const DoneCallback& done_callback) { | 588 const DoneCallback& done_callback) { |
| 588 callback_ = done_callback; | 589 callback_ = done_callback; |
| 589 dialog_ = new DesktopMediaPickerDialogView( | 590 dialog_ = new DesktopMediaPickerDialogView( |
| 590 context, parent, this, app_name, model.Pass()); | 591 context, parent, this, app_name, media_list.Pass()); |
| 591 } | 592 } |
| 592 | 593 |
| 593 void DesktopMediaPickerViews::NotifyDialogResult( | 594 void DesktopMediaPickerViews::NotifyDialogResult( |
| 594 DesktopMediaID source) { | 595 DesktopMediaID source) { |
| 595 // Once this method is called the |dialog_| will close and destroy itself. | 596 // Once this method is called the |dialog_| will close and destroy itself. |
| 596 dialog_->DetachParent(); | 597 dialog_->DetachParent(); |
| 597 dialog_ = NULL; | 598 dialog_ = NULL; |
| 598 | 599 |
| 599 DCHECK(!callback_.is_null()); | 600 DCHECK(!callback_.is_null()); |
| 600 | 601 |
| 601 // Notify the |callback_| asynchronously because it may need to destroy | 602 // Notify the |callback_| asynchronously because it may need to destroy |
| 602 // DesktopMediaPicker. | 603 // DesktopMediaPicker. |
| 603 content::BrowserThread::PostTask( | 604 content::BrowserThread::PostTask( |
| 604 content::BrowserThread::UI, FROM_HERE, | 605 content::BrowserThread::UI, FROM_HERE, |
| 605 base::Bind(callback_, source)); | 606 base::Bind(callback_, source)); |
| 606 callback_.Reset(); | 607 callback_.Reset(); |
| 607 } | 608 } |
| 608 | 609 |
| 609 } // namespace | 610 } // namespace |
| 610 | 611 |
| 611 // static | 612 // static |
| 612 scoped_ptr<DesktopMediaPicker> DesktopMediaPicker::Create() { | 613 scoped_ptr<DesktopMediaPicker> DesktopMediaPicker::Create() { |
| 613 return scoped_ptr<DesktopMediaPicker>(new DesktopMediaPickerViews()); | 614 return scoped_ptr<DesktopMediaPicker>(new DesktopMediaPickerViews()); |
| 614 } | 615 } |
| OLD | NEW |