Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_background_view.h" | 5 #import "chrome/browser/ui/cocoa/tabs/tab_strip_background_view_private.h" |
| 6 | 6 |
| 7 #include "chrome/browser/themes/theme_properties.h" | |
| 7 #import "chrome/browser/ui/cocoa/framed_browser_window.h" | 8 #import "chrome/browser/ui/cocoa/framed_browser_window.h" |
| 8 #import "ui/base/cocoa/nsview_additions.h" | 9 #import "ui/base/cocoa/nsview_additions.h" |
| 10 #include "ui/base/theme_provider.h" | |
| 9 | 11 |
| 10 @implementation TabStripBackgroundView | 12 @interface TabStripThemeBackgroundView : NSView |
| 13 @property(nonatomic) BOOL inATabDraggingOverlayWindow; | |
| 14 @end | |
| 15 | |
| 16 @implementation TabStripThemeBackgroundView | |
| 17 | |
| 18 @synthesize inATabDraggingOverlayWindow = _inATabDraggingOverlayWindow; | |
|
Robert Sesek
2017/01/11 19:23:39
Chrome uses trailingUnderscores_ for ObjC ivars.
Sidney San Martín
2017/01/13 00:10:07
Done.
| |
| 11 | 19 |
| 12 - (void)drawRect:(NSRect)dirtyRect { | 20 - (void)drawRect:(NSRect)dirtyRect { |
| 13 // Only the top corners are rounded. For simplicity, round all 4 corners but | 21 // Only the top corners are rounded. For simplicity, round all 4 corners but |
| 14 // draw the bottom corners outside of the visible bounds. | 22 // draw the bottom corners outside of the visible bounds. |
| 15 float cornerRadius = 4.0; | 23 float cornerRadius = 4.0; |
| 24 bool isFullScreen = (self.window.styleMask & NSFullScreenWindowMask) != 0; | |
|
spqchan
2017/01/11 19:33:56
nit: Change to isFullscreen
We want to use "Fulls
Sidney San Martín
2017/01/13 00:10:07
Done.
| |
| 25 | |
| 16 NSRect roundedRect = [self bounds]; | 26 NSRect roundedRect = [self bounds]; |
| 17 roundedRect.origin.y -= cornerRadius; | 27 if (!isFullScreen) { |
| 18 roundedRect.size.height += cornerRadius; | 28 roundedRect.origin.y -= cornerRadius; |
| 19 [[NSBezierPath bezierPathWithRoundedRect:roundedRect | 29 roundedRect.size.height += cornerRadius; |
| 20 xRadius:cornerRadius | 30 [[NSBezierPath bezierPathWithRoundedRect:roundedRect |
| 21 yRadius:cornerRadius] addClip]; | 31 xRadius:cornerRadius |
| 32 yRadius:cornerRadius] addClip]; | |
| 33 } | |
| 22 BOOL themed = [FramedBrowserWindow drawWindowThemeInDirtyRect:dirtyRect | 34 BOOL themed = [FramedBrowserWindow drawWindowThemeInDirtyRect:dirtyRect |
| 23 forView:self | 35 forView:self |
| 24 bounds:roundedRect | 36 bounds:roundedRect |
| 25 forceBlackBackground:NO]; | 37 forceBlackBackground:NO]; |
| 26 | 38 |
| 27 // Draw a 1px border on the top edge and top corners. | 39 // Draw a 1px border on the top edge and top corners. |
| 28 if (themed) { | 40 if (themed) { |
| 29 CGFloat lineWidth = [self cr_lineWidth]; | 41 if (!isFullScreen) { |
| 30 // Inset the vertical lines by 0.5px so that the top line gets a full pixel. | 42 CGFloat lineWidth = [self cr_lineWidth]; |
| 31 // Outset the horizontal lines by 0.5px so that they are not visible, but | 43 // Inset the vertical lines by 0.5px so that the top line gets a full |
| 32 // still get the rounded corners to get a border. | 44 // pixel. Outset the horizontal lines by 0.5px so that they are not |
| 33 NSRect strokeRect = NSInsetRect(roundedRect, -lineWidth/2, lineWidth/2); | 45 // visible, but still get the rounded corners to get a border. |
| 34 NSBezierPath* path = [NSBezierPath bezierPathWithRoundedRect:strokeRect | 46 NSRect strokeRect = |
| 35 xRadius:cornerRadius | 47 NSInsetRect(roundedRect, -lineWidth / 2, lineWidth / 2); |
| 36 yRadius:cornerRadius]; | 48 NSBezierPath* path = |
| 37 [path setLineWidth:lineWidth]; | 49 [NSBezierPath bezierPathWithRoundedRect:strokeRect |
| 38 [[NSColor colorWithCalibratedWhite:1.0 alpha:0.5] set]; | 50 xRadius:cornerRadius |
| 39 [path stroke]; | 51 yRadius:cornerRadius]; |
| 52 [path setLineWidth:lineWidth]; | |
| 53 [[NSColor colorWithCalibratedWhite:1.0 alpha:0.5] set]; | |
| 54 [path stroke]; | |
| 55 } | |
| 56 } else if (!_inATabDraggingOverlayWindow) { | |
| 57 // If the window is not themed, and not being used to drag tabs between | |
| 58 // browser windows, decrease the tab strip background's translucency by | |
| 59 // overlaying it with a partially-transparent gray. The gray is somewhat | |
| 60 // opaque for Incognito mode, very opaque for non-Incognito mode, and | |
| 61 // completely opaque when the window is not active. | |
| 62 if (const ui::ThemeProvider* themeProvider = | |
| 63 [[self window] themeProvider]) { | |
| 64 NSColor* overlayColor = nil; | |
| 65 if (self.window.isMainWindow) { | |
| 66 NSAppearance* appearance = self.effectiveAppearance; | |
| 67 if ([appearance respondsToSelector:@selector(allowsVibrancy)] && | |
| 68 appearance.allowsVibrancy) { | |
| 69 overlayColor = themeProvider->GetNSColor( | |
| 70 ThemeProperties::COLOR_FRAME_VIBRANCY_OVERLAY); | |
| 71 } else if (themeProvider->InIncognitoMode()) { | |
| 72 overlayColor = [NSColor colorWithSRGBRed:20 / 255. | |
| 73 green:22 / 255. | |
| 74 blue:24 / 255. | |
| 75 alpha:1]; | |
| 76 } else { | |
| 77 overlayColor = | |
| 78 themeProvider->GetNSColor(ThemeProperties::COLOR_FRAME); | |
| 79 } | |
| 80 } else { | |
| 81 overlayColor = | |
| 82 themeProvider->GetNSColor(ThemeProperties::COLOR_FRAME_INACTIVE); | |
| 83 } | |
| 84 | |
| 85 if (overlayColor) { | |
| 86 [overlayColor set]; | |
| 87 NSRectFillUsingOperation(dirtyRect, NSCompositeSourceOver); | |
| 88 } | |
| 89 } | |
| 40 } | 90 } |
| 41 } | 91 } |
| 92 @end | |
| 93 | |
| 94 @implementation TabStripBackgroundView { | |
| 95 TabStripThemeBackgroundView* _themeBackgroundView; | |
|
Robert Sesek
2017/01/11 19:23:39
Here too use trailing underscores.
Sidney San Martín
2017/01/13 00:10:07
Done.
| |
| 96 NSVisualEffectView* _visualEffectView; | |
| 97 } | |
| 98 | |
| 99 - (instancetype)initWithFrame:(NSRect)frame { | |
| 100 if ((self = [super initWithFrame:frame])) { | |
| 101 _themeBackgroundView = [[[TabStripThemeBackgroundView alloc] | |
| 102 initWithFrame:self.bounds] autorelease]; | |
|
Robert Sesek
2017/01/11 19:23:39
Who owns this? Needs a comment in the ivar.
Sidney San Martín
2017/01/13 00:10:07
Done.
| |
| 103 _themeBackgroundView.autoresizingMask = | |
| 104 NSViewWidthSizable | NSViewHeightSizable; | |
| 105 [self addSubview:_themeBackgroundView]; | |
| 106 } | |
| 107 return self; | |
| 108 } | |
| 109 | |
| 110 - (BOOL)inATabDraggingOverlayWindow { | |
| 111 return _themeBackgroundView.inATabDraggingOverlayWindow; | |
| 112 } | |
| 113 | |
| 114 - (void)setInATabDraggingOverlayWindow:(BOOL)inATabDraggingOverlayWindow { | |
| 115 _themeBackgroundView.inATabDraggingOverlayWindow = | |
| 116 inATabDraggingOverlayWindow; | |
| 117 } | |
| 118 | |
| 119 - (void)updateVisualEffectView { | |
|
Robert Sesek
2017/01/11 19:23:40
Does this need to be gated by a runtime version ch
Sidney San Martín
2017/01/13 00:10:07
It doesn't _need_ to be. NSVisualEffectView is a w
| |
| 120 const ui::ThemeProvider* themeProvider = [[self window] themeProvider]; | |
| 121 const bool isFullScreen = | |
| 122 (self.window.styleMask & NSFullScreenWindowMask) != 0; | |
| 123 | |
| 124 // Visual effects cause higher power consumption in full screen. | |
|
spqchan
2017/01/11 19:33:56
nit: "full screen" -> "fullscreen"
Sidney San Martín
2017/01/13 00:10:07
Done.
| |
| 125 if (!isFullScreen && themeProvider->UsingSystemTheme()) { | |
| 126 if (!_visualEffectView) { | |
| 127 _visualEffectView = | |
| 128 [[[NSVisualEffectView alloc] initWithFrame:self.bounds] autorelease]; | |
| 129 if (!_visualEffectView) | |
| 130 return; | |
| 131 | |
| 132 _visualEffectView.autoresizingMask = | |
| 133 NSViewWidthSizable | NSViewHeightSizable; | |
| 134 _visualEffectView.appearance = | |
| 135 [NSAppearance appearanceNamed:themeProvider->InIncognitoMode() | |
| 136 ? NSAppearanceNameVibrantDark | |
| 137 : NSAppearanceNameVibrantLight]; | |
| 138 [self addSubview:_visualEffectView]; | |
| 139 [_visualEffectView addSubview:_themeBackgroundView]; | |
| 140 } | |
| 141 } else { | |
| 142 if (_visualEffectView) { | |
| 143 [self addSubview:_themeBackgroundView]; | |
| 144 [_visualEffectView removeFromSuperview]; | |
| 145 _visualEffectView = nil; | |
| 146 } | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 - (void)viewWillMoveToWindow:(NSWindow*)newWindow { | |
| 151 // AppKit calls this method when the view's position in the view hierarchy | |
| 152 // changes, even if its parent window won't change. | |
| 153 if (newWindow == self.window) | |
| 154 return; | |
| 155 if (self.window) | |
| 156 [self.window removeObserver:self forKeyPath:@"styleMask"]; | |
|
Robert Sesek
2017/01/11 19:23:39
Do you need to remove the observer in dealloc, or
Sidney San Martín
2017/01/13 00:10:07
AppKit takes care of it:
| |
| 157 if (newWindow) | |
| 158 [newWindow addObserver:self forKeyPath:@"styleMask" options:0 context:nil]; | |
| 159 } | |
| 160 | |
| 161 - (void)observeValueForKeyPath:(NSString*)keyPath | |
| 162 ofObject:(id)object | |
| 163 change:(NSDictionary*)change | |
| 164 context:(void*)context { | |
| 165 DCHECK(object == self.window); | |
|
Robert Sesek
2017/01/11 19:23:40
DCHECK_EQ
Sidney San Martín
2017/01/13 00:10:07
Done.
| |
| 166 DCHECK([keyPath isEqualToString:@"styleMask"]); | |
| 167 [self updateVisualEffectView]; | |
| 168 } | |
| 42 | 169 |
| 43 // ThemedWindowDrawing implementation. | 170 // ThemedWindowDrawing implementation. |
| 44 | 171 |
| 45 - (void)windowDidChangeTheme { | 172 - (void)windowDidChangeTheme { |
| 46 [self setNeedsDisplay:YES]; | 173 [self updateVisualEffectView]; |
| 174 [_themeBackgroundView setNeedsDisplay:YES]; | |
| 47 } | 175 } |
| 48 | 176 |
| 49 - (void)windowDidChangeActive { | 177 - (void)windowDidChangeActive { |
| 50 [self setNeedsDisplay:YES]; | 178 // TODO(sdy): It shouldn't be necessary to update the visual effect view |
| 179 // here, since it only changes when the theme changes), but the window isn't | |
| 180 // associated with a themeProvider at init time. | |
| 181 [self updateVisualEffectView]; | |
| 182 [_themeBackgroundView setNeedsDisplay:YES]; | |
| 51 } | 183 } |
| 52 | 184 |
| 53 @end | 185 @end |
| OLD | NEW |