OLD | NEW |
| (Empty) |
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 "ash/aura/wm_window_aura.h" | |
8 #include "ash/common/display/display_info.h" | |
9 #include "ash/common/session/session_state_delegate.h" | |
10 #include "ash/common/wallpaper/wallpaper_delegate.h" | |
11 #include "ash/common/wm/overview/window_selector_controller.h" | |
12 #include "ash/common/wm_lookup.h" | |
13 #include "ash/common/wm_shell.h" | |
14 #include "ash/desktop_background/desktop_background_controller.h" | |
15 #include "ash/desktop_background/desktop_background_widget_controller.h" | |
16 #include "ash/root_window_controller.h" | |
17 #include "ash/shell.h" | |
18 #include "ui/display/display.h" | |
19 #include "ui/display/screen.h" | |
20 #include "ui/gfx/canvas.h" | |
21 #include "ui/gfx/geometry/safe_integer_conversions.h" | |
22 #include "ui/gfx/geometry/size_conversions.h" | |
23 #include "ui/gfx/transform.h" | |
24 #include "ui/views/widget/widget.h" | |
25 | |
26 namespace ash { | |
27 namespace { | |
28 | |
29 // A view that controls the child view's layer so that the layer always has the | |
30 // same size as the display's original, un-scaled size in DIP. The layer is then | |
31 // transformed to fit to the virtual screen size when laid-out. This is to avoid | |
32 // scaling the image at painting time, then scaling it back to the screen size | |
33 // in the compositor. | |
34 class LayerControlView : public views::View { | |
35 public: | |
36 explicit LayerControlView(views::View* view) { | |
37 AddChildView(view); | |
38 view->SetPaintToLayer(true); | |
39 } | |
40 | |
41 // Overrides views::View. | |
42 void Layout() override { | |
43 WmWindow* window = WmLookup::Get()->GetWindowForWidget(GetWidget()); | |
44 // Keep |this| at the bottom since there may be other windows on top of the | |
45 // background view such as an overview mode shield. | |
46 window->GetParent()->StackChildAtBottom(window); | |
47 display::Display display = window->GetDisplayNearestWindow(); | |
48 DisplayInfo info = WmShell::Get()->GetDisplayInfo(display.id()); | |
49 float ui_scale = info.GetEffectiveUIScale(); | |
50 gfx::Size rounded_size = | |
51 gfx::ScaleToFlooredSize(display.size(), 1.f / ui_scale); | |
52 DCHECK_EQ(1, child_count()); | |
53 views::View* child = child_at(0); | |
54 child->SetBounds(0, 0, rounded_size.width(), rounded_size.height()); | |
55 gfx::Transform transform; | |
56 // Apply RTL transform explicitly becacuse Views layer code | |
57 // doesn't handle RTL. crbug.com/458753. | |
58 transform.Translate(-child->GetMirroredX(), 0); | |
59 transform.Scale(ui_scale, ui_scale); | |
60 child->SetTransform(transform); | |
61 } | |
62 | |
63 private: | |
64 DISALLOW_COPY_AND_ASSIGN(LayerControlView); | |
65 }; | |
66 | |
67 } // namespace | |
68 | |
69 // This event handler receives events in the pre-target phase and takes care of | |
70 // the following: | |
71 // - Disabling overview mode on touch release. | |
72 // - Disabling overview mode on mouse release. | |
73 class PreEventDispatchHandler : public ui::EventHandler { | |
74 public: | |
75 PreEventDispatchHandler() {} | |
76 ~PreEventDispatchHandler() override {} | |
77 | |
78 private: | |
79 // ui::EventHandler: | |
80 void OnMouseEvent(ui::MouseEvent* event) override { | |
81 CHECK_EQ(ui::EP_PRETARGET, event->phase()); | |
82 WindowSelectorController* controller = | |
83 WmShell::Get()->window_selector_controller(); | |
84 if (event->type() == ui::ET_MOUSE_RELEASED && controller->IsSelecting()) { | |
85 controller->ToggleOverview(); | |
86 event->StopPropagation(); | |
87 } | |
88 } | |
89 | |
90 void OnGestureEvent(ui::GestureEvent* event) override { | |
91 CHECK_EQ(ui::EP_PRETARGET, event->phase()); | |
92 WindowSelectorController* controller = | |
93 WmShell::Get()->window_selector_controller(); | |
94 if (event->type() == ui::ET_GESTURE_TAP && controller->IsSelecting()) { | |
95 controller->ToggleOverview(); | |
96 event->StopPropagation(); | |
97 } | |
98 } | |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(PreEventDispatchHandler); | |
101 }; | |
102 | |
103 //////////////////////////////////////////////////////////////////////////////// | |
104 // DesktopBackgroundView, public: | |
105 | |
106 DesktopBackgroundView::DesktopBackgroundView() | |
107 : pre_dispatch_handler_(new PreEventDispatchHandler()) { | |
108 set_context_menu_controller(this); | |
109 AddPreTargetHandler(pre_dispatch_handler_.get()); | |
110 } | |
111 | |
112 DesktopBackgroundView::~DesktopBackgroundView() { | |
113 RemovePreTargetHandler(pre_dispatch_handler_.get()); | |
114 } | |
115 | |
116 //////////////////////////////////////////////////////////////////////////////// | |
117 // DesktopBackgroundView, views::View overrides: | |
118 | |
119 void DesktopBackgroundView::OnPaint(gfx::Canvas* canvas) { | |
120 // Scale the image while maintaining the aspect ratio, cropping as | |
121 // necessary to fill the background. Ideally the image should be larger | |
122 // than the largest display supported, if not we will scale and center it if | |
123 // the layout is wallpaper::WALLPAPER_LAYOUT_CENTER_CROPPED. | |
124 DesktopBackgroundController* controller = | |
125 Shell::GetInstance()->desktop_background_controller(); | |
126 gfx::ImageSkia wallpaper = controller->GetWallpaper(); | |
127 wallpaper::WallpaperLayout layout = controller->GetWallpaperLayout(); | |
128 | |
129 // Wallpapers with png format could be partially transparent. | |
130 // Fill the canvas with black background to make it opaque | |
131 // before painting wallpaper | |
132 canvas->FillRect(GetLocalBounds(), SK_ColorBLACK); | |
133 | |
134 if (wallpaper.isNull()) | |
135 return; | |
136 | |
137 if (layout == wallpaper::WALLPAPER_LAYOUT_CENTER_CROPPED) { | |
138 // The dimension with the smallest ratio must be cropped, the other one | |
139 // is preserved. Both are set in gfx::Size cropped_size. | |
140 double horizontal_ratio = | |
141 static_cast<double>(width()) / static_cast<double>(wallpaper.width()); | |
142 double vertical_ratio = | |
143 static_cast<double>(height()) / static_cast<double>(wallpaper.height()); | |
144 | |
145 gfx::Size cropped_size; | |
146 if (vertical_ratio > horizontal_ratio) { | |
147 cropped_size = gfx::Size( | |
148 gfx::ToFlooredInt(static_cast<double>(width()) / vertical_ratio), | |
149 wallpaper.height()); | |
150 } else { | |
151 cropped_size = gfx::Size( | |
152 wallpaper.width(), | |
153 gfx::ToFlooredInt(static_cast<double>(height()) / horizontal_ratio)); | |
154 } | |
155 | |
156 gfx::Rect wallpaper_cropped_rect(0, 0, wallpaper.width(), | |
157 wallpaper.height()); | |
158 wallpaper_cropped_rect.ClampToCenteredSize(cropped_size); | |
159 canvas->DrawImageInt( | |
160 wallpaper, wallpaper_cropped_rect.x(), wallpaper_cropped_rect.y(), | |
161 wallpaper_cropped_rect.width(), wallpaper_cropped_rect.height(), 0, 0, | |
162 width(), height(), true); | |
163 } else if (layout == wallpaper::WALLPAPER_LAYOUT_TILE) { | |
164 canvas->TileImageInt(wallpaper, 0, 0, width(), height()); | |
165 } else if (layout == wallpaper::WALLPAPER_LAYOUT_STRETCH) { | |
166 // This is generally not recommended as it may show artifacts. | |
167 canvas->DrawImageInt(wallpaper, 0, 0, wallpaper.width(), wallpaper.height(), | |
168 0, 0, width(), height(), true); | |
169 } else { | |
170 float image_scale = canvas->image_scale(); | |
171 gfx::Rect wallpaper_rect(0, 0, wallpaper.width() / image_scale, | |
172 wallpaper.height() / image_scale); | |
173 // All other are simply centered, and not scaled (but may be clipped). | |
174 canvas->DrawImageInt(wallpaper, 0, 0, wallpaper.width(), wallpaper.height(), | |
175 (width() - wallpaper_rect.width()) / 2, | |
176 (height() - wallpaper_rect.height()) / 2, | |
177 wallpaper_rect.width(), wallpaper_rect.height(), true); | |
178 } | |
179 } | |
180 | |
181 bool DesktopBackgroundView::OnMousePressed(const ui::MouseEvent& event) { | |
182 return true; | |
183 } | |
184 | |
185 void DesktopBackgroundView::ShowContextMenuForView( | |
186 views::View* source, | |
187 const gfx::Point& point, | |
188 ui::MenuSourceType source_type) { | |
189 WmShell::Get()->ShowContextMenu(point, source_type); | |
190 } | |
191 | |
192 views::Widget* CreateDesktopBackground(WmWindow* root_window, | |
193 int container_id) { | |
194 aura::Window* aura_root_window = WmWindowAura::GetAuraWindow(root_window); | |
195 DesktopBackgroundController* controller = | |
196 Shell::GetInstance()->desktop_background_controller(); | |
197 WallpaperDelegate* wallpaper_delegate = WmShell::Get()->wallpaper_delegate(); | |
198 | |
199 views::Widget* desktop_widget = new views::Widget; | |
200 views::Widget::InitParams params( | |
201 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | |
202 params.name = "DesktopBackgroundView"; | |
203 if (controller->GetWallpaper().isNull()) | |
204 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
205 params.parent = aura_root_window->GetChildById(container_id); | |
206 desktop_widget->Init(params); | |
207 desktop_widget->SetContentsView( | |
208 new LayerControlView(new DesktopBackgroundView())); | |
209 int animation_type = wallpaper_delegate->GetAnimationType(); | |
210 WmWindow* desktop_window = | |
211 WmLookup::Get()->GetWindowForWidget(desktop_widget); | |
212 desktop_window->SetVisibilityAnimationType(animation_type); | |
213 | |
214 RootWindowController* root_window_controller = | |
215 GetRootWindowController(aura_root_window); | |
216 | |
217 // Enable wallpaper transition for the following cases: | |
218 // 1. Initial(OOBE) wallpaper animation. | |
219 // 2. Wallpaper fades in from a non empty background. | |
220 // 3. From an empty background, chrome transit to a logged in user session. | |
221 // 4. From an empty background, guest user logged in. | |
222 if (wallpaper_delegate->ShouldShowInitialAnimation() || | |
223 root_window_controller->animating_wallpaper_controller() || | |
224 WmShell::Get()->GetSessionStateDelegate()->NumberOfLoggedInUsers()) { | |
225 desktop_window->SetVisibilityAnimationTransition(::wm::ANIMATE_SHOW); | |
226 int duration_override = wallpaper_delegate->GetAnimationDurationOverride(); | |
227 if (duration_override) { | |
228 desktop_window->SetVisibilityAnimationDuration( | |
229 base::TimeDelta::FromMilliseconds(duration_override)); | |
230 } | |
231 } else { | |
232 // Disable animation if transition to login screen from an empty background. | |
233 desktop_window->SetVisibilityAnimationTransition(::wm::ANIMATE_NONE); | |
234 } | |
235 | |
236 desktop_widget->SetBounds(params.parent->bounds()); | |
237 return desktop_widget; | |
238 } | |
239 | |
240 } // namespace ash | |
OLD | NEW |