Skip to content

Commit e029398

Browse files
feat(💚): macOS support (#3045)
Co-authored-by: William Candillon <[email protected]>
1 parent 006bc63 commit e029398

9 files changed

+42
-5
lines changed

‎packages/skia/apple/MetalWindowContext.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
_layer.framebufferOnly = NO;
1616
_layer.device = device;
1717
_layer.opaque = false;
18+
#if !TARGET_OS_OSX
1819
_layer.contentsScale = [UIScreen mainScreen].scale;
20+
#else
21+
_layer.contentsScale = [NSScreen mainScreen].backingScaleFactor;
22+
#endif // !TARGET_OS_OSX
1923
_layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
2024
_layer.contentsGravity = kCAGravityBottomLeft;
2125
_layer.drawableSize = CGSizeMake(width, height);

‎packages/skia/apple/RNSkApplePlatformContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ class RNSkApplePlatformContext : public RNSkPlatformContext {
2323
RNSkApplePlatformContext(
2424
RCTBridge *bridge,
2525
std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker)
26+
#if !TARGET_OS_OSX
2627
: RNSkPlatformContext(jsCallInvoker, [[UIScreen mainScreen] scale]) {
28+
#else
29+
: RNSkPlatformContext(jsCallInvoker, [[NSScreen mainScreen] backingScaleFactor]) {
30+
#endif // !TARGET_OS_OSX
2731

2832
// Create screenshot manager
2933
_screenshotService =

‎packages/skia/apple/RNSkMetalCanvasProvider.mm

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,13 @@
6363
// NOTE: UIApplication.sharedApplication.applicationState can only be
6464
// accessed from the main thread so we need to check here.
6565
if ([[NSThread currentThread] isMainThread]) {
66+
#if !TARGET_OS_OSX
6667
auto state = UIApplication.sharedApplication.applicationState;
67-
if (state == UIApplicationStateBackground) {
68+
bool appIsBackgrounded = (state == UIApplicationStateBackground);
69+
#else
70+
bool appIsBackgrounded = !NSApplication.sharedApplication.isActive;
71+
#endif // !TARGET_OS_OSX
72+
if (appIsBackgrounded) {
6873
// Request a redraw in the next run loop callback
6974
_requestRedraw();
7075
// and don't draw now since it might cause errors in the metal renderer if

‎packages/skia/apple/SkiaPictureViewManager.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ - (SkiaManager *)skiaManager {
3737
[(SkiaUIView *)view setOpaque:opaque];
3838
}
3939

40+
#if !TARGET_OS_OSX
4041
- (UIView *)view {
42+
#else
43+
- (RCTUIView *)view {
44+
#endif // !TARGET_OS_OSX
4145
auto skManager = [[self skiaManager] skManager];
4246
// Pass SkManager as a raw pointer to avoid circular dependenciesr
4347
return [[SkiaUIView alloc]

‎packages/skia/apple/SkiaUIView.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
#import <string>
55

66
#import <CoreFoundation/CoreFoundation.h>
7+
#if !TARGET_OS_OSX
78
#import <UIKit/UIKit.h>
9+
#else
10+
#import <React/RCTUIKit.h>
11+
#endif // !TARGET_OS_OSX
812

913
#import "RNSkManager.h"
1014
#import "RNSkAppleView.h"
@@ -18,7 +22,11 @@
1822
#if RCT_NEW_ARCH_ENABLED
1923
RCTViewComponentView
2024
#else
25+
#if !TARGET_OS_OSX
2126
UIView
27+
#else
28+
RCTUIView
29+
#endif // !TARGET_OS_OSX
2230
#endif // RCT_NEW_ARCH_ENABLED
2331

2432
- (instancetype)

‎packages/skia/apple/SkiaUIView.mm

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ - (void)willInvalidateModules {
5555

5656
#pragma mark Lifecycle
5757

58-
- (void)willMoveToSuperview:(UIView *)newWindow {
59-
if (newWindow != nullptr) {
58+
#if !TARGET_OS_OSX
59+
- (void)willMoveToSuperview:(UIView *)newSuperView {
60+
#else
61+
- (void)viewWillMoveToSuperview:(NSView *)newSuperView {
62+
#endif // !TARGET_OS_OSX
63+
if (newSuperView != nullptr) {
6064
// Create implementation view when the parent view is set
6165
if (_impl == nullptr && _manager != nullptr) {
6266
_impl = _factory(_manager->getPlatformContext());

‎packages/skia/apple/ViewScreenshotService.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#pragma once
22

33
#import <CoreFoundation/CoreFoundation.h>
4+
#if !TARGET_OS_OSX
45
#import <UIKit/UIKit.h>
6+
#else
7+
#import <Appkit/Appkit.h>
8+
#endif // !TARGET_OS_OSX
59

610
#import <React/RCTUIManager.h>
711

‎packages/skia/apple/ViewScreenshotService.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ - (instancetype)initWithUiManager:(RCTUIManager *)uiManager {
2020
}
2121

2222
- (sk_sp<SkImage>)screenshotOfViewWithTag:(NSNumber *)viewTag {
23+
#if !TARGET_OS_OSX
2324
// Find view corresponding to the tag
2425
auto view = [_uiManager viewForReactTag:viewTag];
2526
if (view == NULL) {
@@ -80,6 +81,9 @@ - (instancetype)initWithUiManager:(RCTUIManager *)uiManager {
8081

8182
// ... and then create the SkImage itself!
8283
return SkImages::RasterFromData(info, skData, bytesPerRow);
84+
#else
85+
return nullptr;
86+
#endif // !TARGET_OS_OSX
8387
}
8488

8589
@end

‎packages/skia/react-native-skia.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Pod::Spec.new do |s|
4141
"Christian Falch" => "[email protected]",
4242
"William Candillon" => "[email protected]"
4343
}
44-
s.platforms = { :ios => "13.0", :tvos => "13.0" }
44+
s.platforms = { :ios => "13.0", :tvos => "13.0", :osx => "11" }
4545
s.source = { :git => "https://github.com/shopify/react-native-skia/react-native-skia.git", :tag => "#{s.version}" }
4646

4747
s.requires_arc = true
@@ -54,7 +54,7 @@ Pod::Spec.new do |s|
5454

5555
s.frameworks = ['MetalKit', 'AVFoundation', 'AVKit', 'CoreMedia']
5656

57-
s.vendored_frameworks = use_graphite ?
57+
s.vendored_frameworks = use_graphite ?
5858
base_frameworks + graphite_frameworks :
5959
base_frameworks
6060

0 commit comments

Comments
 (0)