2026-10-11 22:07:48 +08:00
|
|
|
|
import { AbilityConstant, Configuration, ConfigurationConstant, EnvironmentCallback,
|
|
|
|
|
|
UIAbility, Want } from '@kit.AbilityKit';
|
|
|
|
|
|
import { hilog } from '@kit.PerformanceAnalysisKit';
|
|
|
|
|
|
import { window } from '@kit.ArkUI';
|
|
|
|
|
|
|
|
|
|
|
|
const DOMAIN: number = 0x0000;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 家书 · 应用入口。
|
|
|
|
|
|
*
|
2026-10-11 23:20:49 +08:00
|
|
|
|
* 只做三件事:加载主页面、开启沉浸式(窗口铺满整屏)、跟随系统深浅色刷系统栏配色。
|
2026-10-11 22:07:48 +08:00
|
|
|
|
*
|
|
|
|
|
|
* ⚠️ 这里**不申请任何权限**(除 module.json5 里已声明的 INTERNET)。
|
|
|
|
|
|
* 要权限必须在用户主动点对应功能时再申请 —— 启动即弹窗会被审核驳回。
|
|
|
|
|
|
*/
|
|
|
|
|
|
export default class EntryAbility extends UIAbility {
|
|
|
|
|
|
private mainWindow?: window.Window;
|
|
|
|
|
|
|
|
|
|
|
|
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
|
|
|
|
|
|
hilog.info(DOMAIN, 'FamilyBook', '%{public}s', 'Ability onCreate');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onDestroy(): void {
|
|
|
|
|
|
hilog.info(DOMAIN, 'FamilyBook', '%{public}s', 'Ability onDestroy');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onWindowStageCreate(windowStage: window.WindowStage): void {
|
|
|
|
|
|
// ① 先跟随系统深浅色。必须在取窗口之前调用,否则会读到尚未更新的色彩配置。
|
|
|
|
|
|
try {
|
|
|
|
|
|
this.context.getApplicationContext()
|
|
|
|
|
|
.setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
hilog.error(DOMAIN, 'FamilyBook', 'setColorMode failed: %{public}s', JSON.stringify(err));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-11 23:20:49 +08:00
|
|
|
|
// ② 沉浸式 + 系统栏配色。
|
2026-10-11 22:07:48 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const win: window.Window = windowStage.getMainWindowSync();
|
|
|
|
|
|
this.mainWindow = win;
|
2026-10-11 23:20:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 🔴 关键一步:让窗口**铺满整屏**(内容延伸到状态栏 / 手势条区域)。
|
|
|
|
|
|
// 不开这个的话,系统会把窗口整体推到状态栏下方 —— 那时如果页面再自己
|
|
|
|
|
|
// "让出安全区",就会**重复避让**(实测 156px 的顶部空白,用户反馈
|
|
|
|
|
|
// 「首页距离上面那么远」就是这个原因)。
|
|
|
|
|
|
// 开了之后:窗口占满全屏,页面用 getWindowAvoidArea 的高度自己让位,
|
|
|
|
|
|
// 于是顶栏底色能一直顶到屏幕最上沿,状态栏看起来是"融"在顶栏里的。
|
|
|
|
|
|
win.setWindowLayoutFullScreen(true);
|
|
|
|
|
|
|
|
|
|
|
|
// 页面侧要在 aboutToAppear 里**同步**读避让区,而 window.getLastWindow()
|
|
|
|
|
|
// 只有异步版 ⇒ 由这里存一份窗口引用进 AppStorage。
|
2026-10-11 22:07:48 +08:00
|
|
|
|
AppStorage.setOrCreate<window.Window>('fbMainWindow', win);
|
2026-10-11 23:20:49 +08:00
|
|
|
|
|
2026-10-11 22:07:48 +08:00
|
|
|
|
this.applySystemBars(win);
|
|
|
|
|
|
} catch (err) {
|
2026-10-11 23:20:49 +08:00
|
|
|
|
hilog.error(DOMAIN, 'FamilyBook', 'immersive setup failed: %{public}s', JSON.stringify(err));
|
2026-10-11 22:07:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
windowStage.loadContent('pages/Index', (err) => {
|
|
|
|
|
|
if (err.code) {
|
|
|
|
|
|
hilog.error(DOMAIN, 'FamilyBook', 'loadContent failed: %{public}s', JSON.stringify(err));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
hilog.info(DOMAIN, 'FamilyBook', '%{public}s', 'loadContent succeeded');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// ③ 系统深浅色切换时重刷系统栏配色。
|
|
|
|
|
|
// 用 applicationContext 的 environment 回调比 onConfigurationUpdate 更可靠
|
|
|
|
|
|
// (后者在部分场景不回调,会出现「页面已变深色但状态栏还是浅色」)。
|
|
|
|
|
|
try {
|
|
|
|
|
|
const cb: EnvironmentCallback = {
|
|
|
|
|
|
onConfigurationUpdated: (config: Configuration): void => {
|
|
|
|
|
|
if (this.mainWindow !== undefined) {
|
|
|
|
|
|
this.applySystemBars(this.mainWindow);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
onMemoryLevel: (level: AbilityConstant.MemoryLevel): void => {
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
this.context.getApplicationContext().on('environment', cb);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
hilog.error(DOMAIN, 'FamilyBook', 'watch environment failed: %{public}s', JSON.stringify(err));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 兜底:部分设备/场景只回调 UIAbility.onConfigurationUpdate */
|
|
|
|
|
|
onConfigurationUpdate(): void {
|
|
|
|
|
|
if (this.mainWindow !== undefined) {
|
|
|
|
|
|
this.applySystemBars(this.mainWindow);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onWindowStageDestroy(): void {
|
|
|
|
|
|
hilog.info(DOMAIN, 'FamilyBook', '%{public}s', 'Ability onWindowStageDestroy');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onForeground(): void {
|
|
|
|
|
|
hilog.info(DOMAIN, 'FamilyBook', '%{public}s', 'Ability onForeground');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onBackground(): void {
|
|
|
|
|
|
hilog.info(DOMAIN, 'FamilyBook', '%{public}s', 'Ability onBackground');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-10-11 23:20:49 +08:00
|
|
|
|
* 取某资源色的真实取值(经 dark/ 限定符解析)。以「资源系统实际解析出的颜色」
|
|
|
|
|
|
* 为准来刷系统栏 —— 直接按 colorMode 判断会在系统切换的那一帧读到旧值。
|
2026-10-11 22:07:48 +08:00
|
|
|
|
*/
|
2026-10-11 23:20:49 +08:00
|
|
|
|
private themeColor(id: number, fallback: string): string {
|
2026-10-11 22:07:48 +08:00
|
|
|
|
try {
|
2026-10-11 23:20:49 +08:00
|
|
|
|
const c: number = this.context.resourceManager.getColorSync(id) >>> 0;
|
2026-10-11 22:07:48 +08:00
|
|
|
|
return '#' + c.toString(16).padStart(8, '0');
|
|
|
|
|
|
} catch (err) {
|
2026-10-11 23:20:49 +08:00
|
|
|
|
return fallback;
|
2026-10-11 22:07:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 按背景亮度决定系统栏前景色(浅底用深字,深底用浅字) */
|
|
|
|
|
|
private isDarkBg(bg: string): boolean {
|
|
|
|
|
|
const s: string = bg.startsWith('#') ? bg.substring(1) : bg;
|
|
|
|
|
|
if (s.length < 6) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
const off: number = s.length >= 8 ? 2 : 0;
|
|
|
|
|
|
const r: number = parseInt(s.substring(off, off + 2), 16);
|
|
|
|
|
|
const g: number = parseInt(s.substring(off + 2, off + 4), 16);
|
|
|
|
|
|
const b: number = parseInt(s.substring(off + 4, off + 6), 16);
|
|
|
|
|
|
if (isNaN(r) || isNaN(g) || isNaN(b)) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return (0.2126 * r + 0.7152 * g + 0.0722 * b) < 128;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-11 23:20:49 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 系统栏配色。
|
|
|
|
|
|
*
|
|
|
|
|
|
* 🔴 关键:**状态栏底色必须与它紧邻的元素同色**,否则会出现一条割裂的色带。
|
|
|
|
|
|
* 实测教训 —— 原先这里把状态栏设为「页面底色(宣纸米 #F7F3EA)」,
|
|
|
|
|
|
* 而顶栏是白色 #FFFFFF,于是顶部出现了
|
|
|
|
|
|
* 「一条米色状态栏 + 一条白色顶栏」的拼接感,用户体感就是
|
|
|
|
|
|
* 「首页距离上面那么远 / 不够沉浸」。
|
|
|
|
|
|
* ⇒ 状态栏一律取**顶栏底色**(app.color.surface),两者连成一片。
|
|
|
|
|
|
* 底部手势条则取**页面底色**,与页面下部同色。
|
|
|
|
|
|
*
|
|
|
|
|
|
* ⚠️ 取色都走 resourceManager(经 dark/ 限定符解析),不硬编码 —— 深色模式下一并生效。
|
|
|
|
|
|
*/
|
2026-10-11 22:07:48 +08:00
|
|
|
|
private applySystemBars(win: window.Window): void {
|
2026-10-11 23:20:49 +08:00
|
|
|
|
const surfaceBg: string = this.themeColor($r('app.color.surface').id, '#FFFFFF');
|
|
|
|
|
|
const paperBg: string = this.themeColor($r('app.color.page_bg').id, '#F7F3EA');
|
2026-10-11 22:07:48 +08:00
|
|
|
|
try {
|
|
|
|
|
|
win.setWindowSystemBarProperties({
|
2026-10-11 23:20:49 +08:00
|
|
|
|
// 顶部:与顶栏同色(白)⇒ 状态栏"融"进顶栏
|
|
|
|
|
|
statusBarColor: surfaceBg,
|
|
|
|
|
|
statusBarContentColor: this.isDarkBg(surfaceBg) ? '#FFFFFF' : '#2C231D',
|
|
|
|
|
|
// 底部:与页面底色同色 ⇒ 手势条不突兀
|
|
|
|
|
|
navigationBarColor: paperBg,
|
|
|
|
|
|
navigationBarContentColor: this.isDarkBg(paperBg) ? '#FFFFFF' : '#2C231D'
|
2026-10-11 22:07:48 +08:00
|
|
|
|
});
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
hilog.error(DOMAIN, 'FamilyBook', 'setSystemBarProperties failed: %{public}s',
|
|
|
|
|
|
JSON.stringify(err));
|
|
|
|
|
|
}
|
|
|
|
|
|
// ⚠️ 这里**不再调** `win.setWindowBackgroundColor()` —— 该 API 对
|
|
|
|
|
|
// phone / tablet 不可用(编译器告警 "This API is unavailable to phone, tablet."),
|
2026-10-11 23:20:49 +08:00
|
|
|
|
// 而页面自身已经用同色铺满整屏,窗口底色这层是多余的。
|
2026-10-11 22:07:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|