OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
Robert Sesek
2017/02/14 17:32:51
How about a unit test?
spqchan
2017/02/16 15:25:51
I tried to write some tests, but they're currently
Robert Sesek
2017/02/16 18:19:38
If the test is flaky, doesn't that mean that the u
spqchan
2017/02/16 18:35:42
Sorry, I should've clarified. It's more like I'm t
Robert Sesek
2017/02/16 18:43:22
Looking at the trybot failures from PS5, this is t
spqchan
2017/02/16 19:05:23
Thanks! I was trying to use that a while ago, but
Robert Sesek
2017/02/16 19:13:38
Does the IsAtMostOS10_12() check let the test run
spqchan
2017/02/16 19:27:48
Done.
| |
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/search_engines/util.h" | |
20 #include "components/toolbar/vector_icons.h" | |
21 #include "ui/base/l10n/l10n_util.h" | |
22 #include "ui/gfx/color_palette.h" | |
23 #include "ui/gfx/color_utils.h" | |
24 #include "ui/gfx/image/image.h" | |
25 #include "ui/gfx/image/image_skia_util_mac.h" | |
26 #include "ui/gfx/paint_vector_icon.h" | |
27 #include "ui/gfx/vector_icons_public.h" | |
28 | |
29 namespace { | |
30 | |
31 // The touch bar's identifier. | |
32 const NSTouchBarCustomizationIdentifier kBrowserWindowTouchBarId = | |
33 @"BrowserWindowTouchBarId"; | |
34 | |
35 // Touch bar items identifiers. | |
36 const NSTouchBarItemIdentifier kBackForwardTouchId = @"BackForwardTouchId"; | |
37 const NSTouchBarItemIdentifier kReloadOrStopTouchId = @"ReloadOrStopTouchId"; | |
38 const NSTouchBarItemIdentifier kSearchTouchId = @"SearchTouchId"; | |
39 const NSTouchBarItemIdentifier kNewTabTouchId = @"NewTabTouchId"; | |
40 const NSTouchBarItemIdentifier kStarTouchId = @"StarTouchId"; | |
41 | |
42 // The button indexes in the back and forward segment control. | |
43 const int kBackSegmentIndex = 0; | |
44 const int kForwardSegmentIndex = 1; | |
45 | |
46 // Touch bar icon colors values. | |
47 const SkColor kTouchBarDefaultIconColor = SK_ColorWHITE; | |
48 const SkColor kTouchBarStarIconColor = gfx::kGoogleBlue500; | |
49 | |
50 // The size of the touch bar icons. | |
51 const int kTouchBarIconSize = 16; | |
52 | |
53 // The width of the search button in the touch bar. | |
54 const int kTouchBarSearchButtonWidth = 280; | |
55 | |
56 // Creates an NSImage from the given VectorIcon. | |
57 NSImage* CreateNSImageFromId(const gfx::VectorIcon* icon, | |
58 SkColor color = kTouchBarDefaultIconColor) { | |
59 return NSImageFromImageSkiaWithColorSpace( | |
60 gfx::CreateVectorIcon(*icon, kTouchBarIconSize, color), | |
61 base::mac::GetSRGBColorSpace()); | |
62 } | |
63 | |
64 // Creates a NSButton for the touch bar. | |
65 NSButton* CreateTouchBarButton(const gfx::VectorIcon* icon, | |
66 BrowserWindowTouchBar* owner, | |
67 int command, | |
68 SkColor color = kTouchBarDefaultIconColor) { | |
69 NSButton* button = [NSButton buttonWithImage:CreateNSImageFromId(icon, color) | |
70 target:owner | |
71 action:@selector(executeCommand:)]; | |
72 button.tag = command; | |
73 return button; | |
74 } | |
75 | |
76 } // end namespace | |
Robert Sesek
2017/02/14 17:32:51
Just "namespace"
spqchan
2017/02/16 15:25:52
Done.
| |
77 | |
78 @interface BrowserWindowTouchBar () { | |
79 // Used to execute commands such as navigating back and forward. | |
80 CommandUpdater* commandUpdater_; // Weak, owned by Browser. | |
81 | |
82 // The browser associated with the touch bar. | |
83 Browser* browser_; // Weak. | |
84 } | |
85 | |
86 // Creates and return the back and forward segmented buttons. | |
87 - (NSView*)backOrForwardTouchBarView; | |
88 | |
89 // Creates and returns the search button. | |
90 - (NSView*)searchTouchBarView; | |
91 @end | |
92 | |
93 @implementation BrowserWindowTouchBar | |
94 | |
95 @synthesize isPageLoading = isPageLoading_; | |
96 @synthesize isStarred = isStarred_; | |
97 | |
98 - (instancetype)initWithBrowser:(Browser*)browser { | |
99 if ((self = [self init])) { | |
100 DCHECK(browser); | |
101 commandUpdater_ = browser->command_controller()->command_updater(); | |
102 browser_ = browser; | |
103 } | |
104 | |
105 return self; | |
106 } | |
107 | |
108 - (NSTouchBar*)makeTouchBar { | |
109 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar)) | |
110 return nil; | |
111 | |
112 base::scoped_nsobject<NSTouchBar> touchBar( | |
113 [[NSClassFromString(@"NSTouchBar") alloc] init]); | |
114 NSArray* touchBarItemIdentifiers = @[ | |
115 kBackForwardTouchId, kReloadOrStopTouchId, kSearchTouchId, kNewTabTouchId, | |
116 kStarTouchId | |
117 ]; | |
118 [touchBar setCustomizationIdentifier:kBrowserWindowTouchBarId]; | |
119 [touchBar setDefaultItemIdentifiers:touchBarItemIdentifiers]; | |
120 [touchBar setCustomizationAllowedItemIdentifiers:touchBarItemIdentifiers]; | |
121 [touchBar setDelegate:self]; | |
122 | |
123 return touchBar.autorelease(); | |
124 } | |
125 | |
126 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar | |
127 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { | |
128 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString( | |
129 @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]); | |
130 if ([identifier isEqualTo:kBackForwardTouchId]) { | |
131 [touchBarItem setView:[self backOrForwardTouchBarView]]; | |
132 } else if ([identifier isEqualTo:kReloadOrStopTouchId]) { | |
133 const gfx::VectorIcon* icon = | |
134 isPageLoading_ ? &kNavigateStopIcon : &kNavigateReloadIcon; | |
135 int command_id = isPageLoading_ ? IDC_STOP : IDC_RELOAD; | |
136 [touchBarItem setView:CreateTouchBarButton(icon, self, command_id)]; | |
137 } else if ([identifier isEqualTo:kNewTabTouchId]) { | |
138 [touchBarItem | |
139 setView:CreateTouchBarButton(&kNewTabIcon, self, IDC_NEW_TAB)]; | |
140 } else if ([identifier isEqualTo:kStarTouchId]) { | |
141 const gfx::VectorIcon* icon = | |
142 isStarred_ ? &toolbar::kStarActiveIcon : &toolbar::kStarIcon; | |
143 SkColor iconColor = | |
144 isStarred_ ? kTouchBarStarIconColor : kTouchBarDefaultIconColor; | |
145 [touchBarItem | |
146 setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE, iconColor)]; | |
147 } else if ([identifier isEqualTo:kSearchTouchId]) { | |
148 [touchBarItem setView:[self searchTouchBarView]]; | |
149 } | |
150 | |
151 return touchBarItem.autorelease(); | |
152 } | |
153 | |
154 - (NSView*)backOrForwardTouchBarView { | |
155 NSArray* images = @[ | |
156 CreateNSImageFromId(&kNavigateBackIcon), | |
157 CreateNSImageFromId(&kNavigateForwardIcon) | |
158 ]; | |
159 | |
160 NSSegmentedControl* control = [NSSegmentedControl | |
161 segmentedControlWithImages:images | |
162 trackingMode:NSSegmentSwitchTrackingMomentary | |
163 target:self | |
164 action:@selector(backOrForward:)]; | |
165 control.segmentStyle = NSSegmentStyleSeparated; | |
166 [control setEnabled:commandUpdater_->IsCommandEnabled(IDC_BACK) | |
167 forSegment:kBackSegmentIndex]; | |
168 [control setEnabled:commandUpdater_->IsCommandEnabled(IDC_FORWARD) | |
169 forSegment:kForwardSegmentIndex]; | |
170 return control; | |
171 } | |
172 | |
173 - (NSView*)searchTouchBarView { | |
174 TemplateURLService* template_url_service = | |
Robert Sesek
2017/02/14 17:32:51
naming: templateUrlService
spqchan
2017/02/16 15:25:52
Done.
| |
175 TemplateURLServiceFactory::GetForProfile(browser_->profile()); | |
176 const TemplateURL* default_provider = | |
177 template_url_service->GetDefaultSearchProvider(); | |
178 BOOL isGoogle = | |
179 default_provider->GetEngineType( | |
180 template_url_service->search_terms_data()) == SEARCH_ENGINE_GOOGLE; | |
181 | |
182 base::string16 title = l10n_util::GetStringUTF16(IDS_TOUCH_BAR_SEARCH); | |
183 | |
184 gfx::VectorIconId iconId = isGoogle ? gfx::VectorIconId::G_SEARCH | |
185 : gfx::VectorIconId::OMNIBOX_SEARCH; | |
186 NSImage* image = NSImageFromImageSkiaWithColorSpace( | |
187 gfx::CreateVectorIcon(iconId, kTouchBarIconSize, | |
Robert Sesek
2017/02/14 17:32:51
Should this use CreateNSImageFromId?
spqchan
2017/02/16 15:25:52
No, iconId is type VectorIconId, which is differen
| |
188 kTouchBarDefaultIconColor), | |
189 base::mac::GetSRGBColorSpace()); | |
190 | |
191 NSButton* searchButton = | |
192 [NSButton buttonWithTitle:base::SysUTF16ToNSString(title) | |
193 image:image | |
194 target:self | |
195 action:@selector(executeCommand:)]; | |
196 searchButton.imageHugsTitle = YES; | |
197 searchButton.tag = IDC_FOCUS_LOCATION; | |
198 [searchButton.widthAnchor | |
199 constraintEqualToConstant:kTouchBarSearchButtonWidth] | |
200 .active = YES; | |
201 return searchButton; | |
202 } | |
203 | |
204 - (void)backOrForward:(id)sender { | |
205 NSSegmentedControl* control = sender; | |
206 if ([control selectedSegment] == kBackSegmentIndex) | |
207 commandUpdater_->ExecuteCommand(IDC_BACK); | |
208 else | |
209 commandUpdater_->ExecuteCommand(IDC_FORWARD); | |
210 } | |
211 | |
212 - (void)executeCommand:(id)sender { | |
213 NSButton* button = (NSButton*)sender; | |
Robert Sesek
2017/02/14 17:32:51
No C-style casts. Use ObjCCast or just do [button
spqchan
2017/02/16 15:25:52
Done.
| |
214 commandUpdater_->ExecuteCommand(button.tag); | |
215 } | |
216 | |
217 @end | |
OLD | NEW |