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

Side by Side Diff: chrome/browser/ui/cocoa/tabs/tab_strip_controller.mm

Issue 18486007: Fix the misalignment on CrOS of the tab background images (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 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 | Annotate | Revision Log
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 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h" 5 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
6 6
7 #import <QuartzCore/QuartzCore.h> 7 #import <QuartzCore/QuartzCore.h>
8 8
9 #include <cmath> 9 #include <cmath>
10 #include <limits> 10 #include <limits>
(...skipping 12 matching lines...) Expand all
23 #include "chrome/browser/devtools/devtools_window.h" 23 #include "chrome/browser/devtools/devtools_window.h"
24 #include "chrome/browser/extensions/tab_helper.h" 24 #include "chrome/browser/extensions/tab_helper.h"
25 #include "chrome/browser/favicon/favicon_tab_helper.h" 25 #include "chrome/browser/favicon/favicon_tab_helper.h"
26 #include "chrome/browser/profiles/profile.h" 26 #include "chrome/browser/profiles/profile.h"
27 #include "chrome/browser/profiles/profile_manager.h" 27 #include "chrome/browser/profiles/profile_manager.h"
28 #include "chrome/browser/themes/theme_service.h" 28 #include "chrome/browser/themes/theme_service.h"
29 #include "chrome/browser/ui/browser.h" 29 #include "chrome/browser/ui/browser.h"
30 #include "chrome/browser/ui/browser_navigator.h" 30 #include "chrome/browser/ui/browser_navigator.h"
31 #include "chrome/browser/ui/browser_tabstrip.h" 31 #include "chrome/browser/ui/browser_tabstrip.h"
32 #import "chrome/browser/ui/cocoa/browser_window_controller.h" 32 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
33 #import "chrome/browser/ui/cocoa/browser_window_utils.h"
33 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_con troller.h" 34 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_con troller.h"
34 #include "chrome/browser/ui/cocoa/drag_util.h" 35 #include "chrome/browser/ui/cocoa/drag_util.h"
35 #import "chrome/browser/ui/cocoa/image_button_cell.h" 36 #import "chrome/browser/ui/cocoa/image_button_cell.h"
36 #import "chrome/browser/ui/cocoa/new_tab_button.h" 37 #import "chrome/browser/ui/cocoa/new_tab_button.h"
37 #import "chrome/browser/ui/cocoa/tab_contents/favicon_util_mac.h" 38 #import "chrome/browser/ui/cocoa/tab_contents/favicon_util_mac.h"
38 #import "chrome/browser/ui/cocoa/tab_contents/tab_contents_controller.h" 39 #import "chrome/browser/ui/cocoa/tab_contents/tab_contents_controller.h"
39 #import "chrome/browser/ui/cocoa/tabs/tab_audio_indicator_view_mac.h" 40 #import "chrome/browser/ui/cocoa/tabs/tab_audio_indicator_view_mac.h"
40 #import "chrome/browser/ui/cocoa/tabs/tab_controller.h" 41 #import "chrome/browser/ui/cocoa/tabs/tab_controller.h"
41 #import "chrome/browser/ui/cocoa/tabs/tab_projecting_image_view.h" 42 #import "chrome/browser/ui/cocoa/tabs/tab_projecting_image_view.h"
42 #import "chrome/browser/ui/cocoa/tabs/tab_strip_drag_controller.h" 43 #import "chrome/browser/ui/cocoa/tabs/tab_strip_drag_controller.h"
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 [result addRepresentation:bmpImageRep]; 185 [result addRepresentation:bmpImageRep];
185 } 186 }
186 [NSGraphicsContext restoreGraphicsState]; 187 [NSGraphicsContext restoreGraphicsState];
187 188
188 return result.release(); 189 return result.release();
189 } 190 }
190 191
191 // Takes a normal bitmap and a mask image and returns an image the size of the 192 // Takes a normal bitmap and a mask image and returns an image the size of the
192 // mask that has pixels from |image| but alpha information from |mask|. 193 // mask that has pixels from |image| but alpha information from |mask|.
193 NSImage* ApplyMask(NSImage* image, NSImage* mask) { 194 NSImage* ApplyMask(NSImage* image, NSImage* mask) {
195 DCHECK_EQ([image size].width, [mask size].width);
196 DCHECK_EQ([image size].height, [mask size].height);
197
194 return [CreateImageWithSize([mask size], ^(NSSize size) { 198 return [CreateImageWithSize([mask size], ^(NSSize size) {
195 // Skip a few pixels from the top of the tab background gradient, because 199 [image drawAtPoint:NSZeroPoint
196 // the new tab button is not drawn at the very top of the browser window. 200 fromRect:NSMakeRect(0, 0, size.width, size.height)
197 const int kYOffset = 10; 201 operation:NSCompositeCopy
198 CGFloat width = size.width; 202 fraction:1.0];
199 CGFloat height = size.height;
200
201 // In some themes, the tab background image is narrower than the
202 // new tab button, so tile the background image.
203 CGFloat x = 0;
204 // The floor() is to make sure images with odd widths don't draw to the
205 // same pixel twice on retina displays. (Using NSDrawThreePartImage()
206 // caused a startup perf regression, so that cannot be used.)
207 CGFloat tileWidth = floor(std::min(width, [image size].width));
208 while (x < width) {
209 [image drawAtPoint:NSMakePoint(x, 0)
210 fromRect:NSMakeRect(0,
211 [image size].height - height - kYOffset,
212 tileWidth,
213 height)
214 operation:NSCompositeCopy
215 fraction:1.0];
216 x += tileWidth;
217 }
218
219 [mask drawAtPoint:NSZeroPoint 203 [mask drawAtPoint:NSZeroPoint
220 fromRect:NSMakeRect(0, 0, width, height) 204 fromRect:NSMakeRect(0, 0, size.width, size.height)
221 operation:NSCompositeDestinationIn 205 operation:NSCompositeDestinationIn
222 fraction:1.0]; 206 fraction:1.0];
223 }) autorelease]; 207 }) autorelease];
224 } 208 }
225 209
226 // Creates a modified favicon used for the recording case. The mask is used for 210 // Creates a modified favicon used for the recording case. The mask is used for
227 // making part of the favicon transparent. (The part where the recording dot 211 // making part of the favicon transparent. (The part where the recording dot
228 // later is drawn.) 212 // later is drawn.)
229 NSImage* CreateMaskedFaviconForRecording(NSImage* image, 213 NSImage* CreateMaskedFaviconForRecording(NSImage* image,
230 NSImage* mask, 214 NSImage* mask,
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 fromRect:NSMakeRect(0, 0, width, height) 252 fromRect:NSMakeRect(0, 0, width, height)
269 operation:NSCompositeCopy 253 operation:NSCompositeCopy
270 fraction:1.0]; 254 fraction:1.0];
271 [overlay drawAtPoint:NSZeroPoint 255 [overlay drawAtPoint:NSZeroPoint
272 fromRect:NSMakeRect(0, 0, width, height) 256 fromRect:NSMakeRect(0, 0, width, height)
273 operation:NSCompositeSourceOver 257 operation:NSCompositeSourceOver
274 fraction:alpha]; 258 fraction:alpha];
275 }) autorelease]; 259 }) autorelease];
276 } 260 }
277 261
262 // Tiles |image| horizontally into a new NSImage of |dest_size|. |dest_y_inset|
263 // refers to the position from the bottom of the returned image at which the top
264 // of |image| should be painted.
265 NSImage* TileHorizontally(NSImage* image,
266 CGFloat dest_y_inset,
267 NSSize dest_size) {
268 return [CreateImageWithSize(dest_size, ^(NSSize size) {
269 CGFloat width = size.width;
270 CGFloat height = size.height;
271
272 CGFloat x = 0;
273 // The floor() is to make sure images with odd widths and height don't
274 // draw to the same pixel twice on retina displays. (Using
275 // NSDrawThreePartImage() caused a startup perf regression, so that
276 // cannot be used.)
277 CGFloat tile_width = floor(std::min(width, [image size].width));
278 while (x < width) {
279 [image drawAtPoint:NSMakePoint(x, 0)
280 fromRect:NSMakeRect(0,
281 [image size].height - dest_y_inset,
282 tile_width,
283 height)
284 operation:NSCompositeCopy
285 fraction:1.0];
286 x += tile_width;
287 }
288 }) autorelease];
289 }
290
291 // Computes the y position that the top of a theme image with |alignment|
292 // should be painted at. The returned y position is in the coordinates of an
293 // NSImage of |dst_height| which is centered on the new tab button. This method
294 // is a helper for [TabStripController setNewTabImages].
295 int NewTabButtonThemeBackgroundYPosition(TabStripView* tabstrip,
296 ThemeImageAlignment alignment,
297 CGFloat dst_height) {
298 CGFloat y_position_in_tabstrip =
299 [BrowserWindowUtils themeImagePositionInTabStripCoords:tabstrip
300 alignment:alignment].y;
301 // The image is centered in the new tab button.
302 NSView* new_tab_button = [tabstrip getNewTabButton];
303 return y_position_in_tabstrip -
304 roundf((NSHeight([new_tab_button bounds]) - dst_height) / 2.0);
305 }
306
278 } // namespace 307 } // namespace
279 308
280 @interface TabStripController (Private) 309 @interface TabStripController (Private)
281 - (void)addSubviewToPermanentList:(NSView*)aView; 310 - (void)addSubviewToPermanentList:(NSView*)aView;
282 - (void)regenerateSubviewList; 311 - (void)regenerateSubviewList;
283 - (NSInteger)indexForContentsView:(NSView*)view; 312 - (NSInteger)indexForContentsView:(NSView*)view;
284 - (NSImageView*)iconImageViewForContents:(content::WebContents*)contents; 313 - (NSImageView*)iconImageViewForContents:(content::WebContents*)contents;
285 - (void)updateFaviconForContents:(content::WebContents*)contents 314 - (void)updateFaviconForContents:(content::WebContents*)contents
286 atIndex:(NSInteger)modelIndex; 315 atIndex:(NSInteger)modelIndex;
287 - (void)layoutTabsWithAnimation:(BOOL)animate 316 - (void)layoutTabsWithAnimation:(BOOL)animate
(...skipping 1973 matching lines...) Expand 10 before | Expand all | Expand 10 after
2261 ThemeService *theme = 2290 ThemeService *theme =
2262 static_cast<ThemeService*>([[tabStripView_ window] themeProvider]); 2291 static_cast<ThemeService*>([[tabStripView_ window] themeProvider]);
2263 if (!theme) 2292 if (!theme)
2264 return; 2293 return;
2265 2294
2266 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 2295 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
2267 NSImage* mask = rb.GetNativeImageNamed(IDR_NEWTAB_BUTTON_MASK).ToNSImage(); 2296 NSImage* mask = rb.GetNativeImageNamed(IDR_NEWTAB_BUTTON_MASK).ToNSImage();
2268 NSImage* normal = rb.GetNativeImageNamed(IDR_NEWTAB_BUTTON).ToNSImage(); 2297 NSImage* normal = rb.GetNativeImageNamed(IDR_NEWTAB_BUTTON).ToNSImage();
2269 NSImage* hover = rb.GetNativeImageNamed(IDR_NEWTAB_BUTTON_H).ToNSImage(); 2298 NSImage* hover = rb.GetNativeImageNamed(IDR_NEWTAB_BUTTON_H).ToNSImage();
2270 NSImage* pressed = rb.GetNativeImageNamed(IDR_NEWTAB_BUTTON_P).ToNSImage(); 2299 NSImage* pressed = rb.GetNativeImageNamed(IDR_NEWTAB_BUTTON_P).ToNSImage();
2300 NSImage* bgActive = theme->GetNSImageNamed(IDR_THEME_TAB_BACKGROUND);
2301 NSImage* bgActiveOverlay = nil;
2302 if (theme->HasCustomImage(IDR_THEME_TAB_BACKGROUND_OVERLAY)) {
2303 bgActiveOverlay = theme->GetNSImageNamed(
2304 IDR_THEME_TAB_BACKGROUND_OVERLAY);
2305 }
2271 2306
2272 NSImage* foreground = ApplyMask( 2307 int yPositionAlignWithFrame = NewTabButtonThemeBackgroundYPosition(
2273 theme->GetNSImageNamed(IDR_THEME_TAB_BACKGROUND), mask); 2308 tabStripView_, THEME_IMAGE_ALIGN_WITH_FRAME, [mask size].height);
2309 int yPositionAlignWithTabStrip = NewTabButtonThemeBackgroundYPosition(
2310 tabStripView_, THEME_IMAGE_ALIGN_WITH_TAB_STRIP, [mask size].height);
2311
2312 // Create the foreground image by combining the tinted frame image
2313 // (IDR_THEME_TAB_BACKGROUND) and the custom theme provided overlay
2314 // (IDR_THEME_TAB_BACKGROUND_OVERLAY). Tile the images because the images
2315 // can be narrower than the new tab button for some themes.
2316 NSImage* unmaskedForeground = TileHorizontally(bgActive,
2317 yPositionAlignWithFrame, [mask size]);
2318 if (bgActiveOverlay) {
2319 NSImage* tiledOverlay = TileHorizontally(bgActiveOverlay,
2320 yPositionAlignWithTabStrip, [mask size]);
2321 unmaskedForeground = Overlay(unmaskedForeground, tiledOverlay, 1.0);
2322 }
2323 NSImage* foreground = ApplyMask(unmaskedForeground, mask);
2274 2324
2275 [[newTabButton_ cell] setImage:Overlay(foreground, normal, 1.0) 2325 [[newTabButton_ cell] setImage:Overlay(foreground, normal, 1.0)
2276 forButtonState:image_button_cell::kDefaultState]; 2326 forButtonState:image_button_cell::kDefaultState];
2277 [[newTabButton_ cell] setImage:Overlay(foreground, hover, 1.0) 2327 [[newTabButton_ cell] setImage:Overlay(foreground, hover, 1.0)
2278 forButtonState:image_button_cell::kHoverState]; 2328 forButtonState:image_button_cell::kHoverState];
2279 [[newTabButton_ cell] setImage:Overlay(foreground, pressed, 1.0) 2329 [[newTabButton_ cell] setImage:Overlay(foreground, pressed, 1.0)
2280 forButtonState:image_button_cell::kPressedState]; 2330 forButtonState:image_button_cell::kPressedState];
2281 2331
2282 // IDR_THEME_TAB_BACKGROUND_INACTIVE is only used with the default theme. 2332 // IDR_THEME_TAB_BACKGROUND_INACTIVE is only used with the default theme.
2283 if (theme->UsingDefaultTheme()) { 2333 if (theme->UsingDefaultTheme()) {
2284 const CGFloat alpha = tabs::kImageNoFocusAlpha; 2334 const CGFloat alpha = tabs::kImageNoFocusAlpha;
2285 NSImage* background = ApplyMask( 2335 NSImage* unmaskedBackground = TileHorizontally(
2286 theme->GetNSImageNamed(IDR_THEME_TAB_BACKGROUND_INACTIVE), mask); 2336 rb.GetNativeImageNamed(IDR_THEME_TAB_BACKGROUND_INACTIVE).ToNSImage(),
2337 yPositionAlignWithTabStrip,
2338 [mask size]);
2339 NSImage* background = ApplyMask(unmaskedBackground, mask);
2287 [[newTabButton_ cell] setImage:Overlay(background, normal, alpha) 2340 [[newTabButton_ cell] setImage:Overlay(background, normal, alpha)
2288 forButtonState:image_button_cell::kDefaultStateBackground]; 2341 forButtonState:image_button_cell::kDefaultStateBackground];
2289 [[newTabButton_ cell] setImage:Overlay(background, hover, alpha) 2342 [[newTabButton_ cell] setImage:Overlay(background, hover, alpha)
2290 forButtonState:image_button_cell::kHoverStateBackground]; 2343 forButtonState:image_button_cell::kHoverStateBackground];
2291 } else { 2344 } else {
2292 [[newTabButton_ cell] setImage:nil 2345 [[newTabButton_ cell] setImage:nil
2293 forButtonState:image_button_cell::kDefaultStateBackground]; 2346 forButtonState:image_button_cell::kDefaultStateBackground];
2294 [[newTabButton_ cell] setImage:nil 2347 [[newTabButton_ cell] setImage:nil
2295 forButtonState:image_button_cell::kHoverStateBackground]; 2348 forButtonState:image_button_cell::kHoverStateBackground];
2296 } 2349 }
2297 } 2350 }
2298 2351
2299 @end 2352 @end
2300 2353
2301 NSView* GetSheetParentViewForWebContents(WebContents* web_contents) { 2354 NSView* GetSheetParentViewForWebContents(WebContents* web_contents) {
2302 // View hierarchy of the contents view: 2355 // View hierarchy of the contents view:
2303 // NSView -- switchView, same for all tabs 2356 // NSView -- switchView, same for all tabs
2304 // +- NSView -- TabContentsController's view 2357 // +- NSView -- TabContentsController's view
2305 // +- TabContentsViewCocoa 2358 // +- TabContentsViewCocoa
2306 // 2359 //
2307 // Changing it? Do not forget to modify 2360 // Changing it? Do not forget to modify
2308 // -[TabStripController swapInTabAtIndex:] too. 2361 // -[TabStripController swapInTabAtIndex:] too.
2309 return [web_contents->GetView()->GetNativeView() superview]; 2362 return [web_contents->GetView()->GetNativeView() superview];
2310 } 2363 }
OLDNEW
« no previous file with comments | « chrome/browser/themes/browser_theme_pack_unittest.cc ('k') | chrome/browser/ui/cocoa/tabs/tab_view.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698