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/mac_util.h" | 10 #include "base/mac/mac_util.h" |
10 #import "base/mac/scoped_nsobject.h" | 11 #import "base/mac/scoped_nsobject.h" |
11 #import "base/mac/sdk_forward_declarations.h" | 12 #import "base/mac/sdk_forward_declarations.h" |
12 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
13 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
14 #include "chrome/app/chrome_command_ids.h" | 15 #include "chrome/app/chrome_command_ids.h" |
15 #include "chrome/app/vector_icons/vector_icons.h" | 16 #include "chrome/app/vector_icons/vector_icons.h" |
16 #include "chrome/browser/command_updater.h" | 17 #include "chrome/browser/command_updater.h" |
17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/search_engines/template_url_service_factory.h" | 19 #include "chrome/browser/search_engines/template_url_service_factory.h" |
(...skipping 27 matching lines...) Expand all Loading... |
46 STOP, | 47 STOP, |
47 RELOAD, | 48 RELOAD, |
48 HOME, | 49 HOME, |
49 SEARCH, | 50 SEARCH, |
50 STAR, | 51 STAR, |
51 NEW_TAB, | 52 NEW_TAB, |
52 TOUCH_BAR_ACTION_COUNT | 53 TOUCH_BAR_ACTION_COUNT |
53 }; | 54 }; |
54 | 55 |
55 // The touch bar's identifier. | 56 // The touch bar's identifier. |
56 const NSTouchBarCustomizationIdentifier kBrowserWindowTouchBarId = | 57 NSString* const kBrowserWindowTouchBarId = @"browser-window"; |
57 @"BrowserWindowTouchBarId"; | |
58 | 58 |
59 // Touch bar items identifiers. | 59 // Touch bar items identifiers. |
60 const NSTouchBarItemIdentifier kBackForwardTouchId = @"BackForwardTouchId"; | 60 NSString* const kBackForwardTouchId = @"BACK-FWD"; |
61 const NSTouchBarItemIdentifier kReloadOrStopTouchId = @"ReloadOrStopTouchId"; | 61 NSString* const kReloadOrStopTouchId = @"RELOAD-STOP"; |
62 const NSTouchBarItemIdentifier kHomeTouchId = @"HomeTouchId"; | 62 NSString* const kHomeTouchId = @"HOME"; |
63 const NSTouchBarItemIdentifier kSearchTouchId = @"SearchTouchId"; | 63 NSString* const kSearchTouchId = @"SEARCH"; |
64 const NSTouchBarItemIdentifier kStarTouchId = @"StarTouchId"; | 64 NSString* const kStarTouchId = @"BOOKMARK"; |
65 const NSTouchBarItemIdentifier kNewTabTouchId = @"NewTabTouchId"; | 65 NSString* const kNewTabTouchId = @"NEW-TAB"; |
66 | 66 |
67 // The button indexes in the back and forward segment control. | 67 // The button indexes in the back and forward segment control. |
68 const int kBackSegmentIndex = 0; | 68 const int kBackSegmentIndex = 0; |
69 const int kForwardSegmentIndex = 1; | 69 const int kForwardSegmentIndex = 1; |
70 | 70 |
71 // Touch bar icon colors values. | 71 // Touch bar icon colors values. |
72 const SkColor kTouchBarDefaultIconColor = SK_ColorWHITE; | 72 const SkColor kTouchBarDefaultIconColor = SK_ColorWHITE; |
73 const SkColor kTouchBarStarActiveColor = gfx::kGoogleBlue500; | 73 const SkColor kTouchBarStarActiveColor = gfx::kGoogleBlue500; |
74 | 74 |
75 // The size of the touch bar icons. | 75 // The size of the touch bar icons. |
76 const int kTouchBarIconSize = 16; | 76 const int kTouchBarIconSize = 16; |
77 | 77 |
78 // The width of the search button in the touch bar. | 78 // The min width of the search button in the touch bar. |
79 const int kSearchBtnWidthWithHomeBtn = 205; | 79 const int kSearchBtnMinWidth = 205; |
80 const int kSearchBtnWidthWithoutHomeBtn = 280; | |
81 | 80 |
82 // Creates an NSImage from the given VectorIcon. | 81 // Creates an NSImage from the given VectorIcon. |
83 NSImage* CreateNSImageFromIcon(const gfx::VectorIcon& icon, | 82 NSImage* CreateNSImageFromIcon(const gfx::VectorIcon& icon, |
84 SkColor color = kTouchBarDefaultIconColor) { | 83 SkColor color = kTouchBarDefaultIconColor) { |
85 return NSImageFromImageSkiaWithColorSpace( | 84 return NSImageFromImageSkiaWithColorSpace( |
86 gfx::CreateVectorIcon(icon, kTouchBarIconSize, color), | 85 gfx::CreateVectorIcon(icon, kTouchBarIconSize, color), |
87 base::mac::GetSRGBColorSpace()); | 86 base::mac::GetSRGBColorSpace()); |
88 } | 87 } |
89 | 88 |
90 // Creates a NSButton for the touch bar. | 89 // Creates a NSButton for the touch bar. |
91 NSButton* CreateTouchBarButton(const gfx::VectorIcon& icon, | 90 NSButton* CreateTouchBarButton(const gfx::VectorIcon& icon, |
92 BrowserWindowTouchBar* owner, | 91 BrowserWindowTouchBar* owner, |
93 int command, | 92 int command, |
94 int tooltip_id, | 93 int tooltip_id, |
95 SkColor color = kTouchBarDefaultIconColor) { | 94 SkColor color = kTouchBarDefaultIconColor) { |
96 NSButton* button = | 95 NSButton* button = |
97 [NSButton buttonWithImage:CreateNSImageFromIcon(icon, color) | 96 [NSButton buttonWithImage:CreateNSImageFromIcon(icon, color) |
98 target:owner | 97 target:owner |
99 action:@selector(executeCommand:)]; | 98 action:@selector(executeCommand:)]; |
100 button.tag = command; | 99 button.tag = command; |
101 [button setAccessibilityLabel:l10n_util::GetNSString(tooltip_id)]; | 100 [button setAccessibilityLabel:l10n_util::GetNSString(tooltip_id)]; |
102 return button; | 101 return button; |
103 } | 102 } |
104 | 103 |
| 104 NSString* GetTouchBarId(NSString* const touch_bar_id) { |
| 105 NSString* chrome_bundle_id = |
| 106 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 |
| 113 stringWithFormat:@"%@-%@", GetTouchBarId(touch_bar_id), item_id]; |
| 114 } |
| 115 |
105 TouchBarAction TouchBarActionFromCommand(int command) { | 116 TouchBarAction TouchBarActionFromCommand(int command) { |
106 switch (command) { | 117 switch (command) { |
107 case IDC_BACK: | 118 case IDC_BACK: |
108 return TouchBarAction::BACK; | 119 return TouchBarAction::BACK; |
109 case IDC_FORWARD: | 120 case IDC_FORWARD: |
110 return TouchBarAction::FORWARD; | 121 return TouchBarAction::FORWARD; |
111 case IDC_STOP: | 122 case IDC_STOP: |
112 return TouchBarAction::STOP; | 123 return TouchBarAction::STOP; |
113 case IDC_RELOAD: | 124 case IDC_RELOAD: |
114 return TouchBarAction::RELOAD; | 125 return TouchBarAction::RELOAD; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 | 209 |
199 return self; | 210 return self; |
200 } | 211 } |
201 | 212 |
202 - (NSTouchBar*)makeTouchBar { | 213 - (NSTouchBar*)makeTouchBar { |
203 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar)) | 214 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar)) |
204 return nil; | 215 return nil; |
205 | 216 |
206 base::scoped_nsobject<NSTouchBar> touchBar( | 217 base::scoped_nsobject<NSTouchBar> touchBar( |
207 [[NSClassFromString(@"NSTouchBar") alloc] init]); | 218 [[NSClassFromString(@"NSTouchBar") alloc] init]); |
208 NSArray* touchBarItemIdentifiers; | 219 NSMutableArray* customIdentifiers = [NSMutableArray arrayWithCapacity:7]; |
209 if (showHomeButton_.GetValue()) { | 220 NSMutableArray* defaultIdentifiers = [NSMutableArray arrayWithCapacity:6]; |
210 touchBarItemIdentifiers = @[ | 221 |
211 kBackForwardTouchId, kReloadOrStopTouchId, kHomeTouchId, kSearchTouchId, | 222 NSArray* touchBarItems = @[ |
212 kStarTouchId, kNewTabTouchId | 223 kBackForwardTouchId, kReloadOrStopTouchId, kHomeTouchId, kSearchTouchId, |
213 ]; | 224 kStarTouchId, kNewTabTouchId |
214 } else { | 225 ]; |
215 touchBarItemIdentifiers = @[ | 226 |
216 kBackForwardTouchId, kReloadOrStopTouchId, kSearchTouchId, kStarTouchId, | 227 for (NSString* item in touchBarItems) { |
217 kNewTabTouchId | 228 NSString* itemIdentifier = |
218 ]; | 229 GetTouchBarItemId(kBrowserWindowTouchBarId, item); |
| 230 [customIdentifiers addObject:itemIdentifier]; |
| 231 |
| 232 // Don't add the home button if it's not shown in the toolbar. |
| 233 if (showHomeButton_.GetValue() || ![item isEqualTo:kHomeTouchId]) |
| 234 [defaultIdentifiers addObject:itemIdentifier]; |
219 } | 235 } |
220 | 236 |
221 [touchBar setCustomizationIdentifier:kBrowserWindowTouchBarId]; | 237 [customIdentifiers addObject:NSTouchBarItemIdentifierFlexibleSpace]; |
222 [touchBar setDefaultItemIdentifiers:touchBarItemIdentifiers]; | 238 |
223 [touchBar setCustomizationAllowedItemIdentifiers:touchBarItemIdentifiers]; | 239 [touchBar setCustomizationIdentifier:GetTouchBarId(kBrowserWindowTouchBarId)]; |
| 240 [touchBar setDefaultItemIdentifiers:defaultIdentifiers]; |
| 241 [touchBar setCustomizationAllowedItemIdentifiers:customIdentifiers]; |
224 [touchBar setDelegate:self]; | 242 [touchBar setDelegate:self]; |
225 | 243 |
226 return touchBar.autorelease(); | 244 return touchBar.autorelease(); |
227 } | 245 } |
228 | 246 |
229 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar | 247 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar |
230 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { | 248 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { |
231 if (!touchBar) | 249 if (!touchBar) |
232 return nil; | 250 return nil; |
233 | 251 |
234 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString( | 252 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString( |
235 @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]); | 253 @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]); |
236 if ([identifier isEqualTo:kBackForwardTouchId]) { | 254 if ([identifier hasSuffix:kBackForwardTouchId]) { |
237 [touchBarItem setView:[self backOrForwardTouchBarView]]; | 255 [touchBarItem setView:[self backOrForwardTouchBarView]]; |
238 } else if ([identifier isEqualTo:kReloadOrStopTouchId]) { | 256 [touchBarItem setCustomizationLabel: |
| 257 l10n_util::GetNSString( |
| 258 IDS_TOUCH_BAR_BACK_FORWARD_CUSTOMIZATION_LABEL)]; |
| 259 } else if ([identifier hasSuffix:kReloadOrStopTouchId]) { |
239 const gfx::VectorIcon& icon = | 260 const gfx::VectorIcon& icon = |
240 isPageLoading_ ? kNavigateStopIcon : kNavigateReloadIcon; | 261 isPageLoading_ ? kNavigateStopIcon : kNavigateReloadIcon; |
241 int commandId = isPageLoading_ ? IDC_STOP : IDC_RELOAD; | 262 int commandId = isPageLoading_ ? IDC_STOP : IDC_RELOAD; |
242 int tooltipId = isPageLoading_ ? IDS_TOOLTIP_STOP : IDS_TOOLTIP_RELOAD; | 263 int tooltipId = isPageLoading_ ? IDS_TOOLTIP_STOP : IDS_TOOLTIP_RELOAD; |
243 [touchBarItem | 264 [touchBarItem |
244 setView:CreateTouchBarButton(icon, self, commandId, tooltipId)]; | 265 setView:CreateTouchBarButton(icon, self, commandId, tooltipId)]; |
245 } else if ([identifier isEqualTo:kHomeTouchId]) { | 266 [touchBarItem setCustomizationLabel: |
| 267 l10n_util::GetNSString( |
| 268 IDS_TOUCH_BAR_STOP_RELOAD_CUSTOMIZATION_LABEL)]; |
| 269 } else if ([identifier hasSuffix:kHomeTouchId]) { |
246 [touchBarItem setView:CreateTouchBarButton(kNavigateHomeIcon, self, | 270 [touchBarItem setView:CreateTouchBarButton(kNavigateHomeIcon, self, |
247 IDC_HOME, IDS_TOOLTIP_HOME)]; | 271 IDC_HOME, IDS_TOOLTIP_HOME)]; |
248 } else if ([identifier isEqualTo:kNewTabTouchId]) { | 272 [touchBarItem |
| 273 setCustomizationLabel:l10n_util::GetNSString( |
| 274 IDS_TOUCH_BAR_HOME_CUSTOMIZATION_LABEL)]; |
| 275 } else if ([identifier hasSuffix:kNewTabTouchId]) { |
249 [touchBarItem | 276 [touchBarItem |
250 setView:CreateTouchBarButton(kNewTabMacTouchbarIcon, self, IDC_NEW_TAB, | 277 setView:CreateTouchBarButton(kNewTabMacTouchbarIcon, self, IDC_NEW_TAB, |
251 IDS_TOOLTIP_NEW_TAB)]; | 278 IDS_TOOLTIP_NEW_TAB)]; |
252 } else if ([identifier isEqualTo:kStarTouchId]) { | 279 [touchBarItem |
| 280 setCustomizationLabel:l10n_util::GetNSString( |
| 281 IDS_TOUCH_BAR_NEW_TAB_CUSTOMIZATION_LABEL)]; |
| 282 } else if ([identifier hasSuffix:kStarTouchId]) { |
253 const gfx::VectorIcon& icon = | 283 const gfx::VectorIcon& icon = |
254 isStarred_ ? toolbar::kStarActiveIcon : toolbar::kStarIcon; | 284 isStarred_ ? toolbar::kStarActiveIcon : toolbar::kStarIcon; |
255 SkColor iconColor = | 285 SkColor iconColor = |
256 isStarred_ ? kTouchBarStarActiveColor : kTouchBarDefaultIconColor; | 286 isStarred_ ? kTouchBarStarActiveColor : kTouchBarDefaultIconColor; |
257 int tooltipId = isStarred_ ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR; | 287 int tooltipId = isStarred_ ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR; |
258 [touchBarItem setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE, | 288 [touchBarItem setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE, |
259 tooltipId, iconColor)]; | 289 tooltipId, iconColor)]; |
260 } else if ([identifier isEqualTo:kSearchTouchId]) { | 290 [touchBarItem |
| 291 setCustomizationLabel:l10n_util::GetNSString( |
| 292 IDS_TOUCH_BAR_BOOKMARK_CUSTOMIZATION_LABEL)]; |
| 293 } else if ([identifier hasSuffix:kSearchTouchId]) { |
261 [touchBarItem setView:[self searchTouchBarView]]; | 294 [touchBarItem setView:[self searchTouchBarView]]; |
| 295 [touchBarItem setCustomizationLabel:l10n_util::GetNSString( |
| 296 IDS_TOUCH_BAR_GOOGLE_SEARCH)]; |
262 } | 297 } |
263 | 298 |
264 return touchBarItem.autorelease(); | 299 return touchBarItem.autorelease(); |
265 } | 300 } |
266 | 301 |
267 - (NSView*)backOrForwardTouchBarView { | 302 - (NSView*)backOrForwardTouchBarView { |
268 NSArray* images = @[ | 303 NSArray* images = @[ |
269 CreateNSImageFromIcon(ui::kBackArrowIcon), | 304 CreateNSImageFromIcon(ui::kBackArrowIcon), |
270 CreateNSImageFromIcon(ui::kForwardArrowIcon) | 305 CreateNSImageFromIcon(ui::kForwardArrowIcon) |
271 ]; | 306 ]; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 image = CreateNSImageFromIcon(ui::kSearchIcon); | 357 image = CreateNSImageFromIcon(ui::kSearchIcon); |
323 } | 358 } |
324 | 359 |
325 NSButton* searchButton = | 360 NSButton* searchButton = |
326 [NSButton buttonWithTitle:base::SysUTF16ToNSString(title) | 361 [NSButton buttonWithTitle:base::SysUTF16ToNSString(title) |
327 image:image | 362 image:image |
328 target:self | 363 target:self |
329 action:@selector(executeCommand:)]; | 364 action:@selector(executeCommand:)]; |
330 searchButton.imageHugsTitle = YES; | 365 searchButton.imageHugsTitle = YES; |
331 searchButton.tag = IDC_FOCUS_LOCATION; | 366 searchButton.tag = IDC_FOCUS_LOCATION; |
332 int width = showHomeButton_.GetValue() ? kSearchBtnWidthWithHomeBtn | 367 [searchButton.widthAnchor |
333 : kSearchBtnWidthWithoutHomeBtn; | 368 constraintGreaterThanOrEqualToConstant:kSearchBtnMinWidth] |
334 [searchButton.widthAnchor constraintEqualToConstant:width].active = YES; | 369 .active = YES; |
| 370 [searchButton |
| 371 setContentHuggingPriority:1.0 |
| 372 forOrientation:NSLayoutConstraintOrientationHorizontal]; |
335 return searchButton; | 373 return searchButton; |
336 } | 374 } |
337 | 375 |
338 - (void)backOrForward:(id)sender { | 376 - (void)backOrForward:(id)sender { |
339 NSSegmentedControl* control = sender; | 377 NSSegmentedControl* control = sender; |
340 int command = | 378 int command = |
341 [control selectedSegment] == kBackSegmentIndex ? IDC_BACK : IDC_FORWARD; | 379 [control selectedSegment] == kBackSegmentIndex ? IDC_BACK : IDC_FORWARD; |
342 LogTouchBarUMA(command); | 380 LogTouchBarUMA(command); |
343 commandUpdater_->ExecuteCommand(command); | 381 commandUpdater_->ExecuteCommand(command); |
344 } | 382 } |
345 | 383 |
346 - (void)executeCommand:(id)sender { | 384 - (void)executeCommand:(id)sender { |
347 int command = [sender tag]; | 385 int command = [sender tag]; |
348 LogTouchBarUMA(command); | 386 LogTouchBarUMA(command); |
349 commandUpdater_->ExecuteCommand(command); | 387 commandUpdater_->ExecuteCommand(command); |
350 } | 388 } |
351 | 389 |
352 @end | 390 @end |
OLD | NEW |