| 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/foundation_util.h" | |
| 10 #include "base/mac/mac_util.h" | 9 #include "base/mac/mac_util.h" |
| 11 #import "base/mac/scoped_nsobject.h" | 10 #import "base/mac/scoped_nsobject.h" |
| 12 #import "base/mac/sdk_forward_declarations.h" | 11 #import "base/mac/sdk_forward_declarations.h" |
| 13 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 14 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
| 15 #include "chrome/app/chrome_command_ids.h" | 14 #include "chrome/app/chrome_command_ids.h" |
| 16 #include "chrome/app/vector_icons/vector_icons.h" | 15 #include "chrome/app/vector_icons/vector_icons.h" |
| 17 #include "chrome/browser/command_updater.h" | 16 #include "chrome/browser/command_updater.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/search_engines/template_url_service_factory.h" | 18 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 20 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
| 21 #include "chrome/browser/ui/browser_command_controller.h" | 20 #include "chrome/browser/ui/browser_command_controller.h" |
| 22 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 21 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| 23 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" | 22 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" |
| 24 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h" | 23 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h" |
| 25 #include "chrome/browser/ui/exclusive_access/fullscreen_controller.h" | 24 #include "chrome/browser/ui/exclusive_access/fullscreen_controller.h" |
| 26 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 25 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 27 #include "chrome/common/chrome_features.h" | 26 #include "chrome/common/chrome_features.h" |
| 28 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
| 29 #include "chrome/grit/generated_resources.h" | 28 #include "chrome/grit/generated_resources.h" |
| 30 #include "components/omnibox/browser/vector_icons.h" | 29 #include "components/omnibox/browser/vector_icons.h" |
| 31 #include "components/prefs/pref_member.h" | 30 #include "components/prefs/pref_member.h" |
| 32 #include "components/search_engines/util.h" | 31 #include "components/search_engines/util.h" |
| 33 #include "components/strings/grit/components_strings.h" | 32 #include "components/strings/grit/components_strings.h" |
| 34 #include "components/toolbar/vector_icons.h" | 33 #include "components/toolbar/vector_icons.h" |
| 35 #include "content/public/browser/web_contents.h" | 34 #include "content/public/browser/web_contents.h" |
| 35 #import "ui/base/cocoa/touch_bar_util.h" |
| 36 #include "ui/base/l10n/l10n_util.h" | 36 #include "ui/base/l10n/l10n_util.h" |
| 37 #include "ui/base/l10n/l10n_util_mac.h" | 37 #include "ui/base/l10n/l10n_util_mac.h" |
| 38 #include "ui/gfx/color_palette.h" | 38 #include "ui/gfx/color_palette.h" |
| 39 #include "ui/gfx/color_utils.h" | 39 #include "ui/gfx/color_utils.h" |
| 40 #include "ui/gfx/image/image.h" | 40 #include "ui/gfx/image/image.h" |
| 41 #include "ui/gfx/image/image_skia_util_mac.h" | 41 #include "ui/gfx/image/image_skia_util_mac.h" |
| 42 #include "ui/gfx/paint_vector_icon.h" | 42 #include "ui/gfx/paint_vector_icon.h" |
| 43 #include "ui/vector_icons/vector_icons.h" | 43 #include "ui/vector_icons/vector_icons.h" |
| 44 | 44 |
| 45 namespace { | 45 namespace { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 SkColor color = kTouchBarDefaultIconColor) { | 102 SkColor color = kTouchBarDefaultIconColor) { |
| 103 NSButton* button = | 103 NSButton* button = |
| 104 [NSButton buttonWithImage:CreateNSImageFromIcon(icon, color) | 104 [NSButton buttonWithImage:CreateNSImageFromIcon(icon, color) |
| 105 target:owner | 105 target:owner |
| 106 action:@selector(executeCommand:)]; | 106 action:@selector(executeCommand:)]; |
| 107 button.tag = command; | 107 button.tag = command; |
| 108 [button setAccessibilityLabel:l10n_util::GetNSString(tooltip_id)]; | 108 [button setAccessibilityLabel:l10n_util::GetNSString(tooltip_id)]; |
| 109 return button; | 109 return button; |
| 110 } | 110 } |
| 111 | 111 |
| 112 NSString* GetTouchBarId(NSString* touch_bar_id) { | |
| 113 NSString* chrome_bundle_id = | |
| 114 base::SysUTF8ToNSString(base::mac::BaseBundleID()); | |
| 115 return [NSString stringWithFormat:@"%@.%@", chrome_bundle_id, touch_bar_id]; | |
| 116 } | |
| 117 | |
| 118 TouchBarAction TouchBarActionFromCommand(int command) { | 112 TouchBarAction TouchBarActionFromCommand(int command) { |
| 119 switch (command) { | 113 switch (command) { |
| 120 case IDC_BACK: | 114 case IDC_BACK: |
| 121 return TouchBarAction::BACK; | 115 return TouchBarAction::BACK; |
| 122 case IDC_FORWARD: | 116 case IDC_FORWARD: |
| 123 return TouchBarAction::FORWARD; | 117 return TouchBarAction::FORWARD; |
| 124 case IDC_STOP: | 118 case IDC_STOP: |
| 125 return TouchBarAction::STOP; | 119 return TouchBarAction::STOP; |
| 126 case IDC_RELOAD: | 120 case IDC_RELOAD: |
| 127 return TouchBarAction::RELOAD; | 121 return TouchBarAction::RELOAD; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 | 183 |
| 190 // Creates and returns the search button. | 184 // Creates and returns the search button. |
| 191 - (NSView*)searchTouchBarView; | 185 - (NSView*)searchTouchBarView; |
| 192 @end | 186 @end |
| 193 | 187 |
| 194 @implementation BrowserWindowTouchBar | 188 @implementation BrowserWindowTouchBar |
| 195 | 189 |
| 196 @synthesize isPageLoading = isPageLoading_; | 190 @synthesize isPageLoading = isPageLoading_; |
| 197 @synthesize isStarred = isStarred_; | 191 @synthesize isStarred = isStarred_; |
| 198 | 192 |
| 199 + (NSString*)identifierForTouchBarId:(NSString*)touchBarId | |
| 200 itemId:(NSString*)itemId { | |
| 201 return | |
| 202 [NSString stringWithFormat:@"%@-%@", GetTouchBarId(touchBarId), itemId]; | |
| 203 } | |
| 204 | |
| 205 - (instancetype)initWithBrowser:(Browser*)browser | 193 - (instancetype)initWithBrowser:(Browser*)browser |
| 206 browserWindowController:(BrowserWindowController*)bwc { | 194 browserWindowController:(BrowserWindowController*)bwc { |
| 207 if ((self = [self init])) { | 195 if ((self = [self init])) { |
| 208 DCHECK(browser); | 196 DCHECK(browser); |
| 209 commandUpdater_ = browser->command_controller()->command_updater(); | 197 commandUpdater_ = browser->command_controller()->command_updater(); |
| 210 browser_ = browser; | 198 browser_ = browser; |
| 211 bwc_ = bwc; | 199 bwc_ = bwc; |
| 212 | 200 |
| 213 notificationBridge_.reset(new HomePrefNotificationBridge(bwc_)); | 201 notificationBridge_.reset(new HomePrefNotificationBridge(bwc_)); |
| 214 PrefService* prefs = browser->profile()->GetPrefs(); | 202 PrefService* prefs = browser->profile()->GetPrefs(); |
| 215 showHomeButton_.Init( | 203 showHomeButton_.Init( |
| 216 prefs::kShowHomeButton, prefs, | 204 prefs::kShowHomeButton, prefs, |
| 217 base::Bind(&HomePrefNotificationBridge::UpdateTouchBar, | 205 base::Bind(&HomePrefNotificationBridge::UpdateTouchBar, |
| 218 base::Unretained(notificationBridge_.get()))); | 206 base::Unretained(notificationBridge_.get()))); |
| 219 } | 207 } |
| 220 | 208 |
| 221 return self; | 209 return self; |
| 222 } | 210 } |
| 223 | 211 |
| 224 - (NSTouchBar*)makeTouchBar { | 212 - (NSTouchBar*)makeTouchBar { |
| 225 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar)) | 213 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar)) |
| 226 return nil; | 214 return nil; |
| 227 | 215 |
| 228 // When in tab fullscreen, we should show a touch bar containing only | 216 // When in tab fullscreen, we should show a touch bar containing only |
| 229 // items associated with that mode. Since the toolbar is hidden, only | 217 // items associated with that mode. Since the toolbar is hidden, only |
| 230 // the option to exit fullscreen should show up. | 218 // the option to exit fullscreen should show up. |
| 231 if ([bwc_ isFullscreenForTabContentOrExtension]) | 219 if ([bwc_ isFullscreenForTabContentOrExtension]) |
| 232 return [self createTabFullscreenTouchBar]; | 220 return [self createTabFullscreenTouchBar]; |
| 233 | 221 |
| 234 base::scoped_nsobject<NSTouchBar> touchBar( | 222 base::scoped_nsobject<NSTouchBar> touchBar([[ui::NSTouchBar() alloc] init]); |
| 235 [[NSClassFromString(@"NSTouchBar") alloc] init]); | 223 [touchBar |
| 236 [touchBar setCustomizationIdentifier:GetTouchBarId(kBrowserWindowTouchBarId)]; | 224 setCustomizationIdentifier:ui::GetTouchBarId(kBrowserWindowTouchBarId)]; |
| 237 [touchBar setDelegate:self]; | 225 [touchBar setDelegate:self]; |
| 238 | 226 |
| 239 NSMutableArray* customIdentifiers = [NSMutableArray arrayWithCapacity:7]; | 227 NSMutableArray* customIdentifiers = [NSMutableArray arrayWithCapacity:7]; |
| 240 NSMutableArray* defaultIdentifiers = [NSMutableArray arrayWithCapacity:6]; | 228 NSMutableArray* defaultIdentifiers = [NSMutableArray arrayWithCapacity:6]; |
| 241 | 229 |
| 242 NSArray* touchBarItems = @[ | 230 NSArray* touchBarItems = @[ |
| 243 kBackForwardTouchId, kReloadOrStopTouchId, kHomeTouchId, kSearchTouchId, | 231 kBackForwardTouchId, kReloadOrStopTouchId, kHomeTouchId, kSearchTouchId, |
| 244 kStarTouchId, kNewTabTouchId | 232 kStarTouchId, kNewTabTouchId |
| 245 ]; | 233 ]; |
| 246 | 234 |
| 247 for (NSString* item in touchBarItems) { | 235 for (NSString* item in touchBarItems) { |
| 248 NSString* itemIdentifier = | 236 NSString* itemIdentifier = |
| 249 [BrowserWindowTouchBar identifierForTouchBarId:kBrowserWindowTouchBarId | 237 ui::GetTouchBarItemId(kBrowserWindowTouchBarId, item); |
| 250 itemId:item]; | |
| 251 [customIdentifiers addObject:itemIdentifier]; | 238 [customIdentifiers addObject:itemIdentifier]; |
| 252 | 239 |
| 253 // Don't add the home button if it's not shown in the toolbar. | 240 // Don't add the home button if it's not shown in the toolbar. |
| 254 if (showHomeButton_.GetValue() || ![item isEqualTo:kHomeTouchId]) | 241 if (showHomeButton_.GetValue() || ![item isEqualTo:kHomeTouchId]) |
| 255 [defaultIdentifiers addObject:itemIdentifier]; | 242 [defaultIdentifiers addObject:itemIdentifier]; |
| 256 } | 243 } |
| 257 | 244 |
| 258 [customIdentifiers addObject:NSTouchBarItemIdentifierFlexibleSpace]; | 245 [customIdentifiers addObject:NSTouchBarItemIdentifierFlexibleSpace]; |
| 259 | 246 |
| 260 [touchBar setDefaultItemIdentifiers:defaultIdentifiers]; | 247 [touchBar setDefaultItemIdentifiers:defaultIdentifiers]; |
| 261 [touchBar setCustomizationAllowedItemIdentifiers:customIdentifiers]; | 248 [touchBar setCustomizationAllowedItemIdentifiers:customIdentifiers]; |
| 262 | 249 |
| 263 return touchBar.autorelease(); | 250 return touchBar.autorelease(); |
| 264 } | 251 } |
| 265 | 252 |
| 266 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar | 253 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar |
| 267 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { | 254 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { |
| 268 if (!touchBar) | 255 if (!touchBar) |
| 269 return nil; | 256 return nil; |
| 270 | 257 |
| 271 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString( | 258 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem( |
| 272 @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]); | 259 [[ui::NSCustomTouchBarItem() alloc] initWithIdentifier:identifier]); |
| 273 if ([identifier hasSuffix:kBackForwardTouchId]) { | 260 if ([identifier hasSuffix:kBackForwardTouchId]) { |
| 274 [touchBarItem setView:[self backOrForwardTouchBarView]]; | 261 [touchBarItem setView:[self backOrForwardTouchBarView]]; |
| 275 [touchBarItem setCustomizationLabel: | 262 [touchBarItem setCustomizationLabel: |
| 276 l10n_util::GetNSString( | 263 l10n_util::GetNSString( |
| 277 IDS_TOUCH_BAR_BACK_FORWARD_CUSTOMIZATION_LABEL)]; | 264 IDS_TOUCH_BAR_BACK_FORWARD_CUSTOMIZATION_LABEL)]; |
| 278 } else if ([identifier hasSuffix:kReloadOrStopTouchId]) { | 265 } else if ([identifier hasSuffix:kReloadOrStopTouchId]) { |
| 279 const gfx::VectorIcon& icon = | 266 const gfx::VectorIcon& icon = |
| 280 isPageLoading_ ? kNavigateStopIcon : kNavigateReloadIcon; | 267 isPageLoading_ ? kNavigateStopIcon : kNavigateReloadIcon; |
| 281 int commandId = isPageLoading_ ? IDC_STOP : IDC_RELOAD; | 268 int commandId = isPageLoading_ ? IDC_STOP : IDC_RELOAD; |
| 282 int tooltipId = isPageLoading_ ? IDS_TOOLTIP_STOP : IDS_TOOLTIP_RELOAD; | 269 int tooltipId = isPageLoading_ ? IDS_TOOLTIP_STOP : IDS_TOOLTIP_RELOAD; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 [touchBarItem | 322 [touchBarItem |
| 336 setView:[NSTextField labelWithAttributedString:attributedString.get()]]; | 323 setView:[NSTextField labelWithAttributedString:attributedString.get()]]; |
| 337 } else if ([identifier hasSuffix:kExitFullscreenTouchId]) { | 324 } else if ([identifier hasSuffix:kExitFullscreenTouchId]) { |
| 338 return nil; | 325 return nil; |
| 339 } | 326 } |
| 340 | 327 |
| 341 return touchBarItem.autorelease(); | 328 return touchBarItem.autorelease(); |
| 342 } | 329 } |
| 343 | 330 |
| 344 - (NSTouchBar*)createTabFullscreenTouchBar { | 331 - (NSTouchBar*)createTabFullscreenTouchBar { |
| 345 base::scoped_nsobject<NSTouchBar> touchBar( | 332 base::scoped_nsobject<NSTouchBar> touchBar([[ui::NSTouchBar() alloc] init]); |
| 346 [[NSClassFromString(@"NSTouchBar") alloc] init]); | |
| 347 [touchBar setDelegate:self]; | 333 [touchBar setDelegate:self]; |
| 348 | 334 |
| 349 if ([touchBar respondsToSelector: | 335 if ([touchBar respondsToSelector: |
| 350 @selector(setEscapeKeyReplacementItemIdentifier:)]) { | 336 @selector(setEscapeKeyReplacementItemIdentifier:)]) { |
| 351 NSString* exitIdentifier = | 337 NSString* exitIdentifier = |
| 352 [BrowserWindowTouchBar identifierForTouchBarId:kTabFullscreenTouchBarId | 338 ui::GetTouchBarItemId(kTabFullscreenTouchBarId, kExitFullscreenTouchId); |
| 353 itemId:kExitFullscreenTouchId]; | |
| 354 [touchBar setEscapeKeyReplacementItemIdentifier:exitIdentifier]; | 339 [touchBar setEscapeKeyReplacementItemIdentifier:exitIdentifier]; |
| 355 [touchBar setDefaultItemIdentifiers:@[ | 340 [touchBar setDefaultItemIdentifiers:@[ ui::GetTouchBarItemId( |
| 356 [BrowserWindowTouchBar | 341 kTabFullscreenTouchBarId, |
| 357 identifierForTouchBarId:kTabFullscreenTouchBarId | 342 kFullscreenOriginLabelTouchId) ]]; |
| 358 itemId:kFullscreenOriginLabelTouchId] | |
| 359 ]]; | |
| 360 | 343 |
| 361 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem( | 344 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem( |
| 362 [[NSClassFromString(@"NSCustomTouchBarItem") alloc] | 345 [[ui::NSCustomTouchBarItem() alloc] initWithIdentifier:exitIdentifier]); |
| 363 initWithIdentifier:exitIdentifier]); | |
| 364 | 346 |
| 365 [touchBarItem | 347 [touchBarItem |
| 366 setView:[NSButton buttonWithTitle:l10n_util::GetNSString( | 348 setView:[NSButton buttonWithTitle:l10n_util::GetNSString( |
| 367 IDS_TOUCH_BAR_EXIT_FULLSCREEN) | 349 IDS_TOUCH_BAR_EXIT_FULLSCREEN) |
| 368 target:self | 350 target:self |
| 369 action:@selector(exitFullscreenForTab:)]]; | 351 action:@selector(exitFullscreenForTab:)]]; |
| 370 [touchBar | 352 [touchBar |
| 371 setTemplateItems:[NSSet setWithObject:touchBarItem.autorelease()]]; | 353 setTemplateItems:[NSSet setWithObject:touchBarItem.autorelease()]]; |
| 372 } | 354 } |
| 373 | 355 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 ->ExitExclusiveAccessIfNecessary(); | 444 ->ExitExclusiveAccessIfNecessary(); |
| 463 } | 445 } |
| 464 | 446 |
| 465 - (void)executeCommand:(id)sender { | 447 - (void)executeCommand:(id)sender { |
| 466 int command = [sender tag]; | 448 int command = [sender tag]; |
| 467 LogTouchBarUMA(command); | 449 LogTouchBarUMA(command); |
| 468 commandUpdater_->ExecuteCommand(command); | 450 commandUpdater_->ExecuteCommand(command); |
| 469 } | 451 } |
| 470 | 452 |
| 471 @end | 453 @end |
| OLD | NEW |