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 #include "base/command_line.h"
6 #import "chrome/browser/ui/cocoa/nsview_additions.h"
7 #include "chrome/common/chrome_switches.h"
8 #include "content/public/common/content_switches.h"
10 #include "base/logging.h"
12 #if !defined(MAC_OS_X_VERSION_10_7) || \
13 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
15 @interface NSView (LionAPI)
16 - (NSSize)convertSizeFromBacking:(NSSize)size;
21 // Replicate specific 10.9 SDK declarations for building with prior SDKs.
22 #if !defined(MAC_OS_X_VERSION_10_9) || \
23 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_9
25 @interface NSView (MavericksAPI)
26 // Flatten all child views that did not call setWantsLayer:YES into this
28 - (void)setCanDrawSubviewsIntoLayer:(BOOL)flag;
31 #endif // MAC_OS_X_VERSION_10_9
33 @implementation NSView (ChromeAdditions)
35 - (CGFloat)cr_lineWidth {
36 // All shipping retina macs run at least 10.7.
37 if (![self respondsToSelector:@selector(convertSizeFromBacking:)])
39 return [self convertSizeFromBacking:NSMakeSize(1, 1)].width;
42 - (BOOL)cr_isMouseInView {
43 NSPoint mouseLoc = [[self window] mouseLocationOutsideOfEventStream];
44 mouseLoc = [[self superview] convertPoint:mouseLoc fromView:nil];
45 return [self hitTest:mouseLoc] == self;
48 - (BOOL)cr_isBelowView:(NSView*)otherView {
49 NSArray* subviews = [[self superview] subviews];
51 NSUInteger selfIndex = [subviews indexOfObject:self];
52 DCHECK_NE(NSNotFound, selfIndex);
54 NSUInteger otherIndex = [subviews indexOfObject:otherView];
55 DCHECK_NE(NSNotFound, otherIndex);
57 return selfIndex < otherIndex;
60 - (BOOL)cr_isAboveView:(NSView*)otherView {
61 return ![self cr_isBelowView:otherView];
64 - (void)cr_ensureSubview:(NSView*)subview
65 isPositioned:(NSWindowOrderingMode)place
66 relativeTo:(NSView *)otherView {
67 DCHECK(place == NSWindowAbove || place == NSWindowBelow);
68 BOOL isAbove = place == NSWindowAbove;
69 if ([[subview superview] isEqual:self] &&
70 [subview cr_isAboveView:otherView] == isAbove) {
74 [subview removeFromSuperview];
75 [self addSubview:subview
77 relativeTo:otherView];
80 - (NSColor*)cr_keyboardFocusIndicatorColor {
81 return [[NSColor keyboardFocusIndicatorColor]
82 colorWithAlphaComponent:0.5 / [self cr_lineWidth]];
85 - (void)cr_recursivelySetNeedsDisplay:(BOOL)flag {
86 [self setNeedsDisplay:YES];
87 for (NSView* child in [self subviews])
88 [child cr_recursivelySetNeedsDisplay:flag];
91 - (BOOL)cr_supportsLayerSquashing {
92 return [self respondsToSelector:@selector(setCanDrawSubviewsIntoLayer:)];
95 - (void)cr_setWantsLayer:(BOOL)wantsLayer
96 withSquashing:(BOOL)squashing {
97 if (!CommandLine::ForCurrentProcess()->HasSwitch(
98 switches::kUseCoreAnimation))
100 [self setWantsLayer:wantsLayer];
102 if (CommandLine::ForCurrentProcess()->HasSwitch(
103 switches::kDisableCoreAnimationLayerSquashing))
105 if ([self cr_supportsLayerSquashing])
106 [self setCanDrawSubviewsIntoLayer:squashing];