Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-24982] Android/iOS: Add WebView zoomLevel #9226

Merged
merged 38 commits into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
9c15b71
add zoomBy
m1ga Jun 7, 2017
c2b5910
Merge branch 'master' into zoomBy
m1ga Jul 19, 2017
5f3b916
Merge branch 'master' into zoomBy
m1ga Jul 19, 2017
ec3faf9
Merge branch 'zoomBy' of https://github.com/m1ga/titanium_mobile into…
m1ga Jul 19, 2017
5fbb647
[TIMOB-24982] Expose "zoomLevel", add docs
hansemannn Jul 19, 2017
4c75fd3
Merge pull request #3 from hansemannn/TIMOB-24982
m1ga Jul 19, 2017
1b17f3f
zoomBy -> zoomLevel
m1ga Jul 19, 2017
26b6280
Merge branch 'zoomBy' of https://github.com/m1ga/titanium_mobile into…
m1ga Jul 19, 2017
adba36a
Merge branch 'master' into zoomBy
m1ga Jul 19, 2017
85696af
Update WebView.yml
hansemannn Jul 20, 2017
7a43e59
Update WebView.yml
hansemannn Jul 20, 2017
64d9ac1
Update WebViewProxy.java
m1ga Jul 21, 2017
884ebb3
Merge branch 'master' into zoomBy
m1ga Jul 21, 2017
bff8cd2
Merge branch 'master' into zoomBy
m1ga Aug 11, 2017
582dc39
Update TiUIWebViewProxy.m
hansemannn Sep 8, 2017
30af365
Update default value, check api level, change since version
m1ga Sep 10, 2017
2145a8b
update scale
m1ga Nov 3, 2017
c1a4ff8
merge master
m1ga Nov 3, 2017
0fd721e
removed log
m1ga Nov 8, 2017
1cb5ce3
move version check into zoomBy function
m1ga Nov 8, 2017
809281f
Merge branch 'master' into zoomBy
m1ga Nov 8, 2017
bba3c90
Merge branch 'master' into zoomBy
m1ga Jan 14, 2018
26674c7
Update WebView.yml
hansemannn Feb 15, 2018
9ca3005
Update TiUIWebView.m
hansemannn Feb 15, 2018
acd0d8f
Update TiUIWebViewProxy.m
hansemannn Feb 15, 2018
18b2d7d
Merge branch 'master' into zoomBy
hansemannn Feb 15, 2018
f9a5ae1
clang-format
m1ga Feb 16, 2018
fff87a6
Merge branch 'master' into zoomBy
m1ga Feb 16, 2018
1d1f4b5
clang off/on
m1ga Feb 16, 2018
619bdf7
Merge branch 'master' into zoomBy
m1ga Feb 17, 2018
7f84e75
Update WebView.yml
hansemannn May 9, 2018
7fba15b
Merge branch 'master' into zoomBy
hansemannn May 9, 2018
e2ec1bd
Merge branch 'master' into zoomBy
hansemannn May 9, 2018
a9f7aff
Attempt to fix merge conflict
hansemannn May 9, 2018
e892eb6
Merge branch 'master' into zoomBy
lokeshchdhry May 9, 2018
1158f03
return correct values
m1ga May 9, 2018
9a356ed
Merge branch 'master' into zoomBy
m1ga May 9, 2018
25cf084
Merge branch 'master' into zoomBy
hansemannn May 15, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class WebViewProxy extends ViewProxy implements Handler.Callback, OnLifec
private static final int MSG_RESUME = MSG_FIRST_ID + 112;
private static final int MSG_SET_HEADERS = MSG_FIRST_ID + 113;
private static final int MSG_GET_HEADERS = MSG_FIRST_ID + 114;
private static final int MSG_ZOOM_BY = MSG_FIRST_ID + 115;

protected static final int MSG_LAST_ID = MSG_FIRST_ID + 999;
private static String fusername;
Expand All @@ -75,6 +76,7 @@ public WebViewProxy()
defaultValues.put(TiC.PROPERTY_LIGHT_TOUCH_ENABLED, true);
defaultValues.put(TiC.PROPERTY_ENABLE_JAVASCRIPT_INTERFACE, true);
defaultValues.put(TiC.PROPERTY_DISABLE_CONTEXT_MENU, false);
defaultValues.put(TiC.PROPERTY_ZOOM_LEVEL, 1.0);
}

@Override
Expand Down Expand Up @@ -206,6 +208,9 @@ public boolean handleMessage(Message msg)
String html = TiConvert.toString(getProperty(TiC.PROPERTY_HTML));
getWebView().setHtml(html);
return true;
case MSG_ZOOM_BY:
getWebView().zoomBy(TiConvert.toFloat(getProperty(TiC.PROPERTY_ZOOM_LEVEL)));
return true;
}
}
return super.handleMessage(msg);
Expand Down Expand Up @@ -447,6 +452,40 @@ public boolean getEnableZoomControls()
return enabled;
}

// clang-format off
@Kroll.method
@Kroll.getProperty
public float getZoomLevel()
// clang-format on
{
TiUIView v = peekView();
if (v != null) {
return TiConvert.toFloat(getProperty(TiC.PROPERTY_ZOOM_LEVEL), 1.0f);
} else {
return 1.0f;
}
}

// clang-format off
@Kroll.method
@Kroll.setProperty
public void setZoomLevel(float value)
// clang-format on
{
setProperty(TiC.PROPERTY_ZOOM_LEVEL, value);

// If the web view has not been created yet, don't set html here. It will be set in processProperties() when the
// view is created.
TiUIView v = peekView();
if (v != null) {
if (TiApplication.isUIThread()) {
((TiUIWebView) v).zoomBy(value);
} else {
getMainHandler().sendEmptyMessage(MSG_ZOOM_BY);
}
}
}

public void clearBasicAuthentication()
{
fusername = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2013 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2009-2018 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
Expand Down Expand Up @@ -63,6 +63,9 @@ public class TiUIWebView extends TiUIView
private boolean isLocalHTML = false;
private boolean disableContextMenu = false;
private HashMap<String, String> extraHeaders = new HashMap<String, String>();
private float zoomLevel =
TiApplication.getInstance().getApplicationContext().getResources().getDisplayMetrics().density;
private float initScale = zoomLevel;

private static Enum<?> enumPluginStateOff;
private static Enum<?> enumPluginStateOn;
Expand Down Expand Up @@ -479,6 +482,10 @@ public void processProperties(KrollDict d)
if (d.containsKey(TiC.PROPERTY_USER_AGENT)) {
((WebViewProxy) getProxy()).setUserAgent(d.getString(TiC.PROPERTY_USER_AGENT));
}

if (d.containsKey(TiC.PROPERTY_ZOOM_LEVEL)) {
zoomBy(getWebView(), TiConvert.toFloat(d, TiC.PROPERTY_ZOOM_LEVEL));
}
}

@Override
Expand Down Expand Up @@ -510,6 +517,8 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
}
} else if (TiC.PROPERTY_DISABLE_CONTEXT_MENU.equals(key)) {
disableContextMenu = TiConvert.toBoolean(newValue);
} else if (TiC.PROPERTY_ZOOM_LEVEL.equals(key)) {
zoomBy(getWebView(), TiConvert.toFloat(newValue, 1.0f));
} else if (TiC.PROPERTY_USER_AGENT.equals(key)) {
((WebViewProxy) getProxy()).setUserAgent(TiConvert.toString(newValue));
} else {
Expand All @@ -526,6 +535,36 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
}
}

private void zoomBy(WebView webView, float scale)
{
if (Build.VERSION.SDK_INT >= 21 && webView != null) {
if (scale <= 0.0f) {
scale = 0.01f;
} else if (scale >= 100.0f) {
scale = 100.0f;
}

float targetVal = (initScale * scale) / zoomLevel;
webView.zoomBy(targetVal);
}
}

public void zoomBy(float scale)
{
zoomBy(getWebView(), scale);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This public zoomBy() method is missing an API Level 21 check like how we handle the zoom level property. Either add the API Level check here too or move all the API Level checks to the private zoomBy() method.

}

public float getZoomLevel()
{
return zoomLevel;
}

public void setZoomLevel(float value)
{
getProxy().setProperty(TiC.PROPERTY_ZOOM_LEVEL, value / initScale);
zoomLevel = value;
}

private boolean mightBeHtml(String url)
{
String mime = TiMimeTypeHelper.getMimeType(url);
Expand Down Expand Up @@ -950,6 +989,7 @@ public boolean shouldInjectBindingCode()
public void setBindingCodeInjected(boolean injected)
{
bindingCodeInjected = injected;
initScale = getZoomLevel();
}

public boolean interceptOnBackPressed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,11 @@ public void onLoadResource(WebView view, String url)
data.put(TiC.PROPERTY_URL, url);
proxy.fireEvent(TiC.EVENT_WEBVIEW_ON_LOAD_RESOURCE, data);
}

@Override
public void onScaleChanged(WebView view, float oldScale, float newScale)
{
super.onScaleChanged(view, oldScale, newScale);
webView.setZoomLevel(newScale);
}
}
5 changes: 5 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Original file line number Diff line number Diff line change
Expand Up @@ -3313,6 +3313,11 @@ public class TiC
*/
public static final String PROPERTY_ZINDEX = "zIndex";

/**
* @module.api
*/
public static final String PROPERTY_ZOOM_LEVEL = "zoomLevel";

/**
* @module.api
*/
Expand Down
8 changes: 7 additions & 1 deletion apidoc/Titanium/UI/WebView.yml
Original file line number Diff line number Diff line change
Expand Up @@ -684,9 +684,15 @@ properties:
On Android you should avoid Calling `setRequestHeaders()` right after `createWebView()`. Use the `requestHeaders` property
inside `createWebView()` or put it inside the window `open` event.
type: Dictionary
platforms: [android,iphone,ipad]
platforms: [android, iphone, ipad]
since: "6.1.0"

- name: zoomLevel
summary: Manage the zoom-level of the current page.
type: Number
default: undefined. Behaves as no zoom applied.
platforms: [android, iphone, ipad]
since: "7.2.0"

examples:
- title: Basic Web View to External URL
Expand Down
7 changes: 7 additions & 0 deletions iphone/Classes/TiUIWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,13 @@ - (void)setAutoDetect_:(NSArray *)values
[[self webview] setDataDetectorTypes:result];
}

- (void)setZoomLevel_:(id)value
{
ENSURE_TYPE(value, NSNumber);

[[self webview] stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = %@;", value]];
}

- (void)setHtml_:(NSString *)content withObject:(id)property
{
NSString *baseURLString = [TiUtils stringValue:@"baseURL" properties:property];
Expand Down
5 changes: 5 additions & 0 deletions iphone/Classes/TiUIWebViewProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ - (id)loading
return NUMBOOL(loading);
}

- (NSNumber *)zoomLevel
{
return [[(TiUIWebView *)[self view] webview] stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom"];
}

- (void)goBack:(id)args
{
TiThreadPerformOnMainThread(^{
Expand Down