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-12 08:43:32 +08:00
|
|
|
// ② 沉浸式:系统栏**全透明**,让应用自己的背景透上去。
|
2026-10-11 22:07:48 +08:00
|
|
|
try {
|
|
|
|
|
const win: window.Window = windowStage.getMainWindowSync();
|
|
|
|
|
this.mainWindow = win;
|
2026-10-12 08:43:32 +08:00
|
|
|
// ⚠️ 这里**不调** `setWindowLayoutFullScreen(true)`。
|
|
|
|
|
// 本机另一个应用(SyncCalendar)用的是"透明系统栏 + expandSafeArea"那套,
|
|
|
|
|
// 实测效果更干净:窗口布局不动(子组件位置不受影响),
|
|
|
|
|
// 只把系统栏涂成全透明、让窗口底色透出来 ⇒ 顶部通知栏与页面**同色**,
|
|
|
|
|
// 页面侧也不用自己去算状态栏高度、加 padding。
|
|
|
|
|
// 之前用 setWindowLayoutFullScreen 时,窗口真的铺到状态栏底下,
|
|
|
|
|
// 页面反而要手动让位,容易算错(踩过一次"重复避让"的坑)。
|
2026-10-11 22:07:48 +08:00
|
|
|
AppStorage.setOrCreate<window.Window>('fbMainWindow', win);
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
|
* 系统栏配色。
|
|
|
|
|
*
|
2026-10-12 10:06:55 +08:00
|
|
|
* 用户 2026-10-12 定的要求:
|
|
|
|
|
* ① 家书标题栏用**浅色(白)**;
|
|
|
|
|
* ② 上面通知栏也是**浅色**,两者融为一体。
|
2026-10-11 23:20:49 +08:00
|
|
|
*
|
2026-10-12 10:06:55 +08:00
|
|
|
* ⇒ 做法:把状态栏设成与顶栏**同一个颜色**(`surface` = 白)。
|
|
|
|
|
* 这是一个"配置出来的"同色,比"让系统栏透明、再指望紧邻元素恰好同色"稳得多 ——
|
|
|
|
|
* 后者一旦顶栏改了底色就会立刻出现色带(已经踩过一次)。
|
2026-10-12 08:43:32 +08:00
|
|
|
*
|
2026-10-12 10:06:55 +08:00
|
|
|
* ⚠️ 底部导航条取**页面底色**(宣纸米),与页面下部同色,不突兀。
|
|
|
|
|
* ⚠️ 取色走 resourceManager(经 dark/ 限定符解析)—— 深色模式一并生效。
|
2026-10-11 23:20:49 +08:00
|
|
|
*/
|
2026-10-11 22:07:48 +08:00
|
|
|
private applySystemBars(win: window.Window): void {
|
2026-10-12 10:06:55 +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-12 10:06:55 +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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|