6.6.3 - Alpha - 修复导航栏遮挡在线文档内容问题 (代码重构)
This commit is contained in:
@@ -21,6 +21,7 @@ import org.autojs.autojs6.R
|
||||
import org.autojs.autojs6.databinding.FragmentOnlineDocsBinding
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import org.greenrobot.eventbus.Subscribe
|
||||
import org.intellij.lang.annotations.Language
|
||||
|
||||
/**
|
||||
* Created by Stardust on Aug 22, 2017.
|
||||
@@ -59,7 +60,25 @@ class DocumentationFragment : ViewPagerFragment(ROTATION_GONE), BackPressedHandl
|
||||
if (ViewUtils.isNightModeYes(requireContext())) {
|
||||
WebViewUtils.adaptDarkMode(webView)
|
||||
}
|
||||
WebViewUtils.excludeWebViewFromNavigationBar(ewebView, webView)
|
||||
WebViewUtils.excludeWebViewFromNavigationBar(ewebView, webView) { systemBarInsetsBottom ->
|
||||
@Language("JavaScript")
|
||||
val injectCode = """
|
||||
(function() {
|
||||
let mainElements = document.querySelectorAll('main > *');
|
||||
if (mainElements.length > 0) {
|
||||
// Online docs (zh-CN: 在线文档)
|
||||
mainElements.forEach(ele => {
|
||||
let basePaddingBottom = ele.className === 'sidebar-toggle' ? 10 : 0;
|
||||
ele.style.paddingBottom = (basePaddingBottom + ${systemBarInsetsBottom}) / window.devicePixelRatio + 'px';
|
||||
});
|
||||
} else {
|
||||
// Local docs (zh-CN: 本地文档)
|
||||
document.body.style.paddingBottom = '${systemBarInsetsBottom}px';
|
||||
}
|
||||
})();
|
||||
""".trimIndent()
|
||||
injectCode
|
||||
}
|
||||
}
|
||||
}
|
||||
restoreViewStates()
|
||||
|
||||
@@ -18,6 +18,7 @@ import org.intellij.lang.annotations.Language
|
||||
*/
|
||||
class WebViewUtils {
|
||||
|
||||
@Suppress("SameParameterValue")
|
||||
companion object {
|
||||
|
||||
// @Hint by SuperMonster003 on Aug 26, 2022.
|
||||
@@ -72,11 +73,37 @@ class WebViewUtils {
|
||||
excludeWebViewFromSystemBars(webViewWrapper, webView, excludeStatusBar = false, excludeNavigationBar = true)
|
||||
}
|
||||
|
||||
fun excludeWebViewFromNavigationBar(webViewWrapper: View, webView: WebView, excludeNavigationBarInjectCode: (systemBarInsetsBottom: Int) -> String) {
|
||||
excludeWebViewFromSystemBars(webViewWrapper, webView, excludeStatusBar = false, excludeNavigationBarInjectCode = excludeNavigationBarInjectCode)
|
||||
}
|
||||
|
||||
fun excludeWebViewFromStatusBarAndNavigationBar(webViewWrapper: View, webView: WebView) {
|
||||
excludeWebViewFromSystemBars(webViewWrapper, webView, excludeStatusBar = true, excludeNavigationBar = true)
|
||||
}
|
||||
|
||||
@Suppress("SameParameterValue")
|
||||
private fun excludeWebViewFromSystemBars(webViewWrapper: View, webView: WebView, excludeStatusBar: Boolean, excludeNavigationBarInjectCode: (systemBarInsetsBottom: Int) -> String) {
|
||||
var mSystemBarInsets: Insets? = null
|
||||
ViewCompat.setOnApplyWindowInsetsListener(webViewWrapper) { v, insets ->
|
||||
val systemBarInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars()).also {
|
||||
mSystemBarInsets = it
|
||||
}
|
||||
if (excludeStatusBar) {
|
||||
v.setPadding(0, systemBarInsets.top, 0, 0)
|
||||
}
|
||||
webView.setPadding(0, 0, 0, systemBarInsets.bottom)
|
||||
insets
|
||||
}
|
||||
webView.webViewClient = object : WebViewClient() {
|
||||
override fun onPageFinished(view: WebView?, url: String?) {
|
||||
super.onPageFinished(view, url)
|
||||
val systemBarInsets = mSystemBarInsets ?: return
|
||||
val webView = view ?: return
|
||||
val injectCode = excludeNavigationBarInjectCode(systemBarInsets.bottom)
|
||||
adjustBodyPaddingToAvoidNavOverlay(webView, injectCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun excludeWebViewFromSystemBars(webViewWrapper: View, webView: WebView, excludeStatusBar: Boolean, excludeNavigationBar: Boolean) {
|
||||
var mSystemBarInsets: Insets? = null
|
||||
ViewCompat.setOnApplyWindowInsetsListener(webViewWrapper) { v, insets ->
|
||||
@@ -97,34 +124,20 @@ class WebViewUtils {
|
||||
super.onPageFinished(view, url)
|
||||
val systemBarInsets = mSystemBarInsets ?: return
|
||||
val webView = view ?: return
|
||||
adjustBodyPaddingToAvoidNavOverlay(webView, systemBarInsets)
|
||||
// @Hint by SuperMonster003 on Mar 12, 2025.
|
||||
// ! Inject JavaScript code for document body with adding a bottom padding
|
||||
// ! to ensure that content won't be covered by nav bar when scrolling to the end.
|
||||
// ! zh-CN: 注入 JavaScript, 为 body 添加底部 padding, 确保滚动到底部时导航栏不遮挡内容.
|
||||
@Language("JavaScript")
|
||||
val defaultInjectCode = "document.body.style.paddingBottom = '${systemBarInsets.bottom}px'"
|
||||
adjustBodyPaddingToAvoidNavOverlay(webView, defaultInjectCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun adjustBodyPaddingToAvoidNavOverlay(webView: WebView, systemBarInsets: Insets) {
|
||||
// @Hint by SuperMonster003 on Mar 12, 2025.
|
||||
// ! Inject JavaScript code for document body with adding a bottom padding
|
||||
// ! to ensure that content won't be covered by nav bar when scrolling to the end.
|
||||
// ! zh-CN: 注入 JavaScript, 为 body 添加底部 padding, 确保滚动到底部时导航栏不遮挡内容.
|
||||
@Language("JavaScript")
|
||||
val jsCode = """
|
||||
(function() {
|
||||
let mainElements = document.querySelectorAll('main > *');
|
||||
if (mainElements.length > 0) {
|
||||
const additionalPadding = ${systemBarInsets.bottom};
|
||||
mainElements.forEach(ele => {
|
||||
const computedStyle = window.getComputedStyle(ele);
|
||||
const currentPadding = parseFloat(computedStyle.paddingBottom) || 0;
|
||||
ele.style.paddingBottom = (currentPadding + additionalPadding) / window.devicePixelRatio + 'px';
|
||||
});
|
||||
} else {
|
||||
document.body.style.paddingBottom = '${systemBarInsets.bottom}px';
|
||||
}
|
||||
})();
|
||||
""".trimIndent()
|
||||
webView.evaluateJavascript(jsCode, null)
|
||||
private fun adjustBodyPaddingToAvoidNavOverlay(webView: WebView, injectCode: String) {
|
||||
webView.evaluateJavascript(injectCode, null)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user