// 桌面路由:侧边栏 5 页 + 页面切换 + 顶部时间
// Launcher router: 5 sidebar pages + page switching + top clock
// 侧边栏图标:内联 SVG(stroke=currentColor,跟随主题色),替代 emoji
// Sidebar icons: inline SVG (stroke=currentColor, follows theme color), replacing emoji
const SVG_ICONS = {
home: '',
immersive: '',
webapp: '',
app: '',
settings: '',
};
const PAGES = [
{ id: 'home', label: '首页', icon: SVG_ICONS.home },
{ id: 'immersive', label: '沉浸首页', icon: SVG_ICONS.immersive },
{ id: 'webapplist', label: 'H5 应用', icon: SVG_ICONS.webapp },
{ id: 'applist', label: '安卓 APP', icon: SVG_ICONS.app },
{ id: 'settings', label: '设置', icon: SVG_ICONS.settings },
];
const router = {
current: 'home',
navigate(id) {
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';
// 移除 webapp-active:webapp 已丢后台,恢复 webapp 列表显示
// Remove webapp-active: the webapp is backgrounded, restore the list display
document.body.classList.remove('webapp-active');
// 关闭长按菜单(切页时)
// Close the long-press menu on page switch
document.querySelectorAll('.wapp-menu').forEach((m) => m.remove());
// 非首页时侧边栏顶部显示小时间(容器始终占位,仅切换可见性避免撑动图标)
// Show the small clock at the sidebar top when not on home (the container
// always occupies space; only toggle visibility to avoid shifting icons)
const clock = document.getElementById('rail-clock');
if (clock) clock.style.visibility = (id === 'home') ? 'hidden' : 'visible';
// 延迟内容加载到下一帧:让切换动画先流畅播放,避免加载重活(查应用/拉清单)
// 在动画帧内阻塞导致卡顿
// Defer content loading to the next frame: let the switch animation play
// smoothly first, avoiding jank from heavy loads (app query / manifest fetch)
// blocking within the animation frame
requestAnimationFrame(() => {
if (id === 'home') window.updateHomeCards && window.updateHomeCards();
if (id === 'applist') window.loadAppList && window.loadAppList();
if (id === 'webapplist') window.loadWebAppList && window.loadWebAppList();
if (id === 'settings') window.loadSettings && window.loadSettings();
});
}
};