| 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 #import "base/mac/sdk_forward_declarations.h" | 10 #import "base/mac/sdk_forward_declarations.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 bool IsRectInsidePath(NSRect rect, NSBezierPath* path) { | 48 bool IsRectInsidePath(NSRect rect, NSBezierPath* path) { |
| 49 return [path containsPoint:rect.origin] && | 49 return [path containsPoint:rect.origin] && |
| 50 [path containsPoint:NSMakePoint(rect.origin.x + rect.size.width, | 50 [path containsPoint:NSMakePoint(rect.origin.x + rect.size.width, |
| 51 rect.origin.y)] && | 51 rect.origin.y)] && |
| 52 [path containsPoint:NSMakePoint(rect.origin.x, | 52 [path containsPoint:NSMakePoint(rect.origin.x, |
| 53 rect.origin.y + rect.size.height)] && | 53 rect.origin.y + rect.size.height)] && |
| 54 [path containsPoint:NSMakePoint(rect.origin.x + rect.size.width, | 54 [path containsPoint:NSMakePoint(rect.origin.x + rect.size.width, |
| 55 rect.origin.y + rect.size.height)]; | 55 rect.origin.y + rect.size.height)]; |
| 56 } | 56 } |
| 57 | 57 |
| 58 // Convert a |point| in |source_window|'s AppKit coordinate system (origin at | |
| 59 // the bottom left of the window) to |target_window|'s content rect, with the | |
| 60 // origin at the top left of the content area. | |
| 61 // If |source_window| is nil, |point| will be treated as screen coordinates. | |
| 62 gfx::Point MovePointToWindow(const NSPoint& point, | |
| 63 NSWindow* source_window, | |
| 64 NSWindow* target_window) { | |
| 65 NSPoint point_in_screen = source_window | |
| 66 ? ui::ConvertPointFromWindowToScreen(source_window, point) | |
| 67 : point; | |
| 68 | |
| 69 NSPoint point_in_window = | |
| 70 ui::ConvertPointFromScreenToWindow(target_window, point_in_screen); | |
| 71 NSRect content_rect = | |
| 72 [target_window contentRectForFrameRect:[target_window frame]]; | |
| 73 return gfx::Point(point_in_window.x, | |
| 74 NSHeight(content_rect) - point_in_window.y); | |
| 75 } | |
| 76 | |
| 77 // Dispatch |event| to |menu_controller| and return true if |event| is | 58 // Dispatch |event| to |menu_controller| and return true if |event| is |
| 78 // swallowed. | 59 // swallowed. |
| 79 bool DispatchEventToMenu(MenuController* menu_controller, ui::KeyEvent* event) { | 60 bool DispatchEventToMenu(MenuController* menu_controller, ui::KeyEvent* event) { |
| 80 return menu_controller && | 61 return menu_controller && |
| 81 menu_controller->OnWillDispatchKeyEvent(event) == | 62 menu_controller->OnWillDispatchKeyEvent(event) == |
| 82 ui::POST_DISPATCH_NONE; | 63 ui::POST_DISPATCH_NONE; |
| 83 } | 64 } |
| 84 | 65 |
| 85 // Returns true if |client| has RTL text. | 66 // Returns true if |client| has RTL text. |
| 86 bool IsTextRTL(const ui::TextInputClient* client) { | 67 bool IsTextRTL(const ui::TextInputClient* client) { |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 } | 319 } |
| 339 | 320 |
| 340 - (void)clearView { | 321 - (void)clearView { |
| 341 textInputClient_ = nullptr; | 322 textInputClient_ = nullptr; |
| 342 hostedView_ = nullptr; | 323 hostedView_ = nullptr; |
| 343 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self]; | 324 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self]; |
| 344 [cursorTrackingArea_.get() clearOwner]; | 325 [cursorTrackingArea_.get() clearOwner]; |
| 345 [self removeTrackingArea:cursorTrackingArea_.get()]; | 326 [self removeTrackingArea:cursorTrackingArea_.get()]; |
| 346 } | 327 } |
| 347 | 328 |
| 348 - (void)processCapturedMouseEvent:(NSEvent*)theEvent { | |
| 349 if (!hostedView_) | |
| 350 return; | |
| 351 | |
| 352 NSWindow* source = [theEvent window]; | |
| 353 NSWindow* target = [self window]; | |
| 354 DCHECK(target); | |
| 355 | |
| 356 // If it's the view's window, process normally. | |
| 357 if ([target isEqual:source]) { | |
| 358 [self mouseEvent:theEvent]; | |
| 359 return; | |
| 360 } | |
| 361 | |
| 362 ui::MouseEvent event(theEvent); | |
| 363 event.set_location( | |
| 364 MovePointToWindow([theEvent locationInWindow], source, target)); | |
| 365 hostedView_->GetWidget()->OnMouseEvent(&event); | |
| 366 } | |
| 367 | |
| 368 - (void)updateTooltipIfRequiredAt:(const gfx::Point&)locationInContent { | 329 - (void)updateTooltipIfRequiredAt:(const gfx::Point&)locationInContent { |
| 369 DCHECK(hostedView_); | 330 DCHECK(hostedView_); |
| 370 base::string16 newTooltipText; | 331 base::string16 newTooltipText; |
| 371 | 332 |
| 372 views::View* view = hostedView_->GetTooltipHandlerForPoint(locationInContent); | 333 views::View* view = hostedView_->GetTooltipHandlerForPoint(locationInContent); |
| 373 if (view) { | 334 if (view) { |
| 374 gfx::Point viewPoint = locationInContent; | 335 gfx::Point viewPoint = locationInContent; |
| 375 views::View::ConvertPointToScreen(hostedView_, &viewPoint); | 336 views::View::ConvertPointToScreen(hostedView_, &viewPoint); |
| 376 views::View::ConvertPointFromScreen(view, &viewPoint); | 337 views::View::ConvertPointFromScreen(view, &viewPoint); |
| 377 if (!view->GetTooltipText(viewPoint, &newTooltipText)) | 338 if (!view->GetTooltipText(viewPoint, &newTooltipText)) |
| 378 DCHECK(newTooltipText.empty()); | 339 DCHECK(newTooltipText.empty()); |
| 379 } | 340 } |
| 380 if (newTooltipText != lastTooltipText_) { | 341 if (newTooltipText != lastTooltipText_) { |
| 381 std::swap(newTooltipText, lastTooltipText_); | 342 std::swap(newTooltipText, lastTooltipText_); |
| 382 [self setToolTipAtMousePoint:base::SysUTF16ToNSString(lastTooltipText_)]; | 343 [self setToolTipAtMousePoint:base::SysUTF16ToNSString(lastTooltipText_)]; |
| 383 } | 344 } |
| 384 } | 345 } |
| 385 | 346 |
| 347 - (void)hideTooltipIfRequired { |
| 348 if (!lastTooltipText_.empty()) { |
| 349 lastTooltipText_.clear(); |
| 350 [self setToolTipAtMousePoint:nil]; |
| 351 } |
| 352 } |
| 353 |
| 386 - (void)updateWindowMask { | 354 - (void)updateWindowMask { |
| 387 DCHECK(![self inLiveResize]); | 355 DCHECK(![self inLiveResize]); |
| 388 DCHECK(base::mac::IsOS10_9()); | 356 DCHECK(base::mac::IsOS10_9()); |
| 389 DCHECK(hostedView_); | 357 DCHECK(hostedView_); |
| 390 | 358 |
| 391 views::Widget* widget = hostedView_->GetWidget(); | 359 views::Widget* widget = hostedView_->GetWidget(); |
| 392 if (!widget->non_client_view()) | 360 if (!widget->non_client_view()) |
| 393 return; | 361 return; |
| 394 | 362 |
| 395 const NSRect frameRect = [self bounds]; | 363 const NSRect frameRect = [self bounds]; |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 eventFlags:ui::EF_CONTROL_DOWN]; | 541 eventFlags:ui::EF_CONTROL_DOWN]; |
| 574 } | 542 } |
| 575 | 543 |
| 576 // BaseView implementation. | 544 // BaseView implementation. |
| 577 | 545 |
| 578 // Don't use tracking areas from BaseView. BridgedContentView's tracks | 546 // Don't use tracking areas from BaseView. BridgedContentView's tracks |
| 579 // NSTrackingCursorUpdate and Apple's documentation suggests it's incompatible. | 547 // NSTrackingCursorUpdate and Apple's documentation suggests it's incompatible. |
| 580 - (void)enableTracking { | 548 - (void)enableTracking { |
| 581 } | 549 } |
| 582 | 550 |
| 551 // Explicitly ignore BaseView's |dragging_| logic |
| 552 - (void)mouseDown:(NSEvent*)theEvent { |
| 553 [self mouseEvent:theEvent]; |
| 554 } |
| 555 |
| 583 // Translates the location of |theEvent| to toolkit-views coordinates and passes | 556 // Translates the location of |theEvent| to toolkit-views coordinates and passes |
| 584 // the event to NativeWidgetMac for handling. | 557 // the event to NativeWidgetMac for handling. |
| 585 - (void)mouseEvent:(NSEvent*)theEvent { | 558 - (void)mouseEvent:(NSEvent*)theEvent { |
| 586 if (!hostedView_) | 559 if (!hostedView_) |
| 587 return; | 560 return; |
| 588 | 561 views::NativeWidgetMac::GetBridgeForNativeWindow([self window]) |
| 589 ui::MouseEvent event(theEvent); | 562 ->OnMouseEvent(theEvent); |
| 590 | |
| 591 // Aura updates tooltips with the help of aura::Window::AddPreTargetHandler(). | |
| 592 // Mac hooks in here. | |
| 593 [self updateTooltipIfRequiredAt:event.location()]; | |
| 594 | |
| 595 hostedView_->GetWidget()->OnMouseEvent(&event); | |
| 596 } | 563 } |
| 597 | 564 |
| 598 // NSView implementation. | 565 // NSView implementation. |
| 599 | 566 |
| 600 - (BOOL)acceptsFirstResponder { | 567 - (BOOL)acceptsFirstResponder { |
| 601 return YES; | 568 return YES; |
| 602 } | 569 } |
| 603 | 570 |
| 604 - (BOOL)becomeFirstResponder { | 571 - (BOOL)becomeFirstResponder { |
| 605 BOOL result = [super becomeFirstResponder]; | 572 BOOL result = [super becomeFirstResponder]; |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 769 | 736 |
| 770 - (void)keyUp:(NSEvent*)theEvent { | 737 - (void)keyUp:(NSEvent*)theEvent { |
| 771 ui::KeyEvent event(theEvent); | 738 ui::KeyEvent event(theEvent); |
| 772 [self handleKeyEvent:&event]; | 739 [self handleKeyEvent:&event]; |
| 773 } | 740 } |
| 774 | 741 |
| 775 - (void)scrollWheel:(NSEvent*)theEvent { | 742 - (void)scrollWheel:(NSEvent*)theEvent { |
| 776 if (!hostedView_) | 743 if (!hostedView_) |
| 777 return; | 744 return; |
| 778 | 745 |
| 779 ui::ScrollEvent event(theEvent); | 746 // Passes the event to BridgedNativeWidget for handling. |
| 780 hostedView_->GetWidget()->OnScrollEvent(&event); | 747 views::NativeWidgetMac::GetBridgeForNativeWindow([self window]) |
| 748 ->OnMouseEvent(theEvent); |
| 781 } | 749 } |
| 782 | 750 |
| 783 - (void)quickLookWithEvent:(NSEvent*)theEvent { | 751 - (void)quickLookWithEvent:(NSEvent*)theEvent { |
| 784 if (!hostedView_) | 752 if (!hostedView_) |
| 785 return; | 753 return; |
| 786 | 754 |
| 787 const gfx::Point locationInContent = ui::EventLocationFromNative(theEvent); | 755 const gfx::Point locationInContent = ui::EventLocationFromNative(theEvent); |
| 788 views::View* target = hostedView_->GetEventHandlerForPoint(locationInContent); | 756 views::View* target = hostedView_->GetEventHandlerForPoint(locationInContent); |
| 789 if (!target) | 757 if (!target) |
| 790 return; | 758 return; |
| (...skipping 641 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1432 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; | 1400 return [hostedView_->GetNativeViewAccessible() accessibilityHitTest:point]; |
| 1433 } | 1401 } |
| 1434 | 1402 |
| 1435 - (id)accessibilityFocusedUIElement { | 1403 - (id)accessibilityFocusedUIElement { |
| 1436 if (!hostedView_) | 1404 if (!hostedView_) |
| 1437 return nil; | 1405 return nil; |
| 1438 return [hostedView_->GetNativeViewAccessible() accessibilityFocusedUIElement]; | 1406 return [hostedView_->GetNativeViewAccessible() accessibilityFocusedUIElement]; |
| 1439 } | 1407 } |
| 1440 | 1408 |
| 1441 @end | 1409 @end |
| OLD | NEW |