This source file includes following definitions.
- RoundPositive
- Layout
- OnPaint
- OnMousePressed
- ShowContextMenuForView
- CreateDesktopBackground
#include "ash/desktop_background/desktop_background_view.h"
#include <limits>
#include "ash/ash_export.h"
#include "ash/desktop_background/desktop_background_controller.h"
#include "ash/desktop_background/desktop_background_widget_controller.h"
#include "ash/desktop_background/user_wallpaper_delegate.h"
#include "ash/display/display_manager.h"
#include "ash/root_window_controller.h"
#include "ash/session_state_delegate.h"
#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ash/wm/window_animations.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/size_conversions.h"
#include "ui/gfx/transform.h"
#include "ui/views/widget/widget.h"
namespace ash {
namespace {
int RoundPositive(double x) {
return static_cast<int>(floor(x + 0.5));
}
class LayerControlView : public views::View {
public:
explicit LayerControlView(views::View* view) {
AddChildView(view);
view->SetPaintToLayer(true);
}
virtual void Layout() OVERRIDE {
gfx::Display display = Shell::GetScreen()->GetDisplayNearestWindow(
GetWidget()->GetNativeView());
DisplayManager* display_manager = Shell::GetInstance()->display_manager();
DisplayInfo info = display_manager->GetDisplayInfo(display.id());
float ui_scale = info.GetEffectiveUIScale();
gfx::SizeF pixel_size = display.size();
pixel_size.Scale(1.0f / ui_scale);
gfx::Size rounded_size = gfx::ToCeiledSize(pixel_size);
DCHECK_EQ(1, child_count());
views::View* child = child_at(0);
child->SetBounds(0, 0, rounded_size.width(), rounded_size.height());
gfx::Transform transform;
transform.Scale(ui_scale, ui_scale);
child->SetTransform(transform);
}
private:
DISALLOW_COPY_AND_ASSIGN(LayerControlView);
};
}
DesktopBackgroundView::DesktopBackgroundView() {
set_context_menu_controller(this);
}
DesktopBackgroundView::~DesktopBackgroundView() {
}
void DesktopBackgroundView::OnPaint(gfx::Canvas* canvas) {
DesktopBackgroundController* controller =
Shell::GetInstance()->desktop_background_controller();
gfx::ImageSkia wallpaper = controller->GetWallpaper();
WallpaperLayout wallpaper_layout = controller->GetWallpaperLayout();
if (wallpaper.isNull()) {
canvas->FillRect(GetLocalBounds(), SK_ColorBLACK);
return;
}
gfx::NativeView native_view = GetWidget()->GetNativeView();
gfx::Display display = gfx::Screen::GetScreenFor(native_view)->
GetDisplayNearestWindow(native_view);
DisplayManager* display_manager = Shell::GetInstance()->display_manager();
DisplayInfo display_info = display_manager->GetDisplayInfo(display.id());
float scaling = display_info.GetEffectiveUIScale();
if (scaling <= 1.0f)
scaling = 1.0f;
gfx::Rect wallpaper_rect(
0, 0, wallpaper.width() * scaling, wallpaper.height() * scaling);
if (wallpaper_layout == WALLPAPER_LAYOUT_CENTER_CROPPED) {
double horizontal_ratio = static_cast<double>(width()) /
static_cast<double>(wallpaper.width());
double vertical_ratio = static_cast<double>(height()) /
static_cast<double>(wallpaper.height());
gfx::Size cropped_size;
if (vertical_ratio > horizontal_ratio) {
cropped_size = gfx::Size(
RoundPositive(static_cast<double>(width()) / vertical_ratio),
wallpaper.height());
} else {
cropped_size = gfx::Size(wallpaper.width(),
RoundPositive(static_cast<double>(height()) / horizontal_ratio));
}
gfx::Rect wallpaper_cropped_rect(
0, 0, wallpaper.width(), wallpaper.height());
wallpaper_cropped_rect.ClampToCenteredSize(cropped_size);
canvas->DrawImageInt(wallpaper,
wallpaper_cropped_rect.x(), wallpaper_cropped_rect.y(),
wallpaper_cropped_rect.width(), wallpaper_cropped_rect.height(),
0, 0, width(), height(),
true);
} else if (wallpaper_layout == WALLPAPER_LAYOUT_TILE) {
canvas->TileImageInt(wallpaper, 0, 0, width(), height());
} else if (wallpaper_layout == WALLPAPER_LAYOUT_STRETCH) {
canvas->DrawImageInt(wallpaper, 0, 0, wallpaper.width(),
wallpaper.height(), 0, 0, width(), height(), true);
} else {
canvas->FillRect(GetLocalBounds(), SK_ColorBLACK);
canvas->DrawImageInt(
wallpaper,
0, 0, wallpaper.width(), wallpaper.height(),
(width() - wallpaper_rect.width()) / 2,
(height() - wallpaper_rect.height()) / 2,
wallpaper_rect.width(),
wallpaper_rect.height(),
true);
}
}
bool DesktopBackgroundView::OnMousePressed(const ui::MouseEvent& event) {
return true;
}
void DesktopBackgroundView::ShowContextMenuForView(
views::View* source,
const gfx::Point& point,
ui::MenuSourceType source_type) {
Shell::GetInstance()->ShowContextMenu(point, source_type);
}
views::Widget* CreateDesktopBackground(aura::Window* root_window,
int container_id) {
DesktopBackgroundController* controller =
Shell::GetInstance()->desktop_background_controller();
UserWallpaperDelegate* wallpaper_delegate =
Shell::GetInstance()->user_wallpaper_delegate();
views::Widget* desktop_widget = new views::Widget;
views::Widget::InitParams params(
views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
if (controller->GetWallpaper().isNull())
params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
params.parent = root_window->GetChildById(container_id);
desktop_widget->Init(params);
desktop_widget->GetNativeWindow()->layer()->SetMasksToBounds(true);
desktop_widget->SetContentsView(
new LayerControlView(new DesktopBackgroundView()));
int animation_type = wallpaper_delegate->GetAnimationType();
wm::SetWindowVisibilityAnimationType(
desktop_widget->GetNativeView(), animation_type);
RootWindowController* root_window_controller =
GetRootWindowController(root_window);
if (wallpaper_delegate->ShouldShowInitialAnimation() ||
root_window_controller->animating_wallpaper_controller() ||
Shell::GetInstance()->session_state_delegate()->NumberOfLoggedInUsers()) {
wm::SetWindowVisibilityAnimationTransition(
desktop_widget->GetNativeView(), wm::ANIMATE_SHOW);
int duration_override = wallpaper_delegate->GetAnimationDurationOverride();
if (duration_override) {
wm::SetWindowVisibilityAnimationDuration(
desktop_widget->GetNativeView(),
base::TimeDelta::FromMilliseconds(duration_override));
}
} else {
wm::SetWindowVisibilityAnimationTransition(
desktop_widget->GetNativeView(), wm::ANIMATE_NONE);
}
desktop_widget->SetBounds(params.parent->bounds());
return desktop_widget;
}
}