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 CHROME_BROWSER_CHROMEOS_STATUS_NETWORK_MENU_ICON_H_
6 #define CHROME_BROWSER_CHROMEOS_STATUS_NETWORK_MENU_ICON_H_
8 // NetworkMenuIcon Manages an icon that reflects the current state of the
9 // network (see chromeos::NetworkLibrary). It takes an optional Delegate
10 // argument in the constructor that signals the delegate when the icon changes.
12 // class MyIconDelegate : public NetworkMenuIcon::Delegate {
13 // virtual void NetworkMenuIconChanged() OVERRIDE {
15 // const ImageSkia* image = network_icon_->GetIconAndText(&tooltip);
17 // SetTooltip(tooltip);
21 // MyIconDelegate my_delegate;
22 // NetworkMenuIcon icon(&my_delegate, NetworkMenuIcon::MENU_MODE);
24 // NetworkMenuIcon also provides static functions for fetching network images
25 // (e.g. for network entries in the menu or settings).
27 // Network* network = network_library->FindNetworkByPath(my_network_path_);
28 // SetIcon(NetworkMenuIcon::GetBitmap(network);
30 // This class is not explicitly thread-safe and functions are expected to be
31 // called from the UI thread.
36 #include "base/memory/scoped_ptr.h"
37 #include "chrome/browser/chromeos/cros/network_library.h"
38 #include "ui/base/animation/animation_delegate.h"
39 #include "ui/base/animation/throb_animation.h"
40 #include "ui/gfx/image/image_skia.h"
46 class NetworkMenuIcon
: public ui::AnimationDelegate
{
49 MENU_MODE
, // Prioritizes connecting networks and sets tooltips.
50 DROPDOWN_MODE
, // Prioritizes connected networks and sets display text.
53 enum ResourceColorTheme
{
58 // Used for calls to GetBitmap() and GetNumBitmaps() below.
67 virtual ~Delegate() {}
68 // Called when the image has changed due to animation. The callback should
69 // trigger a call to GetIconAndText() to generate and retrieve the image.
70 virtual void NetworkMenuIconChanged() = 0;
73 DISALLOW_COPY_AND_ASSIGN(Delegate
);
76 // NetworkMenuIcon is owned by the caller. |delegate| can be NULL.
77 // |mode| determines the menu behavior (see enum).
78 NetworkMenuIcon(Delegate
* delegate
, Mode mode
);
79 virtual ~NetworkMenuIcon();
81 // Sets the resource color theme (e.g. light or dark icons).
82 void SetResourceColorTheme(ResourceColorTheme color
);
84 // Returns true if the icon should be visible in a system tray.
85 bool ShouldShowIconInTray();
87 // Generates and returns the icon image. If |text| is not NULL, sets it to
88 // the tooltip or display text to show, based on the value of mode_.
89 const gfx::ImageSkia
GetIconAndText(string16
* text
);
90 // Generates and returns the icon image for vpn network connection.
91 const gfx::ImageSkia
GetVpnIconAndText(string16
* text
);
94 // ui::AnimationDelegate implementation.
95 virtual void AnimationProgressed(const ui::Animation
* animation
) OVERRIDE
;
97 // Static functions for generating network icon images:
99 // Composites the images to generate a network icon. Input parameters are
100 // the icon and badges that are composited to generate |result|. Public
101 // primarily for unit tests.
102 static const gfx::ImageSkia
GenerateImageFromComponents(
103 const gfx::ImageSkia
& icon
,
104 const gfx::ImageSkia
* top_left_badge
,
105 const gfx::ImageSkia
* top_right_badge
,
106 const gfx::ImageSkia
* bottom_left_badge
,
107 const gfx::ImageSkia
* bottom_right_badge
);
109 // Returns a modified version of |source| representing the connecting state
110 // of a network. Public for unit tests.
111 static const gfx::ImageSkia
GenerateConnectingImage(
112 const gfx::ImageSkia
& source
);
114 // Returns an image associated with |network|, reflecting its current state.
115 static const gfx::ImageSkia
GetImage(const Network
* network
,
116 ResourceColorTheme color
);
118 // Access a specific image of the specified color theme. If index is out of
119 // range, an empty image will be returned.
120 static const gfx::ImageSkia
GetImage(ImageType type
,
122 ResourceColorTheme color
);
124 // Gets the disconnected image for given type.
125 static const gfx::ImageSkia
GetDisconnectedImage(ImageType type
,
126 ResourceColorTheme color
);
128 // Gets the connected image for given type.
129 static const gfx::ImageSkia
GetConnectedImage(ImageType type
,
130 ResourceColorTheme color
);
132 // Gets a network image for VPN.
133 static gfx::ImageSkia
* GetVirtualNetworkImage();
135 // Returns total number of images for given type.
136 static int NumImages(ImageType type
);
139 // Starts the connection animation if necessary and returns its current value.
140 // Virtual so that unit tests can override this.
141 virtual double GetAnimation();
144 // Returns the appropriate connecting network if any.
145 const Network
* GetConnectingNetwork();
146 // Sets the icon based on the state of the network and the network library.
147 // Sets text_ to the appropriate tooltip or display text.
148 void SetIconAndText();
149 // Sets the icon and text for VPN connection.
150 void SetVpnIconAndText();
151 // Set the icon and text to show a warning if unable to load the cros library.
152 void SetWarningIconAndText();
153 // Sets the icon and text when displaying a connecting state.
154 void SetConnectingIconAndText();
155 // Sets the icon and text when connected to |network|.
156 void SetActiveNetworkIconAndText(const Network
* network
);
157 // Sets the icon and text when disconnected.
158 void SetDisconnectedIconAndText();
160 // Specifies whether this icon is for a normal menu or a dropdown menu.
162 // A delegate may be specified to receive notifications when this animates.
164 // Generated image for connecting to a VPN.
165 gfx::ImageSkia vpn_connecting_badge_
;
166 ResourceColorTheme resource_color_theme_
;
167 // Animation throbber for animating the icon while conencting.
168 ui::ThrobAnimation animation_connecting_
;
169 // The generated icon image.
170 scoped_ptr
<NetworkIcon
> icon_
;
171 // A weak pointer to the currently connecting network. Used only for
172 // comparison purposes; accessing this directly may be invalid.
173 const Network
* connecting_network_
;
174 // The tooltip or display text associated with the menu icon.
177 DISALLOW_COPY_AND_ASSIGN(NetworkMenuIcon
);
180 } // namespace chromeos
182 #endif // CHROME_BROWSER_CHROMEOS_STATUS_NETWORK_MENU_ICON_H_