9
9
10
10
#if OPENSWIFTUI_TARGET_OS_DARWIN
11
11
#include < Foundation/Foundation.h>
12
+ #include " OpenSwiftUICoreTextGraphicsContextProvider.h"
13
+ #include " Shims/UIFoundation/NSTextGraphicsContextInternal.h"
14
+
15
+ #if OPENSWIFTUI_TARGET_OS_OSX
16
+ #include < AppKit/AppKit.h>
17
+ #endif
12
18
13
19
static _Thread_local __unsafe_unretained OpenSwiftUICoreGraphicsContext * _current = NULL ;
14
20
21
+ dispatch_once_t _once;
22
+ #if OPENSWIFTUI_TARGET_OS_OSX
23
+ Class _nsGraphicsContextClass;
24
+ #else
15
25
IMP _pushContextIMP;
16
26
IMP _popContextIMP;
27
+ #endif
17
28
18
29
@interface OpenSwiftUICoreGraphicsContext () {
19
30
OpenSwiftUICoreGraphicsContext *_next;
20
31
CGContextRef _ctx;
21
32
}
33
+ #if !OPENSWIFTUI_TARGET_OS_OSX
22
34
- (id )__createsImages ;
35
+ #endif
23
36
@end
24
37
25
38
@implementation OpenSwiftUICoreGraphicsContext
26
39
27
40
- (instancetype )initWithCGContext : (CGContextRef )ctx {
28
- static dispatch_once_t __once;
29
- dispatch_once (&__once, ^{
41
+ dispatch_once (&_once, ^{
42
+ #if OPENSWIFTUI_TARGET_OS_OSX
43
+ _nsGraphicsContextClass = NSClassFromString (@" NSGraphicsContext" );
44
+ #else
30
45
Class renderClass = NSClassFromString (@" UIGraphicsRenderer" );
31
46
if (renderClass) {
32
47
_pushContextIMP = [renderClass instanceMethodForSelector: @selector (pushContext: )];
33
48
_popContextIMP = [renderClass instanceMethodForSelector: @selector (popContext: )];
34
49
} else {
35
- // TODO: CoreTextGraphicsContextProvider.sharedProvider
50
+ InitializeCoreTextGraphicsContextProvider ();
36
51
}
52
+ #endif
37
53
});
38
54
self = [super init ];
39
55
if (self) {
@@ -43,25 +59,43 @@ - (instancetype)initWithCGContext:(CGContextRef)ctx {
43
59
}
44
60
45
61
- (void )push {
62
+ #if OPENSWIFTUI_TARGET_OS_OSX
63
+ _next = _current;
64
+ _current = self;
65
+ if (_nsGraphicsContextClass) {
66
+ [_nsGraphicsContextClass saveGraphicsState ];
67
+ [_nsGraphicsContextClass graphicsContextWithCGContext: _ctx flipped: YES ];
68
+ NSGraphicsContext *graphicsContext = [_nsGraphicsContextClass graphicsContextWithCGContext: _ctx flipped: YES ];
69
+ [_nsGraphicsContextClass setCurrentContext: graphicsContext];
70
+ }
71
+ #else
46
72
_next = _current;
47
73
_current = self;
48
74
if (_pushContextIMP != NULL && _popContextIMP != NULL ) {
49
75
typedef BOOL (*FUNC)(id , SEL , OpenSwiftUICoreGraphicsContext *);
50
76
((FUNC)(_pushContextIMP))(NULL , @selector (pushContext: ), _current);
51
77
}
78
+ #endif
52
79
}
53
80
54
81
- (void )pop {
82
+ #if OPENSWIFTUI_TARGET_OS_OSX
83
+ _current = _next;
84
+ [_nsGraphicsContextClass restoreGraphicsState ];
85
+ #else
55
86
_current = _next;
56
87
if (_pushContextIMP != NULL && _popContextIMP != NULL ) {
57
88
typedef BOOL (*FUNC)(id , SEL , OpenSwiftUICoreGraphicsContext *);
58
89
((FUNC)(_popContextIMP))(NULL , @selector (popContext: ), _current);
59
90
}
91
+ #endif
60
92
}
61
93
94
+ #if !OPENSWIFTUI_TARGET_OS_OSX
62
95
- (id )__createsImages {
63
96
return nil ;
64
97
}
98
+ #endif
65
99
66
100
- (CGContextRef )CGContext {
67
101
return _ctx;
0 commit comments