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 #ifndef ASH_DESKTOP_BACKGROUND_DESKTOP_BACKGROUND_CONTROLLER_H_
6 #define ASH_DESKTOP_BACKGROUND_DESKTOP_BACKGROUND_CONTROLLER_H_
8 #include "ash/ash_export.h"
9 #include "base/basictypes.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/observer_list.h"
13 #include "ui/aura/window.h"
14 #include "ui/aura/window_observer.h"
15 #include "ui/compositor/layer.h"
16 #include "ui/gfx/image/image_skia.h"
18 typedef unsigned int SkColor
;
26 class DesktopBackgroundControllerTest
;
27 } // namespace internal
29 enum WallpaperLayout
{
30 WALLPAPER_LAYOUT_CENTER
,
31 WALLPAPER_LAYOUT_CENTER_CROPPED
,
32 WALLPAPER_LAYOUT_STRETCH
,
33 WALLPAPER_LAYOUT_TILE
,
36 enum WallpaperResolution
{
37 WALLPAPER_RESOLUTION_LARGE
,
38 WALLPAPER_RESOLUTION_SMALL
41 const SkColor kLoginWallpaperColor
= 0xFEFEFE;
43 // Encapsulates wallpaper infomation needed by desktop background controller.
44 struct ASH_EXPORT WallpaperInfo
{
46 WallpaperLayout layout
;
49 ASH_EXPORT
extern const WallpaperInfo kDefaultLargeWallpaper
;
50 ASH_EXPORT
extern const WallpaperInfo kDefaultSmallWallpaper
;
51 ASH_EXPORT
extern const WallpaperInfo kGuestLargeWallpaper
;
52 ASH_EXPORT
extern const WallpaperInfo kGuestSmallWallpaper
;
54 // The width and height of small/large resolution wallpaper. When screen size is
55 // smaller than |kSmallWallpaperMaxWidth| and |kSmallWallpaperMaxHeight|, the
56 // small resolution wallpaper should be used. Otherwise, uses the large
57 // resolution wallpaper.
58 ASH_EXPORT
extern const int kSmallWallpaperMaxWidth
;
59 ASH_EXPORT
extern const int kSmallWallpaperMaxHeight
;
60 ASH_EXPORT
extern const int kLargeWallpaperMaxWidth
;
61 ASH_EXPORT
extern const int kLargeWallpaperMaxHeight
;
63 // The width and heigh of wallpaper thumbnails.
64 ASH_EXPORT
extern const int kWallpaperThumbnailWidth
;
65 ASH_EXPORT
extern const int kWallpaperThumbnailHeight
;
67 class DesktopBackgroundControllerObserver
;
68 class WallpaperResizer
;
70 // Loads selected desktop wallpaper from file system asynchronously and updates
71 // background layer if loaded successfully.
72 class ASH_EXPORT DesktopBackgroundController
: public aura::WindowObserver
{
77 BACKGROUND_SOLID_COLOR
80 DesktopBackgroundController();
81 virtual ~DesktopBackgroundController();
83 // Gets the desktop background mode.
84 BackgroundMode
desktop_background_mode() const {
85 return desktop_background_mode_
;
88 // Add/Remove observers.
89 void AddObserver(DesktopBackgroundControllerObserver
* observer
);
90 void RemoveObserver(DesktopBackgroundControllerObserver
* observer
);
92 gfx::ImageSkia
GetWallpaper() const;
94 WallpaperLayout
GetWallpaperLayout() const;
96 // Provides current image on the background, or empty gfx::ImageSkia if there
97 // is no image, e.g. background is solid color.
98 gfx::ImageSkia
GetCurrentWallpaperImage();
100 // Gets the IDR of current wallpaper. Returns -1 if current wallpaper is not
101 // a builtin wallpaper.
102 int GetWallpaperIDR() const;
104 // Initialize root window's background.
105 void OnRootWindowAdded(aura::RootWindow
* root_window
);
107 // Loads builtin wallpaper asynchronously and sets to current wallpaper after
109 void SetDefaultWallpaper(const WallpaperInfo
& info
);
111 // Sets the user selected custom wallpaper. Called when user selected a file
112 // from file system or changed the layout of wallpaper.
113 void SetCustomWallpaper(const gfx::ImageSkia
& wallpaper
,
114 WallpaperLayout layout
);
116 // Cancels the current wallpaper loading operation.
117 void CancelPendingWallpaperOperation();
119 // Sets the desktop background to solid color mode and creates a solid
121 void SetDesktopBackgroundSolidColorMode(SkColor color
);
123 // Creates an empty wallpaper. Some tests require a wallpaper widget is ready
124 // when running. However, the wallpaper widgets are now created asynchronously
125 // . If loading a real wallpaper, there are cases that these tests crash
126 // because the required widget is not ready. This function synchronously
127 // creates an empty widget for those tests to prevent crashes. An example test
128 // is SystemGestureEventFilterTest.ThreeFingerSwipe.
129 void CreateEmptyWallpaper();
131 // Returns the appropriate wallpaper resolution for all root windows.
132 WallpaperResolution
GetAppropriateResolution();
134 // Move all desktop widgets to locked container.
135 // Returns true if the desktop moved.
136 bool MoveDesktopToLockedContainer();
138 // Move all desktop widgets to unlocked container.
139 // Returns true if the desktop moved.
140 bool MoveDesktopToUnlockedContainer();
142 // WindowObserver implementation.
143 virtual void OnWindowDestroying(aura::Window
* window
) OVERRIDE
;
146 friend class internal::DesktopBackgroundControllerTest
;
148 // An operation to asynchronously loads wallpaper.
149 class WallpaperLoader
;
151 // Creates view for all root windows, or notifies them to repaint if they
153 void SetDesktopBackgroundImageMode();
155 // Creates a new background widget and sets the background mode to image mode.
156 // Called after wallpaper loaded successfully.
157 void OnWallpaperLoadCompleted(scoped_refptr
<WallpaperLoader
> wl
);
159 // Adds layer with solid |color| to container |container_id| in |root_window|.
160 ui::Layer
* SetColorLayerForContainer(SkColor color
,
161 aura::RootWindow
* root_window
,
164 // Creates and adds component for current mode (either Widget or Layer) to
166 void InstallDesktopController(aura::RootWindow
* root_window
);
168 // Creates and adds component for current mode (either Widget or Layer) to
170 void InstallDesktopControllerForAllWindows();
172 // Moves all desktop components from one container to other across all root
173 // windows. Returns true if a desktop moved.
174 bool ReparentBackgroundWidgets(int src_container
, int dst_container
);
176 // Returns id for background container for unlocked and locked states.
177 int GetBackgroundContainerId(bool locked
);
179 // Send notification that background animation finished.
180 void NotifyAnimationFinished();
182 // Can change at runtime.
185 BackgroundMode desktop_background_mode_
;
187 SkColor background_color_
;
189 ObserverList
<DesktopBackgroundControllerObserver
> observers_
;
191 // The current wallpaper.
192 scoped_ptr
<WallpaperResizer
> current_wallpaper_
;
194 scoped_refptr
<WallpaperLoader
> wallpaper_loader_
;
196 base::WeakPtrFactory
<DesktopBackgroundController
> weak_ptr_factory_
;
198 DISALLOW_COPY_AND_ASSIGN(DesktopBackgroundController
);
203 #endif // ASH_DESKTOP_BACKGROUND_DESKTOP_BACKGROUND_CONTROLLER_H_