app/internal/window: [iOS] move redraw logic to Go

We're about to move the display link to common Go code. To do that,
we need the redraw logic in Go as well.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-05-30 14:16:54 +02:00
parent 266b01bb5d
commit 3817941175
2 changed files with 57 additions and 37 deletions
+32 -18
View File
@@ -11,12 +11,19 @@ package window
#include <UIKit/UIKit.h>
#include <stdint.h>
struct drawParams {
CGFloat dpi, sdpi;
CGFloat width, height;
CGFloat top, right, bottom, left;
};
__attribute__ ((visibility ("hidden"))) void gio_showTextInput(CFTypeRef viewRef);
__attribute__ ((visibility ("hidden"))) void gio_hideTextInput(CFTypeRef viewRef);
__attribute__ ((visibility ("hidden"))) void gio_addLayerToView(CFTypeRef viewRef, CFTypeRef layerRef);
__attribute__ ((visibility ("hidden"))) void gio_updateView(CFTypeRef viewRef, CFTypeRef layerRef);
__attribute__ ((visibility ("hidden"))) void gio_removeLayer(CFTypeRef layerRef);
__attribute__ ((visibility ("hidden"))) void gio_setAnimating(CFTypeRef viewRef, int anim);
__attribute__ ((visibility ("hidden"))) struct drawParams gio_viewDrawParams(CFTypeRef viewRef);
__attribute__ ((visibility ("hidden"))) CFTypeRef gio_readClipboard(void);
__attribute__ ((visibility ("hidden"))) void gio_writeClipboard(unichar *chars, NSUInteger length);
*/
@@ -74,42 +81,49 @@ func onCreate(view C.CFTypeRef) {
w.w.Event(system.StageEvent{Stage: system.StagePaused})
}
//export onDraw
func onDraw(view C.CFTypeRef, dpi, sdpi, width, height C.CGFloat, sync C.int, top, right, bottom, left C.CGFloat) {
if width == 0 || height == 0 {
//export gio_onFrameCallback
func gio_onFrameCallback(view C.CFTypeRef) {
w := views[view]
w.draw(false)
}
//export gio_onDraw
func gio_onDraw(view C.CFTypeRef) {
w := views[view]
w.draw(true)
}
func (w *window) draw(sync bool) {
params := C.gio_viewDrawParams(w.view)
if params.width == 0 || params.height == 0 {
return
}
w := views[view]
wasVisible := w.isVisible()
w.visible.Store(true)
C.gio_updateView(view, w.layer)
C.gio_updateView(w.view, w.layer)
if !wasVisible {
w.w.Event(system.StageEvent{Stage: system.StageRunning})
}
isSync := false
if sync != 0 {
isSync = true
}
const inchPrDp = 1.0 / 163
w.w.Event(FrameEvent{
FrameEvent: system.FrameEvent{
Size: image.Point{
X: int(width + .5),
Y: int(height + .5),
X: int(params.width + .5),
Y: int(params.height + .5),
},
Insets: system.Insets{
Top: unit.Px(float32(top)),
Right: unit.Px(float32(right)),
Bottom: unit.Px(float32(bottom)),
Left: unit.Px(float32(left)),
Top: unit.Px(float32(params.top)),
Right: unit.Px(float32(params.right)),
Bottom: unit.Px(float32(params.bottom)),
Left: unit.Px(float32(params.left)),
},
Config: &config{
pxPerDp: float32(dpi) * inchPrDp,
pxPerSp: float32(sdpi) * inchPrDp,
pxPerDp: float32(params.dpi) * inchPrDp,
pxPerSp: float32(params.sdpi) * inchPrDp,
now: time.Now(),
},
},
Sync: isSync,
Sync: sync,
})
}
+25 -19
View File
@@ -16,22 +16,6 @@
@property(weak) UIScreen *screen;
@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;
UIEdgeInsets insets = v.layoutMargins;
if (@available(iOS 11.0, tvOS 11.0, *)) {
UIFontMetrics *metrics = [UIFontMetrics defaultMetrics];
sdpi = [metrics scaledValueForValue:sdpi];
insets = v.safeAreaInsets;
}
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();
@@ -60,7 +44,7 @@ static void redraw(CFTypeRef viewRef, BOOL sync) {
UIView *drawView = c.view.subviews[0];
if (drawView != nil) {
CFTypeRef viewRef = (__bridge CFTypeRef)drawView;
redraw(viewRef, YES);
gio_onDraw(viewRef);
}
}
@end
@@ -108,7 +92,7 @@ CGFloat _keyboardHeight;
// Adjust view bounds to make room for the keyboard.
frame.size.height -= _keyboardHeight;
view.frame = frame;
redraw((__bridge CFTypeRef)view, YES);
gio_onDraw((__bridge CFTypeRef)view);
}
- (void)didReceiveMemoryWarning {
@@ -154,7 +138,7 @@ CADisplayLink *displayLink;
NSArray<UIKeyCommand *> *_keyCommands;
- (void)onFrameCallback:(CADisplayLink *)link {
redraw((__bridge CFTypeRef)self, NO);
gio_onFrameCallback((__bridge CFTypeRef)self);
}
- (instancetype)initWithFrame:(CGRect)frame {
@@ -329,3 +313,25 @@ void gio_removeLayer(CFTypeRef layerRef) {
CALayer *layer = (__bridge CALayer *)layerRef;
[layer removeFromSuperlayer];
}
struct drawParams gio_viewDrawParams(CFTypeRef viewRef) {
UIView *v = (__bridge UIView *)viewRef;
struct drawParams params;
CGFloat scale = v.layer.contentsScale;
// Use 163 as the standard ppi on iOS.
params.dpi = 163*scale;
params.sdpi = params.dpi;
UIEdgeInsets insets = v.layoutMargins;
if (@available(iOS 11.0, tvOS 11.0, *)) {
UIFontMetrics *metrics = [UIFontMetrics defaultMetrics];
params.sdpi = [metrics scaledValueForValue:params.sdpi];
insets = v.safeAreaInsets;
}
params.width = v.bounds.size.width*scale;
params.height = v.bounds.size.height*scale;
params.top = insets.top*scale;
params.right = insets.right*scale;
params.bottom = insets.bottom*scale;
params.left = insets.left*scale;
return params;
}