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/wallpaper/wallpaper_view.h" | |
6 | |
7 #include "ash/aura/wm_window_aura.h" | |
8 #include "ash/common/session/session_state_delegate.h" | |
9 #include "ash/common/wallpaper/wallpaper_delegate.h" | |
10 #include "ash/common/wm/overview/window_selector_controller.h" | |
11 #include "ash/common/wm_lookup.h" | |
12 #include "ash/common/wm_shell.h" | |
13 #include "ash/root_window_controller.h" | |
14 #include "ash/shell.h" | |
15 #include "ash/wallpaper/wallpaper_controller.h" | |
16 #include "ash/wallpaper/wallpaper_widget_controller.h" | |
17 #include "ui/display/display.h" | |
18 #include "ui/display/manager/managed_display_info.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 // wallpaper view such as an overview mode shield. | |
46 window->GetParent()->StackChildAtBottom(window); | |
47 display::Display display = window->GetDisplayNearestWindow(); | |
48 display::ManagedDisplayInfo info = | |
49 WmShell::Get()->GetDisplayInfo(display.id()); | |
50 float ui_scale = info.GetEffectiveUIScale(); | |
51 gfx::Size rounded_size = | |
52 gfx::ScaleToFlooredSize(display.size(), 1.f / ui_scale); | |
53 DCHECK_EQ(1, child_count()); | |
54 views::View* child = child_at(0); | |
55 child->SetBounds(0, 0, rounded_size.width(), rounded_size.height()); | |
56 gfx::Transform transform; | |
57 // Apply RTL transform explicitly becacuse Views layer code | |
58 // doesn't handle RTL. crbug.com/458753. | |
59 transform.Translate(-child->GetMirroredX(), 0); | |
60 transform.Scale(ui_scale, ui_scale); | |
61 child->SetTransform(transform); | |
62 } | |
63 | |
64 private: | |
65 DISALLOW_COPY_AND_ASSIGN(LayerControlView); | |
66 }; | |
67 | |
68 } // namespace | |
69 | |
70 // This event handler receives events in the pre-target phase and takes care of | |
71 // the following: | |
72 // - Disabling overview mode on touch release. | |
73 // - Disabling overview mode on mouse release. | |
74 class PreEventDispatchHandler : public ui::EventHandler { | |
75 public: | |
76 PreEventDispatchHandler() {} | |
77 ~PreEventDispatchHandler() override {} | |
78 | |
79 private: | |
80 // ui::EventHandler: | |
81 void OnMouseEvent(ui::MouseEvent* event) override { | |
82 CHECK_EQ(ui::EP_PRETARGET, event->phase()); | |
83 WindowSelectorController* controller = | |
84 WmShell::Get()->window_selector_controller(); | |
85 if (event->type() == ui::ET_MOUSE_RELEASED && controller->IsSelecting()) { | |
86 controller->ToggleOverview(); | |
87 event->StopPropagation(); | |
88 } | |
89 } | |
90 | |
91 void OnGestureEvent(ui::GestureEvent* event) override { | |
92 CHECK_EQ(ui::EP_PRETARGET, event->phase()); | |
93 WindowSelectorController* controller = | |
94 WmShell::Get()->window_selector_controller(); | |
95 if (event->type() == ui::ET_GESTURE_TAP && controller->IsSelecting()) { | |
96 controller->ToggleOverview(); | |
97 event->StopPropagation(); | |
98 } | |
99 } | |
100 | |
101 DISALLOW_COPY_AND_ASSIGN(PreEventDispatchHandler); | |
102 }; | |
103 | |
104 //////////////////////////////////////////////////////////////////////////////// | |
105 // WallpaperView, public: | |
106 | |
107 WallpaperView::WallpaperView() | |
108 : pre_dispatch_handler_(new PreEventDispatchHandler()) { | |
109 set_context_menu_controller(this); | |
110 AddPreTargetHandler(pre_dispatch_handler_.get()); | |
111 } | |
112 | |
113 WallpaperView::~WallpaperView() { | |
114 RemovePreTargetHandler(pre_dispatch_handler_.get()); | |
115 } | |
116 | |
117 //////////////////////////////////////////////////////////////////////////////// | |
118 // WallpaperView, views::View overrides: | |
119 | |
120 void WallpaperView::OnPaint(gfx::Canvas* canvas) { | |
121 // Scale the image while maintaining the aspect ratio, cropping as necessary | |
122 // to fill the wallpaper. Ideally the image should be larger than the largest | |
123 // display supported, if not we will scale and center it if the layout is | |
124 // wallpaper::WALLPAPER_LAYOUT_CENTER_CROPPED. | |
125 WallpaperController* controller = | |
126 Shell::GetInstance()->wallpaper_controller(); | |
127 gfx::ImageSkia wallpaper = controller->GetWallpaper(); | |
128 wallpaper::WallpaperLayout layout = controller->GetWallpaperLayout(); | |
129 | |
130 // Wallpapers with png format could be partially transparent. Fill the canvas | |
131 // with black to make it opaque before painting the 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 WallpaperView::OnMousePressed(const ui::MouseEvent& event) { | |
182 return true; | |
183 } | |
184 | |
185 void WallpaperView::ShowContextMenuForView(views::View* source, | |
186 const gfx::Point& point, | |
187 ui::MenuSourceType source_type) { | |
188 WmShell::Get()->ShowContextMenu(point, source_type); | |
189 } | |
190 | |
191 views::Widget* CreateWallpaper(WmWindow* root_window, int container_id) { | |
192 aura::Window* aura_root_window = WmWindowAura::GetAuraWindow(root_window); | |
193 WallpaperController* controller = | |
194 Shell::GetInstance()->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 params.parent = aura_root_window->GetChildById(container_id); | |
204 wallpaper_widget->Init(params); | |
205 wallpaper_widget->SetContentsView(new LayerControlView(new WallpaperView())); | |
206 int animation_type = wallpaper_delegate->GetAnimationType(); | |
207 WmWindow* wallpaper_window = | |
208 WmLookup::Get()->GetWindowForWidget(wallpaper_widget); | |
209 wallpaper_window->SetVisibilityAnimationType(animation_type); | |
210 | |
211 RootWindowController* root_window_controller = | |
212 GetRootWindowController(aura_root_window); | |
213 | |
214 // Enable wallpaper transition for the following cases: | |
215 // 1. Initial(OOBE) wallpaper animation. | |
216 // 2. Wallpaper fades in from a non empty background. | |
217 // 3. From an empty background, chrome transit to a logged in user session. | |
218 // 4. From an empty background, guest user logged in. | |
219 if (wallpaper_delegate->ShouldShowInitialAnimation() || | |
220 root_window_controller->animating_wallpaper_widget_controller() || | |
221 WmShell::Get()->GetSessionStateDelegate()->NumberOfLoggedInUsers()) { | |
222 wallpaper_window->SetVisibilityAnimationTransition(::wm::ANIMATE_SHOW); | |
223 int duration_override = wallpaper_delegate->GetAnimationDurationOverride(); | |
224 if (duration_override) { | |
225 wallpaper_window->SetVisibilityAnimationDuration( | |
226 base::TimeDelta::FromMilliseconds(duration_override)); | |
227 } | |
228 } else { | |
229 // Disable animation if transition to login screen from an empty background. | |
230 wallpaper_window->SetVisibilityAnimationTransition(::wm::ANIMATE_NONE); | |
231 } | |
232 | |
233 wallpaper_widget->SetBounds(params.parent->bounds()); | |
234 return wallpaper_widget; | |
235 } | |
236 | |
237 } // namespace ash | |
OLD | NEW |