Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(213)

Side by Side Diff: ash/desktop_background/desktop_background_view.cc

Issue 2200893002: mash: Partially migrate ash/desktop_background to ash common types. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix mash browser test crash in GetMaxDisplaySizeInNative. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/desktop_background/desktop_background_view.h" 5 #include "ash/desktop_background/desktop_background_view.h"
6 6
7 #include <limits> 7 #include "ash/aura/wm_window_aura.h"
8 8 #include "ash/common/display/display_info.h"
9 #include "ash/ash_export.h"
10 #include "ash/common/session/session_state_delegate.h" 9 #include "ash/common/session/session_state_delegate.h"
11 #include "ash/common/shell_window_ids.h"
12 #include "ash/common/wm/overview/window_selector_controller.h" 10 #include "ash/common/wm/overview/window_selector_controller.h"
11 #include "ash/common/wm_lookup.h"
13 #include "ash/common/wm_shell.h" 12 #include "ash/common/wm_shell.h"
14 #include "ash/desktop_background/desktop_background_controller.h" 13 #include "ash/desktop_background/desktop_background_controller.h"
15 #include "ash/desktop_background/desktop_background_widget_controller.h" 14 #include "ash/desktop_background/desktop_background_widget_controller.h"
16 #include "ash/desktop_background/user_wallpaper_delegate.h" 15 #include "ash/desktop_background/user_wallpaper_delegate.h"
17 #include "ash/display/display_manager.h"
18 #include "ash/root_window_controller.h" 16 #include "ash/root_window_controller.h"
19 #include "ash/shell.h" 17 #include "ash/shell.h"
20 #include "ash/wm/window_animations.h" 18 #include "ui/display/display.h"
21 #include "base/message_loop/message_loop.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "ui/aura/window_event_dispatcher.h"
24 #include "ui/compositor/layer.h"
25 #include "ui/display/screen.h" 19 #include "ui/display/screen.h"
26 #include "ui/gfx/canvas.h" 20 #include "ui/gfx/canvas.h"
27 #include "ui/gfx/geometry/safe_integer_conversions.h" 21 #include "ui/gfx/geometry/safe_integer_conversions.h"
28 #include "ui/gfx/geometry/size_conversions.h" 22 #include "ui/gfx/geometry/size_conversions.h"
29 #include "ui/gfx/image/image.h"
30 #include "ui/gfx/transform.h" 23 #include "ui/gfx/transform.h"
31 #include "ui/views/widget/widget.h" 24 #include "ui/views/widget/widget.h"
32 25
33 using wallpaper::WallpaperLayout;
34 using wallpaper::WALLPAPER_LAYOUT_CENTER;
35 using wallpaper::WALLPAPER_LAYOUT_CENTER_CROPPED;
36 using wallpaper::WALLPAPER_LAYOUT_STRETCH;
37 using wallpaper::WALLPAPER_LAYOUT_TILE;
38
39 namespace ash { 26 namespace ash {
40 namespace { 27 namespace {
41 28
42 // A view that controls the child view's layer so that the layer 29 // A view that controls the child view's layer so that the layer always has the
43 // always has the same size as the display's original, un-scaled size 30 // same size as the display's original, un-scaled size in DIP. The layer is then
44 // in DIP. The layer then transformed to fit to the virtual screen 31 // transformed to fit to the virtual screen size when laid-out. This is to avoid
45 // size when laid-out. 32 // scaling the image at painting time, then scaling it back to the screen size
46 // This is to avoid scaling the image at painting time, then scaling 33 // in the compositor.
47 // it back to the screen size in the compositor.
48 class LayerControlView : public views::View { 34 class LayerControlView : public views::View {
49 public: 35 public:
50 explicit LayerControlView(views::View* view) { 36 explicit LayerControlView(views::View* view) {
51 AddChildView(view); 37 AddChildView(view);
52 view->SetPaintToLayer(true); 38 view->SetPaintToLayer(true);
53 } 39 }
54 40
55 // Overrides views::View. 41 // Overrides views::View.
56 void Layout() override { 42 void Layout() override {
57 display::Display display = 43 WmWindow* window = WmLookup::Get()->GetWindowForWidget(GetWidget());
58 display::Screen::GetScreen()->GetDisplayNearestWindow( 44 display::Display display = window->GetDisplayNearestWindow();
59 GetWidget()->GetNativeView()); 45 DisplayInfo info = WmShell::Get()->GetDisplayInfo(display.id());
60 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
61 DisplayInfo info = display_manager->GetDisplayInfo(display.id());
62 float ui_scale = info.GetEffectiveUIScale(); 46 float ui_scale = info.GetEffectiveUIScale();
63 gfx::Size rounded_size = 47 gfx::Size rounded_size =
64 gfx::ScaleToFlooredSize(display.size(), 1.f / ui_scale); 48 gfx::ScaleToFlooredSize(display.size(), 1.f / ui_scale);
65 DCHECK_EQ(1, child_count()); 49 DCHECK_EQ(1, child_count());
66 views::View* child = child_at(0); 50 views::View* child = child_at(0);
67 child->SetBounds(0, 0, rounded_size.width(), rounded_size.height()); 51 child->SetBounds(0, 0, rounded_size.width(), rounded_size.height());
68 gfx::Transform transform; 52 gfx::Transform transform;
69 // Apply RTL transform explicitly becacuse Views layer code 53 // Apply RTL transform explicitly becacuse Views layer code
70 // doesn't handle RTL. crbug.com/458753. 54 // doesn't handle RTL. crbug.com/458753.
71 transform.Translate(-child->GetMirroredX(), 0); 55 transform.Translate(-child->GetMirroredX(), 0);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 RemovePreTargetHandler(pre_dispatch_handler_.get()); 110 RemovePreTargetHandler(pre_dispatch_handler_.get());
127 } 111 }
128 112
129 //////////////////////////////////////////////////////////////////////////////// 113 ////////////////////////////////////////////////////////////////////////////////
130 // DesktopBackgroundView, views::View overrides: 114 // DesktopBackgroundView, views::View overrides:
131 115
132 void DesktopBackgroundView::OnPaint(gfx::Canvas* canvas) { 116 void DesktopBackgroundView::OnPaint(gfx::Canvas* canvas) {
133 // Scale the image while maintaining the aspect ratio, cropping as 117 // Scale the image while maintaining the aspect ratio, cropping as
134 // necessary to fill the background. Ideally the image should be larger 118 // necessary to fill the background. Ideally the image should be larger
135 // than the largest display supported, if not we will scale and center it if 119 // than the largest display supported, if not we will scale and center it if
136 // the layout is WALLPAPER_LAYOUT_CENTER_CROPPED. 120 // the layout is wallpaper::WALLPAPER_LAYOUT_CENTER_CROPPED.
137 DesktopBackgroundController* controller = 121 DesktopBackgroundController* controller =
138 Shell::GetInstance()->desktop_background_controller(); 122 Shell::GetInstance()->desktop_background_controller();
139 gfx::ImageSkia wallpaper = controller->GetWallpaper(); 123 gfx::ImageSkia wallpaper = controller->GetWallpaper();
140 WallpaperLayout wallpaper_layout = controller->GetWallpaperLayout(); 124 wallpaper::WallpaperLayout layout = controller->GetWallpaperLayout();
141 125
142 // Wallpapers with png format could be partially transparent. 126 // Wallpapers with png format could be partially transparent.
143 // Fill the canvas with black background to make it opaque 127 // Fill the canvas with black background to make it opaque
144 // before painting wallpaper 128 // before painting wallpaper
145 canvas->FillRect(GetLocalBounds(), SK_ColorBLACK); 129 canvas->FillRect(GetLocalBounds(), SK_ColorBLACK);
146 130
147 if (wallpaper.isNull()) 131 if (wallpaper.isNull())
148 return; 132 return;
149 133
150 if (wallpaper_layout == WALLPAPER_LAYOUT_CENTER_CROPPED) { 134 if (layout == wallpaper::WALLPAPER_LAYOUT_CENTER_CROPPED) {
151 // The dimension with the smallest ratio must be cropped, the other one 135 // The dimension with the smallest ratio must be cropped, the other one
152 // is preserved. Both are set in gfx::Size cropped_size. 136 // is preserved. Both are set in gfx::Size cropped_size.
153 double horizontal_ratio = 137 double horizontal_ratio =
154 static_cast<double>(width()) / static_cast<double>(wallpaper.width()); 138 static_cast<double>(width()) / static_cast<double>(wallpaper.width());
155 double vertical_ratio = 139 double vertical_ratio =
156 static_cast<double>(height()) / static_cast<double>(wallpaper.height()); 140 static_cast<double>(height()) / static_cast<double>(wallpaper.height());
157 141
158 gfx::Size cropped_size; 142 gfx::Size cropped_size;
159 if (vertical_ratio > horizontal_ratio) { 143 if (vertical_ratio > horizontal_ratio) {
160 cropped_size = gfx::Size( 144 cropped_size = gfx::Size(
161 gfx::ToFlooredInt(static_cast<double>(width()) / vertical_ratio), 145 gfx::ToFlooredInt(static_cast<double>(width()) / vertical_ratio),
162 wallpaper.height()); 146 wallpaper.height());
163 } else { 147 } else {
164 cropped_size = gfx::Size( 148 cropped_size = gfx::Size(
165 wallpaper.width(), 149 wallpaper.width(),
166 gfx::ToFlooredInt(static_cast<double>(height()) / horizontal_ratio)); 150 gfx::ToFlooredInt(static_cast<double>(height()) / horizontal_ratio));
167 } 151 }
168 152
169 gfx::Rect wallpaper_cropped_rect(0, 0, wallpaper.width(), 153 gfx::Rect wallpaper_cropped_rect(0, 0, wallpaper.width(),
170 wallpaper.height()); 154 wallpaper.height());
171 wallpaper_cropped_rect.ClampToCenteredSize(cropped_size); 155 wallpaper_cropped_rect.ClampToCenteredSize(cropped_size);
172 canvas->DrawImageInt( 156 canvas->DrawImageInt(
173 wallpaper, wallpaper_cropped_rect.x(), wallpaper_cropped_rect.y(), 157 wallpaper, wallpaper_cropped_rect.x(), wallpaper_cropped_rect.y(),
174 wallpaper_cropped_rect.width(), wallpaper_cropped_rect.height(), 0, 0, 158 wallpaper_cropped_rect.width(), wallpaper_cropped_rect.height(), 0, 0,
175 width(), height(), true); 159 width(), height(), true);
176 } else if (wallpaper_layout == WALLPAPER_LAYOUT_TILE) { 160 } else if (layout == wallpaper::WALLPAPER_LAYOUT_TILE) {
177 canvas->TileImageInt(wallpaper, 0, 0, width(), height()); 161 canvas->TileImageInt(wallpaper, 0, 0, width(), height());
178 } else if (wallpaper_layout == WALLPAPER_LAYOUT_STRETCH) { 162 } else if (layout == wallpaper::WALLPAPER_LAYOUT_STRETCH) {
179 // This is generally not recommended as it may show artifacts. 163 // This is generally not recommended as it may show artifacts.
180 canvas->DrawImageInt(wallpaper, 0, 0, wallpaper.width(), wallpaper.height(), 164 canvas->DrawImageInt(wallpaper, 0, 0, wallpaper.width(), wallpaper.height(),
181 0, 0, width(), height(), true); 165 0, 0, width(), height(), true);
182 } else { 166 } else {
183 float image_scale = canvas->image_scale(); 167 float image_scale = canvas->image_scale();
184 gfx::Rect wallpaper_rect(0, 0, wallpaper.width() / image_scale, 168 gfx::Rect wallpaper_rect(0, 0, wallpaper.width() / image_scale,
185 wallpaper.height() / image_scale); 169 wallpaper.height() / image_scale);
186 // All other are simply centered, and not scaled (but may be clipped). 170 // All other are simply centered, and not scaled (but may be clipped).
187 canvas->DrawImageInt(wallpaper, 0, 0, wallpaper.width(), wallpaper.height(), 171 canvas->DrawImageInt(wallpaper, 0, 0, wallpaper.width(), wallpaper.height(),
188 (width() - wallpaper_rect.width()) / 2, 172 (width() - wallpaper_rect.width()) / 2,
189 (height() - wallpaper_rect.height()) / 2, 173 (height() - wallpaper_rect.height()) / 2,
190 wallpaper_rect.width(), wallpaper_rect.height(), true); 174 wallpaper_rect.width(), wallpaper_rect.height(), true);
191 } 175 }
192 } 176 }
193 177
194 bool DesktopBackgroundView::OnMousePressed(const ui::MouseEvent& event) { 178 bool DesktopBackgroundView::OnMousePressed(const ui::MouseEvent& event) {
195 return true; 179 return true;
196 } 180 }
197 181
198 void DesktopBackgroundView::ShowContextMenuForView( 182 void DesktopBackgroundView::ShowContextMenuForView(
199 views::View* source, 183 views::View* source,
200 const gfx::Point& point, 184 const gfx::Point& point,
201 ui::MenuSourceType source_type) { 185 ui::MenuSourceType source_type) {
202 Shell::GetInstance()->ShowContextMenu(point, source_type); 186 WmShell::Get()->ShowContextMenu(point, source_type);
203 } 187 }
204 188
205 views::Widget* CreateDesktopBackground(aura::Window* root_window, 189 views::Widget* CreateDesktopBackground(WmWindow* root_window,
206 int container_id) { 190 int container_id) {
191 aura::Window* aura_root_window = WmWindowAura::GetAuraWindow(root_window);
207 DesktopBackgroundController* controller = 192 DesktopBackgroundController* controller =
208 Shell::GetInstance()->desktop_background_controller(); 193 Shell::GetInstance()->desktop_background_controller();
209 UserWallpaperDelegate* wallpaper_delegate = 194 UserWallpaperDelegate* wallpaper_delegate =
210 Shell::GetInstance()->user_wallpaper_delegate(); 195 Shell::GetInstance()->user_wallpaper_delegate();
211 196
212 views::Widget* desktop_widget = new views::Widget; 197 views::Widget* desktop_widget = new views::Widget;
213 views::Widget::InitParams params( 198 views::Widget::InitParams params(
214 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); 199 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
215 params.name = "DesktopBackgroundView"; 200 params.name = "DesktopBackgroundView";
216 if (controller->GetWallpaper().isNull()) 201 if (controller->GetWallpaper().isNull())
217 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; 202 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
218 params.parent = root_window->GetChildById(container_id); 203 params.parent = aura_root_window->GetChildById(container_id);
219 desktop_widget->Init(params); 204 desktop_widget->Init(params);
220 desktop_widget->SetContentsView( 205 desktop_widget->SetContentsView(
221 new LayerControlView(new DesktopBackgroundView())); 206 new LayerControlView(new DesktopBackgroundView()));
222 int animation_type = wallpaper_delegate->GetAnimationType(); 207 int animation_type = wallpaper_delegate->GetAnimationType();
223 ::wm::SetWindowVisibilityAnimationType(desktop_widget->GetNativeView(), 208 WmWindow* desktop_window =
224 animation_type); 209 WmLookup::Get()->GetWindowForWidget(desktop_widget);
210 desktop_window->SetVisibilityAnimationType(animation_type);
225 211
226 RootWindowController* root_window_controller = 212 RootWindowController* root_window_controller =
227 GetRootWindowController(root_window); 213 GetRootWindowController(aura_root_window);
228 214
229 // Enable wallpaper transition for the following cases: 215 // Enable wallpaper transition for the following cases:
230 // 1. Initial(OOBE) wallpaper animation. 216 // 1. Initial(OOBE) wallpaper animation.
231 // 2. Wallpaper fades in from a non empty background. 217 // 2. Wallpaper fades in from a non empty background.
232 // 3. From an empty background, chrome transit to a logged in user session. 218 // 3. From an empty background, chrome transit to a logged in user session.
233 // 4. From an empty background, guest user logged in. 219 // 4. From an empty background, guest user logged in.
234 if (wallpaper_delegate->ShouldShowInitialAnimation() || 220 if (wallpaper_delegate->ShouldShowInitialAnimation() ||
235 root_window_controller->animating_wallpaper_controller() || 221 root_window_controller->animating_wallpaper_controller() ||
236 WmShell::Get()->GetSessionStateDelegate()->NumberOfLoggedInUsers()) { 222 WmShell::Get()->GetSessionStateDelegate()->NumberOfLoggedInUsers()) {
237 ::wm::SetWindowVisibilityAnimationTransition( 223 desktop_window->SetVisibilityAnimationTransition(::wm::ANIMATE_SHOW);
238 desktop_widget->GetNativeView(), ::wm::ANIMATE_SHOW);
239 int duration_override = wallpaper_delegate->GetAnimationDurationOverride(); 224 int duration_override = wallpaper_delegate->GetAnimationDurationOverride();
240 if (duration_override) { 225 if (duration_override) {
241 ::wm::SetWindowVisibilityAnimationDuration( 226 desktop_window->SetVisibilityAnimationDuration(
242 desktop_widget->GetNativeView(),
243 base::TimeDelta::FromMilliseconds(duration_override)); 227 base::TimeDelta::FromMilliseconds(duration_override));
244 } 228 }
245 } else { 229 } else {
246 // Disable animation if transition to login screen from an empty background. 230 // Disable animation if transition to login screen from an empty background.
247 ::wm::SetWindowVisibilityAnimationTransition( 231 desktop_window->SetVisibilityAnimationTransition(::wm::ANIMATE_NONE);
248 desktop_widget->GetNativeView(), ::wm::ANIMATE_NONE);
249 } 232 }
250 233
251 desktop_widget->SetBounds(params.parent->bounds()); 234 desktop_widget->SetBounds(params.parent->bounds());
252 return desktop_widget; 235 return desktop_widget;
253 } 236 }
254 237
255 } // namespace ash 238 } // namespace ash
OLDNEW
« no previous file with comments | « ash/desktop_background/desktop_background_view.h ('k') | ash/desktop_background/desktop_background_widget_controller.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698