| 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" | 9 #include "base/mac/foundation_util.h" |
| 10 #include "base/mac/mac_util.h" | 10 #include "base/mac/mac_util.h" |
| 11 #import "base/mac/scoped_nsobject.h" | 11 #import "base/mac/scoped_nsobject.h" |
| 12 #import "base/mac/sdk_forward_declarations.h" | 12 #import "base/mac/sdk_forward_declarations.h" |
| 13 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 14 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
| 15 #include "chrome/app/chrome_command_ids.h" | 15 #include "chrome/app/chrome_command_ids.h" |
| 16 #include "chrome/app/vector_icons/vector_icons.h" | 16 #include "chrome/app/vector_icons/vector_icons.h" |
| 17 #include "chrome/browser/command_updater.h" | 17 #include "chrome/browser/command_updater.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/search_engines/template_url_service_factory.h" | 19 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 20 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
| 21 #include "chrome/browser/ui/browser_command_controller.h" | 21 #include "chrome/browser/ui/browser_command_controller.h" |
| 22 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 22 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| 23 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" |
| 24 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h" |
| 25 #include "chrome/browser/ui/exclusive_access/fullscreen_controller.h" |
| 26 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 23 #include "chrome/common/chrome_features.h" | 27 #include "chrome/common/chrome_features.h" |
| 24 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
| 25 #include "chrome/grit/generated_resources.h" | 29 #include "chrome/grit/generated_resources.h" |
| 26 #include "components/omnibox/browser/vector_icons.h" | 30 #include "components/omnibox/browser/vector_icons.h" |
| 27 #include "components/prefs/pref_member.h" | 31 #include "components/prefs/pref_member.h" |
| 28 #include "components/search_engines/util.h" | 32 #include "components/search_engines/util.h" |
| 29 #include "components/strings/grit/components_strings.h" | 33 #include "components/strings/grit/components_strings.h" |
| 30 #include "components/toolbar/vector_icons.h" | 34 #include "components/toolbar/vector_icons.h" |
| 35 #include "content/public/browser/web_contents.h" |
| 31 #include "ui/base/l10n/l10n_util.h" | 36 #include "ui/base/l10n/l10n_util.h" |
| 32 #include "ui/base/l10n/l10n_util_mac.h" | 37 #include "ui/base/l10n/l10n_util_mac.h" |
| 33 #include "ui/gfx/color_palette.h" | 38 #include "ui/gfx/color_palette.h" |
| 34 #include "ui/gfx/color_utils.h" | 39 #include "ui/gfx/color_utils.h" |
| 35 #include "ui/gfx/image/image.h" | 40 #include "ui/gfx/image/image.h" |
| 36 #include "ui/gfx/image/image_skia_util_mac.h" | 41 #include "ui/gfx/image/image_skia_util_mac.h" |
| 37 #include "ui/gfx/paint_vector_icon.h" | 42 #include "ui/gfx/paint_vector_icon.h" |
| 38 #include "ui/vector_icons/vector_icons.h" | 43 #include "ui/vector_icons/vector_icons.h" |
| 39 | 44 |
| 40 namespace { | 45 namespace { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 56 // The touch bar's identifier. | 61 // The touch bar's identifier. |
| 57 NSString* const kBrowserWindowTouchBarId = @"browser-window"; | 62 NSString* const kBrowserWindowTouchBarId = @"browser-window"; |
| 58 | 63 |
| 59 // Touch bar items identifiers. | 64 // Touch bar items identifiers. |
| 60 NSString* const kBackForwardTouchId = @"BACK-FWD"; | 65 NSString* const kBackForwardTouchId = @"BACK-FWD"; |
| 61 NSString* const kReloadOrStopTouchId = @"RELOAD-STOP"; | 66 NSString* const kReloadOrStopTouchId = @"RELOAD-STOP"; |
| 62 NSString* const kHomeTouchId = @"HOME"; | 67 NSString* const kHomeTouchId = @"HOME"; |
| 63 NSString* const kSearchTouchId = @"SEARCH"; | 68 NSString* const kSearchTouchId = @"SEARCH"; |
| 64 NSString* const kStarTouchId = @"BOOKMARK"; | 69 NSString* const kStarTouchId = @"BOOKMARK"; |
| 65 NSString* const kNewTabTouchId = @"NEW-TAB"; | 70 NSString* const kNewTabTouchId = @"NEW-TAB"; |
| 71 NSString* const kExitFullscreenTouchId = @"EXIT-FULLSCREEN"; |
| 72 NSString* const kFullscreenOriginLabelTouchId = @"FULLSCREEN-ORIGIN-LABEL"; |
| 66 | 73 |
| 67 // The button indexes in the back and forward segment control. | 74 // The button indexes in the back and forward segment control. |
| 68 const int kBackSegmentIndex = 0; | 75 const int kBackSegmentIndex = 0; |
| 69 const int kForwardSegmentIndex = 1; | 76 const int kForwardSegmentIndex = 1; |
| 70 | 77 |
| 71 // Touch bar icon colors values. | 78 // Touch bar icon colors values. |
| 72 const SkColor kTouchBarDefaultIconColor = SK_ColorWHITE; | 79 const SkColor kTouchBarDefaultIconColor = SK_ColorWHITE; |
| 73 const SkColor kTouchBarStarActiveColor = gfx::kGoogleBlue500; | 80 const SkColor kTouchBarStarActiveColor = gfx::kGoogleBlue500; |
| 74 | 81 |
| 75 // The size of the touch bar icons. | 82 // The size of the touch bar icons. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 94 SkColor color = kTouchBarDefaultIconColor) { | 101 SkColor color = kTouchBarDefaultIconColor) { |
| 95 NSButton* button = | 102 NSButton* button = |
| 96 [NSButton buttonWithImage:CreateNSImageFromIcon(icon, color) | 103 [NSButton buttonWithImage:CreateNSImageFromIcon(icon, color) |
| 97 target:owner | 104 target:owner |
| 98 action:@selector(executeCommand:)]; | 105 action:@selector(executeCommand:)]; |
| 99 button.tag = command; | 106 button.tag = command; |
| 100 [button setAccessibilityLabel:l10n_util::GetNSString(tooltip_id)]; | 107 [button setAccessibilityLabel:l10n_util::GetNSString(tooltip_id)]; |
| 101 return button; | 108 return button; |
| 102 } | 109 } |
| 103 | 110 |
| 104 NSString* GetTouchBarId(NSString* const touch_bar_id) { | 111 NSString* GetTouchBarId() { |
| 105 NSString* chrome_bundle_id = | 112 NSString* chrome_bundle_id = |
| 106 base::SysUTF8ToNSString(base::mac::BaseBundleID()); | 113 base::SysUTF8ToNSString(base::mac::BaseBundleID()); |
| 107 return [NSString stringWithFormat:@"%@.%@", chrome_bundle_id, touch_bar_id]; | |
| 108 } | |
| 109 | |
| 110 NSString* GetTouchBarItemId(NSString* const touch_bar_id, | |
| 111 NSString* const item_id) { | |
| 112 return [NSString | 114 return [NSString |
| 113 stringWithFormat:@"%@-%@", GetTouchBarId(touch_bar_id), item_id]; | 115 stringWithFormat:@"%@.%@", chrome_bundle_id, kBrowserWindowTouchBarId]; |
| 114 } | 116 } |
| 115 | 117 |
| 116 TouchBarAction TouchBarActionFromCommand(int command) { | 118 TouchBarAction TouchBarActionFromCommand(int command) { |
| 117 switch (command) { | 119 switch (command) { |
| 118 case IDC_BACK: | 120 case IDC_BACK: |
| 119 return TouchBarAction::BACK; | 121 return TouchBarAction::BACK; |
| 120 case IDC_FORWARD: | 122 case IDC_FORWARD: |
| 121 return TouchBarAction::FORWARD; | 123 return TouchBarAction::FORWARD; |
| 122 case IDC_STOP: | 124 case IDC_STOP: |
| 123 return TouchBarAction::STOP; | 125 return TouchBarAction::STOP; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 | 186 |
| 185 // Creates and returns the search button. | 187 // Creates and returns the search button. |
| 186 - (NSView*)searchTouchBarView; | 188 - (NSView*)searchTouchBarView; |
| 187 @end | 189 @end |
| 188 | 190 |
| 189 @implementation BrowserWindowTouchBar | 191 @implementation BrowserWindowTouchBar |
| 190 | 192 |
| 191 @synthesize isPageLoading = isPageLoading_; | 193 @synthesize isPageLoading = isPageLoading_; |
| 192 @synthesize isStarred = isStarred_; | 194 @synthesize isStarred = isStarred_; |
| 193 | 195 |
| 196 + (NSString*)touchBarIdForItemId:(NSString*)id { |
| 197 return [NSString stringWithFormat:@"%@-%@", GetTouchBarId(), id]; |
| 198 } |
| 199 |
| 194 - (instancetype)initWithBrowser:(Browser*)browser | 200 - (instancetype)initWithBrowser:(Browser*)browser |
| 195 browserWindowController:(BrowserWindowController*)bwc { | 201 browserWindowController:(BrowserWindowController*)bwc { |
| 196 if ((self = [self init])) { | 202 if ((self = [self init])) { |
| 197 DCHECK(browser); | 203 DCHECK(browser); |
| 198 commandUpdater_ = browser->command_controller()->command_updater(); | 204 commandUpdater_ = browser->command_controller()->command_updater(); |
| 199 browser_ = browser; | 205 browser_ = browser; |
| 200 bwc_ = bwc; | 206 bwc_ = bwc; |
| 201 | 207 |
| 202 notificationBridge_.reset(new HomePrefNotificationBridge(bwc_)); | 208 notificationBridge_.reset(new HomePrefNotificationBridge(bwc_)); |
| 203 PrefService* prefs = browser->profile()->GetPrefs(); | 209 PrefService* prefs = browser->profile()->GetPrefs(); |
| 204 showHomeButton_.Init( | 210 showHomeButton_.Init( |
| 205 prefs::kShowHomeButton, prefs, | 211 prefs::kShowHomeButton, prefs, |
| 206 base::Bind(&HomePrefNotificationBridge::UpdateTouchBar, | 212 base::Bind(&HomePrefNotificationBridge::UpdateTouchBar, |
| 207 base::Unretained(notificationBridge_.get()))); | 213 base::Unretained(notificationBridge_.get()))); |
| 208 } | 214 } |
| 209 | 215 |
| 210 return self; | 216 return self; |
| 211 } | 217 } |
| 212 | 218 |
| 213 - (NSTouchBar*)makeTouchBar { | 219 - (NSTouchBar*)makeTouchBar { |
| 214 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar)) | 220 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar)) |
| 215 return nil; | 221 return nil; |
| 216 | 222 |
| 217 base::scoped_nsobject<NSTouchBar> touchBar( | 223 base::scoped_nsobject<NSTouchBar> touchBar( |
| 218 [[NSClassFromString(@"NSTouchBar") alloc] init]); | 224 [[NSClassFromString(@"NSTouchBar") alloc] init]); |
| 225 [touchBar setCustomizationIdentifier:GetTouchBarId()]; |
| 226 [touchBar setDelegate:self]; |
| 227 |
| 228 // When in tab fullscreen, only the option to exit fullscreen should show up |
| 229 // on the touch bar since the toolbar is hidden in that state. |
| 230 if ([bwc_ isFullscreenForTabContentOrExtension]) { |
| 231 if ([touchBar respondsToSelector: |
| 232 @selector(setEscapeKeyReplacementItemIdentifier:)]) { |
| 233 [touchBar setEscapeKeyReplacementItemIdentifier: |
| 234 [BrowserWindowTouchBar |
| 235 touchBarIdForItemId:kExitFullscreenTouchId]]; |
| 236 [touchBar setDefaultItemIdentifiers:@[ |
| 237 [BrowserWindowTouchBar |
| 238 touchBarIdForItemId:kFullscreenOriginLabelTouchId] |
| 239 ]]; |
| 240 } |
| 241 return touchBar.autorelease(); |
| 242 } |
| 243 |
| 219 NSMutableArray* customIdentifiers = [NSMutableArray arrayWithCapacity:7]; | 244 NSMutableArray* customIdentifiers = [NSMutableArray arrayWithCapacity:7]; |
| 220 NSMutableArray* defaultIdentifiers = [NSMutableArray arrayWithCapacity:6]; | 245 NSMutableArray* defaultIdentifiers = [NSMutableArray arrayWithCapacity:6]; |
| 221 | 246 |
| 222 NSArray* touchBarItems = @[ | 247 NSArray* touchBarItems = @[ |
| 223 kBackForwardTouchId, kReloadOrStopTouchId, kHomeTouchId, kSearchTouchId, | 248 kBackForwardTouchId, kReloadOrStopTouchId, kHomeTouchId, kSearchTouchId, |
| 224 kStarTouchId, kNewTabTouchId | 249 kStarTouchId, kNewTabTouchId |
| 225 ]; | 250 ]; |
| 226 | 251 |
| 227 for (NSString* item in touchBarItems) { | 252 for (NSString* item in touchBarItems) { |
| 228 NSString* itemIdentifier = | 253 NSString* itemIdentifier = [BrowserWindowTouchBar touchBarIdForItemId:item]; |
| 229 GetTouchBarItemId(kBrowserWindowTouchBarId, item); | |
| 230 [customIdentifiers addObject:itemIdentifier]; | 254 [customIdentifiers addObject:itemIdentifier]; |
| 231 | 255 |
| 232 // Don't add the home button if it's not shown in the toolbar. | 256 // Don't add the home button if it's not shown in the toolbar. |
| 233 if (showHomeButton_.GetValue() || ![item isEqualTo:kHomeTouchId]) | 257 if (showHomeButton_.GetValue() || ![item isEqualTo:kHomeTouchId]) |
| 234 [defaultIdentifiers addObject:itemIdentifier]; | 258 [defaultIdentifiers addObject:itemIdentifier]; |
| 235 } | 259 } |
| 236 | 260 |
| 237 [customIdentifiers addObject:NSTouchBarItemIdentifierFlexibleSpace]; | 261 [customIdentifiers addObject:NSTouchBarItemIdentifierFlexibleSpace]; |
| 238 | 262 |
| 239 [touchBar setCustomizationIdentifier:GetTouchBarId(kBrowserWindowTouchBarId)]; | |
| 240 [touchBar setDefaultItemIdentifiers:defaultIdentifiers]; | 263 [touchBar setDefaultItemIdentifiers:defaultIdentifiers]; |
| 241 [touchBar setCustomizationAllowedItemIdentifiers:customIdentifiers]; | 264 [touchBar setCustomizationAllowedItemIdentifiers:customIdentifiers]; |
| 242 [touchBar setDelegate:self]; | |
| 243 | 265 |
| 244 return touchBar.autorelease(); | 266 return touchBar.autorelease(); |
| 245 } | 267 } |
| 246 | 268 |
| 247 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar | 269 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar |
| 248 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { | 270 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { |
| 249 if (!touchBar) | 271 if (!touchBar) |
| 250 return nil; | 272 return nil; |
| 251 | 273 |
| 252 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString( | 274 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString( |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 int tooltipId = isStarred_ ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR; | 309 int tooltipId = isStarred_ ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR; |
| 288 [touchBarItem setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE, | 310 [touchBarItem setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE, |
| 289 tooltipId, iconColor)]; | 311 tooltipId, iconColor)]; |
| 290 [touchBarItem | 312 [touchBarItem |
| 291 setCustomizationLabel:l10n_util::GetNSString( | 313 setCustomizationLabel:l10n_util::GetNSString( |
| 292 IDS_TOUCH_BAR_BOOKMARK_CUSTOMIZATION_LABEL)]; | 314 IDS_TOUCH_BAR_BOOKMARK_CUSTOMIZATION_LABEL)]; |
| 293 } else if ([identifier hasSuffix:kSearchTouchId]) { | 315 } else if ([identifier hasSuffix:kSearchTouchId]) { |
| 294 [touchBarItem setView:[self searchTouchBarView]]; | 316 [touchBarItem setView:[self searchTouchBarView]]; |
| 295 [touchBarItem setCustomizationLabel:l10n_util::GetNSString( | 317 [touchBarItem setCustomizationLabel:l10n_util::GetNSString( |
| 296 IDS_TOUCH_BAR_GOOGLE_SEARCH)]; | 318 IDS_TOUCH_BAR_GOOGLE_SEARCH)]; |
| 319 } else if ([identifier hasSuffix:kExitFullscreenTouchId]) { |
| 320 NSButton* button = [NSButton |
| 321 buttonWithTitle:l10n_util::GetNSString(IDS_TOUCH_BAR_EXIT_FULLSCREEN) |
| 322 target:self |
| 323 action:@selector(exitFullscreenForTab:)]; |
| 324 [touchBarItem setView:button]; |
| 325 } else if ([identifier hasSuffix:kFullscreenOriginLabelTouchId]) { |
| 326 content::WebContents* contents = |
| 327 browser_->tab_strip_model()->GetActiveWebContents(); |
| 328 GURL originUrl = contents->GetLastCommittedURL(); |
| 329 |
| 330 base::string16 displayText = base::ASCIIToUTF16(originUrl.GetContent()); |
| 331 size_t hostLength = originUrl.host().length(); |
| 332 base::scoped_nsobject<NSMutableAttributedString> attributedString( |
| 333 [[NSMutableAttributedString alloc] |
| 334 initWithString:base::SysUTF16ToNSString(displayText)]); |
| 335 [attributedString |
| 336 addAttribute:NSForegroundColorAttributeName |
| 337 value:OmniboxViewMac::BaseTextColor(true) |
| 338 range:NSMakeRange(hostLength, |
| 339 [attributedString length] - hostLength)]; |
| 340 [touchBarItem |
| 341 setView:[NSTextField labelWithAttributedString:attributedString.get()]]; |
| 297 } | 342 } |
| 298 | 343 |
| 299 return touchBarItem.autorelease(); | 344 return touchBarItem.autorelease(); |
| 300 } | 345 } |
| 301 | 346 |
| 302 - (NSView*)backOrForwardTouchBarView { | 347 - (NSView*)backOrForwardTouchBarView { |
| 303 NSArray* images = @[ | 348 NSArray* images = @[ |
| 304 CreateNSImageFromIcon(ui::kBackArrowIcon), | 349 CreateNSImageFromIcon(ui::kBackArrowIcon), |
| 305 CreateNSImageFromIcon(ui::kForwardArrowIcon) | 350 CreateNSImageFromIcon(ui::kForwardArrowIcon) |
| 306 ]; | 351 ]; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 } | 419 } |
| 375 | 420 |
| 376 - (void)backOrForward:(id)sender { | 421 - (void)backOrForward:(id)sender { |
| 377 NSSegmentedControl* control = sender; | 422 NSSegmentedControl* control = sender; |
| 378 int command = | 423 int command = |
| 379 [control selectedSegment] == kBackSegmentIndex ? IDC_BACK : IDC_FORWARD; | 424 [control selectedSegment] == kBackSegmentIndex ? IDC_BACK : IDC_FORWARD; |
| 380 LogTouchBarUMA(command); | 425 LogTouchBarUMA(command); |
| 381 commandUpdater_->ExecuteCommand(command); | 426 commandUpdater_->ExecuteCommand(command); |
| 382 } | 427 } |
| 383 | 428 |
| 429 - (void)exitFullscreenForTab:(id)sender { |
| 430 browser_->exclusive_access_manager() |
| 431 ->fullscreen_controller() |
| 432 ->ExitExclusiveAccessIfNecessary(); |
| 433 } |
| 434 |
| 384 - (void)executeCommand:(id)sender { | 435 - (void)executeCommand:(id)sender { |
| 385 int command = [sender tag]; | 436 int command = [sender tag]; |
| 386 LogTouchBarUMA(command); | 437 LogTouchBarUMA(command); |
| 387 commandUpdater_->ExecuteCommand(command); | 438 commandUpdater_->ExecuteCommand(command); |
| 388 } | 439 } |
| 389 | 440 |
| 390 @end | 441 @end |
| OLD | NEW |