| 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 "base/mac/mac_util.h" | 7 #include "base/mac/mac_util.h" |
| 8 #import "base/mac/scoped_nsobject.h" | 8 #import "base/mac/scoped_nsobject.h" |
| 9 #import "base/mac/sdk_forward_declarations.h" | 9 #import "base/mac/sdk_forward_declarations.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| 11 #include "chrome/app/chrome_command_ids.h" | 11 #include "chrome/app/chrome_command_ids.h" |
| 12 #include "chrome/app/vector_icons/vector_icons.h" | 12 #include "chrome/app/vector_icons/vector_icons.h" |
| 13 #include "chrome/browser/command_updater.h" | 13 #include "chrome/browser/command_updater.h" |
| 14 #include "chrome/browser/search_engines/template_url_service_factory.h" | 14 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/browser_command_controller.h" | 16 #include "chrome/browser/ui/browser_command_controller.h" |
| 17 #include "chrome/common/chrome_features.h" | 17 #include "chrome/common/chrome_features.h" |
| 18 #include "chrome/grit/generated_resources.h" | 18 #include "chrome/grit/generated_resources.h" |
| 19 #include "components/omnibox/browser/vector_icons.h" | 19 #include "components/omnibox/browser/vector_icons.h" |
| 20 #include "components/search_engines/util.h" | 20 #include "components/search_engines/util.h" |
| 21 #include "components/toolbar/vector_icons.h" | 21 #include "components/toolbar/vector_icons.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "ui/gfx/color_palette.h" | 23 #include "ui/gfx/color_palette.h" |
| 24 #include "ui/gfx/color_utils.h" | 24 #include "ui/gfx/color_utils.h" |
| 25 #include "ui/gfx/image/image.h" | 25 #include "ui/gfx/image/image.h" |
| 26 #include "ui/gfx/image/image_skia_util_mac.h" | 26 #include "ui/gfx/image/image_skia_util_mac.h" |
| 27 #include "ui/gfx/paint_vector_icon.h" | 27 #include "ui/gfx/paint_vector_icon.h" |
| 28 #include "ui/native_theme/native_theme.h" | 28 #include "ui/gfx/vector_icons_public.h" |
| 29 #include "ui/vector_icons/vector_icons.h" | 29 #include "ui/vector_icons/vector_icons.h" |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 // The touch bar's identifier. | 33 // The touch bar's identifier. |
| 34 const NSTouchBarCustomizationIdentifier kBrowserWindowTouchBarId = | 34 const NSTouchBarCustomizationIdentifier kBrowserWindowTouchBarId = |
| 35 @"BrowserWindowTouchBarId"; | 35 @"BrowserWindowTouchBarId"; |
| 36 | 36 |
| 37 // Touch bar items identifiers. | 37 // Touch bar items identifiers. |
| 38 const NSTouchBarItemIdentifier kBackForwardTouchId = @"BackForwardTouchId"; | 38 const NSTouchBarItemIdentifier kBackForwardTouchId = @"BackForwardTouchId"; |
| 39 const NSTouchBarItemIdentifier kReloadOrStopTouchId = @"ReloadOrStopTouchId"; | 39 const NSTouchBarItemIdentifier kReloadOrStopTouchId = @"ReloadOrStopTouchId"; |
| 40 const NSTouchBarItemIdentifier kSearchTouchId = @"SearchTouchId"; | 40 const NSTouchBarItemIdentifier kSearchTouchId = @"SearchTouchId"; |
| 41 const NSTouchBarItemIdentifier kStarTouchId = @"StarTouchId"; |
| 41 const NSTouchBarItemIdentifier kNewTabTouchId = @"NewTabTouchId"; | 42 const NSTouchBarItemIdentifier kNewTabTouchId = @"NewTabTouchId"; |
| 42 const NSTouchBarItemIdentifier kStarTouchId = @"StarTouchId"; | |
| 43 | 43 |
| 44 // The button indexes in the back and forward segment control. | 44 // The button indexes in the back and forward segment control. |
| 45 const int kBackSegmentIndex = 0; | 45 const int kBackSegmentIndex = 0; |
| 46 const int kForwardSegmentIndex = 1; | 46 const int kForwardSegmentIndex = 1; |
| 47 | 47 |
| 48 // Touch bar icon colors values. | 48 // Touch bar icon colors values. |
| 49 const SkColor kTouchBarDefaultIconColor = SK_ColorWHITE; | 49 const SkColor kTouchBarDefaultIconColor = SK_ColorWHITE; |
| 50 const SkColor kTouchBarStarActiveColor = gfx::kGoogleBlue500; |
| 50 | 51 |
| 51 // The size of the touch bar icons. | 52 // The size of the touch bar icons. |
| 52 const int kTouchBarIconSize = 16; | 53 const int kTouchBarIconSize = 16; |
| 53 | 54 |
| 54 // The width of the search button in the touch bar. | 55 // The width of the search button in the touch bar. |
| 55 const int kTouchBarSearchButtonWidth = 280; | 56 const int kTouchBarSearchButtonWidth = 280; |
| 56 | 57 |
| 57 // Creates an NSImage from the given VectorIcon. | 58 // Creates an NSImage from the given VectorIcon. |
| 58 NSImage* CreateNSImageFromIcon(const gfx::VectorIcon& icon, | 59 NSImage* CreateNSImageFromIcon(const gfx::VectorIcon& icon, |
| 59 SkColor color = kTouchBarDefaultIconColor) { | 60 SkColor color = kTouchBarDefaultIconColor) { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 return self; | 108 return self; |
| 108 } | 109 } |
| 109 | 110 |
| 110 - (NSTouchBar*)makeTouchBar { | 111 - (NSTouchBar*)makeTouchBar { |
| 111 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar)) | 112 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar)) |
| 112 return nil; | 113 return nil; |
| 113 | 114 |
| 114 base::scoped_nsobject<NSTouchBar> touchBar( | 115 base::scoped_nsobject<NSTouchBar> touchBar( |
| 115 [[NSClassFromString(@"NSTouchBar") alloc] init]); | 116 [[NSClassFromString(@"NSTouchBar") alloc] init]); |
| 116 NSArray* touchBarItemIdentifiers = @[ | 117 NSArray* touchBarItemIdentifiers = @[ |
| 117 kBackForwardTouchId, kReloadOrStopTouchId, kSearchTouchId, kNewTabTouchId, | 118 kBackForwardTouchId, kReloadOrStopTouchId, kSearchTouchId, kStarTouchId, |
| 118 kStarTouchId | 119 kNewTabTouchId |
| 119 ]; | 120 ]; |
| 120 [touchBar setCustomizationIdentifier:kBrowserWindowTouchBarId]; | 121 [touchBar setCustomizationIdentifier:kBrowserWindowTouchBarId]; |
| 121 [touchBar setDefaultItemIdentifiers:touchBarItemIdentifiers]; | 122 [touchBar setDefaultItemIdentifiers:touchBarItemIdentifiers]; |
| 122 [touchBar setCustomizationAllowedItemIdentifiers:touchBarItemIdentifiers]; | 123 [touchBar setCustomizationAllowedItemIdentifiers:touchBarItemIdentifiers]; |
| 123 [touchBar setDelegate:self]; | 124 [touchBar setDelegate:self]; |
| 124 | 125 |
| 125 return touchBar.autorelease(); | 126 return touchBar.autorelease(); |
| 126 } | 127 } |
| 127 | 128 |
| 128 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar | 129 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar |
| 129 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { | 130 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { |
| 130 if (!touchBar) | 131 if (!touchBar) |
| 131 return nil; | 132 return nil; |
| 132 | 133 |
| 133 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString( | 134 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString( |
| 134 @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]); | 135 @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]); |
| 135 if ([identifier isEqualTo:kBackForwardTouchId]) { | 136 if ([identifier isEqualTo:kBackForwardTouchId]) { |
| 136 [touchBarItem setView:[self backOrForwardTouchBarView]]; | 137 [touchBarItem setView:[self backOrForwardTouchBarView]]; |
| 137 } else if ([identifier isEqualTo:kReloadOrStopTouchId]) { | 138 } else if ([identifier isEqualTo:kReloadOrStopTouchId]) { |
| 138 const gfx::VectorIcon& icon = | 139 const gfx::VectorIcon& icon = |
| 139 isPageLoading_ ? kNavigateStopIcon : kNavigateReloadIcon; | 140 isPageLoading_ ? kNavigateStopIcon : kNavigateReloadIcon; |
| 140 int command_id = isPageLoading_ ? IDC_STOP : IDC_RELOAD; | 141 int command_id = isPageLoading_ ? IDC_STOP : IDC_RELOAD; |
| 141 [touchBarItem setView:CreateTouchBarButton(icon, self, command_id)]; | 142 [touchBarItem setView:CreateTouchBarButton(icon, self, command_id)]; |
| 142 } else if ([identifier isEqualTo:kNewTabTouchId]) { | 143 } else if ([identifier isEqualTo:kNewTabTouchId]) { |
| 143 [touchBarItem setView:CreateTouchBarButton(kNewTabMacTouchbarIcon, self, | 144 [touchBarItem setView:CreateTouchBarButton(kNewTabMacTouchbarIcon, self, |
| 144 IDC_NEW_TAB)]; | 145 IDC_NEW_TAB)]; |
| 145 } else if ([identifier isEqualTo:kStarTouchId]) { | 146 } else if ([identifier isEqualTo:kStarTouchId]) { |
| 146 const SkColor kStarredIconColor = | |
| 147 ui::NativeTheme::GetInstanceForNativeUi()->GetSystemColor( | |
| 148 ui::NativeTheme::kColorId_ProminentButtonColor); | |
| 149 const gfx::VectorIcon& icon = | 147 const gfx::VectorIcon& icon = |
| 150 isStarred_ ? toolbar::kStarActiveIcon : toolbar::kStarIcon; | 148 isStarred_ ? toolbar::kStarActiveIcon : toolbar::kStarIcon; |
| 151 SkColor iconColor = | 149 SkColor iconColor = |
| 152 isStarred_ ? kStarredIconColor : kTouchBarDefaultIconColor; | 150 isStarred_ ? kTouchBarStarActiveColor : kTouchBarDefaultIconColor; |
| 153 [touchBarItem | 151 [touchBarItem |
| 154 setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE, iconColor)]; | 152 setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE, iconColor)]; |
| 155 } else if ([identifier isEqualTo:kSearchTouchId]) { | 153 } else if ([identifier isEqualTo:kSearchTouchId]) { |
| 156 [touchBarItem setView:[self searchTouchBarView]]; | 154 [touchBarItem setView:[self searchTouchBarView]]; |
| 157 } | 155 } |
| 158 | 156 |
| 159 return touchBarItem.autorelease(); | 157 return touchBarItem.autorelease(); |
| 160 } | 158 } |
| 161 | 159 |
| 162 - (NSView*)backOrForwardTouchBarView { | 160 - (NSView*)backOrForwardTouchBarView { |
| 163 NSArray* images = @[ | 161 NSArray* images = @[ |
| 164 CreateNSImageFromIcon(ui::kBackArrowIcon), | 162 CreateNSImageFromIcon(ui::kBackArrowIcon), |
| 165 CreateNSImageFromIcon(ui::kForwardArrowIcon) | 163 CreateNSImageFromIcon(ui::kForwardArrowIcon) |
| 166 ]; | 164 ]; |
| 167 | 165 |
| 168 NSSegmentedControl* control = [NSSegmentedControl | 166 NSSegmentedControl* control = [NSSegmentedControl |
| 169 segmentedControlWithImages:images | 167 segmentedControlWithImages:images |
| 170 trackingMode:NSSegmentSwitchTrackingMomentary | 168 trackingMode:NSSegmentSwitchTrackingMomentary |
| 171 target:self | 169 target:self |
| 172 action:@selector(backOrForward:)]; | 170 action:@selector(backOrForward:)]; |
| 173 control.segmentStyle = NSSegmentStyleSeparated; | 171 control.segmentStyle = NSSegmentStyleSeparated; |
| 174 [control setEnabled:commandUpdater_->IsCommandEnabled(IDC_BACK) | 172 [control setEnabled:commandUpdater_->IsCommandEnabled(IDC_BACK) |
| 175 forSegment:kBackSegmentIndex]; | 173 forSegment:kBackSegmentIndex]; |
| 176 [control setEnabled:commandUpdater_->IsCommandEnabled(IDC_FORWARD) | 174 [control setEnabled:commandUpdater_->IsCommandEnabled(IDC_FORWARD) |
| 177 forSegment:kForwardSegmentIndex]; | 175 forSegment:kForwardSegmentIndex]; |
| 178 return control; | 176 return control; |
| 179 } | 177 } |
| 180 | 178 |
| 181 - (NSView*)searchTouchBarView { | 179 - (NSView*)searchTouchBarView { |
| 182 // TODO(spqchan): Use the Google search icon if the default search engine is | 180 TemplateURLService* templateUrlService = |
| 183 // Google. | 181 TemplateURLServiceFactory::GetForProfile(browser_->profile()); |
| 184 base::string16 title = l10n_util::GetStringUTF16(IDS_TOUCH_BAR_SEARCH); | 182 const TemplateURL* defaultProvider = |
| 183 templateUrlService->GetDefaultSearchProvider(); |
| 184 BOOL isGoogle = |
| 185 defaultProvider->GetEngineType(templateUrlService->search_terms_data()) == |
| 186 SEARCH_ENGINE_GOOGLE; |
| 187 |
| 188 base::string16 title = |
| 189 isGoogle ? l10n_util::GetStringUTF16(IDS_TOUCH_BAR_GOOGLE_SEARCH) |
| 190 : l10n_util::GetStringFUTF16(IDS_TOUCH_BAR_SEARCH, |
| 191 defaultProvider->short_name()); |
| 192 |
| 193 NSImage* image; |
| 194 if (isGoogle) { |
| 195 image = NSImageFromImageSkiaWithColorSpace( |
| 196 gfx::CreateVectorIcon(gfx::VectorIconId::GOOGLE_SEARCH_MAC_TOUCHBAR, |
| 197 kTouchBarIconSize, gfx::kPlaceholderColor), |
| 198 base::mac::GetSRGBColorSpace()); |
| 199 } else { |
| 200 image = CreateNSImageFromIcon(omnibox::kSearchIcon); |
| 201 } |
| 202 |
| 185 NSButton* searchButton = | 203 NSButton* searchButton = |
| 186 [NSButton buttonWithTitle:base::SysUTF16ToNSString(title) | 204 [NSButton buttonWithTitle:base::SysUTF16ToNSString(title) |
| 187 image:CreateNSImageFromIcon(omnibox::kSearchIcon) | 205 image:image |
| 188 target:self | 206 target:self |
| 189 action:@selector(executeCommand:)]; | 207 action:@selector(executeCommand:)]; |
| 190 searchButton.imageHugsTitle = YES; | 208 searchButton.imageHugsTitle = YES; |
| 191 searchButton.tag = IDC_FOCUS_LOCATION; | 209 searchButton.tag = IDC_FOCUS_LOCATION; |
| 192 [searchButton.widthAnchor | 210 [searchButton.widthAnchor |
| 193 constraintEqualToConstant:kTouchBarSearchButtonWidth] | 211 constraintEqualToConstant:kTouchBarSearchButtonWidth] |
| 194 .active = YES; | 212 .active = YES; |
| 195 return searchButton; | 213 return searchButton; |
| 196 } | 214 } |
| 197 | 215 |
| 198 - (void)backOrForward:(id)sender { | 216 - (void)backOrForward:(id)sender { |
| 199 NSSegmentedControl* control = sender; | 217 NSSegmentedControl* control = sender; |
| 200 if ([control selectedSegment] == kBackSegmentIndex) | 218 if ([control selectedSegment] == kBackSegmentIndex) |
| 201 commandUpdater_->ExecuteCommand(IDC_BACK); | 219 commandUpdater_->ExecuteCommand(IDC_BACK); |
| 202 else | 220 else |
| 203 commandUpdater_->ExecuteCommand(IDC_FORWARD); | 221 commandUpdater_->ExecuteCommand(IDC_FORWARD); |
| 204 } | 222 } |
| 205 | 223 |
| 206 - (void)executeCommand:(id)sender { | 224 - (void)executeCommand:(id)sender { |
| 207 commandUpdater_->ExecuteCommand([sender tag]); | 225 commandUpdater_->ExecuteCommand([sender tag]); |
| 208 } | 226 } |
| 209 | 227 |
| 210 @end | 228 @end |
| OLD | NEW |