~eliasnaur/gio

ref: 3099e7474f59db7d42f1a9d1c461477c4f6ff31b gio/ui/app/os_ios.m -rw-r--r-- 7.5 KiB
3099e747Elias Naur README.md: fix link 1 year, 1 month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// SPDX-License-Identifier: Unlicense OR MIT

// +build darwin,ios

@import UIKit;

#include <stdint.h>
#include "_cgo_export.h"
#include "os_ios.h"
#include "framework_ios.h"

@interface GioViewController : UIViewController
@property UIScreen *screen;
@end

@interface GioView: UIView <UIKeyInput>
- (void)setAnimating:(BOOL)anim;
@end

static void redraw(CFTypeRef viewRef, BOOL sync) {
	UIView *v = (__bridge UIView *)viewRef;
	CGFloat scale = v.layer.contentsScale;
	// Use 163 as the standard ppi on iOS.
	CGFloat dpi = 163*scale;
	CGFloat sdpi = dpi;
	if (@available(iOS 11.0, tvOS 11.0, *)) {
		UIFontMetrics *metrics = [UIFontMetrics defaultMetrics];
		sdpi = [metrics scaledValueForValue:sdpi];
	}
	UIEdgeInsets insets = v.layoutMargins;
	onDraw(viewRef, dpi, sdpi, v.bounds.size.width*scale, v.bounds.size.height*scale, sync,
			insets.top*scale, insets.right*scale, insets.bottom*scale, insets.left*scale);
}

@implementation GioAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
	gio_runMain();

	self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	GioViewController *controller = [[GioViewController alloc] initWithNibName:nil bundle:nil];
	controller.screen = self.window.screen;
    self.window.rootViewController = controller;
	[[NSNotificationCenter defaultCenter] addObserverForName:UIWindowDidBecomeKeyNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
		UIView *view = self.window.rootViewController.view;
		if (view != nil)
			onFocus((__bridge CFTypeRef)view, YES);
	}];
	[[NSNotificationCenter defaultCenter] addObserverForName:UIWindowDidResignKeyNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
		UIView *view = self.window.rootViewController.view;
		if (view != nil)
			onFocus((__bridge CFTypeRef)view, NO);
	}];
    [self.window makeKeyAndVisible];
	return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
	if (self.window.rootViewController.view != nil) {
		onStop((__bridge CFTypeRef)self.window.rootViewController.view);
	}
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
	GioViewController *c = (GioViewController*)self.window.rootViewController;
	if (c.view != nil) {
		CFTypeRef viewRef = (__bridge CFTypeRef)c.view;
		redraw(viewRef, YES);
	}
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
}

- (void)applicationWillTerminate:(UIApplication *)application {
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
	onLowMemory();
}
@end

@implementation GioViewController
- (void)loadView {
	CGRect zeroFrame = CGRectMake(0, 0, 0, 0);
	self.view = [[GioView alloc] initWithFrame:zeroFrame];
#ifndef TARGET_OS_TV
	self.view.multipleTouchEnabled = YES;
#endif
	self.view.contentScaleFactor = self.screen.nativeScale;
	onCreate((__bridge CFTypeRef)self.view);
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	CFTypeRef viewRef = (__bridge CFTypeRef)self.view;
}

- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
	CFTypeRef viewRef = (__bridge CFTypeRef)self.view;
	onDestroy(viewRef);
}

- (void)viewDidLayoutSubviews {
	redraw((__bridge CFTypeRef)self.view, YES);
}
@end

static void handleTouches(int last, UIView *view, NSSet<UITouch *> *touches, UIEvent *event) {
	CGFloat scale = view.contentScaleFactor;
	NSUInteger i = 0;
	NSUInteger n = [touches count];
	CFTypeRef viewRef = (__bridge CFTypeRef)view;
	for (UITouch *touch in touches) {
		CFTypeRef touchRef = (__bridge CFTypeRef)touch;
		i++;
		NSArray<UITouch *> *coalescedTouches = [event coalescedTouchesForTouch:touch];
		NSUInteger j = 0;
		NSUInteger m = [coalescedTouches count];
		for (UITouch *coalescedTouch in [event coalescedTouchesForTouch:touch]) {
			CGPoint loc = [coalescedTouch locationInView:view];
			j++;
			int lastTouch = last && i == n && j == m;
			onTouch(lastTouch, viewRef, touchRef, touch.phase, loc.x*scale, loc.y*scale, [coalescedTouch timestamp]);
		}
	}
}

@implementation GioView
CADisplayLink *displayLink;
NSArray<UIKeyCommand *> *_keyCommands;

- (void)onFrameCallback:(CADisplayLink *)link {
	redraw((__bridge CFTypeRef)self, NO);
}

- (instancetype)initWithFrame:(CGRect)frame {
	self = [super initWithFrame:frame];
	if (self) {
		__weak id weakSelf = self;
		displayLink = [CADisplayLink displayLinkWithTarget:weakSelf selector:@selector(onFrameCallback:)];
	}
	return self;
}

- (void)dealloc {
	[displayLink invalidate];
}

- (void)setAnimating:(BOOL)anim {
	NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
	if (anim) {
		[displayLink addToRunLoop:runLoop forMode:[runLoop currentMode]];
	} else {
		[displayLink removeFromRunLoop:runLoop forMode:[runLoop currentMode]];
	}
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
	handleTouches(0, self, touches, event);
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
	handleTouches(0, self, touches, event);
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
	handleTouches(1, self, touches, event);
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
	handleTouches(1, self, touches, event);
}

- (void)insertText:(NSString *)text {
	onText((__bridge CFTypeRef)self, (char *)text.UTF8String);
}

- (BOOL)canBecomeFirstResponder {
	return YES;
}

- (BOOL)hasText {
	return YES;
}

- (void)deleteBackward {
	onDeleteBackward((__bridge CFTypeRef)self);
}

- (void)onUpArrow {
	onUpArrow((__bridge CFTypeRef)self);
}

- (void)onDownArrow {
	onDownArrow((__bridge CFTypeRef)self);
}

- (void)onLeftArrow {
	onLeftArrow((__bridge CFTypeRef)self);
}

- (void)onRightArrow {
	onRightArrow((__bridge CFTypeRef)self);
}

- (NSArray<UIKeyCommand *> *)keyCommands {
	if (_keyCommands == nil) {
		_keyCommands = @[
			[UIKeyCommand keyCommandWithInput:UIKeyInputUpArrow
								modifierFlags:0
									   action:@selector(onUpArrow)],
			[UIKeyCommand keyCommandWithInput:UIKeyInputDownArrow
								modifierFlags:0
									   action:@selector(onDownArrow)],
			[UIKeyCommand keyCommandWithInput:UIKeyInputLeftArrow
								modifierFlags:0
									   action:@selector(onLeftArrow)],
			[UIKeyCommand keyCommandWithInput:UIKeyInputRightArrow
								modifierFlags:0
									   action:@selector(onRightArrow)]
		];
	}
	return _keyCommands;
}
@end

void gio_setAnimating(CFTypeRef viewRef, int anim) {
	GioView *view = (__bridge GioView *)viewRef;
	dispatch_async(dispatch_get_main_queue(), ^{
			[view setAnimating:(anim ? YES : NO)];
	});
}

void gio_showTextInput(CFTypeRef viewRef) {
	UIView *view = (__bridge UIView *)viewRef;
	dispatch_async(dispatch_get_main_queue(), ^{
		[view becomeFirstResponder];
	});
}

void gio_hideTextInput(CFTypeRef viewRef) {
	UIView *view = (__bridge UIView *)viewRef;
	dispatch_async(dispatch_get_main_queue(), ^{
		[view resignFirstResponder];
	});
}

void gio_addLayerToView(CFTypeRef viewRef, CFTypeRef layerRef) {
	UIView *view = (__bridge UIView *)viewRef;
	CALayer *layer = (__bridge CALayer *)layerRef;
	[view.layer addSublayer:layer];
}

void gio_updateView(CFTypeRef viewRef, CFTypeRef layerRef) {
	UIView *view = (__bridge UIView *)viewRef;
	CAEAGLLayer *layer = (__bridge CAEAGLLayer *)layerRef;
	layer.contentsScale = view.contentScaleFactor;
	layer.bounds = view.bounds;
}

void gio_removeLayer(CFTypeRef layerRef) {
	CALayer *layer = (__bridge CALayer *)layerRef;
	[layer removeFromSuperlayer];
}