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

Side by Side Diff: chrome/browser/ui/gtk/tabs/tab_renderer_gtk.h

Issue 8392011: GTK: Step 1 of tab strip refresh. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Redo as functions. Created 9 years, 1 month 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_UI_GTK_TABS_TAB_RENDERER_GTK_H_ 5 #ifndef CHROME_BROWSER_UI_GTK_TABS_TAB_RENDERER_GTK_H_
6 #define CHROME_BROWSER_UI_GTK_TABS_TAB_RENDERER_GTK_H_ 6 #define CHROME_BROWSER_UI_GTK_TABS_TAB_RENDERER_GTK_H_
7 #pragma once 7 #pragma once
8 8
9 #include <gtk/gtk.h> 9 #include <gtk/gtk.h>
10 #include <map> 10 #include <map>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/string16.h" 14 #include "base/string16.h"
15 #include "chrome/browser/ui/gtk/cairo_cached_surface.h"
15 #include "content/public/browser/notification_observer.h" 16 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h" 17 #include "content/public/browser/notification_registrar.h"
17 #include "third_party/skia/include/core/SkBitmap.h" 18 #include "third_party/skia/include/core/SkBitmap.h"
18 #include "ui/base/animation/animation_delegate.h" 19 #include "ui/base/animation/animation_delegate.h"
19 #include "ui/base/gtk/gtk_signal.h" 20 #include "ui/base/gtk/gtk_signal.h"
20 #include "ui/base/gtk/owned_widget_gtk.h" 21 #include "ui/base/gtk/owned_widget_gtk.h"
21 #include "ui/gfx/canvas.h" 22 #include "ui/gfx/canvas.h"
22 #include "ui/gfx/font.h" 23 #include "ui/gfx/font.h"
23 #include "ui/gfx/rect.h" 24 #include "ui/gfx/rect.h"
24 25
25 namespace gfx { 26 namespace gfx {
26 class Size; 27 class Size;
27 } // namespace gfx 28 } // namespace gfx
28 29
30 class CairoCachedSurface;
29 class CustomDrawButton; 31 class CustomDrawButton;
30 class TabContents; 32 class TabContents;
31 class ThemeService; 33 class GtkThemeService;
32 34
33 namespace ui { 35 namespace ui {
34 class SlideAnimation; 36 class SlideAnimation;
35 class ThrobAnimation; 37 class ThrobAnimation;
36 } 38 }
37 39
38 class TabRendererGtk : public ui::AnimationDelegate, 40 class TabRendererGtk : public ui::AnimationDelegate {
39 public content::NotificationObserver {
40 public: 41 public:
41 // Possible animation states. 42 // Possible animation states.
42 enum AnimationState { 43 enum AnimationState {
43 ANIMATION_NONE, 44 ANIMATION_NONE,
44 ANIMATION_WAITING, 45 ANIMATION_WAITING,
45 ANIMATION_LOADING 46 ANIMATION_LOADING
46 }; 47 };
47 48
48 class LoadingAnimation : public content::NotificationObserver { 49 class LoadingAnimation : public content::NotificationObserver {
49 public: 50 public:
50 struct Data { 51 struct Data {
51 explicit Data(ThemeService* theme_service); 52 explicit Data(GtkThemeService* theme_service);
52 Data(int loading, int waiting, int waiting_to_loading); 53 Data(int loading, int waiting, int waiting_to_loading);
53 54
54 SkBitmap* waiting_animation_frames;
55 SkBitmap* loading_animation_frames;
56 int loading_animation_frame_count; 55 int loading_animation_frame_count;
57 int waiting_animation_frame_count; 56 int waiting_animation_frame_count;
58 int waiting_to_loading_frame_count_ratio; 57 int waiting_to_loading_frame_count_ratio;
59 }; 58 };
60 59
61 explicit LoadingAnimation(ThemeService* theme_service); 60 explicit LoadingAnimation(GtkThemeService* theme_service);
62 61
63 // Used in unit tests to inject specific data. 62 // Used in unit tests to inject specific data.
64 explicit LoadingAnimation(const LoadingAnimation::Data& data); 63 explicit LoadingAnimation(const LoadingAnimation::Data& data);
65 64
66 virtual ~LoadingAnimation(); 65 virtual ~LoadingAnimation();
67 66
68 // Advance the loading animation to the next frame, or hide the animation if 67 // Advance the loading animation to the next frame, or hide the animation if
69 // the tab isn't loading. Returns |true| if the icon area needs to be 68 // the tab isn't loading. Returns |true| if the icon area needs to be
70 // repainted. 69 // repainted.
71 bool ValidateLoadingAnimation(AnimationState animation_state); 70 bool ValidateLoadingAnimation(AnimationState animation_state);
72 71
73 AnimationState animation_state() const { return animation_state_; } 72 AnimationState animation_state() const { return animation_state_; }
74 int animation_frame() const { return animation_frame_; } 73 int animation_frame() const { return animation_frame_; }
75 74
76 const SkBitmap* waiting_animation_frames() const {
77 return data_->waiting_animation_frames;
78 }
79 const SkBitmap* loading_animation_frames() const {
80 return data_->loading_animation_frames;
81 }
82
83 // Provide content::NotificationObserver implementation. 75 // Provide content::NotificationObserver implementation.
84 virtual void Observe(int type, 76 virtual void Observe(int type,
85 const content::NotificationSource& source, 77 const content::NotificationSource& source,
86 const content::NotificationDetails& details); 78 const content::NotificationDetails& details);
87 79
88 private: 80 private:
89 scoped_ptr<Data> data_; 81 scoped_ptr<Data> data_;
90 82
91 // Used to listen for theme change notifications. 83 // Used to listen for theme change notifications.
92 content::NotificationRegistrar registrar_; 84 content::NotificationRegistrar registrar_;
93 85
94 // Gives us our throbber images. 86 // Gives us our throbber images.
95 ThemeService* theme_service_; 87 GtkThemeService* theme_service_;
96 88
97 // Current state of the animation. 89 // Current state of the animation.
98 AnimationState animation_state_; 90 AnimationState animation_state_;
99 91
100 // The current index into the Animation image strip. 92 // The current index into the Animation image strip.
101 int animation_frame_; 93 int animation_frame_;
102 94
103 DISALLOW_COPY_AND_ASSIGN(LoadingAnimation); 95 DISALLOW_COPY_AND_ASSIGN(LoadingAnimation);
104 }; 96 };
105 97
106 explicit TabRendererGtk(ThemeService* theme_service); 98 explicit TabRendererGtk(GtkThemeService* theme_service);
107 virtual ~TabRendererGtk(); 99 virtual ~TabRendererGtk();
108 100
109 // TabContents. If only the loading state was updated, the loading_only flag 101 // TabContents. If only the loading state was updated, the loading_only flag
110 // should be specified. If other things change, set this flag to false to 102 // should be specified. If other things change, set this flag to false to
111 // update everything. 103 // update everything.
112 virtual void UpdateData(TabContents* contents, bool app, bool loading_only); 104 virtual void UpdateData(TabContents* contents, bool app, bool loading_only);
113 105
114 // Sets the blocked state of the tab. 106 // Sets the blocked state of the tab.
115 void SetBlocked(bool pinned); 107 void SetBlocked(bool pinned);
116 bool is_blocked() const; 108 bool is_blocked() const;
(...skipping 22 matching lines...) Expand all
139 131
140 // Returns true if the Tab is selected, false otherwise. 132 // Returns true if the Tab is selected, false otherwise.
141 virtual bool IsSelected() const; 133 virtual bool IsSelected() const;
142 134
143 // Returns true if the Tab is visible, false otherwise. 135 // Returns true if the Tab is visible, false otherwise.
144 virtual bool IsVisible() const; 136 virtual bool IsVisible() const;
145 137
146 // Sets the visibility of the Tab. 138 // Sets the visibility of the Tab.
147 virtual void SetVisible(bool visible) const; 139 virtual void SetVisible(bool visible) const;
148 140
149 // Paints the tab into |canvas|. 141 // Paints the tab using resources from the display that |widget| is on,
150 virtual void Paint(gfx::Canvas* canvas); 142 // drawing into |cr|.
143 void Paint(GtkWidget* widget, cairo_t* cr);
151 144
152 // Paints the tab, and keeps the result server-side. The returned surface must 145 // Paints the tab, and keeps the result server-side. The returned surface must
153 // be freed with cairo_surface_destroy(). 146 // be freed with cairo_surface_destroy().
154 virtual cairo_surface_t* PaintToSurface(); 147 cairo_surface_t* PaintToSurface(GtkWidget* widget, cairo_t* cr);
155 148
156 // There is no PaintNow available, so the fastest we can do is schedule a 149 // There is no PaintNow available, so the fastest we can do is schedule a
157 // paint with the windowing system. 150 // paint with the windowing system.
158 virtual void SchedulePaint(); 151 void SchedulePaint();
159 152
160 // Notifies the Tab that the close button has been clicked. 153 // Notifies the Tab that the close button has been clicked.
161 virtual void CloseButtonClicked(); 154 virtual void CloseButtonClicked();
162 155
163 // Sets the bounds of the tab. 156 // Sets the bounds of the tab.
164 virtual void SetBounds(const gfx::Rect& bounds); 157 virtual void SetBounds(const gfx::Rect& bounds);
165 158
166 // Provide NotificationObserver implementation.
167 virtual void Observe(int type,
168 const content::NotificationSource& source,
169 const content::NotificationDetails& details);
170
171 // Advance the loading animation to the next frame, or hide the animation if 159 // Advance the loading animation to the next frame, or hide the animation if
172 // the tab isn't loading. Returns |true| if the icon area needs to be 160 // the tab isn't loading. Returns |true| if the icon area needs to be
173 // repainted. 161 // repainted.
174 bool ValidateLoadingAnimation(AnimationState animation_state); 162 bool ValidateLoadingAnimation(AnimationState animation_state);
175 163
176 // Repaint only the area of the tab that contains the favicon. 164 // Repaint only the area of the tab that contains the favicon.
177 void PaintFaviconArea(GdkEventExpose* event); 165 void PaintFaviconArea(GtkWidget* widget, cairo_t* cr);
178 166
179 // Returns whether the Tab should display a favicon. 167 // Returns whether the Tab should display a favicon.
180 bool ShouldShowIcon() const; 168 bool ShouldShowIcon() const;
181 169
182 // Returns the minimum possible size of a single unselected Tab. 170 // Returns the minimum possible size of a single unselected Tab.
183 static gfx::Size GetMinimumUnselectedSize(); 171 static gfx::Size GetMinimumUnselectedSize();
184 // Returns the minimum possible size of a selected Tab. Selected tabs must 172 // Returns the minimum possible size of a selected Tab. Selected tabs must
185 // always show a close button and have a larger minimum size than unselected 173 // always show a close button and have a larger minimum size than unselected
186 // tabs. 174 // tabs.
187 static gfx::Size GetMinimumSelectedSize(); 175 static gfx::Size GetMinimumSelectedSize();
188 // Returns the preferred size of a single Tab, assuming space is 176 // Returns the preferred size of a single Tab, assuming space is
189 // available. 177 // available.
190 static gfx::Size GetStandardSize(); 178 static gfx::Size GetStandardSize();
191 179
192 // Returns the width for mini-tabs. Mini-tabs always have this width. 180 // Returns the width for mini-tabs. Mini-tabs always have this width.
193 static int GetMiniWidth(); 181 static int GetMiniWidth();
194 182
195 // Loads the images to be used for the tab background.
196 static void LoadTabImages();
197
198 // Sets the colors used for painting text on the tabs. 183 // Sets the colors used for painting text on the tabs.
199 static void SetSelectedTitleColor(SkColor color); 184 static void SetSelectedTitleColor(SkColor color);
200 static void SetUnselectedTitleColor(SkColor color); 185 static void SetUnselectedTitleColor(SkColor color);
201 186
202 static gfx::Font* title_font() { return title_font_; } 187 static gfx::Font* title_font() { return title_font_; }
203 188
204 // Returns the bounds of the Tab. 189 // Returns the bounds of the Tab.
205 int x() const { return bounds_.x(); } 190 int x() const { return bounds_.x(); }
206 int y() const { return bounds_.y(); } 191 int y() const { return bounds_.y(); }
207 int width() const { return bounds_.width(); } 192 int width() const { return bounds_.width(); }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 CHROMEGTK_CALLBACK_1(TabRendererGtk, gboolean, OnEnterNotifyEvent, 224 CHROMEGTK_CALLBACK_1(TabRendererGtk, gboolean, OnEnterNotifyEvent,
240 GdkEventCrossing*); 225 GdkEventCrossing*);
241 226
242 // leave-notify-event handler that signals when the mouse enters the tab. 227 // leave-notify-event handler that signals when the mouse enters the tab.
243 CHROMEGTK_CALLBACK_1(TabRendererGtk, gboolean, OnLeaveNotifyEvent, 228 CHROMEGTK_CALLBACK_1(TabRendererGtk, gboolean, OnLeaveNotifyEvent,
244 GdkEventCrossing*); 229 GdkEventCrossing*);
245 230
246 private: 231 private:
247 class FaviconCrashAnimation; 232 class FaviconCrashAnimation;
248 233
249 // The data structure used to hold cached bitmaps. We need to manually free
250 // the bitmap in CachedBitmap when we remove it from |cached_bitmaps_|. We
251 // handle this when we replace images in the map and in the destructor.
252 struct CachedBitmap {
253 int bg_offset_x;
254 int bg_offset_y;
255 SkBitmap* bitmap;
256 };
257 typedef std::map<std::pair<const SkBitmap*, const SkBitmap*>, CachedBitmap>
258 BitmapCache;
259
260 // Model data. We store this here so that we don't need to ask the underlying 234 // Model data. We store this here so that we don't need to ask the underlying
261 // model, which is tricky since instances of this object can outlive the 235 // model, which is tricky since instances of this object can outlive the
262 // corresponding objects in the underlying model. 236 // corresponding objects in the underlying model.
263 struct TabData { 237 struct TabData {
264 TabData() 238 TabData();
265 : is_default_favicon(false), 239 ~TabData();
266 loading(false),
267 crashed(false),
268 incognito(false),
269 show_icon(true),
270 mini(false),
271 blocked(false),
272 animating_mini_change(false),
273 app(false) {
274 }
275 240
276 SkBitmap favicon; 241 SkBitmap favicon;
242 CairoCachedSurface cairo_favicon;
277 bool is_default_favicon; 243 bool is_default_favicon;
278 string16 title; 244 string16 title;
279 bool loading; 245 bool loading;
280 bool crashed; 246 bool crashed;
281 bool incognito; 247 bool incognito;
282 bool show_icon; 248 bool show_icon;
283 bool mini; 249 bool mini;
284 bool blocked; 250 bool blocked;
285 bool animating_mini_change; 251 bool animating_mini_change;
286 bool app; 252 bool app;
287 }; 253 };
288 254
289 // TODO(jhawkins): Move into TabResources class.
290 struct TabImage {
291 SkBitmap* image_l;
292 SkBitmap* image_c;
293 SkBitmap* image_r;
294 int l_width;
295 int r_width;
296 int y_offset;
297 };
298
299 // Overridden from ui::AnimationDelegate: 255 // Overridden from ui::AnimationDelegate:
300 virtual void AnimationProgressed(const ui::Animation* animation); 256 virtual void AnimationProgressed(const ui::Animation* animation);
301 virtual void AnimationCanceled(const ui::Animation* animation); 257 virtual void AnimationCanceled(const ui::Animation* animation);
302 virtual void AnimationEnded(const ui::Animation* animation); 258 virtual void AnimationEnded(const ui::Animation* animation);
303 259
304 // Starts/Stops the crash animation. 260 // Starts/Stops the crash animation.
305 void StartCrashAnimation(); 261 void StartCrashAnimation();
306 void StopCrashAnimation(); 262 void StopCrashAnimation();
307 263
308 // Return true if the crash animation is currently running. 264 // Return true if the crash animation is currently running.
(...skipping 11 matching lines...) Expand all
320 // Returns the local bounds of the tab. This returns the rect 276 // Returns the local bounds of the tab. This returns the rect
321 // {0, 0, width(), height()} for now, as we don't yet support borders. 277 // {0, 0, width(), height()} for now, as we don't yet support borders.
322 gfx::Rect GetLocalBounds(); 278 gfx::Rect GetLocalBounds();
323 279
324 // Moves the close button widget within the GtkFixed container. 280 // Moves the close button widget within the GtkFixed container.
325 void MoveCloseButtonWidget(); 281 void MoveCloseButtonWidget();
326 282
327 // Returns the largest of the favicon, title text, and the close button. 283 // Returns the largest of the favicon, title text, and the close button.
328 static int GetContentHeight(); 284 static int GetContentHeight();
329 285
330 // A helper method for generating the masked bitmaps used to draw the curved 286 void PaintTab(GtkWidget* widget, GdkEventExpose* event);
331 // edges of tabs. We cache the generated bitmaps because they can take a
332 // long time to compute.
333 SkBitmap* GetMaskedBitmap(const SkBitmap* mask,
334 const SkBitmap* background,
335 int bg_offset_x,
336 int bg_offset_y);
337 BitmapCache cached_bitmaps_;
338
339 // Paints the tab, minus the close button.
340 void PaintTab(GdkEventExpose* event);
341 287
342 // Paint various portions of the Tab 288 // Paint various portions of the Tab
343 void PaintTitle(gfx::Canvas* canvas); 289 void PaintTitle(GtkWidget* widget, cairo_t* cr);
344 void PaintIcon(gfx::Canvas* canvas); 290 void PaintIcon(GtkWidget* widget, cairo_t* cr);
345 void PaintTabBackground(gfx::Canvas* canvas); 291 void PaintTabBackground(GtkWidget* widget, cairo_t* cr);
346 void PaintInactiveTabBackground(gfx::Canvas* canvas); 292 void PaintInactiveTabBackground(GtkWidget* widget, cairo_t* cr);
347 void PaintActiveTabBackground(gfx::Canvas* canvas); 293 void PaintActiveTabBackground(GtkWidget* widget, cairo_t* cr);
348 void PaintLoadingAnimation(gfx::Canvas* canvas); 294 void PaintLoadingAnimation(GtkWidget* widget, cairo_t* cairo);
295
296 // Draws the given |tab_bg| onto |cr| using the tab shape masks along the
297 // sides for the rounded tab shape.
298 void DrawTabBackground(cairo_t* cr,
299 GtkWidget* widget,
300 CairoCachedSurface* tab_bg,
301 int offset_x,
302 int offset_y);
303
304 // Draws the tab shadow using the given idr resources onto |cr|.
305 void DrawTabShadow(cairo_t* cr,
306 GtkWidget* widget,
307 int left_idr,
308 int center_idr,
309 int right_idr);
349 310
350 // Returns the number of favicon-size elements that can fit in the tab's 311 // Returns the number of favicon-size elements that can fit in the tab's
351 // current size. 312 // current size.
352 int IconCapacity() const; 313 int IconCapacity() const;
353 314
354
355 // Returns whether the Tab should display a close button. 315 // Returns whether the Tab should display a close button.
356 bool ShouldShowCloseBox() const; 316 bool ShouldShowCloseBox() const;
357 317
358 CustomDrawButton* MakeCloseButton(); 318 CustomDrawButton* MakeCloseButton();
359 319
360 // Gets the throb value for the tab. When a tab is not selected the 320 // Gets the throb value for the tab. When a tab is not selected the
361 // active background is drawn at |GetThrobValue()|%. This is used for hover 321 // active background is drawn at |GetThrobValue()|%. This is used for hover
362 // and mini-tab title change effects. 322 // and mini-tab title change effects.
363 double GetThrobValue(); 323 double GetThrobValue();
364 324
(...skipping 15 matching lines...) Expand all
380 static void InitResources(); 340 static void InitResources();
381 static bool initialized_; 341 static bool initialized_;
382 342
383 // The bounds of various sections of the display. 343 // The bounds of various sections of the display.
384 gfx::Rect favicon_bounds_; 344 gfx::Rect favicon_bounds_;
385 gfx::Rect title_bounds_; 345 gfx::Rect title_bounds_;
386 gfx::Rect close_button_bounds_; 346 gfx::Rect close_button_bounds_;
387 347
388 TabData data_; 348 TabData data_;
389 349
390 static TabImage tab_active_; 350 static int tab_active_l_width_;
Evan Stade 2011/10/26 21:46:05 I'm not sure if you saw my previous comment about
Elliot Glaysher 2011/10/26 23:23:52 gfx::Size would make a static initializer, no?
391 static TabImage tab_inactive_; 351 static int tab_active_l_height_;
392 static TabImage tab_alpha_; 352 static int tab_inactive_l_width_;
353 static int tab_inactive_l_height_;
393 354
394 static gfx::Font* title_font_; 355 static gfx::Font* title_font_;
395 static int title_font_height_; 356 static int title_font_height_;
396 357
397 static int close_button_width_; 358 static int close_button_width_;
398 static int close_button_height_; 359 static int close_button_height_;
399 360
400 static SkColor selected_title_color_; 361 static SkColor selected_title_color_;
401 static SkColor unselected_title_color_; 362 static SkColor unselected_title_color_;
402 363
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 LoadingAnimation loading_animation_; 398 LoadingAnimation loading_animation_;
438 399
439 // The offset used to paint the tab theme images. 400 // The offset used to paint the tab theme images.
440 int background_offset_x_; 401 int background_offset_x_;
441 402
442 // The vertical offset used to paint the tab theme images. Controlled by the 403 // The vertical offset used to paint the tab theme images. Controlled by the
443 // tabstrip and plumbed here to offset the theme image by the size of the 404 // tabstrip and plumbed here to offset the theme image by the size of the
444 // alignment in the BrowserTitlebar. 405 // alignment in the BrowserTitlebar.
445 int background_offset_y_; 406 int background_offset_y_;
446 407
447 ThemeService* theme_service_; 408 GtkThemeService* theme_service_;
448 409
449 // The close button. 410 // The close button.
450 scoped_ptr<CustomDrawButton> close_button_; 411 scoped_ptr<CustomDrawButton> close_button_;
451 412
452 // The current color of the close button. 413 // The current color of the close button.
453 SkColor close_button_color_; 414 SkColor close_button_color_;
454 415
455 // Indicates whether this tab is the active one. 416 // Indicates whether this tab is the active one.
456 bool is_active_; 417 bool is_active_;
457 418
458 // Used to listen for theme change notifications.
459 content::NotificationRegistrar registrar_;
460
461 DISALLOW_COPY_AND_ASSIGN(TabRendererGtk); 419 DISALLOW_COPY_AND_ASSIGN(TabRendererGtk);
462 }; 420 };
463 421
464 #endif // CHROME_BROWSER_UI_GTK_TABS_TAB_RENDERER_GTK_H_ 422 #endif // CHROME_BROWSER_UI_GTK_TABS_TAB_RENDERER_GTK_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698