Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / ash / desktop_background / desktop_background_view.cc
blob4ffc3e11f32c1f48151b0a8c77777f9cafa5af6c
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/size_conversions.h"
26 #include "ui/gfx/image/image.h"
27 #include "ui/gfx/transform.h"
28 #include "ui/views/widget/widget.h"
30 using wallpaper::WallpaperLayout;
31 using wallpaper::WALLPAPER_LAYOUT_CENTER;
32 using wallpaper::WALLPAPER_LAYOUT_CENTER_CROPPED;
33 using wallpaper::WALLPAPER_LAYOUT_STRETCH;
34 using wallpaper::WALLPAPER_LAYOUT_TILE;
36 namespace ash {
37 namespace {
39 // For our scaling ratios we need to round positive numbers.
40 int RoundPositive(double x) {
41 return static_cast<int>(floor(x + 0.5));
44 // A view that controls the child view's layer so that the layer
45 // always has the same size as the display's original, un-scaled size
46 // in DIP. The layer then transformed to fit to the virtual screen
47 // size when laid-out.
48 // This is to avoid scaling the image at painting time, then scaling
49 // it back to the screen size in the compositor.
50 class LayerControlView : public views::View {
51 public:
52 explicit LayerControlView(views::View* view) {
53 AddChildView(view);
54 view->SetPaintToLayer(true);
57 // Overrides views::View.
58 void Layout() override {
59 gfx::Display display = Shell::GetScreen()->GetDisplayNearestWindow(
60 GetWidget()->GetNativeView());
61 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
62 DisplayInfo info = display_manager->GetDisplayInfo(display.id());
63 float ui_scale = info.GetEffectiveUIScale();
64 gfx::SizeF pixel_size = display.size();
65 pixel_size.Scale(1.0f / ui_scale);
66 gfx::Size rounded_size = gfx::ToCeiledSize(pixel_size);
67 DCHECK_EQ(1, child_count());
68 views::View* child = child_at(0);
69 child->SetBounds(0, 0, rounded_size.width(), rounded_size.height());
70 gfx::Transform transform;
71 transform.Scale(ui_scale, ui_scale);
72 child->SetTransform(transform);
75 private:
76 DISALLOW_COPY_AND_ASSIGN(LayerControlView);
79 } // namespace
81 // This event handler receives events in the pre-target phase and takes care of
82 // the following:
83 // - Disabling overview mode on touch release.
84 // - Disabling overview mode on mouse release.
85 class PreEventDispatchHandler : public ui::EventHandler {
86 public:
87 PreEventDispatchHandler() {}
88 ~PreEventDispatchHandler() override {}
90 private:
91 // ui::EventHandler:
92 void OnMouseEvent(ui::MouseEvent* event) override {
93 CHECK_EQ(ui::EP_PRETARGET, event->phase());
94 WindowSelectorController* controller =
95 Shell::GetInstance()->window_selector_controller();
96 if (event->type() == ui::ET_MOUSE_RELEASED && controller->IsSelecting()) {
97 controller->ToggleOverview();
98 event->StopPropagation();
102 void OnGestureEvent(ui::GestureEvent* event) override {
103 CHECK_EQ(ui::EP_PRETARGET, event->phase());
104 WindowSelectorController* controller =
105 Shell::GetInstance()->window_selector_controller();
106 if (event->type() == ui::ET_GESTURE_TAP && controller->IsSelecting()) {
107 controller->ToggleOverview();
108 event->StopPropagation();
112 DISALLOW_COPY_AND_ASSIGN(PreEventDispatchHandler);
115 ////////////////////////////////////////////////////////////////////////////////
116 // DesktopBackgroundView, public:
118 DesktopBackgroundView::DesktopBackgroundView()
119 : pre_dispatch_handler_(new PreEventDispatchHandler()) {
120 set_context_menu_controller(this);
121 AddPreTargetHandler(pre_dispatch_handler_.get());
124 DesktopBackgroundView::~DesktopBackgroundView() {
125 RemovePreTargetHandler(pre_dispatch_handler_.get());
128 ////////////////////////////////////////////////////////////////////////////////
129 // DesktopBackgroundView, views::View overrides:
131 void DesktopBackgroundView::OnPaint(gfx::Canvas* canvas) {
132 // Scale the image while maintaining the aspect ratio, cropping as
133 // necessary to fill the background. Ideally the image should be larger
134 // than the largest display supported, if not we will scale and center it if
135 // the layout is WALLPAPER_LAYOUT_CENTER_CROPPED.
136 DesktopBackgroundController* controller =
137 Shell::GetInstance()->desktop_background_controller();
138 gfx::ImageSkia wallpaper = controller->GetWallpaper();
139 WallpaperLayout wallpaper_layout = controller->GetWallpaperLayout();
141 if (wallpaper.isNull()) {
142 canvas->FillRect(GetLocalBounds(), SK_ColorBLACK);
143 return;
146 gfx::NativeView native_view = GetWidget()->GetNativeView();
147 gfx::Display display = gfx::Screen::GetScreenFor(native_view)->
148 GetDisplayNearestWindow(native_view);
150 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
151 DisplayInfo display_info = display_manager->GetDisplayInfo(display.id());
152 float scaling = display_info.GetEffectiveUIScale();
153 if (scaling <= 1.0f)
154 scaling = 1.0f;
155 // Allow scaling up to the UI scaling.
156 // TODO(oshima): Create separate layer that fits to the image and then
157 // scale to avoid artifacts and be more efficient when clipped.
158 gfx::Rect wallpaper_rect(
159 0, 0, wallpaper.width() * scaling, wallpaper.height() * scaling);
161 if (wallpaper_layout == WALLPAPER_LAYOUT_CENTER_CROPPED) {
162 // The dimension with the smallest ratio must be cropped, the other one
163 // is preserved. Both are set in gfx::Size cropped_size.
164 double horizontal_ratio = static_cast<double>(width()) /
165 static_cast<double>(wallpaper.width());
166 double vertical_ratio = static_cast<double>(height()) /
167 static_cast<double>(wallpaper.height());
169 gfx::Size cropped_size;
170 if (vertical_ratio > horizontal_ratio) {
171 cropped_size = gfx::Size(
172 RoundPositive(static_cast<double>(width()) / vertical_ratio),
173 wallpaper.height());
174 } else {
175 cropped_size = gfx::Size(wallpaper.width(),
176 RoundPositive(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