Disable accessible touch exploration by default.
[chromium-blink-merge.git] / ash / desktop_background / desktop_background_view.cc
blobd0e850c8889d714d825bb80ed3a1ef940299566d
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.
5 #include "ash/desktop_background/desktop_background_view.h"
7 #include <limits>
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/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/window_event_dispatcher.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"
30 namespace ash {
31 namespace {
33 // For our scaling ratios we need to round positive numbers.
34 int RoundPositive(double x) {
35 return static_cast<int>(floor(x + 0.5));
38 // A view that controls the child view's layer so that the layer
39 // always has the same size as the display's original, un-scaled size
40 // in DIP. The layer then transformed to fit to the virtual screen
41 // size when laid-out.
42 // This is to avoid scaling the image at painting time, then scaling
43 // it back to the screen size in the compositor.
44 class LayerControlView : public views::View {
45 public:
46 explicit LayerControlView(views::View* view) {
47 AddChildView(view);
48 view->SetPaintToLayer(true);
51 // Overrides views::View.
52 virtual void Layout() OVERRIDE {
53 gfx::Display display = Shell::GetScreen()->GetDisplayNearestWindow(
54 GetWidget()->GetNativeView());
55 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
56 DisplayInfo info = display_manager->GetDisplayInfo(display.id());
57 float ui_scale = info.GetEffectiveUIScale();
58 gfx::SizeF pixel_size = display.size();
59 pixel_size.Scale(1.0f / ui_scale);
60 gfx::Size rounded_size = gfx::ToCeiledSize(pixel_size);
61 DCHECK_EQ(1, child_count());
62 views::View* child = child_at(0);
63 child->SetBounds(0, 0, rounded_size.width(), rounded_size.height());
64 gfx::Transform transform;
65 transform.Scale(ui_scale, ui_scale);
66 child->SetTransform(transform);
69 private:
70 DISALLOW_COPY_AND_ASSIGN(LayerControlView);
73 } // namespace
75 ////////////////////////////////////////////////////////////////////////////////
76 // DesktopBackgroundView, public:
78 DesktopBackgroundView::DesktopBackgroundView() {
79 set_context_menu_controller(this);
82 DesktopBackgroundView::~DesktopBackgroundView() {
85 ////////////////////////////////////////////////////////////////////////////////
86 // DesktopBackgroundView, views::View overrides:
88 void DesktopBackgroundView::OnPaint(gfx::Canvas* canvas) {
89 // Scale the image while maintaining the aspect ratio, cropping as
90 // necessary to fill the background. Ideally the image should be larger
91 // than the largest display supported, if not we will scale and center it if
92 // the layout is WALLPAPER_LAYOUT_CENTER_CROPPED.
93 DesktopBackgroundController* controller =
94 Shell::GetInstance()->desktop_background_controller();
95 gfx::ImageSkia wallpaper = controller->GetWallpaper();
96 WallpaperLayout wallpaper_layout = controller->GetWallpaperLayout();
98 if (wallpaper.isNull()) {
99 canvas->FillRect(GetLocalBounds(), SK_ColorBLACK);
100 return;
103 gfx::NativeView native_view = GetWidget()->GetNativeView();
104 gfx::Display display = gfx::Screen::GetScreenFor(native_view)->
105 GetDisplayNearestWindow(native_view);
107 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
108 DisplayInfo display_info = display_manager->GetDisplayInfo(display.id());
109 float scaling = display_info.GetEffectiveUIScale();
110 if (scaling <= 1.0f)
111 scaling = 1.0f;
112 // Allow scaling up to the UI scaling.
113 // TODO(oshima): Create separate layer that fits to the image and then
114 // scale to avoid artifacts and be more efficient when clipped.
115 gfx::Rect wallpaper_rect(
116 0, 0, wallpaper.width() * scaling, wallpaper.height() * scaling);
118 if (wallpaper_layout == WALLPAPER_LAYOUT_CENTER_CROPPED) {
119 // The dimension with the smallest ratio must be cropped, the other one
120 // is preserved. Both are set in gfx::Size cropped_size.
121 double horizontal_ratio = static_cast<double>(width()) /
122 static_cast<double>(wallpaper.width());
123 double vertical_ratio = static_cast<double>(height()) /
124 static_cast<double>(wallpaper.height());
126 gfx::Size cropped_size;
127 if (vertical_ratio > horizontal_ratio) {
128 cropped_size = gfx::Size(
129 RoundPositive(static_cast<double>(width()) / vertical_ratio),
130 wallpaper.height());
131 } else {
132 cropped_size = gfx::Size(wallpaper.width(),
133 RoundPositive(static_cast<double>(height()) / horizontal_ratio));
136 gfx::Rect wallpaper_cropped_rect(
137 0, 0, wallpaper.width(), wallpaper.height());
138 wallpaper_cropped_rect.ClampToCenteredSize(cropped_size);
139 canvas->DrawImageInt(wallpaper,
140 wallpaper_cropped_rect.x(), wallpaper_cropped_rect.y(),
141 wallpaper_cropped_rect.width(), wallpaper_cropped_rect.height(),
142 0, 0, width(), height(),
143 true);
144 } else if (wallpaper_layout == WALLPAPER_LAYOUT_TILE) {
145 canvas->TileImageInt(wallpaper, 0, 0, width(), height());
146 } else if (wallpaper_layout == WALLPAPER_LAYOUT_STRETCH) {
147 // This is generally not recommended as it may show artifacts.
148 canvas->DrawImageInt(wallpaper, 0, 0, wallpaper.width(),
149 wallpaper.height(), 0, 0, width(), height(), true);
150 } else {
151 // Fill with black to make sure that the entire area is opaque.
152 canvas->FillRect(GetLocalBounds(), SK_ColorBLACK);
153 // All other are simply centered, and not scaled (but may be clipped).
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);
165 bool DesktopBackgroundView::OnMousePressed(const ui::MouseEvent& event) {
166 return true;
169 void DesktopBackgroundView::ShowContextMenuForView(
170 views::View* source,
171 const gfx::Point& point,
172 ui::MenuSourceType source_type) {
173 Shell::GetInstance()->ShowContextMenu(point, source_type);
176 views::Widget* CreateDesktopBackground(aura::Window* root_window,
177 int container_id) {
178 DesktopBackgroundController* controller =
179 Shell::GetInstance()->desktop_background_controller();
180 UserWallpaperDelegate* wallpaper_delegate =
181 Shell::GetInstance()->user_wallpaper_delegate();
183 views::Widget* desktop_widget = new views::Widget;
184 views::Widget::InitParams params(
185 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
186 if (controller->GetWallpaper().isNull())
187 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
188 params.parent = root_window->GetChildById(container_id);
189 desktop_widget->Init(params);
190 desktop_widget->GetNativeWindow()->layer()->SetMasksToBounds(true);
191 desktop_widget->SetContentsView(
192 new LayerControlView(new DesktopBackgroundView()));
193 int animation_type = wallpaper_delegate->GetAnimationType();
194 wm::SetWindowVisibilityAnimationType(
195 desktop_widget->GetNativeView(), animation_type);
197 RootWindowController* root_window_controller =
198 GetRootWindowController(root_window);
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 wm::SetWindowVisibilityAnimationTransition(
209 desktop_widget->GetNativeView(), wm::ANIMATE_SHOW);
210 int duration_override = wallpaper_delegate->GetAnimationDurationOverride();
211 if (duration_override) {
212 wm::SetWindowVisibilityAnimationDuration(
213 desktop_widget->GetNativeView(),
214 base::TimeDelta::FromMilliseconds(duration_override));
216 } else {
217 // Disable animation if transition to login screen from an empty background.
218 wm::SetWindowVisibilityAnimationTransition(
219 desktop_widget->GetNativeView(), wm::ANIMATE_NONE);
222 desktop_widget->SetBounds(params.parent->bounds());
223 return desktop_widget;
226 } // namespace ash