Skip to content

Commit

Permalink
Merge pull request gree#446 from gree/feature/relative_margin
Browse files Browse the repository at this point in the history
modified to support both absolute and relative margins.
  • Loading branch information
KojiNakamaru authored Aug 23, 2019
2 parents 27c6a65 + 8bcce78 commit daa065a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
26 changes: 18 additions & 8 deletions plugins/WebViewObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private static extern void _CWebViewPlugin_Update(IntPtr instance,
private static extern int _CWebViewPlugin_Destroy(IntPtr instance);
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_SetMargins(
IntPtr instance, float left, float top, float right, float bottom);
IntPtr instance, float left, float top, float right, float bottom, bool relative);
[DllImport("__Internal")]
private static extern void _CWebViewPlugin_SetVisibility(
IntPtr instance, bool visibility);
Expand Down Expand Up @@ -441,7 +441,7 @@ public void SetCenterPositionWithScale(Vector2 center, Vector2 scale)
#endif
}

public void SetMargins(int left, int top, int right, int bottom)
public void SetMargins(int left, int top, int right, int bottom, bool relative = false)
{
#if UNITY_WEBPLAYER
#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
Expand Down Expand Up @@ -470,13 +470,23 @@ public void SetMargins(int left, int top, int right, int bottom)
_CWebViewPlugin_SetRect(webView, width, height);
rect = new Rect(left, bottom, width, height);
#elif UNITY_IPHONE
float leftPercentage = left / Screen.width;
float topPercentage = top / Screen.height;
float rightPercentage = right / Screen.width;
float bottomPercentage = bottom / Screen.height;
_CWebViewPlugin_SetMargins(webView, leftPercentage, topPercentage, rightPercentage, bottomPercentage);
if (relative) {
float w = (float)Screen.width;
float h = (float)Screen.height;
_CWebViewPlugin_SetMargins(webView, left / w, top / h, right / w, bottom / h, true);
} else {
_CWebViewPlugin_SetMargins(webView, (float)left, (float)top, (float)right, (float)bottom, false);
}
#elif UNITY_ANDROID
webView.Call("SetMargins", left, top, right, AdjustBottomMargin(bottom));
if (relative) {
float w = (float)Screen.width;
float h = (float)Screen.height;
int iw = Screen.currentResolution.width;
int ih = Screen.currentResolution.height;
webView.Call("SetMargins", (int)(left / w * iw), (int)(top / h * ih), (int)(right / w * iw), AdjustBottomMargin((int)(bottom / h * ih)));
} else {
webView.Call("SetMargins", left, top, right, AdjustBottomMargin(bottom));
}
#endif
}

Expand Down
37 changes: 23 additions & 14 deletions plugins/iOS/WebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -509,26 +509,35 @@ - (void)setFrame:(NSInteger)x positionY:(NSInteger)y width:(NSInteger)width heig
webView.frame = frame;
}

- (void)setMargins:(float)left top:(float)top right:(float)right bottom:(float)bottom
- (void)setMargins:(float)left top:(float)top right:(float)right bottom:(float)bottom relative:(BOOL)relative
{
if (webView == nil)
return;
UIView *view = UnityGetGLViewController().view;
CGRect frame = webView.frame;
CGRect screen = view.bounds;
CGFloat width = screen.size.width;
CGFloat height = screen.size.height;
CGFloat leftCG = CGFloat(left) * width;
CGFloat topCG = CGFloat(top) * height;
CGFloat rightCG = CGFloat(right) * height;
CGFloat bottomCG = CGFloat(bottom) * width;
frame.size.width = width - (leftCG + rightCG) ;
frame.size.height = height - (topCG + bottomCG) ;
frame.origin.x = leftCG ;
frame.origin.y = topCG ;
if (relative) {
frame.size.width = screen.size.width * (1.0f - left - right);
frame.size.height = screen.size.height * (1.0f - top - bottom);
frame.origin.x = screen.size.width * left;
frame.origin.y = screen.size.height * top;
} else {
CGFloat scale = 1.0f / [self getScale:view];
frame.size.width = screen.size.width - scale * (left + right) ;
frame.size.height = screen.size.height - scale * (top + bottom) ;
frame.origin.x = scale * left ;
frame.origin.y = scale * top ;
}
webView.frame = frame;
}

- (CGFloat)getScale:(UIView *)view
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
return view.window.screen.nativeScale;
return view.contentScaleFactor;
}

- (void)setVisibility:(BOOL)visibility
{
if (webView == nil)
Expand Down Expand Up @@ -645,7 +654,7 @@ - (const char *)getCustomRequestHeaderValue:(const char *)headerKey
void _CWebViewPlugin_Destroy(void *instance);
void _CWebViewPlugin_SetFrame(void* instace, int x, int y, int width, int height);
void _CWebViewPlugin_SetMargins(
void *instance, float left, float top, float right, float bottom);
void *instance, float left, float top, float right, float bottom, BOOL relative);
void _CWebViewPlugin_SetVisibility(void *instance, BOOL visibility);
void _CWebViewPlugin_LoadURL(void *instance, const char *url);
void _CWebViewPlugin_LoadHTML(void *instance, const char *html, const char *baseUrl);
Expand Down Expand Up @@ -692,10 +701,10 @@ void _CWebViewPlugin_SetFrame(void* instance, int x, int y, int width, int heigh
}

void _CWebViewPlugin_SetMargins(
void *instance, float left, float top, float right, float bottom)
void *instance, float left, float top, float right, float bottom, BOOL relative)
{
CWebViewPlugin *webViewPlugin = (__bridge CWebViewPlugin *)instance;
[webViewPlugin setMargins:left top:top right:right bottom:bottom];
[webViewPlugin setMargins:left top:top right:right bottom:bottom relative:relative];
}

void _CWebViewPlugin_SetVisibility(void *instance, BOOL visibility)
Expand Down

0 comments on commit daa065a

Please sign in to comment.