Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / nsview_additions.mm
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.
4
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"
9
10 #include "base/logging.h"
11
12 #if !defined(MAC_OS_X_VERSION_10_7) || \
13     MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
14
15 @interface NSView (LionAPI)
16 - (NSSize)convertSizeFromBacking:(NSSize)size;
17 @end
18
19 #endif  // 10.7
20
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
24
25 @interface NSView (MavericksAPI)
26 // Flatten all child views that did not call setWantsLayer:YES into this
27 // view's CALayer.
28 - (void)setCanDrawSubviewsIntoLayer:(BOOL)flag;
29 @end
30
31 #endif  // MAC_OS_X_VERSION_10_9
32
33 @implementation NSView (ChromeAdditions)
34
35 - (CGFloat)cr_lineWidth {
36   // All shipping retina macs run at least 10.7.
37   if (![self respondsToSelector:@selector(convertSizeFromBacking:)])
38     return 1;
39   return [self convertSizeFromBacking:NSMakeSize(1, 1)].width;
40 }
41
42 - (BOOL)cr_isMouseInView {
43   NSPoint mouseLoc = [[self window] mouseLocationOutsideOfEventStream];
44   mouseLoc = [[self superview] convertPoint:mouseLoc fromView:nil];
45   return [self hitTest:mouseLoc] == self;
46 }
47
48 - (BOOL)cr_isBelowView:(NSView*)otherView {
49   NSArray* subviews = [[self superview] subviews];
50
51   NSUInteger selfIndex = [subviews indexOfObject:self];
52   DCHECK_NE(NSNotFound, selfIndex);
53
54   NSUInteger otherIndex = [subviews indexOfObject:otherView];
55   DCHECK_NE(NSNotFound, otherIndex);
56
57   return selfIndex < otherIndex;
58 }
59
60 - (BOOL)cr_isAboveView:(NSView*)otherView {
61   return ![self cr_isBelowView:otherView];
62 }
63
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) {
71     return;
72   }
73
74   [subview removeFromSuperview];
75   [self addSubview:subview
76         positioned:place
77         relativeTo:otherView];
78 }
79
80 - (NSColor*)cr_keyboardFocusIndicatorColor {
81   return [[NSColor keyboardFocusIndicatorColor]
82       colorWithAlphaComponent:0.5 / [self cr_lineWidth]];
83 }
84
85 - (void)cr_recursivelySetNeedsDisplay:(BOOL)flag {
86   [self setNeedsDisplay:YES];
87   for (NSView* child in [self subviews])
88     [child cr_recursivelySetNeedsDisplay:flag];
89 }
90
91 - (BOOL)cr_supportsLayerSquashing {
92   return [self respondsToSelector:@selector(setCanDrawSubviewsIntoLayer:)];
93 }
94
95 - (void)cr_setWantsLayer:(BOOL)wantsLayer
96            withSquashing:(BOOL)squashing {
97   if (!CommandLine::ForCurrentProcess()->HasSwitch(
98           switches::kUseCoreAnimation))
99     return;
100   [self setWantsLayer:wantsLayer];
101
102   if (CommandLine::ForCurrentProcess()->HasSwitch(
103           switches::kDisableCoreAnimationLayerSquashing))
104     return;
105   if ([self cr_supportsLayerSquashing])
106     [self setCanDrawSubviewsIntoLayer:squashing];
107 }
108
109 @end