Skip to content

Commit

Permalink
fix webview scale
Browse files Browse the repository at this point in the history
  • Loading branch information
ekibun committed Feb 15, 2020
1 parent 00c6385 commit ac7099a
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions app/src/main/java/soko/ekibun/bangumi/ui/view/NestedWebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import soko.ekibun.bangumi.util.HttpUtil
* @property childWebView NestedWebView?
* @constructor
*/
class NestedWebView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = android.R.attr.webViewStyle) : WebView(context, attrs, defStyleAttr) {
class NestedWebView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = android.R.attr.webViewStyle
) : WebView(context, attrs, defStyleAttr) {

var onProgressChanged = { _: WebView, _: Int -> }
var shouldOverrideUrlLoading = { _: WebView, _: WebResourceRequest -> false }
Expand Down Expand Up @@ -59,7 +63,7 @@ class NestedWebView @JvmOverloads constructor(context: Context, attrs: Attribute

@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
if(event.action == MotionEvent.ACTION_DOWN && scrollY <= 0)
if (event.action == MotionEvent.ACTION_DOWN && scrollY <= 0)
scrollTo(scrollX, 1)

return super.onTouchEvent(event)
Expand All @@ -68,7 +72,7 @@ class NestedWebView @JvmOverloads constructor(context: Context, attrs: Attribute
/**
* 关闭窗口
*/
fun close(){
fun close() {
val parentWebView = parentWebView ?: return
val parent = (parent as? ViewGroup) ?: return
parent.removeView(this)
Expand All @@ -82,15 +86,24 @@ class NestedWebView @JvmOverloads constructor(context: Context, attrs: Attribute

companion object {

val mWebChromeClient = object: WebChromeClient(){
override fun onShowFileChooser(webView: WebView?, filePathCallback: ValueCallback<Array<Uri>>?, fileChooserParams: FileChooserParams?): Boolean {
val mWebChromeClient = object : WebChromeClient() {
override fun onShowFileChooser(
webView: WebView?,
filePathCallback: ValueCallback<Array<Uri>>?,
fileChooserParams: FileChooserParams?
): Boolean {
return (webView as? NestedWebView)?.onShowFileChooser?.invoke(filePathCallback, fileChooserParams)
?: super.onShowFileChooser(webView, filePathCallback, fileChooserParams)
?: super.onShowFileChooser(webView, filePathCallback, fileChooserParams)
}

override fun onCreateWindow(view: WebView, isDialog: Boolean, isUserGesture: Boolean, resultMsg: Message): Boolean {
override fun onCreateWindow(
view: WebView,
isDialog: Boolean,
isUserGesture: Boolean,
resultMsg: Message
): Boolean {
val parent = (view.parent as? ViewGroup) ?: return false
val webview = (view as? NestedWebView)?: return false
val webview = (view as? NestedWebView) ?: return false
val newView = NestedWebView(view.context)
newView.onProgressChanged = webview.onProgressChanged
newView.onShowFileChooser = webview.onShowFileChooser
Expand All @@ -116,7 +129,7 @@ class NestedWebView @JvmOverloads constructor(context: Context, attrs: Attribute

override fun onCloseWindow(window: WebView?) {
super.onCloseWindow(window)
val webview = (window as? NestedWebView)?: return
val webview = (window as? NestedWebView) ?: return
webview.close()
}

Expand All @@ -131,10 +144,8 @@ class NestedWebView @JvmOverloads constructor(context: Context, attrs: Attribute
}

fun updateViewPort(view: WebView) {
if (!PreferenceManager.getDefaultSharedPreferences(view.context).getBoolean(
"webview_fix_scale",
true
)
if (!PreferenceManager.getDefaultSharedPreferences(view.context)
.getBoolean("webview_fix_scale", true)
) return
view.evaluateJavascript(
"""
Expand Down

0 comments on commit ac7099a

Please sign in to comment.