Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/browser_window_touch_bar.h" | 5 #import "chrome/browser/ui/cocoa/browser_window_touch_bar.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/mac/mac_util.h" | 9 #include "base/mac/mac_util.h" |
| 10 #import "base/mac/scoped_nsobject.h" | 10 #import "base/mac/scoped_nsobject.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 RELOAD, | 47 RELOAD, |
| 48 HOME, | 48 HOME, |
| 49 SEARCH, | 49 SEARCH, |
| 50 STAR, | 50 STAR, |
| 51 NEW_TAB, | 51 NEW_TAB, |
| 52 TOUCH_BAR_ACTION_COUNT | 52 TOUCH_BAR_ACTION_COUNT |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 // The touch bar's identifier. | 55 // The touch bar's identifier. |
| 56 const NSTouchBarCustomizationIdentifier kBrowserWindowTouchBarId = | 56 const NSTouchBarCustomizationIdentifier kBrowserWindowTouchBarId = |
| 57 @"BrowserWindowTouchBarId"; | 57 @"com.google.chrome.browser-window"; |
|
Robert Sesek
2017/04/13 18:45:32
Why the change to reverse-domain notation?
spqchan
2017/04/13 21:13:50
In this link: https://developer.apple.com/referenc
Robert Sesek
2017/04/14 16:56:20
Okay, then I'd actually recommend formatting with
| |
| 58 | 58 |
| 59 // Touch bar items identifiers. | 59 // Touch bar items identifiers. |
| 60 const NSTouchBarItemIdentifier kBackForwardTouchId = @"BackForwardTouchId"; | 60 const NSTouchBarItemIdentifier kBackForwardTouchId = |
| 61 const NSTouchBarItemIdentifier kReloadOrStopTouchId = @"ReloadOrStopTouchId"; | 61 @"com.google.chrome.browser-window-BACK-FWD"; |
| 62 const NSTouchBarItemIdentifier kHomeTouchId = @"HomeTouchId"; | 62 const NSTouchBarItemIdentifier kReloadOrStopTouchId = |
| 63 const NSTouchBarItemIdentifier kSearchTouchId = @"SearchTouchId"; | 63 @"com.google.chrome.browser-window-RELOAD-STOP"; |
| 64 const NSTouchBarItemIdentifier kStarTouchId = @"StarTouchId"; | 64 const NSTouchBarItemIdentifier kHomeTouchId = |
| 65 const NSTouchBarItemIdentifier kNewTabTouchId = @"NewTabTouchId"; | 65 @"com.google.chrome.browser-window-HOME"; |
| 66 const NSTouchBarItemIdentifier kSearchTouchId = | |
| 67 @"com.google.chrome.browser-window--SEARCH"; | |
| 68 const NSTouchBarItemIdentifier kStarTouchId = | |
| 69 @"com.google.chrome.browser-window-BOOKMARK"; | |
| 70 const NSTouchBarItemIdentifier kNewTabTouchId = | |
| 71 @"com.google.chrome.browser-window-NEW-TAB"; | |
| 66 | 72 |
| 67 // The button indexes in the back and forward segment control. | 73 // The button indexes in the back and forward segment control. |
| 68 const int kBackSegmentIndex = 0; | 74 const int kBackSegmentIndex = 0; |
| 69 const int kForwardSegmentIndex = 1; | 75 const int kForwardSegmentIndex = 1; |
| 70 | 76 |
| 71 // Touch bar icon colors values. | 77 // Touch bar icon colors values. |
| 72 const SkColor kTouchBarDefaultIconColor = SK_ColorWHITE; | 78 const SkColor kTouchBarDefaultIconColor = SK_ColorWHITE; |
| 73 const SkColor kTouchBarStarActiveColor = gfx::kGoogleBlue500; | 79 const SkColor kTouchBarStarActiveColor = gfx::kGoogleBlue500; |
| 74 | 80 |
| 75 // The size of the touch bar icons. | 81 // The size of the touch bar icons. |
| 76 const int kTouchBarIconSize = 16; | 82 const int kTouchBarIconSize = 16; |
| 77 | 83 |
| 78 // The width of the search button in the touch bar. | 84 // The min width of the search button in the touch bar. |
| 79 const int kSearchBtnWidthWithHomeBtn = 205; | 85 const int kSearchBtnMinWidth = 205; |
| 80 const int kSearchBtnWidthWithoutHomeBtn = 280; | |
| 81 | 86 |
| 82 // Creates an NSImage from the given VectorIcon. | 87 // Creates an NSImage from the given VectorIcon. |
| 83 NSImage* CreateNSImageFromIcon(const gfx::VectorIcon& icon, | 88 NSImage* CreateNSImageFromIcon(const gfx::VectorIcon& icon, |
| 84 SkColor color = kTouchBarDefaultIconColor) { | 89 SkColor color = kTouchBarDefaultIconColor) { |
| 85 return NSImageFromImageSkiaWithColorSpace( | 90 return NSImageFromImageSkiaWithColorSpace( |
| 86 gfx::CreateVectorIcon(icon, kTouchBarIconSize, color), | 91 gfx::CreateVectorIcon(icon, kTouchBarIconSize, color), |
| 87 base::mac::GetSRGBColorSpace()); | 92 base::mac::GetSRGBColorSpace()); |
| 88 } | 93 } |
| 89 | 94 |
| 90 // Creates a NSButton for the touch bar. | 95 // Creates a NSButton for the touch bar. |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 return self; | 204 return self; |
| 200 } | 205 } |
| 201 | 206 |
| 202 - (NSTouchBar*)makeTouchBar { | 207 - (NSTouchBar*)makeTouchBar { |
| 203 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar)) | 208 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar)) |
| 204 return nil; | 209 return nil; |
| 205 | 210 |
| 206 base::scoped_nsobject<NSTouchBar> touchBar( | 211 base::scoped_nsobject<NSTouchBar> touchBar( |
| 207 [[NSClassFromString(@"NSTouchBar") alloc] init]); | 212 [[NSClassFromString(@"NSTouchBar") alloc] init]); |
| 208 NSArray* touchBarItemIdentifiers; | 213 NSArray* touchBarItemIdentifiers; |
| 214 NSArray* customTouchBarItemIdentifiers = @[ | |
| 215 kBackForwardTouchId, kReloadOrStopTouchId, kHomeTouchId, kSearchTouchId, | |
| 216 kStarTouchId, kNewTabTouchId, NSTouchBarItemIdentifierFlexibleSpace | |
| 217 ]; | |
| 209 if (showHomeButton_.GetValue()) { | 218 if (showHomeButton_.GetValue()) { |
| 210 touchBarItemIdentifiers = @[ | 219 touchBarItemIdentifiers = @[ |
| 211 kBackForwardTouchId, kReloadOrStopTouchId, kHomeTouchId, kSearchTouchId, | 220 kBackForwardTouchId, kReloadOrStopTouchId, kHomeTouchId, kSearchTouchId, |
| 212 kStarTouchId, kNewTabTouchId | 221 kStarTouchId, kNewTabTouchId |
| 213 ]; | 222 ]; |
| 214 } else { | 223 } else { |
| 215 touchBarItemIdentifiers = @[ | 224 touchBarItemIdentifiers = @[ |
| 216 kBackForwardTouchId, kReloadOrStopTouchId, kSearchTouchId, kStarTouchId, | 225 kBackForwardTouchId, kReloadOrStopTouchId, kSearchTouchId, kStarTouchId, |
| 217 kNewTabTouchId | 226 kNewTabTouchId |
| 218 ]; | 227 ]; |
| 219 } | 228 } |
| 220 | 229 |
| 221 [touchBar setCustomizationIdentifier:kBrowserWindowTouchBarId]; | 230 [touchBar setCustomizationIdentifier:kBrowserWindowTouchBarId]; |
| 222 [touchBar setDefaultItemIdentifiers:touchBarItemIdentifiers]; | 231 [touchBar setDefaultItemIdentifiers:touchBarItemIdentifiers]; |
| 223 [touchBar setCustomizationAllowedItemIdentifiers:touchBarItemIdentifiers]; | 232 [touchBar |
| 233 setCustomizationAllowedItemIdentifiers:customTouchBarItemIdentifiers]; | |
| 224 [touchBar setDelegate:self]; | 234 [touchBar setDelegate:self]; |
| 225 | 235 |
| 226 return touchBar.autorelease(); | 236 return touchBar.autorelease(); |
| 227 } | 237 } |
| 228 | 238 |
| 229 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar | 239 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar |
| 230 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { | 240 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { |
| 231 if (!touchBar) | 241 if (!touchBar) |
| 232 return nil; | 242 return nil; |
| 233 | 243 |
| 234 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString( | 244 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString( |
| 235 @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]); | 245 @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]); |
| 236 if ([identifier isEqualTo:kBackForwardTouchId]) { | 246 if ([identifier isEqualTo:kBackForwardTouchId]) { |
| 237 [touchBarItem setView:[self backOrForwardTouchBarView]]; | 247 [touchBarItem setView:[self backOrForwardTouchBarView]]; |
| 248 [touchBarItem setCustomizationLabel: | |
| 249 l10n_util::GetNSString( | |
| 250 IDS_TOUCH_BAR_BACK_FORWARD_CUSTOMIZATION_LABEL)]; | |
| 238 } else if ([identifier isEqualTo:kReloadOrStopTouchId]) { | 251 } else if ([identifier isEqualTo:kReloadOrStopTouchId]) { |
| 239 const gfx::VectorIcon& icon = | 252 const gfx::VectorIcon& icon = |
| 240 isPageLoading_ ? kNavigateStopIcon : kNavigateReloadIcon; | 253 isPageLoading_ ? kNavigateStopIcon : kNavigateReloadIcon; |
| 241 int commandId = isPageLoading_ ? IDC_STOP : IDC_RELOAD; | 254 int commandId = isPageLoading_ ? IDC_STOP : IDC_RELOAD; |
| 242 int tooltipId = isPageLoading_ ? IDS_TOOLTIP_STOP : IDS_TOOLTIP_RELOAD; | 255 int tooltipId = isPageLoading_ ? IDS_TOOLTIP_STOP : IDS_TOOLTIP_RELOAD; |
| 243 [touchBarItem | 256 [touchBarItem |
| 244 setView:CreateTouchBarButton(icon, self, commandId, tooltipId)]; | 257 setView:CreateTouchBarButton(icon, self, commandId, tooltipId)]; |
| 258 [touchBarItem setCustomizationLabel: | |
| 259 l10n_util::GetNSString( | |
| 260 IDS_TOUCH_BAR_STOP_RELOAD_CUSTOMIZATION_LABEL)]; | |
| 245 } else if ([identifier isEqualTo:kHomeTouchId]) { | 261 } else if ([identifier isEqualTo:kHomeTouchId]) { |
| 246 [touchBarItem setView:CreateTouchBarButton(kNavigateHomeIcon, self, | 262 [touchBarItem setView:CreateTouchBarButton(kNavigateHomeIcon, self, |
| 247 IDC_HOME, IDS_TOOLTIP_HOME)]; | 263 IDC_HOME, IDS_TOOLTIP_HOME)]; |
| 264 [touchBarItem | |
| 265 setCustomizationLabel:l10n_util::GetNSString( | |
| 266 IDS_TOUCH_BAR_HOME_CUSTOMIZATION_LABEL)]; | |
| 248 } else if ([identifier isEqualTo:kNewTabTouchId]) { | 267 } else if ([identifier isEqualTo:kNewTabTouchId]) { |
| 249 [touchBarItem | 268 [touchBarItem |
| 250 setView:CreateTouchBarButton(kNewTabMacTouchbarIcon, self, IDC_NEW_TAB, | 269 setView:CreateTouchBarButton(kNewTabMacTouchbarIcon, self, IDC_NEW_TAB, |
| 251 IDS_TOOLTIP_NEW_TAB)]; | 270 IDS_TOOLTIP_NEW_TAB)]; |
| 271 [touchBarItem | |
| 272 setCustomizationLabel:l10n_util::GetNSString( | |
| 273 IDS_TOUCH_BAR_NEW_TAB_CUSTOMIZATION_LABEL)]; | |
| 252 } else if ([identifier isEqualTo:kStarTouchId]) { | 274 } else if ([identifier isEqualTo:kStarTouchId]) { |
| 253 const gfx::VectorIcon& icon = | 275 const gfx::VectorIcon& icon = |
| 254 isStarred_ ? toolbar::kStarActiveIcon : toolbar::kStarIcon; | 276 isStarred_ ? toolbar::kStarActiveIcon : toolbar::kStarIcon; |
| 255 SkColor iconColor = | 277 SkColor iconColor = |
| 256 isStarred_ ? kTouchBarStarActiveColor : kTouchBarDefaultIconColor; | 278 isStarred_ ? kTouchBarStarActiveColor : kTouchBarDefaultIconColor; |
| 257 int tooltipId = isStarred_ ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR; | 279 int tooltipId = isStarred_ ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR; |
| 258 [touchBarItem setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE, | 280 [touchBarItem setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE, |
| 259 tooltipId, iconColor)]; | 281 tooltipId, iconColor)]; |
| 282 [touchBarItem | |
| 283 setCustomizationLabel:l10n_util::GetNSString( | |
| 284 IDS_TOUCH_BAR_BOOKMARK_CUSTOMIZATION_LABEL)]; | |
| 260 } else if ([identifier isEqualTo:kSearchTouchId]) { | 285 } else if ([identifier isEqualTo:kSearchTouchId]) { |
| 261 [touchBarItem setView:[self searchTouchBarView]]; | 286 [touchBarItem setView:[self searchTouchBarView]]; |
| 287 [touchBarItem setCustomizationLabel:l10n_util::GetNSString( | |
| 288 IDS_TOUCH_BAR_GOOGLE_SEARCH)]; | |
| 262 } | 289 } |
| 263 | 290 |
| 264 return touchBarItem.autorelease(); | 291 return touchBarItem.autorelease(); |
| 265 } | 292 } |
| 266 | 293 |
| 267 - (NSView*)backOrForwardTouchBarView { | 294 - (NSView*)backOrForwardTouchBarView { |
| 268 NSArray* images = @[ | 295 NSArray* images = @[ |
| 269 CreateNSImageFromIcon(ui::kBackArrowIcon), | 296 CreateNSImageFromIcon(ui::kBackArrowIcon), |
| 270 CreateNSImageFromIcon(ui::kForwardArrowIcon) | 297 CreateNSImageFromIcon(ui::kForwardArrowIcon) |
| 271 ]; | 298 ]; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 image = CreateNSImageFromIcon(ui::kSearchIcon); | 349 image = CreateNSImageFromIcon(ui::kSearchIcon); |
| 323 } | 350 } |
| 324 | 351 |
| 325 NSButton* searchButton = | 352 NSButton* searchButton = |
| 326 [NSButton buttonWithTitle:base::SysUTF16ToNSString(title) | 353 [NSButton buttonWithTitle:base::SysUTF16ToNSString(title) |
| 327 image:image | 354 image:image |
| 328 target:self | 355 target:self |
| 329 action:@selector(executeCommand:)]; | 356 action:@selector(executeCommand:)]; |
| 330 searchButton.imageHugsTitle = YES; | 357 searchButton.imageHugsTitle = YES; |
| 331 searchButton.tag = IDC_FOCUS_LOCATION; | 358 searchButton.tag = IDC_FOCUS_LOCATION; |
| 332 int width = showHomeButton_.GetValue() ? kSearchBtnWidthWithHomeBtn | 359 [searchButton.widthAnchor |
| 333 : kSearchBtnWidthWithoutHomeBtn; | 360 constraintGreaterThanOrEqualToConstant:kSearchBtnMinWidth] |
| 334 [searchButton.widthAnchor constraintEqualToConstant:width].active = YES; | 361 .active = YES; |
| 362 [searchButton | |
| 363 setContentHuggingPriority:1.0 | |
| 364 forOrientation:NSLayoutConstraintOrientationHorizontal]; | |
| 335 return searchButton; | 365 return searchButton; |
| 336 } | 366 } |
| 337 | 367 |
| 338 - (void)backOrForward:(id)sender { | 368 - (void)backOrForward:(id)sender { |
| 339 NSSegmentedControl* control = sender; | 369 NSSegmentedControl* control = sender; |
| 340 int command = | 370 int command = |
| 341 [control selectedSegment] == kBackSegmentIndex ? IDC_BACK : IDC_FORWARD; | 371 [control selectedSegment] == kBackSegmentIndex ? IDC_BACK : IDC_FORWARD; |
| 342 LogTouchBarUMA(command); | 372 LogTouchBarUMA(command); |
| 343 commandUpdater_->ExecuteCommand(command); | 373 commandUpdater_->ExecuteCommand(command); |
| 344 } | 374 } |
| 345 | 375 |
| 346 - (void)executeCommand:(id)sender { | 376 - (void)executeCommand:(id)sender { |
| 347 int command = [sender tag]; | 377 int command = [sender tag]; |
| 348 LogTouchBarUMA(command); | 378 LogTouchBarUMA(command); |
| 349 commandUpdater_->ExecuteCommand(command); | 379 commandUpdater_->ExecuteCommand(command); |
| 350 } | 380 } |
| 351 | 381 |
| 352 @end | 382 @end |
| OLD | NEW |