| 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/scoped_nsobject.h" | 8 #import "base/mac/scoped_nsobject.h" |
| 9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "ui/base/ime/text_input_client.h" | 10 #include "ui/base/ime/text_input_client.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 ui::POST_DISPATCH_NONE) | 51 ui::POST_DISPATCH_NONE) |
| 52 return true; | 52 return true; |
| 53 } | 53 } |
| 54 return false; | 54 return false; |
| 55 } | 55 } |
| 56 | 56 |
| 57 } // namespace | 57 } // namespace |
| 58 | 58 |
| 59 @interface BridgedContentView () | 59 @interface BridgedContentView () |
| 60 | 60 |
| 61 // Translates the location of |theEvent| to toolkit-views coordinates and passes | |
| 62 // the event to NativeWidgetMac for handling. | |
| 63 - (void)handleMouseEvent:(NSEvent*)theEvent; | |
| 64 | |
| 65 // Translates keycodes and modifiers on |theEvent| to ui::KeyEvents and passes | 61 // Translates keycodes and modifiers on |theEvent| to ui::KeyEvents and passes |
| 66 // the event to the InputMethod for dispatch. | 62 // the event to the InputMethod for dispatch. |
| 67 - (void)handleKeyEvent:(NSEvent*)theEvent; | 63 - (void)handleKeyEvent:(NSEvent*)theEvent; |
| 68 | 64 |
| 69 // Handles an NSResponder Action Message by mapping it to a corresponding text | 65 // Handles an NSResponder Action Message by mapping it to a corresponding text |
| 70 // editing command from ui_strings.grd and, when not being sent to a | 66 // editing command from ui_strings.grd and, when not being sent to a |
| 71 // TextInputClient, the keyCode that toolkit-views expects internally. | 67 // TextInputClient, the keyCode that toolkit-views expects internally. |
| 72 // For example, moveToLeftEndOfLine: would pass ui::VKEY_HOME in non-RTL locales | 68 // For example, moveToLeftEndOfLine: would pass ui::VKEY_HOME in non-RTL locales |
| 73 // even though the Home key on Mac defaults to moveToBeginningOfDocument:. | 69 // even though the Home key on Mac defaults to moveToBeginningOfDocument:. |
| 74 // This approach also allows action messages a user | 70 // This approach also allows action messages a user |
| (...skipping 28 matching lines...) Expand all Loading... |
| 103 gfx::Rect bounds = viewToHost->bounds(); | 99 gfx::Rect bounds = viewToHost->bounds(); |
| 104 // To keep things simple, assume the origin is (0, 0) until there exists a use | 100 // To keep things simple, assume the origin is (0, 0) until there exists a use |
| 105 // case for something other than that. | 101 // case for something other than that. |
| 106 DCHECK(bounds.origin().IsOrigin()); | 102 DCHECK(bounds.origin().IsOrigin()); |
| 107 NSRect initialFrame = NSMakeRect(0, 0, bounds.width(), bounds.height()); | 103 NSRect initialFrame = NSMakeRect(0, 0, bounds.width(), bounds.height()); |
| 108 if ((self = [super initWithFrame:initialFrame])) { | 104 if ((self = [super initWithFrame:initialFrame])) { |
| 109 hostedView_ = viewToHost; | 105 hostedView_ = viewToHost; |
| 110 | 106 |
| 111 // Apple's documentation says that NSTrackingActiveAlways is incompatible | 107 // Apple's documentation says that NSTrackingActiveAlways is incompatible |
| 112 // with NSTrackingCursorUpdate, so use NSTrackingActiveInActiveApp. | 108 // with NSTrackingCursorUpdate, so use NSTrackingActiveInActiveApp. |
| 113 trackingArea_.reset([[CrTrackingArea alloc] | 109 cursorTrackingArea_.reset([[CrTrackingArea alloc] |
| 114 initWithRect:NSZeroRect | 110 initWithRect:NSZeroRect |
| 115 options:NSTrackingMouseMoved | NSTrackingCursorUpdate | | 111 options:NSTrackingMouseMoved | NSTrackingCursorUpdate | |
| 116 NSTrackingActiveInActiveApp | NSTrackingInVisibleRect | 112 NSTrackingActiveInActiveApp | NSTrackingInVisibleRect |
| 117 owner:self | 113 owner:self |
| 118 userInfo:nil]); | 114 userInfo:nil]); |
| 119 [self addTrackingArea:trackingArea_.get()]; | 115 [self addTrackingArea:cursorTrackingArea_.get()]; |
| 120 } | 116 } |
| 121 return self; | 117 return self; |
| 122 } | 118 } |
| 123 | 119 |
| 124 - (void)clearView { | 120 - (void)clearView { |
| 125 hostedView_ = NULL; | 121 hostedView_ = NULL; |
| 126 [trackingArea_.get() clearOwner]; | 122 [cursorTrackingArea_.get() clearOwner]; |
| 127 [self removeTrackingArea:trackingArea_.get()]; | 123 [self removeTrackingArea:cursorTrackingArea_.get()]; |
| 128 } | 124 } |
| 129 | 125 |
| 130 - (void)processCapturedMouseEvent:(NSEvent*)theEvent { | 126 - (void)processCapturedMouseEvent:(NSEvent*)theEvent { |
| 131 if (!hostedView_) | 127 if (!hostedView_) |
| 132 return; | 128 return; |
| 133 | 129 |
| 134 NSWindow* source = [theEvent window]; | 130 NSWindow* source = [theEvent window]; |
| 135 NSWindow* target = [self window]; | 131 NSWindow* target = [self window]; |
| 136 DCHECK(target); | 132 DCHECK(target); |
| 137 | 133 |
| 138 // If it's the view's window, process normally. | 134 // If it's the view's window, process normally. |
| 139 if ([target isEqual:source]) { | 135 if ([target isEqual:source]) { |
| 140 [self handleMouseEvent:theEvent]; | 136 [self mouseEvent:theEvent]; |
| 141 return; | 137 return; |
| 142 } | 138 } |
| 143 | 139 |
| 144 ui::MouseEvent event(theEvent); | 140 ui::MouseEvent event(theEvent); |
| 145 event.set_location( | 141 event.set_location( |
| 146 MovePointToWindow([theEvent locationInWindow], source, target)); | 142 MovePointToWindow([theEvent locationInWindow], source, target)); |
| 147 hostedView_->GetWidget()->OnMouseEvent(&event); | 143 hostedView_->GetWidget()->OnMouseEvent(&event); |
| 148 } | 144 } |
| 149 | 145 |
| 146 - (void)updateTooltipIfRequiredAt:(const gfx::Point&)locationInWindow { |
| 147 DCHECK(hostedView_); |
| 148 base::string16 newTooltipText; |
| 149 |
| 150 views::View* view = hostedView_->GetTooltipHandlerForPoint(locationInWindow); |
| 151 if (view) { |
| 152 gfx::Point viewPoint = locationInWindow; |
| 153 views::View::ConvertPointToTarget(hostedView_, view, &viewPoint); |
| 154 if (!view->GetTooltipText(viewPoint, &newTooltipText)) |
| 155 DCHECK(newTooltipText.empty()); |
| 156 } |
| 157 if (newTooltipText != lastTooltipText_) { |
| 158 std::swap(newTooltipText, lastTooltipText_); |
| 159 [self setToolTipAtMousePoint:base::SysUTF16ToNSString(lastTooltipText_)]; |
| 160 } |
| 161 } |
| 162 |
| 150 // BridgedContentView private implementation. | 163 // BridgedContentView private implementation. |
| 151 | 164 |
| 152 - (void)handleMouseEvent:(NSEvent*)theEvent { | |
| 153 if (!hostedView_) | |
| 154 return; | |
| 155 | |
| 156 ui::MouseEvent event(theEvent); | |
| 157 hostedView_->GetWidget()->OnMouseEvent(&event); | |
| 158 } | |
| 159 | |
| 160 - (void)handleKeyEvent:(NSEvent*)theEvent { | 165 - (void)handleKeyEvent:(NSEvent*)theEvent { |
| 161 if (!hostedView_) | 166 if (!hostedView_) |
| 162 return; | 167 return; |
| 163 | 168 |
| 164 DCHECK(theEvent); | 169 DCHECK(theEvent); |
| 165 ui::KeyEvent event(theEvent); | 170 ui::KeyEvent event(theEvent); |
| 166 if (DispatchEventToMenu(hostedView_->GetWidget(), event.key_code())) | 171 if (DispatchEventToMenu(hostedView_->GetWidget(), event.key_code())) |
| 167 return; | 172 return; |
| 168 | 173 |
| 169 hostedView_->GetWidget()->GetInputMethod()->DispatchKeyEvent(event); | 174 hostedView_->GetWidget()->GetInputMethod()->DispatchKeyEvent(event); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 } | 241 } |
| 237 | 242 |
| 238 - (void)selectAll:(id)sender { | 243 - (void)selectAll:(id)sender { |
| 239 DCHECK(textInputClient_->IsEditCommandEnabled(IDS_APP_SELECT_ALL)); | 244 DCHECK(textInputClient_->IsEditCommandEnabled(IDS_APP_SELECT_ALL)); |
| 240 [self handleAction:IDS_APP_SELECT_ALL | 245 [self handleAction:IDS_APP_SELECT_ALL |
| 241 keyCode:ui::VKEY_A | 246 keyCode:ui::VKEY_A |
| 242 domCode:ui::DomCode::KEY_A | 247 domCode:ui::DomCode::KEY_A |
| 243 eventFlags:ui::EF_CONTROL_DOWN]; | 248 eventFlags:ui::EF_CONTROL_DOWN]; |
| 244 } | 249 } |
| 245 | 250 |
| 251 // BaseView implementation. |
| 252 |
| 253 // Don't use tracking areas from BaseView. BridgedContentView's tracks |
| 254 // NSTrackingCursorUpdate and Apple's documentation suggests it's incompatible. |
| 255 - (void)enableTracking { |
| 256 } |
| 257 |
| 258 // Translates the location of |theEvent| to toolkit-views coordinates and passes |
| 259 // the event to NativeWidgetMac for handling. |
| 260 - (void)mouseEvent:(NSEvent*)theEvent { |
| 261 if (!hostedView_) |
| 262 return; |
| 263 |
| 264 ui::MouseEvent event(theEvent); |
| 265 |
| 266 // Aura updates tooltips with the help of aura::Window::AddPreTargetHandler(). |
| 267 // Mac hooks in here. |
| 268 [self updateTooltipIfRequiredAt:event.location()]; |
| 269 |
| 270 hostedView_->GetWidget()->OnMouseEvent(&event); |
| 271 } |
| 272 |
| 246 // NSView implementation. | 273 // NSView implementation. |
| 247 | 274 |
| 248 - (BOOL)acceptsFirstResponder { | 275 - (BOOL)acceptsFirstResponder { |
| 249 return YES; | 276 return YES; |
| 250 } | 277 } |
| 251 | 278 |
| 252 - (void)viewDidMoveToWindow { | 279 - (void)viewDidMoveToWindow { |
| 253 // When this view is added to a window, AppKit calls setFrameSize before it is | 280 // When this view is added to a window, AppKit calls setFrameSize before it is |
| 254 // added to the window, so the behavior in setFrameSize is not triggered. | 281 // added to the window, so the behavior in setFrameSize is not triggered. |
| 255 NSWindow* window = [self window]; | 282 NSWindow* window = [self window]; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 | 331 |
| 305 // NSResponder implementation. | 332 // NSResponder implementation. |
| 306 | 333 |
| 307 - (void)keyDown:(NSEvent*)theEvent { | 334 - (void)keyDown:(NSEvent*)theEvent { |
| 308 // Convert the event into an action message, according to OSX key mappings. | 335 // Convert the event into an action message, according to OSX key mappings. |
| 309 inKeyDown_ = YES; | 336 inKeyDown_ = YES; |
| 310 [self interpretKeyEvents:@[ theEvent ]]; | 337 [self interpretKeyEvents:@[ theEvent ]]; |
| 311 inKeyDown_ = NO; | 338 inKeyDown_ = NO; |
| 312 } | 339 } |
| 313 | 340 |
| 314 - (void)mouseDown:(NSEvent*)theEvent { | |
| 315 [self handleMouseEvent:theEvent]; | |
| 316 } | |
| 317 | |
| 318 - (void)rightMouseDown:(NSEvent*)theEvent { | |
| 319 [self handleMouseEvent:theEvent]; | |
| 320 } | |
| 321 | |
| 322 - (void)otherMouseDown:(NSEvent*)theEvent { | |
| 323 [self handleMouseEvent:theEvent]; | |
| 324 } | |
| 325 | |
| 326 - (void)mouseUp:(NSEvent*)theEvent { | |
| 327 [self handleMouseEvent:theEvent]; | |
| 328 } | |
| 329 | |
| 330 - (void)rightMouseUp:(NSEvent*)theEvent { | |
| 331 [self handleMouseEvent:theEvent]; | |
| 332 } | |
| 333 | |
| 334 - (void)otherMouseUp:(NSEvent*)theEvent { | |
| 335 [self handleMouseEvent:theEvent]; | |
| 336 } | |
| 337 | |
| 338 - (void)mouseDragged:(NSEvent*)theEvent { | |
| 339 [self handleMouseEvent:theEvent]; | |
| 340 } | |
| 341 | |
| 342 - (void)rightMouseDragged:(NSEvent*)theEvent { | |
| 343 [self handleMouseEvent:theEvent]; | |
| 344 } | |
| 345 | |
| 346 - (void)otherMouseDragged:(NSEvent*)theEvent { | |
| 347 [self handleMouseEvent:theEvent]; | |
| 348 } | |
| 349 | |
| 350 - (void)mouseMoved:(NSEvent*)theEvent { | |
| 351 // Note: mouseEntered: and mouseExited: are not handled separately. | |
| 352 // |hostedView_| is responsible for converting the move events into entered | |
| 353 // and exited events for the view heirarchy. | |
| 354 [self handleMouseEvent:theEvent]; | |
| 355 } | |
| 356 | |
| 357 - (void)scrollWheel:(NSEvent*)theEvent { | 341 - (void)scrollWheel:(NSEvent*)theEvent { |
| 358 if (!hostedView_) | 342 if (!hostedView_) |
| 359 return; | 343 return; |
| 360 | 344 |
| 361 ui::MouseWheelEvent event(theEvent); | 345 ui::MouseWheelEvent event(theEvent); |
| 362 hostedView_->GetWidget()->OnMouseEvent(&event); | 346 hostedView_->GetWidget()->OnMouseEvent(&event); |
| 363 } | 347 } |
| 364 | 348 |
| 365 //////////////////////////////////////////////////////////////////////////////// | 349 //////////////////////////////////////////////////////////////////////////////// |
| 366 // NSResponder Action Messages. Keep sorted according NSResponder.h (from the | 350 // NSResponder Action Messages. Keep sorted according NSResponder.h (from the |
| (...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 721 } | 705 } |
| 722 | 706 |
| 723 return [super accessibilityAttributeValue:attribute]; | 707 return [super accessibilityAttributeValue:attribute]; |
| 724 } | 708 } |
| 725 | 709 |
| 726 - (id)accessibilityHitTest:(NSPoint)point { | 710 - (id)accessibilityHitTest:(NSPoint)point { |
| 727 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; | 711 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; |
| 728 } | 712 } |
| 729 | 713 |
| 730 @end | 714 @end |
| OLD | NEW |