import { ChangeDetectionStrategy, Component, OnDestroy, computed, inject, signal } from '@angular/core';
import { NavigationEnd, Router, RouterOutlet } from '@angular/router';
import { filter } from 'rxjs';
import { LiveService } from '../../core/live.service';
import { MatrixSessionService } from '../../core/matrix-session.service';
import { PaletteService } from '../../core/palette.service';
import { DeviceService } from '../../core/device.service';
import { UnreadService } from '../../core/unread.service';
import { SessionService } from '../../core/session.service';
import { FmodeApi } from '../../core/api/fmode-api';
import { AppNotificationService } from '../../core/app-notification.service';
import { PermissionGuideComponent } from '../components/permission-guide/permission-guide';
import { PromptModalComponent } from '../../components/domain/prompt-modal/prompt-modal';
interface TabDef {
path: string;
label: string;
icon: string;
}
/**
* 移动外壳 —— 底部 4 Tab + 安全区(视觉基线 `prototypes/fmode-mobile-20261102`)
*
* 与 Demo 对齐的四点:
* · 4 Tab:会话 / 通讯录 / 工作台 / 我的(Demo `TAB_PAGES`)
* · 底栏是**半透明毛玻璃 + 顶部细边**,内容可透出(Demo `.tabbar`)
* · 会话 Tab 带未读角标(红色胶囊,不是小圆点)
* · 内容区为底栏预留 `--mob-tabbar-h + 安全区` 高度,不遮挡(rules/18 §11)
*
* 顶栏由各页面自行渲染(Demo 的圆形浮钮顶栏是页面级组件 `app-m-app-bar`)。
*/
@Component({
selector: 'app-mobile-shell',
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [RouterOutlet, PromptModalComponent, PermissionGuideComponent],
template: `
@if (wideViewport()) {
正在切换到桌面端布局
窗口宽度已达到 768px,已自动适配桌面端。
}
`,
styles: [
`
:host {
display: block;
height: 100dvh;
background: var(--canvas);
color: var(--ink);
}
/*
内容区滚动,并为底栏预留空间 —— 底栏是 fixed,
不预留就会被盖住最后一段内容(rules/18 §11 防遮挡底线)。
*/
.m-main {
height: 100dvh;
overflow-y: auto;
-webkit-overflow-scrolling: touch;
overscroll-behavior-y: contain;
padding-bottom: calc(var(--mob-tabbar-h) + var(--mob-safe-bottom) + var(--sp-4));
box-sizing: border-box;
}
/*
页面自己管滚动(会话列表等三态固定结构)时不叠加外壳滚动。
但**必须**把底栏预留传下去:.m-page--fixed 高度是 100dvh,
若只留外壳 padding,内部滚动区仍会被 fixed 底栏压住(实测遮挡)。
*/
/*
* 页面自己管滚动时,外壳**不再滚动**(2026-11-04 统一)。
* 覆盖两类页面:
* · .m-page--fixed —— 三态固定结构(会话列表、通讯录等)
* · .m-page--scroll —— 固定头部 + 内容滚动(工作台、我的等)
* 🔴 原来的问题:工作台同时有 m-main 和内部 m-body 两个滚动容器,
* 手指滑动时内外层互相抢,出现「滚一半突然跳到另一层」的错乱手感。
* 现在只有页面内部的滚动区生效。
*/
.m-main:has(.m-page--fixed),
.m-main:has(.m-page--scroll) {
overflow: hidden;
padding-bottom: 0;
}
/*
底栏:毛玻璃 + 顶部细边(Demo .tabbar)
🔴 层级必须**低于抽屉**(--z-float 950 < --z-modal 1100):
抽屉滑出时要盖住整个屏幕(含底栏),否则视觉上像被切了一刀。
上一版误把底栏提到 1110,导致抽屉被压在下面(用户实测指出)。
抽屉内部已为底栏预留空间(见 m-drawer 的 padding-bottom),
所以底栏被盖住也不会让内容被切。
*/
.m-tabbar {
position: fixed;
left: 0;
right: 0;
bottom: 0;
z-index: var(--z-float);
display: flex;
align-items: stretch;
height: calc(var(--mob-tabbar-h) + var(--mob-safe-bottom));
padding-bottom: var(--mob-safe-bottom);
background: color-mix(in srgb, var(--canvas) 92%, transparent);
-webkit-backdrop-filter: blur(16px);
backdrop-filter: blur(16px);
border-top: 1px solid var(--edge);
}
.m-tab {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: var(--sp-1);
border: 0;
background: none;
color: var(--muted);
font: inherit;
font-size: var(--fs-12);
cursor: pointer;
transition: color var(--motion-fast) var(--ease-standard);
}
.m-tab.on {
color: var(--primary);
font-weight: 600;
}
.m-tab-icon {
position: relative;
display: inline-grid;
place-items: center;
font-size: var(--fs-20);
line-height: 1;
}
.m-tab-badge {
position: absolute;
top: -6px;
left: 11px;
min-width: 16px;
height: 16px;
padding: 0 4px;
border-radius: var(--r-tag);
background: var(--danger);
color: var(--on-danger);
font-size: var(--fs-11);
font-weight: 600;
line-height: 16px;
text-align: center;
}
.wide-mobile {
position: fixed;
inset: 0;
z-index: var(--z-mask);
display: grid;
place-items: center;
padding: var(--sp-6);
background: var(--canvas);
}
.wide-card {
display: grid;
justify-items: center;
gap: var(--sp-2);
max-width: 360px;
text-align: center;
}
.wide-icon {
display: grid;
place-items: center;
width: 56px;
height: 56px;
border-radius: var(--r-lg);
background: var(--primary-subtle);
color: var(--primary);
font-size: var(--fs-20);
}
.wide-card h2 {
margin: 0;
color: var(--ink);
font-size: var(--fs-20);
}
.wide-card p {
margin: 0;
color: var(--muted);
font-size: var(--fs-13);
line-height: 1.7;
}
/* 两个动作并排:主操作在右(拇指顺位),次要操作在左 */
.wide-actions {
display: flex;
gap: var(--sp-2);
margin-top: var(--sp-3);
}
.wide-btn {
flex: 1;
min-height: var(--touch-min);
padding: 0 var(--sp-3);
border: 0;
border-radius: var(--r-sm);
background: var(--primary);
color: var(--primary-content);
font-size: var(--fs-13);
cursor: pointer;
/* 并排时宽度有限,别让「切换到桌面端」折成两行 */
white-space: nowrap;
}
.wide-btn.ghost {
background: transparent;
color: var(--primary);
border: 1px solid var(--edge);
}
`,
],
})
export class MobileShellComponent implements OnDestroy {
/** 登录后的通知权限引导(2026-11-15,仅移动端) */
private readonly notifySvc = inject(AppNotificationService);
protected readonly permGuideOpen = signal(false);
/**
* 是否需要弹通知权限引导。
*
* 延后 800ms:刚登录进来要加载会话列表、建 Matrix 连接,
* 弹层立刻盖上来会显得很急躁,也让首屏看起来像卡住了。
*/
private async maybeShowPermGuide(): Promise {
if (!this.notifySvc.shouldPromptPermission()) return;
/* 已授权的就没必要问 —— 直接标记,不再打扰 */
if ((await this.notifySvc.isGranted()) === true) {
this.notifySvc.markPrompted();
return;
}
await new Promise((r) => setTimeout(r, 800));
this.permGuideOpen.set(true);
}
/** 用户关掉引导(同意或拒绝都算),记下「问过了」 */
protected onPermDismiss(): void {
this.permGuideOpen.set(false);
this.notifySvc.markPrompted();
}
private readonly live = inject(LiveService);
private readonly matrix = inject(MatrixSessionService);
private readonly palette = inject(PaletteService);
private readonly unread = inject(UnreadService);
private readonly session = inject(SessionService);
private readonly api = inject(FmodeApi);
private readonly router = inject(Router);
readonly device = inject(DeviceService);
/** 4 个 Tab(顺序即底栏顺序;与 Demo TAB_PAGES 一致) */
protected readonly tabs: TabDef[] = [
{ path: '/messages', label: '会话', icon: 'fa-solid fa-comment' },
{ path: '/contacts', label: '通讯录', icon: 'fa-solid fa-user-group' },
{ path: '/workbench', label: '工作台', icon: 'fa-solid fa-table-cells-large' },
{ path: '/mine', label: '我的', icon: 'fa-solid fa-user' },
];
/** 底栏会话 Tab 未读总数(UnreadService 唯一来源) */
protected readonly totalUnread = computed(() =>
Object.values(this.unread.counts()).reduce((n, v) => n + v, 0),
);
protected readonly activeIndex = signal(0);
protected readonly wideViewport = signal(this.isWideViewport());
constructor() {
// 🔴 唯一轮询器的启停点
this.live.start();
// 🔴 Matrix 会话级连接(常驻,未读计数依赖它)
this.matrix.bootstrap();
this.palette.init();
/* 登录后一次性通知权限引导(2026-11-15) */
void this.maybeShowPermGuide();
/*
* 启动时刷新身份(2026-11-05):管理员角色可能在后台被调整过,
* 只登录一次、之后一直用本地副本会导致权限变化不生效。
* 失败静默,不打断使用。
*/
if (this.session.token()) {
/* 本地 session 已过期 → 登出并回登录页(2026-11-12,与桌面端同口径) */
if (this.session.isExpired()) {
this.session.logout();
void this.router.navigateByUrl('/login');
} else {
void this.session.refreshMe((t) => this.api.fetchMe(t));
}
}
if (typeof window !== 'undefined') {
window.addEventListener('resize', () => this.onResize(), { passive: true });
// 首次进入也检查一次(可能直接访问了 /mobile/... 但窗口是宽的)
setTimeout(() => this.onResize(), 300);
}
this.syncFromUrl(this.router.url);
this.router.events
.pipe(filter((e): e is NavigationEnd => e instanceof NavigationEnd))
.subscribe((e) => this.syncFromUrl(e.urlAfterRedirects));
}
protected go(path: string): void {
void this.router.navigateByUrl(`/mobile${path}`);
}
/**
* 窗口尺寸变化 → 自动切换到桌面端(2026-11-10 用户裁决)。
*
* 与桌面外壳对称:防抖 400ms,避免拖窗口时反复导航。
* 只在「够宽」时跳,窄屏不主动跳回移动端(那边自己负责)。
*/
private resizeTimer: ReturnType | null = null;
private onResize(): void {
this.wideViewport.set(this.isWideViewport());
if (this.resizeTimer !== null) clearTimeout(this.resizeTimer);
this.resizeTimer = setTimeout(() => {
this.resizeTimer = null;
/*
* 与桌面侧同一修正:判据用「目标端」,不用 `changed`。
* 用宽窗口直接打开 `/mobile/...` 时 `mode` 本来就是 desktop,
* `changed` 恒为 false → 永远不跳(用户截图实测)。
*/
const { mode } = this.device.recheck();
if (mode !== 'desktop') return;
void this.router.navigateByUrl('/desktop/chat', { replaceUrl: true });
}, 400);
}
/** 卸载时清掉未触发的防抖定时器,避免外壳销毁后仍导航 */
ngOnDestroy(): void {
if (this.resizeTimer !== null) clearTimeout(this.resizeTimer);
this.resizeTimer = null;
}
private isWideViewport(): boolean {
return typeof window !== 'undefined' && window.innerWidth >= 768;
}
private syncFromUrl(url: string): void {
const path = url.split('?')[0].replace(/^\/mobile/, '') || '/messages';
const idx = this.tabs.findIndex((t) => path === t.path || path.startsWith(t.path + '/'));
this.activeIndex.set(idx >= 0 ? idx : 0);
}
}