Commit e7a9525b authored by qiangchen's avatar qiangchen Committed by Commit bot

Display Window Icon In Picker UI

In this CL, we display the window icon with the window source
on the picker window for desktop capture.

BUG=631604

Review-Url: https://codereview.chromium.org/2270543003
Cr-Commit-Position: refs/heads/master@{#415199}
parent 07fbfac9
// Copyright 2016 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/window_icon_util.h"
#include "content/public/browser/desktop_media_id.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window.h"
gfx::ImageSkia GetWindowIcon(content::DesktopMediaID id) {
DCHECK_EQ(content::DesktopMediaID::TYPE_WINDOW, id.type);
aura::Window* window = content::DesktopMediaID::GetAuraWindowById(id);
if (!window)
return gfx::ImageSkia();
const gfx::ImageSkia* icon_image_ptr =
window->GetProperty(aura::client::kWindowIconKey);
return icon_image_ptr ? *icon_image_ptr : gfx::ImageSkia();
}
......@@ -7,6 +7,7 @@
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include "ui/gfx/x/x11_error_tracker.h"
#include "ui/gfx/x/x11_types.h"
gfx::ImageSkia GetWindowIcon(content::DesktopMediaID id) {
......@@ -20,10 +21,20 @@ gfx::ImageSkia GetWindowIcon(content::DesktopMediaID id) {
unsigned long size;
long* data;
// The |error_tracker| essentially provides an empty X error handler for
// the call of XGetWindowProperty. The motivation is to guard against crash
// for any reason that XGetWindowProperty fails. For example, at the time that
// XGetWindowProperty is called, the window handler (a.k.a |id.id|) may
// already be invalid due to the fact that the end user has closed the
// corresponding window, etc.
std::unique_ptr<gfx::X11ErrorTracker> error_tracker(
new gfx::X11ErrorTracker());
int status = XGetWindowProperty(display, id.id, property, 0L, ~0L, False,
AnyPropertyType, &actual_type, &actual_format,
&size, &bytes_after,
reinterpret_cast<unsigned char**>(&data));
error_tracker.reset();
if (status != Success) {
return gfx::ImageSkia();
}
......
......@@ -7,11 +7,17 @@
#include "base/command_line.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/media/desktop_media_list.h"
#include "chrome/browser/media/window_icon_util.h"
#include "chrome/browser/ui/views/desktop_capture/desktop_media_picker_views.h"
#include "chrome/browser/ui/views/desktop_capture/desktop_media_source_view.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/grit/extensions_browser_resources.h"
#include "grit/theme_resources.h"
#include "ui/accessibility/ax_view_state.h"
#include "ui/aura/window.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/views/focus/focus_manager.h"
using content::DesktopMediaID;
......@@ -20,6 +26,25 @@ namespace {
const int kDesktopMediaSourceViewGroupId = 1;
#if defined(USE_ASH)
// Here we are going to display default app icon for app windows without an
// icon, and display product logo for chrome browser windows.
gfx::ImageSkia LoadDefaultIcon(aura::Window* window) {
BrowserView* browser_view =
BrowserView::GetBrowserViewForNativeWindow(window);
Browser* browser = browser_view ? browser_view->browser() : nullptr;
// Apps could be launched in a view other than BrowserView, so we count those
// windows without Browser association as apps.
// Technically dev tool is actually a special app, but we would like to
// display product logo for it, because intuitively it is internal to browser.
bool is_app = !browser || (browser->is_app() && !browser->is_devtools());
int idr = is_app ? IDR_APP_DEFAULT_ICON : IDR_PRODUCT_LOGO_32;
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
return *rb.GetImageSkiaNamed(idr);
}
#endif
} // namespace
DesktopMediaListView::DesktopMediaListView(
......@@ -151,6 +176,19 @@ void DesktopMediaListView::OnSourceAdded(DesktopMediaList* list, int index) {
source_view->SetName(source.name);
source_view->SetGroup(kDesktopMediaSourceViewGroupId);
if (source.id.type == DesktopMediaID::TYPE_WINDOW) {
gfx::ImageSkia icon_image = GetWindowIcon(source.id);
#if defined(USE_ASH)
// Empty icons are used to represent default icon for aura windows. By
// detecting this, we load the default icon from resource.
if (icon_image.isNull()) {
aura::Window* window = DesktopMediaID::GetAuraWindowById(source.id);
if (window)
icon_image = LoadDefaultIcon(window);
}
#endif
source_view->SetIcon(icon_image);
}
AddChildViewAt(source_view, index);
if ((child_count() - 1) % active_style_->columns == 0)
......
......@@ -83,6 +83,7 @@ DesktopMediaPickerDialogView::DesktopMediaPickerDialogView(
const DesktopMediaSourceViewStyle kSingleScreenStyle(
1, // columns
gfx::Size(360, 280), // item_size
gfx::Rect(), // icon_rect
gfx::Rect(), // label_rect
gfx::HorizontalAlignment::ALIGN_CENTER, // text_alignment
gfx::Rect(20, 20, 320, 240), // image_rect
......@@ -92,6 +93,7 @@ DesktopMediaPickerDialogView::DesktopMediaPickerDialogView(
const DesktopMediaSourceViewStyle kGenericScreenStyle(
2, // columns
gfx::Size(270, 220), // item_size
gfx::Rect(), // icon_rect
gfx::Rect(15, 165, 240, 40), // label_rect
gfx::HorizontalAlignment::ALIGN_CENTER, // text_alignment
gfx::Rect(15, 15, 240, 150), // image_rect
......@@ -122,13 +124,14 @@ DesktopMediaPickerDialogView::DesktopMediaPickerDialogView(
source_types_.push_back(DesktopMediaID::TYPE_WINDOW);
const DesktopMediaSourceViewStyle kWindowStyle(
3, // columns
gfx::Size(180, 160), // item_size
gfx::Rect(10, 110, 160, 40), // label_rect
gfx::HorizontalAlignment::ALIGN_CENTER, // text_alignment
gfx::Rect(8, 8, 164, 104), // image_rect
2, // selection_border_thickness
5); // focus_rectangle_inset
3, // columns
gfx::Size(180, 160), // item_size
gfx::Rect(10, 120, 20, 20), // icon_rect
gfx::Rect(32, 110, 138, 40), // label_rect
gfx::HorizontalAlignment::ALIGN_LEFT, // text_alignment
gfx::Rect(8, 8, 164, 104), // image_rect
2, // selection_border_thickness
5); // focus_rectangle_inset
views::ScrollView* window_scroll_view =
views::ScrollView::CreateScrollViewWithBorder();
......@@ -154,6 +157,7 @@ DesktopMediaPickerDialogView::DesktopMediaPickerDialogView(
const DesktopMediaSourceViewStyle kTabStyle(
1, // columns
gfx::Size(600, 30), // item_size
gfx::Rect(), // icon_rect
gfx::Rect(46, 0, 490, 30), // label_rect
gfx::HorizontalAlignment::ALIGN_LEFT, // text_alignment
gfx::Rect(10, 2, 26, 26), // image_rect
......
......@@ -23,6 +23,7 @@ DesktopMediaSourceViewStyle::DesktopMediaSourceViewStyle(
DesktopMediaSourceViewStyle::DesktopMediaSourceViewStyle(
int columns,
const gfx::Size& item_size,
const gfx::Rect& icon_rect,
const gfx::Rect& label_rect,
gfx::HorizontalAlignment text_alignment,
const gfx::Rect& image_rect,
......@@ -30,6 +31,7 @@ DesktopMediaSourceViewStyle::DesktopMediaSourceViewStyle(
int focus_rectangle_inset)
: columns(columns),
item_size(item_size),
icon_rect(icon_rect),
label_rect(label_rect),
text_alignment(text_alignment),
image_rect(image_rect),
......@@ -43,11 +45,14 @@ DesktopMediaSourceView::DesktopMediaSourceView(
: parent_(parent),
source_id_(source_id),
style_(style),
icon_view_(new views::ImageView()),
image_view_(new views::ImageView()),
label_(new views::Label()),
selected_(false) {
AddChildView(icon_view_);
AddChildView(image_view_);
AddChildView(label_);
icon_view_->set_interactive(false);
image_view_->set_interactive(false);
SetFocusBehavior(FocusBehavior::ALWAYS);
SetStyle(style_);
......@@ -66,6 +71,10 @@ void DesktopMediaSourceView::SetThumbnail(const gfx::ImageSkia& thumbnail) {
image_view_->SetImage(thumbnail);
}
void DesktopMediaSourceView::SetIcon(const gfx::ImageSkia& icon) {
icon_view_->SetImage(icon);
}
void DesktopMediaSourceView::SetSelected(bool selected) {
if (selected == selected_)
return;
......@@ -127,6 +136,8 @@ void DesktopMediaSourceView::SetStyle(DesktopMediaSourceViewStyle style) {
image_view_->SetBorder(views::Border::CreateSolidBorder(
style_.selection_border_thickness, border_color));
}
icon_view_->SetBoundsRect(style_.icon_rect);
icon_view_->SetImageSize(style_.icon_rect.size());
label_->SetBoundsRect(style_.label_rect);
label_->SetHorizontalAlignment(style_.text_alignment);
}
......
......@@ -21,22 +21,33 @@ struct DesktopMediaSourceViewStyle {
DesktopMediaSourceViewStyle(const DesktopMediaSourceViewStyle& style);
DesktopMediaSourceViewStyle(int columns,
const gfx::Size& item_size,
const gfx::Rect& icon_rect,
const gfx::Rect& label_rect,
gfx::HorizontalAlignment text_alignment,
const gfx::Rect& image_rect,
int selection_border_thickness,
int focus_rectangle_inset);
// This parameter controls how many source items can be displayed in a row.
// Source items are instances of DesktopMediaSourceView.
int columns;
// The size of a single source item.
gfx::Size item_size;
// The relative position to display icon, label and preview image in the
// source item.
gfx::Rect icon_rect;
gfx::Rect label_rect;
gfx::HorizontalAlignment text_alignment;
gfx::Rect image_rect;
// When a source item is selected, we paint the border to show it. This
// parameter controls how thick the border would be.
int selection_border_thickness;
// When a source item is focused, we paint dotted line. This parameter
// controls the distance between dotted line and the source view boundary.
int focus_rectangle_inset;
};
......@@ -55,6 +66,7 @@ class DesktopMediaSourceView : public views::View {
// Updates thumbnail and title from |source|.
void SetName(const base::string16& name);
void SetThumbnail(const gfx::ImageSkia& thumbnail);
void SetIcon(const gfx::ImageSkia& icon);
// Id for the source shown by this View.
const content::DesktopMediaID& source_id() const { return source_id_; }
......@@ -89,6 +101,7 @@ class DesktopMediaSourceView : public views::View {
content::DesktopMediaID source_id_;
DesktopMediaSourceViewStyle style_;
views::ImageView* icon_view_;
views::ImageView* image_view_;
views::Label* label_;
......
......@@ -1134,6 +1134,7 @@
'browser/download/notification/download_notification_manager.h',
'browser/media/protected_media_identifier_permission_context.cc',
'browser/media/protected_media_identifier_permission_context.h',
'browser/media/window_icon_util_chromeos.cc',
'browser/metrics/chromeos_metrics_provider.cc',
'browser/metrics/chromeos_metrics_provider.h',
'browser/metrics/perf/cpu_identity.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment