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_UI_VIEWS_TABS_TAB_H_
6 #define CHROME_BROWSER_UI_VIEWS_TABS_TAB_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "chrome/browser/ui/views/tabs/tab_renderer_data.h"
14 #include "ui/base/layout.h"
15 #include "ui/gfx/animation/animation_delegate.h"
16 #include "ui/gfx/geometry/point.h"
17 #include "ui/views/context_menu_controller.h"
18 #include "ui/views/controls/button/button.h"
19 #include "ui/views/controls/glow_hover_controller.h"
20 #include "ui/views/masked_targeter_delegate.h"
21 #include "ui/views/view.h"
23 class MediaIndicatorButton
;
28 class AnimationContainer
;
29 class LinearAnimation
;
38 ///////////////////////////////////////////////////////////////////////////////
40 // A View that renders a Tab in a TabStrip.
42 ///////////////////////////////////////////////////////////////////////////////
43 class Tab
: public gfx::AnimationDelegate
,
44 public views::ButtonListener
,
45 public views::ContextMenuController
,
46 public views::MaskedTargeterDelegate
,
49 // The Tab's class name.
50 static const char kViewClassName
[];
52 explicit Tab(TabController
* controller
);
55 TabController
* controller() const { return controller_
; }
57 // Used to set/check whether this Tab is being animated closed.
58 void set_closing(bool closing
) { closing_
= closing
; }
59 bool closing() const { return closing_
; }
61 // See description above field.
62 void set_dragging(bool dragging
) { dragging_
= dragging
; }
63 bool dragging() const { return dragging_
; }
65 // Used to mark the tab as having been detached. Once this has happened, the
66 // tab should be invisibly closed. This is irreversible.
67 void set_detached() { detached_
= true; }
68 bool detached() const { return detached_
; }
70 // Sets the container all animations run from.
71 void set_animation_container(gfx::AnimationContainer
* container
);
73 // Returns true if this tab is the active tab.
74 bool IsActive() const;
76 // Returns true if the tab is selected.
77 bool IsSelected() const;
79 // Sets the data this tabs displays. Invokes DataChanged.
80 void SetData(const TabRendererData
& data
);
81 const TabRendererData
& data() const { return data_
; }
83 // Sets the network state. If the network state changes NetworkStateChanged is
85 void UpdateLoadingAnimation(TabRendererData::NetworkState state
);
87 // Starts/Stops a pulse animation.
91 // Start/stop the mini-tab title animation.
92 void StartMiniTabTitleAnimation();
93 void StopMiniTabTitleAnimation();
95 // Set the background offset used to match the image in the inactive tab
96 // to the frame image.
97 void set_background_offset(const gfx::Point
& offset
) {
98 background_offset_
= offset
;
101 // Returns true if this tab became the active tab selected in
102 // response to the last ui::ET_TAP_DOWN gesture dispatched to
103 // this tab. Only used for collecting UMA metrics.
104 // See ash/touch/touch_uma.cc.
105 bool tab_activated_with_last_tap_down() const {
106 return tab_activated_with_last_tap_down_
;
109 views::GlowHoverController
* hover_controller() {
110 return &hover_controller_
;
113 // Returns the inset within the first dragged tab to use when calculating the
114 // "drag insertion point". If we simply used the x-coordinate of the tab,
115 // we'd be calculating based on a point well before where the user considers
116 // the tab to "be". The value here is chosen to "feel good" based on the
117 // widths of the tab images and the tab overlap.
119 // Note that this must return a value smaller than the midpoint of any tab's
120 // width, or else the user won't be able to drag a tab to the left of the
121 // first tab in the strip.
122 static int leading_width_for_drag() { return 16; }
124 // Returns the minimum possible size of a single unselected Tab.
125 static gfx::Size
GetMinimumUnselectedSize();
126 // Returns the minimum possible size of a selected Tab. Selected tabs must
127 // always show a close button and have a larger minimum size than unselected
129 static gfx::Size
GetMinimumSelectedSize();
130 // Returns the preferred size of a single Tab, assuming space is
132 static gfx::Size
GetStandardSize();
134 // Returns the width for touch tabs.
135 static int GetTouchWidth();
137 // Returns the width for mini-tabs. Mini-tabs always have this width.
138 static int GetMiniWidth();
140 // Returns the height for immersive mode tabs.
141 static int GetImmersiveHeight();
144 friend class TabTest
;
145 FRIEND_TEST_ALL_PREFIXES(TabTest
, CloseButtonLayout
);
147 friend class TabStripTest
;
148 FRIEND_TEST_ALL_PREFIXES(TabStripTest
, TabHitTestMaskWhenStacked
);
149 FRIEND_TEST_ALL_PREFIXES(TabStripTest
, TabCloseButtonVisibilityWhenStacked
);
151 // The animation object used to swap the favicon with the sad tab icon.
152 class FaviconCrashAnimation
;
153 class TabCloseButton
;
155 // Contains a cached image and the values used to generate it.
156 struct ImageCacheEntry
{
160 // ID of the resource used.
163 // Scale factor we're drawing it.
164 ui::ScaleFactor scale_factor
;
167 gfx::ImageSkia image
;
170 typedef std::list
<ImageCacheEntry
> ImageCache
;
172 // gfx::AnimationDelegate:
173 void AnimationProgressed(const gfx::Animation
* animation
) override
;
174 void AnimationCanceled(const gfx::Animation
* animation
) override
;
175 void AnimationEnded(const gfx::Animation
* animation
) override
;
177 // views::ButtonListener:
178 void ButtonPressed(views::Button
* sender
, const ui::Event
& event
) override
;
180 // views::ContextMenuController:
181 void ShowContextMenuForView(views::View
* source
,
182 const gfx::Point
& point
,
183 ui::MenuSourceType source_type
) override
;
185 // views::MaskedTargeterDelegate:
186 bool GetHitTestMask(gfx::Path
* mask
) const override
;
189 void OnPaint(gfx::Canvas
* canvas
) override
;
190 void Layout() override
;
191 void OnThemeChanged() override
;
192 const char* GetClassName() const override
;
193 bool GetTooltipText(const gfx::Point
& p
,
194 base::string16
* tooltip
) const override
;
195 bool GetTooltipTextOrigin(const gfx::Point
& p
,
196 gfx::Point
* origin
) const override
;
197 bool OnMousePressed(const ui::MouseEvent
& event
) override
;
198 bool OnMouseDragged(const ui::MouseEvent
& event
) override
;
199 void OnMouseReleased(const ui::MouseEvent
& event
) override
;
200 void OnMouseCaptureLost() override
;
201 void OnMouseEntered(const ui::MouseEvent
& event
) override
;
202 void OnMouseMoved(const ui::MouseEvent
& event
) override
;
203 void OnMouseExited(const ui::MouseEvent
& event
) override
;
204 void GetAccessibleState(ui::AXViewState
* state
) override
;
207 void OnGestureEvent(ui::GestureEvent
* event
) override
;
209 // Invoked from Layout to adjust the position of the favicon or media
210 // indicator for mini tabs.
211 void MaybeAdjustLeftForMiniTab(gfx::Rect
* bounds
) const;
213 // Invoked from SetData after |data_| has been updated to the new data.
214 void DataChanged(const TabRendererData
& old
);
216 // Paint with the normal tab style.
217 void PaintTab(gfx::Canvas
* canvas
);
219 // Paint with the "immersive mode" light-bar style.
220 void PaintImmersiveTab(gfx::Canvas
* canvas
);
222 // Paint various portions of the Tab
223 void PaintTabBackground(gfx::Canvas
* canvas
);
224 void PaintInactiveTabBackgroundWithTitleChange(gfx::Canvas
* canvas
);
225 void PaintInactiveTabBackground(gfx::Canvas
* canvas
);
226 void PaintInactiveTabBackgroundUsingResourceId(gfx::Canvas
* canvas
,
228 void PaintActiveTabBackground(gfx::Canvas
* canvas
);
230 // Paints the favicon, mirrored for RTL if needed.
231 void PaintIcon(gfx::Canvas
* canvas
);
233 // Invoked if data_.network_state changes, or the network_state is not none.
234 void AdvanceLoadingAnimation(TabRendererData::NetworkState old_state
,
235 TabRendererData::NetworkState state
);
237 // Returns the number of favicon-size elements that can fit in the tab's
239 int IconCapacity() const;
241 // Returns whether the Tab should display a favicon.
242 bool ShouldShowIcon() const;
244 // Returns whether the Tab should display the media indicator.
245 bool ShouldShowMediaIndicator() const;
247 // Returns whether the Tab should display a close button.
248 bool ShouldShowCloseBox() const;
250 // Gets the throb value for the tab. When a tab is not selected the
251 // active background is drawn at |GetThrobValue()|%. This is used for hover,
252 // mini tab title change and pulsing.
253 double GetThrobValue();
255 // Set the temporary offset for the favicon. This is used during the crash
257 void SetFaviconHidingOffset(int offset
);
259 void DisplayCrashedFavicon();
260 void ResetCrashedFavicon();
262 void StopCrashAnimation();
263 void StartCrashAnimation();
265 // Returns true if the crash animation is currently running.
266 bool IsPerformingCrashAnimation() const;
268 // Schedules repaint task for icon.
269 void ScheduleIconPaint();
271 // Returns the rectangle for the light bar in immersive mode.
272 gfx::Rect
GetImmersiveBarRect() const;
274 // Gets the tab id and frame id.
275 void GetTabIdAndFrameId(views::Widget
* widget
,
277 int* frame_id
) const;
279 // Returns |media_indicator_button_|, creating it on-demand.
280 MediaIndicatorButton
* GetMediaIndicatorButton();
282 // Performs a one-time initialization of static resources such as tab images.
283 static void InitTabResources();
285 // Returns the minimum possible size of a single unselected Tab, not
286 // including considering touch mode.
287 static gfx::Size
GetBasicMinimumUnselectedSize();
289 // Loads the images to be used for the tab background.
290 static void LoadTabImages();
292 // Returns the cached image for the specified arguments, or an empty image if
293 // there isn't one cached.
294 static gfx::ImageSkia
GetCachedImage(int resource_id
,
295 const gfx::Size
& size
,
296 ui::ScaleFactor scale_factor
);
298 // Caches the specified image.
299 static void SetCachedImage(int resource_id
,
300 ui::ScaleFactor scale_factor
,
301 const gfx::ImageSkia
& image
);
303 // The controller, never NULL.
304 TabController
* const controller_
;
306 TabRendererData data_
;
308 // True if the tab is being animated closed.
311 // True if the tab is being dragged.
314 // True if the tab has been detached.
317 // The offset used to animate the favicon location. This is used when the tab
319 int favicon_hiding_offset_
;
321 // The current index of the loading animation. The range varies depending on
322 // whether the tab is loading or waiting, see AdvanceLoadingAnimation().
323 int loading_animation_frame_
;
325 // Step in the immersive loading progress indicator.
326 int immersive_loading_step_
;
328 bool should_display_crashed_favicon_
;
330 // Whole-tab throbbing "pulse" animation.
331 scoped_ptr
<gfx::ThrobAnimation
> pulse_animation_
;
333 scoped_ptr
<gfx::MultiAnimation
> mini_title_change_animation_
;
335 // Crash icon animation (in place of favicon).
336 scoped_ptr
<gfx::LinearAnimation
> crash_icon_animation_
;
338 scoped_refptr
<gfx::AnimationContainer
> animation_container_
;
340 views::ImageButton
* close_button_
;
341 MediaIndicatorButton
* media_indicator_button_
; // NULL until first use.
342 views::Label
* title_
;
344 bool tab_activated_with_last_tap_down_
;
346 views::GlowHoverController hover_controller_
;
348 // The bounds of various sections of the display.
349 gfx::Rect favicon_bounds_
;
351 // The offset used to paint the inactive background image.
352 gfx::Point background_offset_
;
355 gfx::ImageSkia
* image_l
;
356 gfx::ImageSkia
* image_c
;
357 gfx::ImageSkia
* image_r
;
361 static TabImage tab_active_
;
362 static TabImage tab_inactive_
;
363 static TabImage tab_alpha_
;
365 // Whether we're showing the icon. It is cached so that we can detect when it
366 // changes and layout appropriately.
369 // Whether we're showing the media indicator. It is cached so that we can
370 // detect when it changes and layout appropriately.
371 bool showing_media_indicator_
;
373 // Whether we are showing the close button. It is cached so that we can
374 // detect when it changes and layout appropriately.
375 bool showing_close_button_
;
377 // The current color of the close button.
378 SkColor close_button_color_
;
380 // As the majority of the tabs are inactive, and painting tabs is slowish,
381 // we cache a handful of the inactive tab backgrounds here.
382 static ImageCache
* image_cache_
;
384 DISALLOW_COPY_AND_ASSIGN(Tab
);
387 #endif // CHROME_BROWSER_UI_VIEWS_TABS_TAB_H_