Moved some utils to ios_utils, webview accepts x,y coords, improved get_scale and get_dpi (#415)

This commit is contained in:
Mirko 2020-01-12 12:21:16 +01:00 committed by Mathieu Virbel
parent 2e8e9a1709
commit 558f987d9d
6 changed files with 174 additions and 47 deletions

View file

@ -19,7 +19,7 @@ cdef extern from "ios_wrapper.h":
int ios_send_email(char *subject, char *text, char *mimetype, char
*filename, char *filename_alias, ios_send_email_cb cb, void *userdata)
void ios_open_url(char *url)
void load_url_webview(char *url, int width, int height)
void load_url_webview(char *url, int x, int y, int width, int height)
float ios_uiscreen_get_scale()
int ios_uiscreen_get_dpi()
padding ios_get_safe_area()
@ -33,11 +33,11 @@ cdef void _send_email_done(char *status, void *data):
#Support for iOS webview
#
class IOSWebView(object):
def open(self, url, width, height):
open_url_wbv(url, width, height)
def open(self, url, x, y, width, height):
open_url_wbv(url, x, y, width, height)
def open_url_wbv(url, width, height):
def open_url_wbv(url, x, y, width, height):
'''
OPEN URL in webview
@ -55,9 +55,9 @@ def open_url_wbv(url, width, height):
import ios
url = "http://www.google.com"
ios.IOSWebView().open(url, width, height)
ios.IOSWebView().open(url, x, y, width, height)
'''
load_url_webview(url, width, height)
load_url_webview(url, x, y, width, height)
#
# Support for webbrowser module
@ -217,6 +217,7 @@ def get_safe_area():
'''
return ios_get_safe_area()
from pyobjus import autoclass, selector, protocol
from pyobjus.protocols import protocols

View file

@ -16,10 +16,10 @@ void ios_open_url(char *url)
/*
* Webview support
*/
void load_url_webview(char *url, int width, int height)
void load_url_webview(char *url, int x, int y, int width, int height)
{
NSString *nsurl = [NSString stringWithCString:(char *)url encoding:NSUTF8StringEncoding];
WKWebView *webView = [[WKWebView alloc] initWithFrame: CGRectMake(0, 0, width, height)];
WKWebView *webView = [[WKWebView alloc] initWithFrame: CGRectMake(x, y, width, height)];
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *view = [window.rootViewController view];
[view addSubview:webView];

View file

@ -107,40 +107,3 @@ int ios_send_email(char *subject, char *text, char *mimetype, char *filename,
return 1;
}
float ios_uiscreen_get_scale() {
float scale = 1.0;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
scale = [[UIScreen mainScreen] scale];
};
return scale;
}
int ios_uiscreen_get_dpi() {
float scale = ios_uiscreen_get_scale();
float dpi = 160 * scale;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
dpi = 132 * scale;
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
dpi = 163 * scale;
}
return dpi;
}
padding ios_get_safe_area() {
padding safearea;
if (@available(iOS 11.0, *)){
UIWindow *window = UIApplication.sharedApplication.windows[0];
safearea.top = window.safeAreaInsets.top;
safearea.bottom = window.safeAreaInsets.bottom;
safearea.left = window.safeAreaInsets.left;
safearea.right = window.safeAreaInsets.right;
} else {
safearea.top = 0;
safearea.bottom = 0;
safearea.left = 0;
safearea.right = 0;
}
return safearea;
}

163
recipes/ios/src/ios_utils.m Normal file
View file

@ -0,0 +1,163 @@
/*
* iOS utils
*
*/
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <sys/utsname.h>
#include "ios_wrapper.h"
float ios_uiscreen_get_scale() {
float scale = 1.0;
if ([[UIScreen mainScreen] respondsToSelector:@selector(nativeScale)]) {
scale = [[UIScreen mainScreen] nativeScale];
};
return scale;
}
int ios_uiscreen_get_dpi() {
/*
* dpi function from src/video/uikit/SDL_uikitmodes.m (SDL2)
*/
/*
* A well up to date list of device info can be found here:
* https://github.com/lmirosevic/GBDeviceInfo/blob/master/GBDeviceInfo/GBDeviceInfo_iOS.m
*/
NSDictionary* devices = @{
@"iPhone1,1": @163,
@"iPhone1,2": @163,
@"iPhone2,1": @163,
@"iPhone3,1": @326,
@"iPhone3,2": @326,
@"iPhone3,3": @326,
@"iPhone4,1": @326,
@"iPhone5,1": @326,
@"iPhone5,2": @326,
@"iPhone5,3": @326,
@"iPhone5,4": @326,
@"iPhone6,1": @326,
@"iPhone6,2": @326,
@"iPhone7,1": @401,
@"iPhone7,2": @326,
@"iPhone8,1": @326,
@"iPhone8,2": @401,
@"iPhone8,4": @326,
@"iPhone9,1": @326,
@"iPhone9,2": @401,
@"iPhone9,3": @326,
@"iPhone9,4": @401,
@"iPhone10,1": @326,
@"iPhone10,2": @401,
@"iPhone10,3": @458,
@"iPhone10,4": @326,
@"iPhone10,5": @401,
@"iPhone10,6": @458,
@"iPhone11,2": @458,
@"iPhone11,4": @458,
@"iPhone11,6": @458,
@"iPhone11,8": @326,
@"iPhone12,1": @326,
@"iPhone12,3": @458,
@"iPhone12,5": @458,
@"iPad1,1": @132,
@"iPad2,1": @132,
@"iPad2,2": @132,
@"iPad2,3": @132,
@"iPad2,4": @132,
@"iPad2,5": @163,
@"iPad2,6": @163,
@"iPad2,7": @163,
@"iPad3,1": @264,
@"iPad3,2": @264,
@"iPad3,3": @264,
@"iPad3,4": @264,
@"iPad3,5": @264,
@"iPad3,6": @264,
@"iPad4,1": @264,
@"iPad4,2": @264,
@"iPad4,3": @264,
@"iPad4,4": @326,
@"iPad4,5": @326,
@"iPad4,6": @326,
@"iPad4,7": @326,
@"iPad4,8": @326,
@"iPad4,9": @326,
@"iPad5,1": @326,
@"iPad5,2": @326,
@"iPad5,3": @264,
@"iPad5,4": @264,
@"iPad6,3": @264,
@"iPad6,4": @264,
@"iPad6,7": @264,
@"iPad6,8": @264,
@"iPad6,11": @264,
@"iPad6,12": @264,
@"iPad7,1": @264,
@"iPad7,2": @264,
@"iPad7,3": @264,
@"iPad7,4": @264,
@"iPad7,5": @264,
@"iPad7,6": @264,
@"iPad7,11": @264,
@"iPad7,12": @264,
@"iPad8,1": @264,
@"iPad8,2": @264,
@"iPad8,3": @264,
@"iPad8,4": @264,
@"iPad8,5": @264,
@"iPad8,6": @264,
@"iPad8,7": @264,
@"iPad8,8": @264,
@"iPad11,1": @326,
@"iPad11,2": @326,
@"iPad11,3": @326,
@"iPad11,4": @326,
@"iPod1,1": @163,
@"iPod2,1": @163,
@"iPod3,1": @163,
@"iPod4,1": @326,
@"iPod5,1": @326,
@"iPod7,1": @326,
@"iPod9,1": @326,
};
struct utsname systemInfo;
uname(&systemInfo);
NSString* deviceName = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
id foundDPI = devices[deviceName];
if (foundDPI) {
return (float)[foundDPI integerValue];
} else {
/*
* Estimate the DPI based on the screen scale multiplied by the base DPI for the device
* type (e.g. based on iPhone 1 and iPad 1)
*/
float scale = ios_uiscreen_get_scale();
float dpi = 160 * scale;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
dpi = 132 * scale;
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
dpi = 163 * scale;
}
return dpi;
}
}
padding ios_get_safe_area() {
padding safearea;
if (@available(iOS 11.0, *)){
UIWindow *window = UIApplication.sharedApplication.windows[0];
safearea.top = window.safeAreaInsets.top;
safearea.bottom = window.safeAreaInsets.bottom;
safearea.left = window.safeAreaInsets.left;
safearea.right = window.safeAreaInsets.right;
} else {
safearea.top = 0;
safearea.bottom = 0;
safearea.left = 0;
safearea.right = 0;
}
return safearea;
}

View file

@ -12,7 +12,7 @@ float ios_uiscreen_get_scale(void);
int ios_uiscreen_get_dpi(void);
padding ios_get_safe_area(void);
void ios_open_url(char *url);
void load_url_webview(char *url, int width, int height);
void load_url_webview(char *url, int x, int y, int width, int height);
typedef void (*ios_send_email_cb)(char *, void *);

View file

@ -5,7 +5,7 @@ setup(name='ios',
version='1.0',
ext_modules=[
Extension(
'ios', ['ios.c', 'ios_mail.m', 'ios_browser.m'],
'ios', ['ios.c', 'ios_utils.m', 'ios_mail.m', 'ios_browser.m'],
libraries=[ ],
library_dirs=[],
)