This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
System mouse cursor: macOS #18131
Merged
dkwingsmt
merged 18 commits into
flutter-team-archive:master
from
dkwingsmt:system-mouse-cursor-macos
May 20, 2020
Merged
System mouse cursor: macOS #18131
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
89b9519
Untested macOS impl
dkwingsmt 3552152
Workable mac
dkwingsmt 929abde
Merge remote-tracking branch 'upstream/master' into mouse-cursor-macos
dkwingsmt 85fb14b
Merge remote-tracking branch 'upstream/master' into system-mouse-curs…
dkwingsmt 38aedbb
Modernize
dkwingsmt 6272fc1
Simplify
dkwingsmt 8ca0900
Address comments
dkwingsmt 55a63a5
Refactor with FlutterPlugin
dkwingsmt cd7abf1
Cached dict
dkwingsmt ec138bc
Update per comments
dkwingsmt bd95f82
format
dkwingsmt efc3035
Merge remote-tracking branch 'upstream/master' into system-mouse-curs…
dkwingsmt 8da2c72
Remove device arg
dkwingsmt 17bd7d8
Format
dkwingsmt 81fd9dd
Update license
dkwingsmt 033014f
Update licences
dkwingsmt 207a5e9
Merge remote-tracking branch 'upstream/master' into system-mouse-curs…
dkwingsmt 021d2c3
Doc update
dkwingsmt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #import <Cocoa/Cocoa.h> | ||
|
|
||
| #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h" | ||
| #import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterViewController.h" | ||
|
|
||
| /** | ||
| * A plugin to handle mouse cursor. | ||
| * | ||
| * Responsible for bridging the native macOS mouse cursor system with the | ||
| * Flutter framework mouse cursor classes, via system channels. | ||
| */ | ||
| @interface FlutterMouseCursorPlugin : NSObject <FlutterPlugin> | ||
|
|
||
| @end |
137 changes: 137 additions & 0 deletions
137
shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.mm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| // Copyright 2019 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #import <objc/message.h> | ||
|
|
||
| #import "FlutterMouseCursorPlugin.h" | ||
| #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterCodecs.h" | ||
|
|
||
| static NSString* const kMouseCursorChannel = @"flutter/mousecursor"; | ||
|
|
||
| static NSString* const kActivateSystemCursorMethod = @"activateSystemCursor"; | ||
| static NSString* const kKindKey = @"kind"; | ||
| static NSString* const kDeviceKey = @"device"; | ||
|
|
||
| static NSString* const kKindValueNone = @"none"; | ||
|
|
||
| static NSMutableDictionary* cachedSystemCursors; | ||
|
|
||
| // Maps a Flutter's constant to a platform's cursor object. | ||
| // | ||
| // Returns the arrow cursor for unknown constants, including kSystemShapeNone. | ||
| static NSCursor* mapKindToCursor(NSString* kind) { | ||
|
dkwingsmt marked this conversation as resolved.
Outdated
|
||
| // The following mapping must be kept in sync with Flutter framework's | ||
| // mouse_cursor.dart | ||
| if ([kind isEqualToString:@"basic"]) | ||
| return [NSCursor arrowCursor]; | ||
| else if ([kind isEqualToString:@"click"]) | ||
| return [NSCursor pointingHandCursor]; | ||
| else if ([kind isEqualToString:@"text"]) | ||
| return [NSCursor IBeamCursor]; | ||
| else if ([kind isEqualToString:@"forbidden"]) | ||
| return [NSCursor operationNotAllowedCursor]; | ||
| else if ([kind isEqualToString:@"grab"]) | ||
| return [NSCursor openHandCursor]; | ||
| else if ([kind isEqualToString:@"grabbing"]) | ||
| return [NSCursor closedHandCursor]; | ||
| else | ||
| return [NSCursor arrowCursor]; | ||
| } | ||
|
|
||
| @interface FlutterMouseCursorPlugin () | ||
| /** | ||
| * The channel used to communicate with Flutter. | ||
| */ | ||
| @property(nonatomic) FlutterMethodChannel* channel; | ||
|
dkwingsmt marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Whether the cursor is currently hidden. | ||
| */ | ||
| @property(nonatomic) BOOL hidden; | ||
|
|
||
| @end | ||
|
|
||
| @implementation FlutterMouseCursorPlugin | ||
|
|
||
| - (instancetype)initWithChannel:(FlutterMethodChannel *)channel { | ||
| self = [super init]; | ||
| if (self) { | ||
| _channel = channel; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| #pragma mark - Private | ||
|
|
||
| - (id)activateSystemCursor:(nonnull NSDictionary*)arguments { | ||
|
dkwingsmt marked this conversation as resolved.
Outdated
dkwingsmt marked this conversation as resolved.
Outdated
|
||
| if (!arguments) { | ||
|
dkwingsmt marked this conversation as resolved.
Outdated
|
||
| return [FlutterError | ||
| errorWithCode:@"error" | ||
| message:@"Missing arguments" | ||
| details:@"Missing arguments while trying to activate system cursor"]; | ||
| } | ||
| NSString* kindArg = arguments[kKindKey]; | ||
| NSNumber* deviceArg = arguments[kDeviceKey]; | ||
| if (!kindArg || !deviceArg) { | ||
| return [FlutterError | ||
| errorWithCode:@"error" | ||
| message:@"Missing argument" | ||
| details:@"Missing argument while trying to activate system cursor"]; | ||
| } | ||
| if ([kindArg isEqualToString:kKindValueNone]) { | ||
| [self hide]; | ||
| return nil; | ||
| } | ||
| NSCursor* shapeObject = [FlutterMouseCursorPlugin resolveKindToCursor:kindArg]; | ||
| [self activateShapeObject:shapeObject]; | ||
| return nil; | ||
| } | ||
|
|
||
| - (void)activateShapeObject: (nonnull NSCursor *)targetShape { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This still needs clang-formating |
||
| [targetShape set]; | ||
| if (_hidden) { | ||
| [NSCursor unhide]; | ||
| } | ||
| _hidden = NO; | ||
| } | ||
|
|
||
| - (void)hide { | ||
| if (!_hidden) { | ||
| [NSCursor hide]; | ||
| } | ||
| _hidden = YES; | ||
|
dkwingsmt marked this conversation as resolved.
|
||
| } | ||
|
|
||
| + (NSCursor*)resolveKindToCursor:(NSString*)kind { | ||
|
dkwingsmt marked this conversation as resolved.
Outdated
|
||
| if (cachedSystemCursors == nil) { | ||
|
dkwingsmt marked this conversation as resolved.
Outdated
|
||
| cachedSystemCursors = [NSMutableDictionary dictionary]; | ||
| } | ||
|
dkwingsmt marked this conversation as resolved.
Outdated
|
||
|
|
||
| NSCursor* cachedValue = [cachedSystemCursors objectForKey:kind]; | ||
| if (cachedValue == nil) { | ||
| cachedValue = mapKindToCursor(kind); | ||
| [cachedSystemCursors setValue:cachedValue forKey:kind]; | ||
| } | ||
| return cachedValue; | ||
| } | ||
|
|
||
| #pragma mark - FlutterPlugin implementation | ||
|
|
||
| + (void)registerWithRegistrar:(id<FlutterPluginRegistrar>)registrar { | ||
| FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:kMouseCursorChannel | ||
| binaryMessenger:registrar.messenger]; | ||
| FlutterMouseCursorPlugin *instance = [[FlutterMouseCursorPlugin alloc] initWithChannel:channel]; | ||
| [registrar addMethodCallDelegate:instance channel:channel]; | ||
| } | ||
|
|
||
| - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result { | ||
| NSString* method = call.method; | ||
| if ([method isEqualToString:kActivateSystemCursorMethod]) { | ||
| result([self activateSystemCursor:call.arguments]); | ||
| } else { | ||
| result(FlutterMethodNotImplemented); | ||
| } | ||
| } | ||
|
|
||
| @end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.