Home | History | Annotate | Download | only in desktop_background
      1 // Copyright (c) 2012 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 "ash/desktop_background/desktop_background_view.h"
      6 
      7 #include <limits>
      8 
      9 #include "ash/ash_export.h"
     10 #include "ash/desktop_background/desktop_background_controller.h"
     11 #include "ash/desktop_background/desktop_background_widget_controller.h"
     12 #include "ash/desktop_background/user_wallpaper_delegate.h"
     13 #include "ash/display/display_manager.h"
     14 #include "ash/root_window_controller.h"
     15 #include "ash/session_state_delegate.h"
     16 #include "ash/shell.h"
     17 #include "ash/shell_window_ids.h"
     18 #include "ash/wm/window_animations.h"
     19 #include "base/message_loop/message_loop.h"
     20 #include "base/strings/utf_string_conversions.h"
     21 #include "ui/aura/root_window.h"
     22 #include "ui/base/resource/resource_bundle.h"
     23 #include "ui/compositor/layer.h"
     24 #include "ui/gfx/canvas.h"
     25 #include "ui/gfx/image/image.h"
     26 #include "ui/gfx/size_conversions.h"
     27 #include "ui/gfx/transform.h"
     28 #include "ui/views/widget/widget.h"
     29 
     30 namespace ash {
     31 namespace internal {
     32 namespace {
     33 
     34 // For our scaling ratios we need to round positive numbers.
     35 int RoundPositive(double x) {
     36   return static_cast<int>(floor(x + 0.5));
     37 }
     38 
     39 // A view that controls the child view's layer so that the layer
     40 // always has the same size as the display's original, un-scaled size
     41 // in DIP. The layer then transformed to fit to the virtual screen
     42 // size when laid-out.
     43 // This is to avoid scaling the image at painting time, then scaling
     44 // it back to the screen size in the compositor.
     45 class LayerControlView : public views::View {
     46  public:
     47   explicit LayerControlView(views::View* view) {
     48     AddChildView(view);
     49     view->SetPaintToLayer(true);
     50   }
     51 
     52   // Overrides views::View.
     53   virtual void Layout() OVERRIDE {
     54     gfx::Display display = Shell::GetScreen()->GetDisplayNearestWindow(
     55         GetWidget()->GetNativeView());
     56     DisplayManager* display_manager = Shell::GetInstance()->display_manager();
     57     DisplayInfo info = display_manager->GetDisplayInfo(display.id());
     58     float ui_scale = info.GetEffectiveUIScale();
     59     gfx::SizeF pixel_size = display.size();
     60     pixel_size.Scale(1.0f / ui_scale);
     61     gfx::Size rounded_size = gfx::ToCeiledSize(pixel_size);
     62     DCHECK_EQ(1, child_count());
     63     views::View* child = child_at(0);
     64     child->SetBounds(0, 0, rounded_size.width(), rounded_size.height());
     65     gfx::Transform transform;
     66     transform.Scale(ui_scale, ui_scale);
     67     child->SetTransform(transform);
     68   }
     69 
     70  private:
     71   DISALLOW_COPY_AND_ASSIGN(LayerControlView);
     72 };
     73 
     74 }  // namespace
     75 
     76 ////////////////////////////////////////////////////////////////////////////////
     77 // DesktopBackgroundView, public:
     78 
     79 DesktopBackgroundView::DesktopBackgroundView() {
     80   set_context_menu_controller(this);
     81 }
     82 
     83 DesktopBackgroundView::~DesktopBackgroundView() {
     84 }
     85 
     86 ////////////////////////////////////////////////////////////////////////////////
     87 // DesktopBackgroundView, views::View overrides:
     88 
     89 void DesktopBackgroundView::OnPaint(gfx::Canvas* canvas) {
     90   // Scale the image while maintaining the aspect ratio, cropping as
     91   // necessary to fill the background. Ideally the image should be larger
     92   // than the largest display supported, if not we will center it rather than
     93   // streching to avoid upsampling artifacts (Note that we could tile too, but
     94   // decided not to do this at the moment).
     95   DesktopBackgroundController* controller =
     96       Shell::GetInstance()->desktop_background_controller();
     97   gfx::ImageSkia wallpaper = controller->GetWallpaper();
     98   WallpaperLayout wallpaper_layout = controller->GetWallpaperLayout();
     99 
    100   gfx::NativeView native_view = GetWidget()->GetNativeView();
    101   gfx::Display display = gfx::Screen::GetScreenFor(native_view)->
    102       GetDisplayNearestWindow(native_view);
    103 
    104   DisplayManager* display_manager = Shell::GetInstance()->display_manager();
    105   DisplayInfo display_info = display_manager->GetDisplayInfo(display.id());
    106   float scaling = display_info.GetEffectiveUIScale();
    107   if (scaling <= 1.0f)
    108     scaling = 1.0f;
    109   // Allow scaling up to the UI scaling.
    110   // TODO(oshima): Create separate layer that fits to the image and then
    111   // scale to avoid artifacts and be more efficient when clipped.
    112   gfx::Rect wallpaper_rect(
    113       0, 0, wallpaper.width() * scaling, wallpaper.height() * scaling);
    114 
    115   if (wallpaper_layout == WALLPAPER_LAYOUT_CENTER_CROPPED &&
    116       wallpaper_rect.width() >= width() &&
    117       wallpaper_rect.height() >= height()) {
    118     // The dimension with the smallest ratio must be cropped, the other one
    119     // is preserved. Both are set in gfx::Size cropped_size.
    120     double horizontal_ratio = static_cast<double>(width()) /
    121         static_cast<double>(wallpaper.width());
    122     double vertical_ratio = static_cast<double>(height()) /
    123         static_cast<double>(wallpaper.height());
    124 
    125     gfx::Size cropped_size;
    126     if (vertical_ratio > horizontal_ratio) {
    127       cropped_size = gfx::Size(
    128           RoundPositive(static_cast<double>(width()) / vertical_ratio),
    129           wallpaper.height());
    130     } else {
    131       cropped_size = gfx::Size(wallpaper.width(),
    132           RoundPositive(static_cast<double>(height()) / horizontal_ratio));
    133     }
    134 
    135     gfx::Rect wallpaper_cropped_rect(
    136         0, 0, wallpaper.width(), wallpaper.height());
    137     wallpaper_cropped_rect.ClampToCenteredSize(cropped_size);
    138     canvas->DrawImageInt(wallpaper,
    139         wallpaper_cropped_rect.x(), wallpaper_cropped_rect.y(),
    140         wallpaper_cropped_rect.width(), wallpaper_cropped_rect.height(),
    141         0, 0, width(), height(),
    142         true);
    143   } else if (wallpaper_layout == WALLPAPER_LAYOUT_TILE) {
    144     canvas->TileImageInt(wallpaper, 0, 0, width(), height());
    145   } else if (wallpaper_layout == WALLPAPER_LAYOUT_STRETCH) {
    146     // This is generally not recommended as it may show artifacts.
    147     canvas->DrawImageInt(wallpaper, 0, 0, wallpaper.width(),
    148         wallpaper.height(), 0, 0, width(), height(), true);
    149   } else {
    150     // Fill with black to make sure that the entire area is opaque.
    151     canvas->FillRect(GetLocalBounds(), SK_ColorBLACK);
    152     // All other are simply centered, and not scaled (but may be clipped).
    153     if (wallpaper.width() && wallpaper.height()) {
    154       canvas->DrawImageInt(
    155           wallpaper,
    156           0, 0, wallpaper.width(), wallpaper.height(),
    157           (width() - wallpaper_rect.width()) / 2,
    158           (height() - wallpaper_rect.height()) / 2,
    159           wallpaper_rect.width(),
    160           wallpaper_rect.height(),
    161           true);
    162     }
    163   }
    164 }
    165 
    166 bool DesktopBackgroundView::OnMousePressed(const ui::MouseEvent& event) {
    167   return true;
    168 }
    169 
    170 void DesktopBackgroundView::ShowContextMenuForView(
    171     views::View* source,
    172     const gfx::Point& point,
    173     ui::MenuSourceType source_type) {
    174   Shell::GetInstance()->ShowContextMenu(point, source_type);
    175 }
    176 
    177 views::Widget* CreateDesktopBackground(aura::Window* root_window,
    178                                        int container_id) {
    179   DesktopBackgroundController* controller =
    180       Shell::GetInstance()->desktop_background_controller();
    181   UserWallpaperDelegate* wallpaper_delegate =
    182       Shell::GetInstance()->user_wallpaper_delegate();
    183 
    184   views::Widget* desktop_widget = new views::Widget;
    185   views::Widget::InitParams params(
    186       views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
    187   if (controller->GetWallpaper().isNull())
    188     params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
    189   params.parent = root_window->GetChildById(container_id);
    190   desktop_widget->Init(params);
    191   desktop_widget->SetContentsView(
    192       new LayerControlView(new DesktopBackgroundView()));
    193   int animation_type = wallpaper_delegate->GetAnimationType();
    194   views::corewm::SetWindowVisibilityAnimationType(
    195       desktop_widget->GetNativeView(), animation_type);
    196 
    197   RootWindowController* root_window_controller =
    198       GetRootWindowController(root_window);
    199 
    200   // Enable wallpaper transition for the following cases:
    201   // 1. Initial(OOBE) wallpaper animation.
    202   // 2. Wallpaper fades in from a non empty background.
    203   // 3. From an empty background, chrome transit to a logged in user session.
    204   // 4. From an empty background, guest user logged in.
    205   if (wallpaper_delegate->ShouldShowInitialAnimation() ||
    206       root_window_controller->animating_wallpaper_controller() ||
    207       Shell::GetInstance()->session_state_delegate()->NumberOfLoggedInUsers()) {
    208     views::corewm::SetWindowVisibilityAnimationTransition(
    209         desktop_widget->GetNativeView(), views::corewm::ANIMATE_SHOW);
    210   } else {
    211     // Disable animation if transition to login screen from an empty background.
    212     views::corewm::SetWindowVisibilityAnimationTransition(
    213         desktop_widget->GetNativeView(), views::corewm::ANIMATE_NONE);
    214   }
    215 
    216   desktop_widget->SetBounds(params.parent->bounds());
    217   return desktop_widget;
    218 }
    219 
    220 }  // namespace internal
    221 }  // namespace ash
    222