cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / ash / desktop_background / desktop_background_view.cc
blob6975c11d66c697b70aa280a5758c1daef98b2098
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 if (wallpaper_layout == WALLPAPER_LAYOUT_CENTER_CROPPED) {
146 // The dimension with the smallest ratio must be cropped, the other one
147 // is preserved. Both are set in gfx::Size cropped_size.
148 double horizontal_ratio = static_cast<double>(width()) /
149 static_cast<double>(wallpaper.width());
150 double vertical_ratio = static_cast<double>(height()) /
151 static_cast<double>(wallpaper.height());
153 gfx::Size cropped_size;
154 if (vertical_ratio > horizontal_ratio) {
155 cropped_size = gfx::Size(
156 gfx::ToFlooredInt(static_cast<double>(width()) / vertical_ratio),
157 wallpaper.height());
158 } else {
159 cropped_size = gfx::Size(
160 wallpaper.width(),
161 gfx::ToFlooredInt(static_cast<double>(height()) / horizontal_ratio));
164 gfx::Rect wallpaper_cropped_rect(
165 0, 0, wallpaper.width(), wallpaper.height());
166 wallpaper_cropped_rect.ClampToCenteredSize(cropped_size);
167 canvas->DrawImageInt(wallpaper,
168 wallpaper_cropped_rect.x(), wallpaper_cropped_rect.y(),
169 wallpaper_cropped_rect.width(), wallpaper_cropped_rect.height(),
170 0, 0, width(), height(),
171 true);
172 } else if (wallpaper_layout == WALLPAPER_LAYOUT_TILE) {
173 canvas->TileImageInt(wallpaper, 0, 0, width(), height());
174 } else if (wallpaper_layout == WALLPAPER_LAYOUT_STRETCH) {
175 // This is generally not recommended as it may show artifacts.
176 canvas->DrawImageInt(wallpaper, 0, 0, wallpaper.width(),
177 wallpaper.height(), 0, 0, width(), height(), true);
178 } else {
179 // Fill with black to make sure that the entire area is opaque.
180 canvas->FillRect(GetLocalBounds(), SK_ColorBLACK);
181 float image_scale = canvas->image_scale();
182 gfx::Rect wallpaper_rect(0, 0, wallpaper.width() / image_scale,
183 wallpaper.height() / image_scale);
184 // All other are simply centered, and not scaled (but may be clipped).
185 canvas->DrawImageInt(
186 wallpaper,
187 0, 0, wallpaper.width(), wallpaper.height(),
188 (width() - wallpaper_rect.width()) / 2,
189 (height() - wallpaper_rect.height()) / 2,
190 wallpaper_rect.width(),
191 wallpaper_rect.height(),
192 true);
196 bool DesktopBackgroundView::OnMousePressed(const ui::MouseEvent& event) {
197 return true;
200 void DesktopBackgroundView::ShowContextMenuForView(
201 views::View* source,
202 const gfx::Point& point,
203 ui::MenuSourceType source_type) {
204 Shell::GetInstance()->ShowContextMenu(point, source_type);
207 views::Widget* CreateDesktopBackground(aura::Window* root_window,
208 int container_id) {
209 DesktopBackgroundController* controller =
210 Shell::GetInstance()->desktop_background_controller();
211 UserWallpaperDelegate* wallpaper_delegate =
212 Shell::GetInstance()->user_wallpaper_delegate();
214 views::Widget* desktop_widget = new views::Widget;
215 views::Widget::InitParams params(
216 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
217 if (controller->GetWallpaper().isNull())
218 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
219 params.parent = root_window->GetChildById(container_id);
220 desktop_widget->Init(params);
221 desktop_widget->SetContentsView(
222 new LayerControlView(new DesktopBackgroundView()));
223 int animation_type = wallpaper_delegate->GetAnimationType();
224 wm::SetWindowVisibilityAnimationType(
225 desktop_widget->GetNativeView(), animation_type);
227 RootWindowController* root_window_controller =
228 GetRootWindowController(root_window);
230 // Enable wallpaper transition for the following cases:
231 // 1. Initial(OOBE) wallpaper animation.
232 // 2. Wallpaper fades in from a non empty background.
233 // 3. From an empty background, chrome transit to a logged in user session.
234 // 4. From an empty background, guest user logged in.
235 if (wallpaper_delegate->ShouldShowInitialAnimation() ||
236 root_window_controller->animating_wallpaper_controller() ||
237 Shell::GetInstance()->session_state_delegate()->NumberOfLoggedInUsers()) {
238 wm::SetWindowVisibilityAnimationTransition(
239 desktop_widget->GetNativeView(), wm::ANIMATE_SHOW);
240 int duration_override = wallpaper_delegate->GetAnimationDurationOverride();
241 if (duration_override) {
242 wm::SetWindowVisibilityAnimationDuration(
243 desktop_widget->GetNativeView(),
244 base::TimeDelta::FromMilliseconds(duration_override));
246 } else {
247 // Disable animation if transition to login screen from an empty background.
248 wm::SetWindowVisibilityAnimationTransition(
249 desktop_widget->GetNativeView(), wm::ANIMATE_NONE);
252 desktop_widget->SetBounds(params.parent->bounds());
253 return desktop_widget;
256 } // namespace ash