Add abhijeet.k@samsung.com to AUTHORS list.
[chromium-blink-merge.git] / ash / desktop_background / desktop_background_view.cc
blob464d802137c21164164bdc7fa02e60d0a4261fbb
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/overview/window_selector_controller.h"
19 #include "ash/wm/window_animations.h"
20 #include "base/message_loop/message_loop.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "ui/aura/window_event_dispatcher.h"
23 #include "ui/compositor/layer.h"
24 #include "ui/gfx/canvas.h"
25 #include "ui/gfx/geometry/safe_integer_conversions.h"
26 #include "ui/gfx/geometry/size_conversions.h"
27 #include "ui/gfx/image/image.h"
28 #include "ui/gfx/transform.h"
29 #include "ui/views/widget/widget.h"
31 using wallpaper::WallpaperLayout;
32 using wallpaper::WALLPAPER_LAYOUT_CENTER;
33 using wallpaper::WALLPAPER_LAYOUT_CENTER_CROPPED;
34 using wallpaper::WALLPAPER_LAYOUT_STRETCH;
35 using wallpaper::WALLPAPER_LAYOUT_TILE;
37 namespace ash {
38 namespace {
40 // A view that controls the child view's layer so that the layer
41 // always has the same size as the display's original, un-scaled size
42 // in DIP. The layer then transformed to fit to the virtual screen
43 // size when laid-out.
44 // This is to avoid scaling the image at painting time, then scaling
45 // it back to the screen size in the compositor.
46 class LayerControlView : public views::View {
47 public:
48 explicit LayerControlView(views::View* view) {
49 AddChildView(view);
50 view->SetPaintToLayer(true);
53 // Overrides views::View.
54 void Layout() override {
55 gfx::Display display = Shell::GetScreen()->GetDisplayNearestWindow(
56 GetWidget()->GetNativeView());
57 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
58 DisplayInfo info = display_manager->GetDisplayInfo(display.id());
59 float ui_scale = info.GetEffectiveUIScale();
60 gfx::SizeF pixel_size = display.size();
61 pixel_size.Scale(1.0f / ui_scale);
62 gfx::Size rounded_size = gfx::ToFlooredSize(pixel_size);
63 DCHECK_EQ(1, child_count());
64 views::View* child = child_at(0);
65 child->SetBounds(0, 0, rounded_size.width(), rounded_size.height());
66 gfx::Transform transform;
67 // Apply RTL transform explicitly becacuse Views layer code
68 // doesn't handle RTL. crbug.com/458753.
69 transform.Translate(-child->GetMirroredX(), 0);
70 transform.Scale(ui_scale, ui_scale);
71 child->SetTransform(transform);
74 private:
75 DISALLOW_COPY_AND_ASSIGN(LayerControlView);
78 } // namespace
80 // This event handler receives events in the pre-target phase and takes care of
81 // the following:
82 // - Disabling overview mode on touch release.
83 // - Disabling overview mode on mouse release.
84 class PreEventDispatchHandler : public ui::EventHandler {
85 public:
86 PreEventDispatchHandler() {}
87 ~PreEventDispatchHandler() override {}
89 private:
90 // ui::EventHandler:
91 void OnMouseEvent(ui::MouseEvent* event) override {
92 CHECK_EQ(ui::EP_PRETARGET, event->phase());
93 WindowSelectorController* controller =
94 Shell::GetInstance()->window_selector_controller();
95 if (event->type() == ui::ET_MOUSE_RELEASED && controller->IsSelecting()) {
96 controller->ToggleOverview();
97 event->StopPropagation();
101 void OnGestureEvent(ui::GestureEvent* event) override {
102 CHECK_EQ(ui::EP_PRETARGET, event->phase());
103 WindowSelectorController* controller =
104 Shell::GetInstance()->window_selector_controller();
105 if (event->type() == ui::ET_GESTURE_TAP && controller->IsSelecting()) {
106 controller->ToggleOverview();
107 event->StopPropagation();
111 DISALLOW_COPY_AND_ASSIGN(PreEventDispatchHandler);
114 ////////////////////////////////////////////////////////////////////////////////
115 // DesktopBackgroundView, public:
117 DesktopBackgroundView::DesktopBackgroundView()
118 : pre_dispatch_handler_(new PreEventDispatchHandler()) {
119 set_context_menu_controller(this);
120 AddPreTargetHandler(pre_dispatch_handler_.get());
123 DesktopBackgroundView::~DesktopBackgroundView() {
124 RemovePreTargetHandler(pre_dispatch_handler_.get());
127 ////////////////////////////////////////////////////////////////////////////////
128 // DesktopBackgroundView, views::View overrides:
130 void DesktopBackgroundView::OnPaint(gfx::Canvas* canvas) {
131 // Scale the image while maintaining the aspect ratio, cropping as
132 // necessary to fill the background. Ideally the image should be larger
133 // than the largest display supported, if not we will scale and center it if
134 // the layout is WALLPAPER_LAYOUT_CENTER_CROPPED.
135 DesktopBackgroundController* controller =
136 Shell::GetInstance()->desktop_background_controller();
137 gfx::ImageSkia wallpaper = controller->GetWallpaper();
138 WallpaperLayout wallpaper_layout = controller->GetWallpaperLayout();
140 if (wallpaper.isNull()) {
141 canvas->FillRect(GetLocalBounds(), SK_ColorBLACK);
142 return;
145 gfx::NativeView native_view = GetWidget()->GetNativeView();
146 gfx::Display display = gfx::Screen::GetScreenFor(native_view)->
147 GetDisplayNearestWindow(native_view);
149 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
150 DisplayInfo display_info = display_manager->GetDisplayInfo(display.id());
151 float scaling = display_info.GetEffectiveUIScale();
152 if (scaling <= 1.0f)
153 scaling = 1.0f;
154 // Allow scaling up to the UI scaling.
155 // TODO(oshima): Create separate layer that fits to the image and then
156 // scale to avoid artifacts and be more efficient when clipped.
157 gfx::Rect wallpaper_rect(
158 0, 0, wallpaper.width() * scaling, wallpaper.height() * scaling);
160 if (wallpaper_layout == WALLPAPER_LAYOUT_CENTER_CROPPED) {
161 // The dimension with the smallest ratio must be cropped, the other one
162 // is preserved. Both are set in gfx::Size cropped_size.
163 double horizontal_ratio = static_cast<double>(width()) /
164 static_cast<double>(wallpaper.width());
165 double vertical_ratio = static_cast<double>(height()) /
166 static_cast<double>(wallpaper.height());
168 gfx::Size cropped_size;
169 if (vertical_ratio > horizontal_ratio) {
170 cropped_size = gfx::Size(
171 gfx::ToFlooredInt(static_cast<double>(width()) / vertical_ratio),
172 wallpaper.height());
173 } else {
174 cropped_size = gfx::Size(
175 wallpaper.width(),
176 gfx::ToFlooredInt(static_cast<double>(height()) / horizontal_ratio));
179 gfx::Rect wallpaper_cropped_rect(
180 0, 0, wallpaper.width(), wallpaper.height());
181 wallpaper_cropped_rect.ClampToCenteredSize(cropped_size);
182 canvas->DrawImageInt(wallpaper,
183 wallpaper_cropped_rect.x(), wallpaper_cropped_rect.y(),
184 wallpaper_cropped_rect.width(), wallpaper_cropped_rect.height(),
185 0, 0, width(), height(),
186 true);
187 } else if (wallpaper_layout == WALLPAPER_LAYOUT_TILE) {
188 canvas->TileImageInt(wallpaper, 0, 0, width(), height());
189 } else if (wallpaper_layout == WALLPAPER_LAYOUT_STRETCH) {
190 // This is generally not recommended as it may show artifacts.
191 canvas->DrawImageInt(wallpaper, 0, 0, wallpaper.width(),
192 wallpaper.height(), 0, 0, width(), height(), true);
193 } else {
194 // Fill with black to make sure that the entire area is opaque.
195 canvas->FillRect(GetLocalBounds(), SK_ColorBLACK);
196 // All other are simply centered, and not scaled (but may be clipped).
197 canvas->DrawImageInt(
198 wallpaper,
199 0, 0, wallpaper.width(), wallpaper.height(),
200 (width() - wallpaper_rect.width()) / 2,
201 (height() - wallpaper_rect.height()) / 2,
202 wallpaper_rect.width(),
203 wallpaper_rect.height(),
204 true);
208 bool DesktopBackgroundView::OnMousePressed(const ui::MouseEvent& event) {
209 return true;
212 void DesktopBackgroundView::ShowContextMenuForView(
213 views::View* source,
214 const gfx::Point& point,
215 ui::MenuSourceType source_type) {
216 Shell::GetInstance()->ShowContextMenu(point, source_type);
219 views::Widget* CreateDesktopBackground(aura::Window* root_window,
220 int container_id) {
221 DesktopBackgroundController* controller =
222 Shell::GetInstance()->desktop_background_controller();
223 UserWallpaperDelegate* wallpaper_delegate =
224 Shell::GetInstance()->user_wallpaper_delegate();
226 views::Widget* desktop_widget = new views::Widget;
227 views::Widget::InitParams params(
228 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
229 if (controller->GetWallpaper().isNull())
230 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
231 params.parent = root_window->GetChildById(container_id);
232 desktop_widget->Init(params);
233 desktop_widget->GetNativeWindow()->layer()->SetMasksToBounds(true);
234 desktop_widget->SetContentsView(
235 new LayerControlView(new DesktopBackgroundView()));
236 int animation_type = wallpaper_delegate->GetAnimationType();
237 wm::SetWindowVisibilityAnimationType(
238 desktop_widget->GetNativeView(), animation_type);
240 RootWindowController* root_window_controller =
241 GetRootWindowController(root_window);
243 // Enable wallpaper transition for the following cases:
244 // 1. Initial(OOBE) wallpaper animation.
245 // 2. Wallpaper fades in from a non empty background.
246 // 3. From an empty background, chrome transit to a logged in user session.
247 // 4. From an empty background, guest user logged in.
248 if (wallpaper_delegate->ShouldShowInitialAnimation() ||
249 root_window_controller->animating_wallpaper_controller() ||
250 Shell::GetInstance()->session_state_delegate()->NumberOfLoggedInUsers()) {
251 wm::SetWindowVisibilityAnimationTransition(
252 desktop_widget->GetNativeView(), wm::ANIMATE_SHOW);
253 int duration_override = wallpaper_delegate->GetAnimationDurationOverride();
254 if (duration_override) {
255 wm::SetWindowVisibilityAnimationDuration(
256 desktop_widget->GetNativeView(),
257 base::TimeDelta::FromMilliseconds(duration_override));
259 } else {
260 // Disable animation if transition to login screen from an empty background.
261 wm::SetWindowVisibilityAnimationTransition(
262 desktop_widget->GetNativeView(), wm::ANIMATE_NONE);
265 desktop_widget->SetBounds(params.parent->bounds());
266 return desktop_widget;
269 } // namespace ash