| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/views/desktop_capture/desktop_media_source_view.h" |
| 6 |
| 7 #include "chrome/browser/media/desktop_media_list.h" |
| 8 #include "chrome/browser/ui/views/desktop_capture/desktop_media_list_view.h" |
| 9 #include "chrome/browser/ui/views/desktop_capture/desktop_media_picker_views.h" |
| 10 #include "ui/gfx/canvas.h" |
| 11 #include "ui/native_theme/native_theme.h" |
| 12 #include "ui/views/background.h" |
| 13 #include "ui/views/controls/image_view.h" |
| 14 #include "ui/views/controls/label.h" |
| 15 |
| 16 using content::DesktopMediaID; |
| 17 |
| 18 DesktopMediaSourceView::DesktopMediaSourceView(DesktopMediaListView* parent, |
| 19 DesktopMediaID source_id) |
| 20 : parent_(parent), |
| 21 source_id_(source_id), |
| 22 image_view_(new views::ImageView()), |
| 23 label_(new views::Label()), |
| 24 selected_(false) { |
| 25 AddChildView(image_view_); |
| 26 AddChildView(label_); |
| 27 SetFocusBehavior(FocusBehavior::ALWAYS); |
| 28 } |
| 29 |
| 30 DesktopMediaSourceView::~DesktopMediaSourceView() {} |
| 31 |
| 32 const char* DesktopMediaSourceView::kDesktopMediaSourceViewClassName = |
| 33 "DesktopMediaPicker_DesktopMediaSourceView"; |
| 34 |
| 35 void DesktopMediaSourceView::SetName(const base::string16& name) { |
| 36 label_->SetText(name); |
| 37 } |
| 38 |
| 39 void DesktopMediaSourceView::SetThumbnail(const gfx::ImageSkia& thumbnail) { |
| 40 image_view_->SetImage(thumbnail); |
| 41 } |
| 42 |
| 43 void DesktopMediaSourceView::SetSelected(bool selected) { |
| 44 if (selected == selected_) |
| 45 return; |
| 46 selected_ = selected; |
| 47 |
| 48 if (selected) { |
| 49 // Unselect all other sources. |
| 50 Views neighbours; |
| 51 parent()->GetViewsInGroup(GetGroup(), &neighbours); |
| 52 for (Views::iterator i(neighbours.begin()); i != neighbours.end(); ++i) { |
| 53 if (*i != this) { |
| 54 DCHECK_EQ((*i)->GetClassName(), |
| 55 DesktopMediaSourceView::kDesktopMediaSourceViewClassName); |
| 56 DesktopMediaSourceView* source_view = |
| 57 static_cast<DesktopMediaSourceView*>(*i); |
| 58 source_view->SetSelected(false); |
| 59 } |
| 60 } |
| 61 |
| 62 const SkColor bg_color = GetNativeTheme()->GetSystemColor( |
| 63 ui::NativeTheme::kColorId_FocusedMenuItemBackgroundColor); |
| 64 set_background(views::Background::CreateSolidBackground(bg_color)); |
| 65 |
| 66 parent_->OnSelectionChanged(); |
| 67 } else { |
| 68 set_background(NULL); |
| 69 } |
| 70 |
| 71 SchedulePaint(); |
| 72 } |
| 73 |
| 74 const char* DesktopMediaSourceView::GetClassName() const { |
| 75 return DesktopMediaSourceView::kDesktopMediaSourceViewClassName; |
| 76 } |
| 77 |
| 78 void DesktopMediaSourceView::Layout() { |
| 79 image_view_->SetBounds(DesktopMediaPickerDialogView::kThumbnailMargin, |
| 80 DesktopMediaPickerDialogView::kThumbnailMargin, |
| 81 DesktopMediaPickerDialogView::kThumbnailWidth, |
| 82 DesktopMediaPickerDialogView::kThumbnailHeight); |
| 83 label_->SetBounds(DesktopMediaPickerDialogView::kThumbnailMargin, |
| 84 DesktopMediaPickerDialogView::kThumbnailHeight + |
| 85 DesktopMediaPickerDialogView::kThumbnailMargin, |
| 86 DesktopMediaPickerDialogView::kThumbnailWidth, |
| 87 DesktopMediaPickerDialogView::kLabelHeight); |
| 88 } |
| 89 |
| 90 views::View* DesktopMediaSourceView::GetSelectedViewForGroup(int group) { |
| 91 Views neighbours; |
| 92 parent()->GetViewsInGroup(group, &neighbours); |
| 93 if (neighbours.empty()) |
| 94 return NULL; |
| 95 |
| 96 for (Views::iterator i(neighbours.begin()); i != neighbours.end(); ++i) { |
| 97 DCHECK_EQ((*i)->GetClassName(), |
| 98 DesktopMediaSourceView::kDesktopMediaSourceViewClassName); |
| 99 DesktopMediaSourceView* source_view = |
| 100 static_cast<DesktopMediaSourceView*>(*i); |
| 101 if (source_view->selected_) |
| 102 return source_view; |
| 103 } |
| 104 return NULL; |
| 105 } |
| 106 |
| 107 bool DesktopMediaSourceView::IsGroupFocusTraversable() const { |
| 108 return false; |
| 109 } |
| 110 |
| 111 void DesktopMediaSourceView::OnPaint(gfx::Canvas* canvas) { |
| 112 View::OnPaint(canvas); |
| 113 if (HasFocus()) { |
| 114 gfx::Rect bounds(GetLocalBounds()); |
| 115 bounds.Inset(DesktopMediaPickerDialogView::kThumbnailMargin / 2, |
| 116 DesktopMediaPickerDialogView::kThumbnailMargin / 2); |
| 117 canvas->DrawFocusRect(bounds); |
| 118 } |
| 119 } |
| 120 |
| 121 void DesktopMediaSourceView::OnFocus() { |
| 122 View::OnFocus(); |
| 123 SetSelected(true); |
| 124 ScrollRectToVisible(gfx::Rect(size())); |
| 125 // We paint differently when focused. |
| 126 SchedulePaint(); |
| 127 } |
| 128 |
| 129 void DesktopMediaSourceView::OnBlur() { |
| 130 View::OnBlur(); |
| 131 // We paint differently when focused. |
| 132 SchedulePaint(); |
| 133 } |
| 134 |
| 135 bool DesktopMediaSourceView::OnMousePressed(const ui::MouseEvent& event) { |
| 136 if (event.GetClickCount() == 1) { |
| 137 RequestFocus(); |
| 138 } else if (event.GetClickCount() == 2) { |
| 139 RequestFocus(); |
| 140 parent_->OnDoubleClick(); |
| 141 } |
| 142 return true; |
| 143 } |
| 144 |
| 145 void DesktopMediaSourceView::OnGestureEvent(ui::GestureEvent* event) { |
| 146 if (event->type() == ui::ET_GESTURE_TAP && |
| 147 event->details().tap_count() == 2) { |
| 148 RequestFocus(); |
| 149 parent_->OnDoubleClick(); |
| 150 event->SetHandled(); |
| 151 return; |
| 152 } |
| 153 |
| 154 // Detect tap gesture using ET_GESTURE_TAP_DOWN so the view also gets focused |
| 155 // on the long tap (when the tap gesture starts). |
| 156 if (event->type() == ui::ET_GESTURE_TAP_DOWN) { |
| 157 RequestFocus(); |
| 158 event->SetHandled(); |
| 159 } |
| 160 } |
| OLD | NEW |