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