OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ui/views/cocoa/bridged_content_view.h" | 5 #import "ui/views/cocoa/bridged_content_view.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #import "base/mac/mac_util.h" | 8 #import "base/mac/mac_util.h" |
9 #import "base/mac/scoped_nsobject.h" | 9 #import "base/mac/scoped_nsobject.h" |
10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
11 #include "skia/ext/skia_utils_mac.h" | 11 #include "skia/ext/skia_utils_mac.h" |
12 #include "ui/base/cocoa/cocoa_base_utils.h" | 12 #include "ui/base/cocoa/cocoa_base_utils.h" |
13 #include "ui/base/ime/input_method.h" | 13 #include "ui/base/ime/input_method.h" |
14 #include "ui/base/ime/text_input_client.h" | 14 #include "ui/base/ime/text_input_client.h" |
15 #include "ui/compositor/canvas_painter.h" | 15 #include "ui/compositor/canvas_painter.h" |
16 #import "ui/events/cocoa/cocoa_event_utils.h" | 16 #import "ui/events/cocoa/cocoa_event_utils.h" |
17 #include "ui/events/keycodes/dom/dom_code.h" | 17 #include "ui/events/keycodes/dom/dom_code.h" |
18 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" | 18 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" |
19 #include "ui/gfx/canvas_paint_mac.h" | 19 #include "ui/gfx/canvas_paint_mac.h" |
20 #include "ui/gfx/geometry/rect.h" | 20 #include "ui/gfx/geometry/rect.h" |
21 #import "ui/gfx/mac/coordinate_conversion.h" | 21 #import "ui/gfx/mac/coordinate_conversion.h" |
22 #include "ui/gfx/path.h" | 22 #include "ui/gfx/path.h" |
23 #import "ui/gfx/path_mac.h" | 23 #import "ui/gfx/path_mac.h" |
24 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 24 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
25 #include "ui/strings/grit/ui_strings.h" | 25 #include "ui/strings/grit/ui_strings.h" |
26 #include "ui/views/controls/menu/menu_config.h" | 26 #include "ui/views/controls/menu/menu_config.h" |
27 #include "ui/views/controls/menu/menu_controller.h" | 27 #include "ui/views/controls/menu/menu_controller.h" |
28 #include "ui/views/view.h" | 28 #include "ui/views/view.h" |
29 #include "ui/views/widget/widget.h" | 29 #include "ui/views/widget/widget.h" |
| 30 #include "base/strings/utf_string_conversions.h" |
30 | 31 |
31 using views::MenuController; | 32 using views::MenuController; |
32 | 33 |
33 namespace { | 34 namespace { |
34 | 35 |
| 36 NSString* const kFullKeyboardAccessChangedNotification = |
| 37 @"com.apple.KeyboardUIModeDidChange"; |
| 38 |
35 // Returns true if all four corners of |rect| are contained inside |path|. | 39 // Returns true if all four corners of |rect| are contained inside |path|. |
36 bool IsRectInsidePath(NSRect rect, NSBezierPath* path) { | 40 bool IsRectInsidePath(NSRect rect, NSBezierPath* path) { |
37 return [path containsPoint:rect.origin] && | 41 return [path containsPoint:rect.origin] && |
38 [path containsPoint:NSMakePoint(rect.origin.x + rect.size.width, | 42 [path containsPoint:NSMakePoint(rect.origin.x + rect.size.width, |
39 rect.origin.y)] && | 43 rect.origin.y)] && |
40 [path containsPoint:NSMakePoint(rect.origin.x, | 44 [path containsPoint:NSMakePoint(rect.origin.x, |
41 rect.origin.y + rect.size.height)] && | 45 rect.origin.y + rect.size.height)] && |
42 [path containsPoint:NSMakePoint(rect.origin.x + rect.size.width, | 46 [path containsPoint:NSMakePoint(rect.origin.x + rect.size.width, |
43 rect.origin.y + rect.size.height)]; | 47 rect.origin.y + rect.size.height)]; |
44 } | 48 } |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 // catered for. | 182 // catered for. |
179 // Note: default key bindings in Mac can be read from StandardKeyBinding.dict | 183 // Note: default key bindings in Mac can be read from StandardKeyBinding.dict |
180 // which lives in /System/Library/Frameworks/AppKit.framework/Resources. Do | 184 // which lives in /System/Library/Frameworks/AppKit.framework/Resources. Do |
181 // `plutil -convert xml1 -o StandardKeyBinding.xml StandardKeyBinding.dict` to | 185 // `plutil -convert xml1 -o StandardKeyBinding.xml StandardKeyBinding.dict` to |
182 // get something readable. | 186 // get something readable. |
183 - (void)handleAction:(int)commandId | 187 - (void)handleAction:(int)commandId |
184 keyCode:(ui::KeyboardCode)keyCode | 188 keyCode:(ui::KeyboardCode)keyCode |
185 domCode:(ui::DomCode)domCode | 189 domCode:(ui::DomCode)domCode |
186 eventFlags:(int)eventFlags; | 190 eventFlags:(int)eventFlags; |
187 | 191 |
| 192 // Notification handler invoked when the Full Keyboard Access mode is changed. |
| 193 - (void)onFullKeyboardAccessModeChanged:(NSNotification*)notification; |
| 194 |
188 // Menu action handlers. | 195 // Menu action handlers. |
189 - (void)undo:(id)sender; | 196 - (void)undo:(id)sender; |
190 - (void)redo:(id)sender; | 197 - (void)redo:(id)sender; |
191 - (void)cut:(id)sender; | 198 - (void)cut:(id)sender; |
192 - (void)copy:(id)sender; | 199 - (void)copy:(id)sender; |
193 - (void)paste:(id)sender; | 200 - (void)paste:(id)sender; |
194 - (void)selectAll:(id)sender; | 201 - (void)selectAll:(id)sender; |
195 | 202 |
196 @end | 203 @end |
197 | 204 |
(...skipping 16 matching lines...) Expand all Loading... |
214 | 221 |
215 // Apple's documentation says that NSTrackingActiveAlways is incompatible | 222 // Apple's documentation says that NSTrackingActiveAlways is incompatible |
216 // with NSTrackingCursorUpdate, so use NSTrackingActiveInActiveApp. | 223 // with NSTrackingCursorUpdate, so use NSTrackingActiveInActiveApp. |
217 cursorTrackingArea_.reset([[CrTrackingArea alloc] | 224 cursorTrackingArea_.reset([[CrTrackingArea alloc] |
218 initWithRect:NSZeroRect | 225 initWithRect:NSZeroRect |
219 options:NSTrackingMouseMoved | NSTrackingCursorUpdate | | 226 options:NSTrackingMouseMoved | NSTrackingCursorUpdate | |
220 NSTrackingActiveInActiveApp | NSTrackingInVisibleRect | 227 NSTrackingActiveInActiveApp | NSTrackingInVisibleRect |
221 owner:self | 228 owner:self |
222 userInfo:nil]); | 229 userInfo:nil]); |
223 [self addTrackingArea:cursorTrackingArea_.get()]; | 230 [self addTrackingArea:cursorTrackingArea_.get()]; |
| 231 |
| 232 // Get notified whenever Full Keyboard Access mode is changed. |
| 233 [[NSDistributedNotificationCenter defaultCenter] |
| 234 addObserver:self |
| 235 selector:@selector(onFullKeyboardAccessModeChanged:) |
| 236 name:kFullKeyboardAccessChangedNotification |
| 237 object:nil]; |
| 238 |
| 239 // Initialize the focus manager with the correct keyboard accessibility |
| 240 // setting. |
| 241 [self updateFullKeyboardAccess]; |
224 } | 242 } |
225 return self; | 243 return self; |
226 } | 244 } |
227 | 245 |
228 - (void)clearView { | 246 - (void)clearView { |
229 textInputClient_ = nullptr; | 247 textInputClient_ = nullptr; |
230 hostedView_ = nullptr; | 248 hostedView_ = nullptr; |
231 [cursorTrackingArea_.get() clearOwner]; | 249 [cursorTrackingArea_.get() clearOwner]; |
232 [self removeTrackingArea:cursorTrackingArea_.get()]; | 250 [self removeTrackingArea:cursorTrackingArea_.get()]; |
| 251 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self]; |
233 } | 252 } |
234 | 253 |
235 - (void)processCapturedMouseEvent:(NSEvent*)theEvent { | 254 - (void)processCapturedMouseEvent:(NSEvent*)theEvent { |
236 if (!hostedView_) | 255 if (!hostedView_) |
237 return; | 256 return; |
238 | 257 |
239 NSWindow* source = [theEvent window]; | 258 NSWindow* source = [theEvent window]; |
240 NSWindow* target = [self window]; | 259 NSWindow* target = [self window]; |
241 DCHECK(target); | 260 DCHECK(target); |
242 | 261 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 | 305 |
287 windowMask_.reset([gfx::CreateNSBezierPathFromSkPath(mask) retain]); | 306 windowMask_.reset([gfx::CreateNSBezierPathFromSkPath(mask) retain]); |
288 | 307 |
289 // Convert to AppKit coordinate system. | 308 // Convert to AppKit coordinate system. |
290 NSAffineTransform* flipTransform = [NSAffineTransform transform]; | 309 NSAffineTransform* flipTransform = [NSAffineTransform transform]; |
291 [flipTransform translateXBy:0.0 yBy:frameRect.size.height]; | 310 [flipTransform translateXBy:0.0 yBy:frameRect.size.height]; |
292 [flipTransform scaleXBy:1.0 yBy:-1.0]; | 311 [flipTransform scaleXBy:1.0 yBy:-1.0]; |
293 [windowMask_ transformUsingAffineTransform:flipTransform]; | 312 [windowMask_ transformUsingAffineTransform:flipTransform]; |
294 } | 313 } |
295 | 314 |
| 315 - (void)updateFullKeyboardAccess { |
| 316 if (!hostedView_) |
| 317 return; |
| 318 |
| 319 DCHECK(hostedView_->GetWidget()->GetFocusManager()); |
| 320 hostedView_->GetWidget()->GetFocusManager()->SetKeyboardAccessible( |
| 321 [NSApp isFullKeyboardAccessEnabled]); |
| 322 } |
| 323 |
296 // BridgedContentView private implementation. | 324 // BridgedContentView private implementation. |
297 | 325 |
298 - (void)handleKeyEvent:(NSEvent*)theEvent { | 326 - (void)handleKeyEvent:(NSEvent*)theEvent { |
299 if (!hostedView_) | 327 if (!hostedView_) |
300 return; | 328 return; |
301 | 329 |
302 DCHECK(theEvent); | 330 DCHECK(theEvent); |
303 ui::KeyEvent event(theEvent); | 331 ui::KeyEvent event(theEvent); |
304 if (DispatchEventToMenu(hostedView_->GetWidget(), event.key_code())) | 332 if (DispatchEventToMenu(hostedView_->GetWidget(), event.key_code())) |
305 return; | 333 return; |
(...skipping 15 matching lines...) Expand all Loading... |
321 // performed. | 349 // performed. |
322 if (commandId && textInputClient_ && | 350 if (commandId && textInputClient_ && |
323 textInputClient_->IsEditCommandEnabled(commandId)) | 351 textInputClient_->IsEditCommandEnabled(commandId)) |
324 textInputClient_->SetEditCommandForNextKeyEvent(commandId); | 352 textInputClient_->SetEditCommandForNextKeyEvent(commandId); |
325 | 353 |
326 // Generate a synthetic event with the keycode toolkit-views expects. | 354 // Generate a synthetic event with the keycode toolkit-views expects. |
327 ui::KeyEvent event(ui::ET_KEY_PRESSED, keyCode, domCode, eventFlags); | 355 ui::KeyEvent event(ui::ET_KEY_PRESSED, keyCode, domCode, eventFlags); |
328 hostedView_->GetWidget()->GetInputMethod()->DispatchKeyEvent(&event); | 356 hostedView_->GetWidget()->GetInputMethod()->DispatchKeyEvent(&event); |
329 } | 357 } |
330 | 358 |
| 359 - (void)onFullKeyboardAccessModeChanged:(NSNotification*)notification { |
| 360 DCHECK([[notification name] |
| 361 isEqualToString:kFullKeyboardAccessChangedNotification]); |
| 362 [self updateFullKeyboardAccess]; |
| 363 } |
| 364 |
331 - (void)undo:(id)sender { | 365 - (void)undo:(id)sender { |
332 // This DCHECK is more strict than a similar check in handleAction:. It can be | 366 // This DCHECK is more strict than a similar check in handleAction:. It can be |
333 // done here because the actors sending these actions should be calling | 367 // done here because the actors sending these actions should be calling |
334 // validateUserInterfaceItem: before enabling UI that allows these messages to | 368 // validateUserInterfaceItem: before enabling UI that allows these messages to |
335 // be sent. Checking it here would be too late to provide correct UI feedback | 369 // be sent. Checking it here would be too late to provide correct UI feedback |
336 // (e.g. there will be no "beep"). | 370 // (e.g. there will be no "beep"). |
337 DCHECK(textInputClient_->IsEditCommandEnabled(IDS_APP_UNDO)); | 371 DCHECK(textInputClient_->IsEditCommandEnabled(IDS_APP_UNDO)); |
338 [self handleAction:IDS_APP_UNDO | 372 [self handleAction:IDS_APP_UNDO |
339 keyCode:ui::VKEY_Z | 373 keyCode:ui::VKEY_Z |
340 domCode:ui::DomCode::US_Z | 374 domCode:ui::DomCode::US_Z |
(...skipping 586 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
927 } | 961 } |
928 | 962 |
929 return [super accessibilityAttributeValue:attribute]; | 963 return [super accessibilityAttributeValue:attribute]; |
930 } | 964 } |
931 | 965 |
932 - (id)accessibilityHitTest:(NSPoint)point { | 966 - (id)accessibilityHitTest:(NSPoint)point { |
933 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; | 967 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; |
934 } | 968 } |
935 | 969 |
936 @end | 970 @end |
OLD | NEW |