Skip to content

Commit 77fba0f

Browse files
authored
Merge pull request #136 from matpaul/cursor
Implement cursor management, possible fix #135
2 parents 8906b3f + b2e0e68 commit 77fba0f

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule Cursor
10+
* @flow
11+
*/
12+
'use strict';
13+
14+
const CursorManager = require('NativeModules').CursorManager;
15+
16+
// All cursor types:
17+
type CursorType = $Enum<{
18+
arrow: string,
19+
IBeam: string,
20+
pointingHand: string,
21+
closedHand: string,
22+
openHand: string,
23+
resizeLeft: string,
24+
resizeRight: string,
25+
resizeLeftRight: string,
26+
resizeUp: string,
27+
resizeDown: string,
28+
resizeUpDown: string,
29+
crosshair: string,
30+
disappearingItem: string,
31+
operationNotAllowed: string,
32+
dragLink: string,
33+
dragCopy: string,
34+
contextualMenu: string,
35+
IBeamForVerticalLayout: string
36+
}>;
37+
38+
const cursorTypeMap = {
39+
arrow: 'arrowCursor',
40+
IBeam: 'IBeamCursor',
41+
pointing: 'pointingHandCursor',
42+
closedHand: 'closedHandCursor',
43+
openHand: 'openHandCursor',
44+
resizeLeft: 'resizeLeftCursor',
45+
resizeRight: 'resizeRightCursor',
46+
resizeLeftRight: 'resizeLeftRightCursor',
47+
resizeUp: 'resizeUpCursor',
48+
resizeDown: 'resizeDownCursor',
49+
resizeUpDown: 'resizeUpDownCursor',
50+
crosshair: 'crosshairCursor',
51+
disappearing: 'disappearingItemCursor',
52+
operationNotAllowed: 'operationNotAllowedCursor',
53+
dragLink: 'dragLinkCursor',
54+
dragCopy: 'dragCopyCursor',
55+
contextualMenu: 'contextualMenuCursor',
56+
IBeamForVerticalLayout: 'IBeamCursorForVerticalLayout'
57+
};
58+
59+
/**
60+
* Let change cursor style
61+
* List of all cursor types:
62+
* https://developer.apple.com/reference/appkit/nscursor?language=objc
63+
*
64+
* // Usage
65+
* ```
66+
* Cursor.set('openHand');
67+
* ```
68+
*/
69+
class Cursor {
70+
static set(type: CursorType): void {
71+
if (!cursorTypeMap[type]) {
72+
console.warn(`${type} isn't supported cursor type`);
73+
} else {
74+
const nativeCursorType = cursorTypeMap[type];
75+
CursorManager[nativeCursorType]();
76+
}
77+
}
78+
}
79+
80+
module.exports = Cursor;

Libraries/react-native/react-native.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const ReactNative = {
7474
get BackAndroid() { return require('BackAndroid'); },
7575
get CameraRoll() { return require('CameraRoll'); },
7676
get Clipboard() { return require('Clipboard'); },
77+
get Cursor() { return require('Cursor'); },
7778
get Dimensions() { return require('Dimensions'); },
7879
get Easing() { return require('Easing'); },
7980
get ImagePickerIOS() { return require('ImagePickerIOS'); },

React/Modules/RCTCursorManager.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import <AppKit/AppKit.h>
11+
#import "RCTBridgeModule.h"
12+
13+
@interface RCTCursorManager : NSObject <RCTBridgeModule>
14+
@end

React/Modules/RCTCursorManager.m

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import "RCTCursorManager.h"
11+
12+
// Macros for creation set methods for provided cursor type
13+
#define EXPORT_CURSOR_SET_METHOD(name) \
14+
RCT_EXPORT_METHOD(name) { \
15+
[[NSCursor name] set]; \
16+
}
17+
18+
19+
@implementation RCTCursorManager
20+
21+
RCT_EXPORT_MODULE();
22+
23+
EXPORT_CURSOR_SET_METHOD(arrowCursor);
24+
EXPORT_CURSOR_SET_METHOD(IBeamCursor);
25+
EXPORT_CURSOR_SET_METHOD(crosshairCursor);
26+
EXPORT_CURSOR_SET_METHOD(closedHandCursor);
27+
EXPORT_CURSOR_SET_METHOD(openHandCursor);
28+
EXPORT_CURSOR_SET_METHOD(pointingHandCursor);
29+
EXPORT_CURSOR_SET_METHOD(resizeLeftCursor);
30+
EXPORT_CURSOR_SET_METHOD(resizeRightCursor);
31+
EXPORT_CURSOR_SET_METHOD(resizeLeftRightCursor);
32+
EXPORT_CURSOR_SET_METHOD(resizeUpCursor);
33+
EXPORT_CURSOR_SET_METHOD(resizeDownCursor);
34+
EXPORT_CURSOR_SET_METHOD(resizeUpDownCursor);
35+
EXPORT_CURSOR_SET_METHOD(disappearingItemCursor);
36+
EXPORT_CURSOR_SET_METHOD(IBeamCursorForVerticalLayout);
37+
EXPORT_CURSOR_SET_METHOD(operationNotAllowedCursor);
38+
EXPORT_CURSOR_SET_METHOD(dragLinkCursor);
39+
EXPORT_CURSOR_SET_METHOD(dragCopyCursor);
40+
EXPORT_CURSOR_SET_METHOD(contextualMenuCursor);
41+
42+
@end

React/React.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
B233E6EA1D2D845D00BC68BA /* RCTI18nManager.m in Sources */ = {isa = PBXBuildFile; fileRef = B233E6E91D2D845D00BC68BA /* RCTI18nManager.m */; };
9191
B95154321D1B34B200FE7B80 /* RCTActivityIndicatorView.m in Sources */ = {isa = PBXBuildFile; fileRef = B95154311D1B34B200FE7B80 /* RCTActivityIndicatorView.m */; };
9292
C924C30F1C2EBDA100D7ED48 /* NSView+NSViewAnimationWithBlocks.m in Sources */ = {isa = PBXBuildFile; fileRef = C924C30E1C2EBDA100D7ED48 /* NSView+NSViewAnimationWithBlocks.m */; };
93+
CE2E95411E12CABF00404FA4 /* RCTCursorManager.m in Sources */ = {isa = PBXBuildFile; fileRef = CE2E95401E12CABF00404FA4 /* RCTCursorManager.m */; };
9394
/* End PBXBuildFile section */
9495

9596
/* Begin PBXCopyFilesBuildPhase section */
@@ -299,6 +300,8 @@
299300
B95154311D1B34B200FE7B80 /* RCTActivityIndicatorView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTActivityIndicatorView.m; sourceTree = "<group>"; };
300301
C924C30D1C2EBDA100D7ED48 /* NSView+NSViewAnimationWithBlocks.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSView+NSViewAnimationWithBlocks.h"; sourceTree = "<group>"; };
301302
C924C30E1C2EBDA100D7ED48 /* NSView+NSViewAnimationWithBlocks.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSView+NSViewAnimationWithBlocks.m"; sourceTree = "<group>"; };
303+
CE2E953F1E12CABF00404FA4 /* RCTCursorManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTCursorManager.h; sourceTree = "<group>"; };
304+
CE2E95401E12CABF00404FA4 /* RCTCursorManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTCursorManager.m; sourceTree = "<group>"; };
302305
E3BBC8EB1ADE6F47001BBD81 /* RCTTextDecorationLineType.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RCTTextDecorationLineType.h; sourceTree = "<group>"; };
303306
/* End PBXFileReference section */
304307

@@ -340,6 +343,8 @@
340343
13B07FE01A69315300A75B9A /* Modules */ = {
341344
isa = PBXGroup;
342345
children = (
346+
CE2E953F1E12CABF00404FA4 /* RCTCursorManager.h */,
347+
CE2E95401E12CABF00404FA4 /* RCTCursorManager.m */,
343348
13D9FEE91CDCCECF00158BD7 /* RCTEventEmitter.h */,
344349
13D9FEEA1CDCCECF00158BD7 /* RCTEventEmitter.m */,
345350
13B07FE71A69327A00A75B9A /* RCTAlertManager.h */,
@@ -741,6 +746,7 @@
741746
1450FF871BCFF28A00208362 /* RCTProfileTrampoline-arm.S in Sources */,
742747
031631861BDF5E2E003A8A7A /* RCTAsyncLocalStorage.m in Sources */,
743748
58114A171AAE854800E7D092 /* RCTPickerManager.m in Sources */,
749+
CE2E95411E12CABF00404FA4 /* RCTCursorManager.m in Sources */,
744750
68EFE4EE1CF6EB3900A1DE13 /* RCTBundleURLProvider.m in Sources */,
745751
B95154321D1B34B200FE7B80 /* RCTActivityIndicatorView.m in Sources */,
746752
13F17A851B8493E5007D4C75 /* RCTRedBox.m in Sources */,

0 commit comments

Comments
 (0)