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