OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "chrome/browser/ui/cocoa/browser_window_touch_bar.h" |
| 6 |
| 7 #include "base/mac/mac_util.h" |
| 8 #import "base/mac/scoped_nsobject.h" |
| 9 #import "base/mac/sdk_forward_declarations.h" |
| 10 #include "base/strings/sys_string_conversions.h" |
| 11 #include "chrome/app/chrome_command_ids.h" |
| 12 #include "chrome/app/vector_icons/vector_icons.h" |
| 13 #include "chrome/browser/command_updater.h" |
| 14 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 15 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/browser_command_controller.h" |
| 17 #include "chrome/common/chrome_features.h" |
| 18 #include "chrome/grit/generated_resources.h" |
| 19 #include "components/omnibox/browser/vector_icons.h" |
| 20 #include "components/search_engines/util.h" |
| 21 #include "components/toolbar/vector_icons.h" |
| 22 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "ui/gfx/color_palette.h" |
| 24 #include "ui/gfx/color_utils.h" |
| 25 #include "ui/gfx/image/image.h" |
| 26 #include "ui/gfx/image/image_skia_util_mac.h" |
| 27 #include "ui/gfx/paint_vector_icon.h" |
| 28 #include "ui/native_theme/native_theme.h" |
| 29 #include "ui/vector_icons/vector_icons.h" |
| 30 |
| 31 namespace { |
| 32 |
| 33 // The touch bar's identifier. |
| 34 const NSTouchBarCustomizationIdentifier kBrowserWindowTouchBarId = |
| 35 @"BrowserWindowTouchBarId"; |
| 36 |
| 37 // Touch bar items identifiers. |
| 38 const NSTouchBarItemIdentifier kBackForwardTouchId = @"BackForwardTouchId"; |
| 39 const NSTouchBarItemIdentifier kReloadOrStopTouchId = @"ReloadOrStopTouchId"; |
| 40 const NSTouchBarItemIdentifier kSearchTouchId = @"SearchTouchId"; |
| 41 const NSTouchBarItemIdentifier kNewTabTouchId = @"NewTabTouchId"; |
| 42 const NSTouchBarItemIdentifier kStarTouchId = @"StarTouchId"; |
| 43 |
| 44 // The button indexes in the back and forward segment control. |
| 45 const int kBackSegmentIndex = 0; |
| 46 const int kForwardSegmentIndex = 1; |
| 47 |
| 48 // Touch bar icon colors values. |
| 49 const SkColor kTouchBarDefaultIconColor = SK_ColorWHITE; |
| 50 |
| 51 // The size of the touch bar icons. |
| 52 const int kTouchBarIconSize = 16; |
| 53 |
| 54 // The width of the search button in the touch bar. |
| 55 const int kTouchBarSearchButtonWidth = 280; |
| 56 |
| 57 // Creates an NSImage from the given VectorIcon. |
| 58 NSImage* CreateNSImageFromIcon(const gfx::VectorIcon& icon, |
| 59 SkColor color = kTouchBarDefaultIconColor) { |
| 60 return NSImageFromImageSkiaWithColorSpace( |
| 61 gfx::CreateVectorIcon(icon, kTouchBarIconSize, color), |
| 62 base::mac::GetSRGBColorSpace()); |
| 63 } |
| 64 |
| 65 // Creates a NSButton for the touch bar. |
| 66 NSButton* CreateTouchBarButton(const gfx::VectorIcon& icon, |
| 67 BrowserWindowTouchBar* owner, |
| 68 int command, |
| 69 SkColor color = kTouchBarDefaultIconColor) { |
| 70 NSButton* button = |
| 71 [NSButton buttonWithImage:CreateNSImageFromIcon(icon, color) |
| 72 target:owner |
| 73 action:@selector(executeCommand:)]; |
| 74 button.tag = command; |
| 75 return button; |
| 76 } |
| 77 |
| 78 } // namespace |
| 79 |
| 80 @interface BrowserWindowTouchBar () { |
| 81 // Used to execute commands such as navigating back and forward. |
| 82 CommandUpdater* commandUpdater_; // Weak, owned by Browser. |
| 83 |
| 84 // The browser associated with the touch bar. |
| 85 Browser* browser_; // Weak. |
| 86 } |
| 87 |
| 88 // Creates and return the back and forward segmented buttons. |
| 89 - (NSView*)backOrForwardTouchBarView; |
| 90 |
| 91 // Creates and returns the search button. |
| 92 - (NSView*)searchTouchBarView; |
| 93 @end |
| 94 |
| 95 @implementation BrowserWindowTouchBar |
| 96 |
| 97 @synthesize isPageLoading = isPageLoading_; |
| 98 @synthesize isStarred = isStarred_; |
| 99 |
| 100 - (instancetype)initWithBrowser:(Browser*)browser { |
| 101 if ((self = [self init])) { |
| 102 DCHECK(browser); |
| 103 commandUpdater_ = browser->command_controller()->command_updater(); |
| 104 browser_ = browser; |
| 105 } |
| 106 |
| 107 return self; |
| 108 } |
| 109 |
| 110 - (NSTouchBar*)makeTouchBar { |
| 111 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar)) |
| 112 return nil; |
| 113 |
| 114 base::scoped_nsobject<NSTouchBar> touchBar( |
| 115 [[NSClassFromString(@"NSTouchBar") alloc] init]); |
| 116 NSArray* touchBarItemIdentifiers = @[ |
| 117 kBackForwardTouchId, kReloadOrStopTouchId, kSearchTouchId, kNewTabTouchId, |
| 118 kStarTouchId |
| 119 ]; |
| 120 [touchBar setCustomizationIdentifier:kBrowserWindowTouchBarId]; |
| 121 [touchBar setDefaultItemIdentifiers:touchBarItemIdentifiers]; |
| 122 [touchBar setCustomizationAllowedItemIdentifiers:touchBarItemIdentifiers]; |
| 123 [touchBar setDelegate:self]; |
| 124 |
| 125 return touchBar.autorelease(); |
| 126 } |
| 127 |
| 128 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar |
| 129 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { |
| 130 if (!touchBar) |
| 131 return nil; |
| 132 |
| 133 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString( |
| 134 @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]); |
| 135 if ([identifier isEqualTo:kBackForwardTouchId]) { |
| 136 [touchBarItem setView:[self backOrForwardTouchBarView]]; |
| 137 } else if ([identifier isEqualTo:kReloadOrStopTouchId]) { |
| 138 const gfx::VectorIcon& icon = |
| 139 isPageLoading_ ? kNavigateStopIcon : kNavigateReloadIcon; |
| 140 int command_id = isPageLoading_ ? IDC_STOP : IDC_RELOAD; |
| 141 [touchBarItem setView:CreateTouchBarButton(icon, self, command_id)]; |
| 142 } else if ([identifier isEqualTo:kNewTabTouchId]) { |
| 143 [touchBarItem setView:CreateTouchBarButton(kNewTabMacTouchbarIcon, self, |
| 144 IDC_NEW_TAB)]; |
| 145 } else if ([identifier isEqualTo:kStarTouchId]) { |
| 146 const SkColor kStarredIconColor = |
| 147 ui::NativeTheme::GetInstanceForNativeUi()->GetSystemColor( |
| 148 ui::NativeTheme::kColorId_ProminentButtonColor); |
| 149 const gfx::VectorIcon& icon = |
| 150 isStarred_ ? toolbar::kStarActiveIcon : toolbar::kStarIcon; |
| 151 SkColor iconColor = |
| 152 isStarred_ ? kStarredIconColor : kTouchBarDefaultIconColor; |
| 153 [touchBarItem |
| 154 setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE, iconColor)]; |
| 155 } else if ([identifier isEqualTo:kSearchTouchId]) { |
| 156 [touchBarItem setView:[self searchTouchBarView]]; |
| 157 } |
| 158 |
| 159 return touchBarItem.autorelease(); |
| 160 } |
| 161 |
| 162 - (NSView*)backOrForwardTouchBarView { |
| 163 NSArray* images = @[ |
| 164 CreateNSImageFromIcon(ui::kBackArrowIcon), |
| 165 CreateNSImageFromIcon(ui::kForwardArrowIcon) |
| 166 ]; |
| 167 |
| 168 NSSegmentedControl* control = [NSSegmentedControl |
| 169 segmentedControlWithImages:images |
| 170 trackingMode:NSSegmentSwitchTrackingMomentary |
| 171 target:self |
| 172 action:@selector(backOrForward:)]; |
| 173 control.segmentStyle = NSSegmentStyleSeparated; |
| 174 [control setEnabled:commandUpdater_->IsCommandEnabled(IDC_BACK) |
| 175 forSegment:kBackSegmentIndex]; |
| 176 [control setEnabled:commandUpdater_->IsCommandEnabled(IDC_FORWARD) |
| 177 forSegment:kForwardSegmentIndex]; |
| 178 return control; |
| 179 } |
| 180 |
| 181 - (NSView*)searchTouchBarView { |
| 182 // TODO(spqchan): Use the Google search icon if the default search engine is |
| 183 // Google. |
| 184 base::string16 title = l10n_util::GetStringUTF16(IDS_TOUCH_BAR_SEARCH); |
| 185 NSButton* searchButton = |
| 186 [NSButton buttonWithTitle:base::SysUTF16ToNSString(title) |
| 187 image:CreateNSImageFromIcon(omnibox::kSearchIcon) |
| 188 target:self |
| 189 action:@selector(executeCommand:)]; |
| 190 searchButton.imageHugsTitle = YES; |
| 191 searchButton.tag = IDC_FOCUS_LOCATION; |
| 192 [searchButton.widthAnchor |
| 193 constraintEqualToConstant:kTouchBarSearchButtonWidth] |
| 194 .active = YES; |
| 195 return searchButton; |
| 196 } |
| 197 |
| 198 - (void)backOrForward:(id)sender { |
| 199 NSSegmentedControl* control = sender; |
| 200 if ([control selectedSegment] == kBackSegmentIndex) |
| 201 commandUpdater_->ExecuteCommand(IDC_BACK); |
| 202 else |
| 203 commandUpdater_->ExecuteCommand(IDC_FORWARD); |
| 204 } |
| 205 |
| 206 - (void)executeCommand:(id)sender { |
| 207 commandUpdater_->ExecuteCommand([sender tag]); |
| 208 } |
| 209 |
| 210 @end |
OLD | NEW |