From fe2dcabdd46a2447d848ea9f0be33e584f0c445b Mon Sep 17 00:00:00 2001 From: AskaEth Date: Sun, 16 Aug 2026 20:27:17 +0800 Subject: [PATCH] feat: hide foreground webapp when switching sidebar pages --- app/src/main/assets/h5/js/router.js | 9 +++++++++ app/src/main/java/top/yeij/hearth/MainActivity.kt | 6 ++++++ app/src/main/java/top/yeij/hearth/webview/JsBridge.kt | 10 ++++++++++ .../main/java/top/yeij/hearth/webview/WebAppHost.kt | 5 +++++ 4 files changed, 30 insertions(+) diff --git a/app/src/main/assets/h5/js/router.js b/app/src/main/assets/h5/js/router.js index 6d26fa2..ec114b2 100644 --- a/app/src/main/assets/h5/js/router.js +++ b/app/src/main/assets/h5/js/router.js @@ -24,6 +24,15 @@ const router = { this.current = id; document.querySelectorAll('[data-page]').forEach(el => el.classList.toggle('active', el.dataset.page === id)); + // 切页时把前台 webapp 丢到后台(隐藏内容 WebView,标签状态保留) + // Hide the foreground webapp when switching pages (keep tab state) + bridge.call('hideWebApps').catch(() => {}); + // 同时隐藏 webapp 顶栏与标签面板(离开 webapp 场景) + // Also hide the webapp topbar and tab panel (leaving the webapp context) + const topbar = document.getElementById('web-topbar'); + if (topbar) topbar.style.display = 'none'; + const tabPanel = document.getElementById('tab-panel'); + if (tabPanel) tabPanel.style.display = 'none'; // 非首页时侧边栏顶部显示小时间(容器始终占位,仅切换可见性避免撑动图标) // Show the small clock at the sidebar top when not on home (the container // always occupies space; only toggle visibility to avoid shifting icons) diff --git a/app/src/main/java/top/yeij/hearth/MainActivity.kt b/app/src/main/java/top/yeij/hearth/MainActivity.kt index d583cb8..d3d059c 100644 --- a/app/src/main/java/top/yeij/hearth/MainActivity.kt +++ b/app/src/main/java/top/yeij/hearth/MainActivity.kt @@ -293,6 +293,12 @@ class MainActivity : Activity() { Log.d(TAG, "switchWebView: id=$id") } + override fun hideAll() { + contentWebViews.values.forEach { it.visibility = View.GONE } + currentVisibleId = null + Log.d(TAG, "hideAll: hidden ${contentWebViews.size} webviews") + } + override fun goBack(): Boolean { val webView = currentVisibleId?.let { contentWebViews[it] } return if (webView != null && webView.canGoBack()) { diff --git a/app/src/main/java/top/yeij/hearth/webview/JsBridge.kt b/app/src/main/java/top/yeij/hearth/webview/JsBridge.kt index 32e29ae..ede4f1c 100644 --- a/app/src/main/java/top/yeij/hearth/webview/JsBridge.kt +++ b/app/src/main/java/top/yeij/hearth/webview/JsBridge.kt @@ -202,6 +202,16 @@ class JsBridge( Log.d("HearthBridge", "webReload called") } + // 隐藏所有内容 WebView(把前台 webapp 丢到后台,标签状态保留) + // Hide all content WebViews (send the foreground webapp to background, + // keeping the tab state) — called when the user switches pages via the sidebar + @android.webkit.JavascriptInterface + fun hideWebApps() { + val host = webAppHost ?: return + onMain { host.hideAll() } + Log.d("HearthBridge", "hideWebApps called") + } + // 清单内存缓存:首次拉取成功后缓存,后续复用(避免 openWebApp 重复 I/O) // 空结果(离线失败)不缓存,下次调用重试拉取,避免空清单被永久缓存 // In-memory manifest cache: cache only on a successful non-empty fetch (avoid diff --git a/app/src/main/java/top/yeij/hearth/webview/WebAppHost.kt b/app/src/main/java/top/yeij/hearth/webview/WebAppHost.kt index e915b6b..43f8472 100644 --- a/app/src/main/java/top/yeij/hearth/webview/WebAppHost.kt +++ b/app/src/main/java/top/yeij/hearth/webview/WebAppHost.kt @@ -35,4 +35,9 @@ interface WebAppHost { // 将标签状态推送到桌面 H5 顶栏 // Push the tab state to the desktop H5 topbar fun syncTabs(tabs: List) + + // 隐藏所有内容 WebView(把前台 webapp 丢到后台,标签状态保留) + // Hide all content WebViews (send the foreground webapp to background, + // keeping the tab state) + fun hideAll() }