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:master
from
dkwingsmt:system-mouse-cursor-macos
May 20, 2020
Merged
System mouse cursor: macOS #18131
Changes from all 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
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
18 changes: 18 additions & 0 deletions
18
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,18 @@ | ||
| // Copyright 2013 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 <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 |
153 changes: 153 additions & 0 deletions
153
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,153 @@ | ||
| // Copyright 2013 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 kKindValueNone = @"none"; | ||
|
|
||
| /** | ||
| * Maps a Flutter's constant to a platform's cursor object. | ||
| * | ||
| * Returns the arrow cursor for unknown constants, including kSystemShapeNone. | ||
| */ | ||
| static NSCursor* GetCursorForKind(NSString* kind) { | ||
| // 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 () | ||
| /** | ||
| * Whether the cursor is currently hidden. | ||
| */ | ||
| @property(nonatomic) BOOL hidden; | ||
|
|
||
| /** | ||
| * Handles the method call that activates a system cursor. | ||
| * | ||
| * Returns a FlutterError if the arguments can not be recognized. Otherwise | ||
| * returns nil. | ||
| */ | ||
| - (FlutterError*)activateSystemCursor:(nonnull NSDictionary*)arguments; | ||
|
|
||
| /** | ||
| * Displays the specified cursor. | ||
| * | ||
| * Unhides the cursor before displaying the cursor, and updates | ||
| * internal states. | ||
| */ | ||
| - (void)displayCursorObject:(nonnull NSCursor*)cursorObject; | ||
|
|
||
| /** | ||
| * Hides the cursor. | ||
| */ | ||
| - (void)hide; | ||
|
|
||
| /** | ||
| * Handles all method calls from Flutter. | ||
| */ | ||
| - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result; | ||
|
|
||
| @end | ||
|
|
||
| @implementation FlutterMouseCursorPlugin | ||
|
|
||
| #pragma mark - Private | ||
|
|
||
| NSMutableDictionary* cachedSystemCursors; | ||
|
|
||
| - (instancetype)init { | ||
| self = [super init]; | ||
| if (self) { | ||
| cachedSystemCursors = [NSMutableDictionary dictionary]; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (void)dealloc { | ||
| if (_hidden) { | ||
| [NSCursor unhide]; | ||
| } | ||
| } | ||
|
|
||
| - (FlutterError*)activateSystemCursor:(nonnull NSDictionary*)arguments { | ||
| NSString* kindArg = arguments[kKindKey]; | ||
| if (!kindArg) { | ||
| 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* cursorObject = [FlutterMouseCursorPlugin cursorFromKind:kindArg]; | ||
| [self displayCursorObject:cursorObject]; | ||
| return nil; | ||
| } | ||
|
|
||
| - (void)displayCursorObject:(nonnull NSCursor*)cursorObject { | ||
| [cursorObject set]; | ||
| if (_hidden) { | ||
| [NSCursor unhide]; | ||
| } | ||
| _hidden = NO; | ||
| } | ||
|
|
||
| - (void)hide { | ||
| if (!_hidden) { | ||
| [NSCursor hide]; | ||
| } | ||
| _hidden = YES; | ||
| } | ||
|
|
||
| + (NSCursor*)cursorFromKind:(NSString*)kind { | ||
| NSCursor* cachedValue = [cachedSystemCursors objectForKey:kind]; | ||
| if (!cachedValue) { | ||
| cachedValue = GetCursorForKind(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] init]; | ||
| [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.