Add ZoomDensity so that we can change the desired default scale.

Fix a bug where we didn't adjust the viewport scale according to our density.
This commit is contained in:
Grace Kloba
2009-06-25 11:20:51 -07:00
parent d4eac5f384
commit 0d8b77c245
3 changed files with 88 additions and 11 deletions

View File

@@ -69,7 +69,24 @@ public class WebSettings {
}
int value;
}
/**
* Enum for specifying the WebView's desired density.
* FAR makes 100% looking like in 240dpi
* MEDIUM makes 100% looking like in 160dpi
* CLOSE makes 100% looking like in 120dpi
* @hide Pending API council approval
*/
public enum ZoomDensity {
FAR(150), // 240dpi
MEDIUM(100), // 160dpi
CLOSE(75); // 120dpi
ZoomDensity(int size) {
value = size;
}
int value;
}
/**
* Default cache usage pattern Use with {@link #setCacheMode}.
*/
@@ -105,6 +122,8 @@ public class WebSettings {
LOW
}
// WebView associated with this WebSettings.
private WebView mWebView;
// BrowserFrame used to access the native frame pointer.
private BrowserFrame mBrowserFrame;
// Flag to prevent multiple SYNC messages at one time.
@@ -145,6 +164,7 @@ public class WebSettings {
// Don't need to synchronize the get/set methods as they
// are basic types, also none of these values are used in
// native WebCore code.
private ZoomDensity mDefaultZoom = ZoomDensity.MEDIUM;
private RenderPriority mRenderPriority = RenderPriority.NORMAL;
private int mOverrideCacheMode = LOAD_DEFAULT;
private boolean mSaveFormData = true;
@@ -237,9 +257,10 @@ public class WebSettings {
* Package constructor to prevent clients from creating a new settings
* instance.
*/
WebSettings(Context context) {
WebSettings(Context context, WebView webview) {
mEventHandler = new EventHandler();
mContext = context;
mWebView = webview;
mDefaultTextEncoding = context.getString(com.android.internal.
R.string.default_text_encoding);
@@ -446,6 +467,31 @@ public class WebSettings {
return mTextSize;
}
/**
* Set the default zoom density of the page. This should be called from UI
* thread.
* @param zoom A ZoomDensity value
* @see WebSettings.ZoomDensity
* @hide Pending API council approval
*/
public void setDefaultZoom(ZoomDensity zoom) {
if (mDefaultZoom != zoom) {
mDefaultZoom = zoom;
mWebView.updateDefaultZoomDensity(zoom.value);
}
}
/**
* Get the default zoom density of the page. This should be called from UI
* thread.
* @return A ZoomDensity value
* @see WebSettings.ZoomDensity
* @hide Pending API council approval
*/
public ZoomDensity getDefaultZoom() {
return mDefaultZoom;
}
/**
* Enables using light touches to make a selection and activate mouseovers.
*/

View File

@@ -408,7 +408,7 @@ public class WebView extends AbsoluteLayout
// default scale. Depending on the display density.
static int DEFAULT_SCALE_PERCENT;
private float DEFAULT_SCALE;
private float mDefaultScale;
// set to true temporarily while the zoom control is being dragged
private boolean mPreviewZoomOnly = false;
@@ -640,7 +640,7 @@ public class WebView extends AbsoluteLayout
mZoomFitPageButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
zoomWithPreview(DEFAULT_SCALE);
zoomWithPreview(mDefaultScale);
updateZoomButtonsEnabled();
}
});
@@ -663,7 +663,7 @@ public class WebView extends AbsoluteLayout
// or out.
mZoomButtonsController.setZoomInEnabled(canZoomIn);
mZoomButtonsController.setZoomOutEnabled(canZoomOut);
mZoomFitPageButton.setEnabled(mActualScale != DEFAULT_SCALE);
mZoomFitPageButton.setEnabled(mActualScale != mDefaultScale);
}
mZoomOverviewButton.setVisibility(canZoomScrollOut() ? View.VISIBLE:
View.GONE);
@@ -685,7 +685,7 @@ public class WebView extends AbsoluteLayout
mNavSlop = (int) (16 * density);
// density adjusted scale factors
DEFAULT_SCALE_PERCENT = (int) (100 * density);
DEFAULT_SCALE = density;
mDefaultScale = density;
mActualScale = density;
mInvActualScale = 1 / density;
DEFAULT_MAX_ZOOM_SCALE = 4.0f * density;
@@ -694,6 +694,23 @@ public class WebView extends AbsoluteLayout
mMinZoomScale = DEFAULT_MIN_ZOOM_SCALE;
}
/* package */void updateDefaultZoomDensity(int zoomDensity) {
final float density = getContext().getResources().getDisplayMetrics().density
* 100 / zoomDensity;
if (Math.abs(density - mDefaultScale) > 0.01) {
float scaleFactor = density / mDefaultScale;
// adjust the limits
mNavSlop = (int) (16 * density);
DEFAULT_SCALE_PERCENT = (int) (100 * density);
DEFAULT_MAX_ZOOM_SCALE = 4.0f * density;
DEFAULT_MIN_ZOOM_SCALE = 0.25f * density;
mDefaultScale = density;
mMaxZoomScale *= scaleFactor;
mMinZoomScale *= scaleFactor;
setNewZoomScale(mActualScale * scaleFactor, false);
}
}
/* package */ boolean onSavePassword(String schemePlusHost, String username,
String password, final Message resumeMsg) {
boolean rVal = false;
@@ -4172,8 +4189,8 @@ public class WebView extends AbsoluteLayout
float oldScale = mActualScale;
// snap to DEFAULT_SCALE if it is close
if (scale > (DEFAULT_SCALE - 0.05) && scale < (DEFAULT_SCALE + 0.05)) {
scale = DEFAULT_SCALE;
if (scale > (mDefaultScale - 0.05) && scale < (mDefaultScale + 0.05)) {
scale = mDefaultScale;
}
setNewZoomScale(scale, false);
@@ -4689,7 +4706,7 @@ public class WebView extends AbsoluteLayout
int initialScale = msg.arg1;
int viewportWidth = msg.arg2;
// start a new page with DEFAULT_SCALE zoom scale.
float scale = DEFAULT_SCALE;
float scale = mDefaultScale;
if (mInitialScale > 0) {
scale = mInitialScale / 100.0f;
} else {

View File

@@ -139,7 +139,7 @@ final class WebViewCore {
// ready.
mEventHub = new EventHub();
// Create a WebSettings object for maintaining all settings
mSettings = new WebSettings(mContext);
mSettings = new WebSettings(mContext, mWebView);
// The WebIconDatabase needs to be initialized within the UI thread so
// just request the instance here.
WebIconDatabase.getInstance();
@@ -1560,6 +1560,20 @@ final class WebViewCore {
// set the viewport settings from WebKit
setViewportSettingsFromNative();
// adjust the default scale to match the density
if (WebView.DEFAULT_SCALE_PERCENT != 100) {
float adjust = WebView.DEFAULT_SCALE_PERCENT / 100;
if (mViewportInitialScale > 0) {
mViewportInitialScale *= adjust;
}
if (mViewportMinimumScale > 0) {
mViewportMinimumScale *= adjust;
}
if (mViewportMaximumScale > 0) {
mViewportMaximumScale *= adjust;
}
}
// infer the values if they are not defined.
if (mViewportWidth == 0) {
if (mViewportInitialScale == 0) {
@@ -1586,7 +1600,7 @@ final class WebViewCore {
mViewportMaximumScale = mViewportInitialScale;
} else if (mViewportInitialScale == 0) {
mViewportInitialScale = mViewportMaximumScale;
}
}
}
if (mViewportWidth < 0
&& mViewportInitialScale == WebView.DEFAULT_SCALE_PERCENT) {