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