Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions shell/platform/darwin/macos/framework/Source/FlutterView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ - (void)didUpdateMouseCursor:(NSCursor*)cursor {
// - When context menu above FlutterView is closed. Context menu will change current cursor to arrow
// and will not restore it back.
- (void)cursorUpdate:(NSEvent*)event {
// Make sure to not override cursor when over a platform view.
NSView* hitTestView = [self hitTest:[self convertPoint:event.locationInWindow fromView:nil]];
if (hitTestView != self) {
return;
}
[_lastCursor set];
// It is possible that there is a platform view with NSTrackingArea below flutter content.
// This could override the mouse cursor as a result of mouse move event. There is no good way
Expand Down
121 changes: 121 additions & 0 deletions shell/platform/darwin/macos/framework/Source/FlutterViewTest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,124 @@ - (BOOL)viewShouldAcceptFirstResponder:(NSView*)view {
viewId:kImplicitViewId];
EXPECT_EQ([view layer:view.layer shouldInheritContentsScale:3.0 fromWindow:view.window], YES);
}

@interface TestFlutterView : FlutterView

@property(readwrite, nonatomic) NSView* (^onHitTest)(NSPoint point);

@end

@implementation TestFlutterView

@synthesize onHitTest;

- (NSView*)hitTest:(NSPoint)point {
return self.onHitTest(point);
}

- (void)reshaped {
// Disable resize synchronization for testing.
}

@end

@interface TestCursor : NSCursor
@property(readwrite, nonatomic) BOOL setCalled;
@end

@implementation TestCursor

- (void)set {
self.setCalled = YES;
}

@end

TEST(FlutterView, CursorUpdateDoesHitTest) {
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
id<MTLCommandQueue> queue = [device newCommandQueue];
TestFlutterViewDelegate* delegate = [[TestFlutterViewDelegate alloc] init];
FlutterThreadSynchronizer* threadSynchronizer = [[FlutterThreadSynchronizer alloc] init];
TestFlutterView* view = [[TestFlutterView alloc] initWithMTLDevice:device
commandQueue:queue
delegate:delegate
threadSynchronizer:threadSynchronizer
viewId:kImplicitViewId];
NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];

TestCursor* cursor = [[TestCursor alloc] init];

window.contentView = view;
__weak NSView* weakView = view;
__block BOOL hitTestCalled = NO;
__block NSPoint hitTestCoordinate = NSZeroPoint;
view.onHitTest = ^NSView*(NSPoint point) {
hitTestCalled = YES;
hitTestCoordinate = point;
return weakView;
};
NSEvent* mouseEvent = [NSEvent mouseEventWithType:NSEventTypeMouseMoved
location:NSMakePoint(100, 100)
modifierFlags:0
timestamp:0
windowNumber:0
context:nil
eventNumber:0
clickCount:0
pressure:0];
[view didUpdateMouseCursor:cursor];
[view cursorUpdate:mouseEvent];

EXPECT_TRUE(hitTestCalled);
// The hit test coordinate should be in the window coordinate system.
EXPECT_TRUE(CGPointEqualToPoint(hitTestCoordinate, CGPointMake(100, 500)));
EXPECT_TRUE(cursor.setCalled);
}

TEST(FlutterView, CursorUpdateDoesNotOverridePlatformView) {
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
id<MTLCommandQueue> queue = [device newCommandQueue];
TestFlutterViewDelegate* delegate = [[TestFlutterViewDelegate alloc] init];
FlutterThreadSynchronizer* threadSynchronizer = [[FlutterThreadSynchronizer alloc] init];
TestFlutterView* view = [[TestFlutterView alloc] initWithMTLDevice:device
commandQueue:queue
delegate:delegate
threadSynchronizer:threadSynchronizer
viewId:kImplicitViewId];
NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];

TestCursor* cursor = [[TestCursor alloc] init];

NSView* platformView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];

window.contentView = view;
__block BOOL hitTestCalled = NO;
__block NSPoint hitTestCoordinate = NSZeroPoint;
view.onHitTest = ^NSView*(NSPoint point) {
hitTestCalled = YES;
hitTestCoordinate = point;
return platformView;
};
NSEvent* mouseEvent = [NSEvent mouseEventWithType:NSEventTypeMouseMoved
location:NSMakePoint(100, 100)
modifierFlags:0
timestamp:0
windowNumber:0
context:nil
eventNumber:0
clickCount:0
pressure:0];
[view didUpdateMouseCursor:cursor];
[view cursorUpdate:mouseEvent];

EXPECT_TRUE(hitTestCalled);
// The hit test coordinate should be in the window coordinate system.
EXPECT_TRUE(CGPointEqualToPoint(hitTestCoordinate, CGPointMake(100, 500)));
EXPECT_FALSE(cursor.setCalled);
}