Chromium Code Reviews| 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/gfx/vector_icons_public.h" | |
|
Evan Stade
2017/02/17 20:52:58
don't think you need this
spqchan
2017/02/17 21:18:35
Done.
| |
| 29 #include "ui/vector_icons/vector_icons.h" | |
|
Evan Stade
2017/02/17 20:52:58
or this
spqchan
2017/02/17 21:18:35
I need it for kBackArrowIcon, etc
| |
| 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 const SkColor kTouchBarStarIconColor = gfx::kGoogleBlue500; | |
|
Evan Stade
2017/02/17 20:52:58
You may want to pull this one from the native them
spqchan
2017/02/17 21:18:35
Done.
| |
| 51 | |
| 52 // The size of the touch bar icons. | |
| 53 const int kTouchBarIconSize = 16; | |
|
Evan Stade
2017/02/17 20:52:58
this shouldn't be necessary. Aren't all the icons
spqchan
2017/02/17 21:18:35
It looks like I need this, otherwise the new tab a
| |
| 54 | |
| 55 // The width of the search button in the touch bar. | |
| 56 const int kTouchBarSearchButtonWidth = 280; | |
| 57 | |
| 58 // Creates an NSImage from the given VectorIcon. | |
| 59 NSImage* CreateNSImageFromId(const gfx::VectorIcon* icon, | |
|
Evan Stade
2017/02/17 20:52:57
const ref
also, the name (referencing "Id") no lo
spqchan
2017/02/17 21:18:35
Done.
| |
| 60 SkColor color = kTouchBarDefaultIconColor) { | |
| 61 return NSImageFromImageSkiaWithColorSpace( | |
| 62 gfx::CreateVectorIcon(*icon, kTouchBarIconSize, color), | |
| 63 base::mac::GetSRGBColorSpace()); | |
| 64 } | |
| 65 | |
| 66 // Creates a NSButton for the touch bar. | |
| 67 NSButton* CreateTouchBarButton(const gfx::VectorIcon* icon, | |
|
Evan Stade
2017/02/17 20:52:57
const ref
spqchan
2017/02/17 21:18:35
Done.
| |
| 68 BrowserWindowTouchBar* owner, | |
| 69 int command, | |
| 70 SkColor color = kTouchBarDefaultIconColor) { | |
| 71 NSButton* button = [NSButton buttonWithImage:CreateNSImageFromId(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 = | |
|
Evan Stade
2017/02/17 20:52:57
const ref
spqchan
2017/02/17 21:18:35
Done.
| |
| 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 gfx::VectorIcon* icon = | |
| 147 isStarred_ ? &toolbar::kStarActiveIcon : &toolbar::kStarIcon; | |
| 148 SkColor iconColor = | |
| 149 isStarred_ ? kTouchBarStarIconColor : kTouchBarDefaultIconColor; | |
| 150 [touchBarItem | |
| 151 setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE, iconColor)]; | |
| 152 } else if ([identifier isEqualTo:kSearchTouchId]) { | |
| 153 [touchBarItem setView:[self searchTouchBarView]]; | |
| 154 } | |
| 155 | |
| 156 return touchBarItem.autorelease(); | |
| 157 } | |
| 158 | |
| 159 - (NSView*)backOrForwardTouchBarView { | |
| 160 NSArray* images = @[ | |
| 161 CreateNSImageFromId(&ui::kBackArrowIcon), | |
| 162 CreateNSImageFromId(&ui::kForwardArrowIcon) | |
| 163 ]; | |
| 164 | |
| 165 NSSegmentedControl* control = [NSSegmentedControl | |
| 166 segmentedControlWithImages:images | |
| 167 trackingMode:NSSegmentSwitchTrackingMomentary | |
| 168 target:self | |
| 169 action:@selector(backOrForward:)]; | |
| 170 control.segmentStyle = NSSegmentStyleSeparated; | |
| 171 [control setEnabled:commandUpdater_->IsCommandEnabled(IDC_BACK) | |
| 172 forSegment:kBackSegmentIndex]; | |
| 173 [control setEnabled:commandUpdater_->IsCommandEnabled(IDC_FORWARD) | |
| 174 forSegment:kForwardSegmentIndex]; | |
| 175 return control; | |
| 176 } | |
| 177 | |
| 178 - (NSView*)searchTouchBarView { | |
| 179 // TODO(spqchan): Use the Google search icon if the default search engine is | |
| 180 // Google. | |
| 181 base::string16 title = l10n_util::GetStringUTF16(IDS_TOUCH_BAR_SEARCH); | |
| 182 NSButton* searchButton = | |
| 183 [NSButton buttonWithTitle:base::SysUTF16ToNSString(title) | |
| 184 image:CreateNSImageFromId(&omnibox::kSearchIcon) | |
| 185 target:self | |
| 186 action:@selector(executeCommand:)]; | |
| 187 searchButton.imageHugsTitle = YES; | |
| 188 searchButton.tag = IDC_FOCUS_LOCATION; | |
| 189 [searchButton.widthAnchor | |
| 190 constraintEqualToConstant:kTouchBarSearchButtonWidth] | |
| 191 .active = YES; | |
| 192 return searchButton; | |
| 193 } | |
| 194 | |
| 195 - (void)backOrForward:(id)sender { | |
| 196 NSSegmentedControl* control = sender; | |
| 197 if ([control selectedSegment] == kBackSegmentIndex) | |
| 198 commandUpdater_->ExecuteCommand(IDC_BACK); | |
| 199 else | |
| 200 commandUpdater_->ExecuteCommand(IDC_FORWARD); | |
| 201 } | |
| 202 | |
| 203 - (void)executeCommand:(id)sender { | |
| 204 commandUpdater_->ExecuteCommand([sender tag]); | |
| 205 } | |
| 206 | |
| 207 @end | |
| OLD | NEW |