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 08:43:32 +08:00
|
|
|
* 🔴 核心手法(照 SyncCalendar 那套,实测最干净):
|
|
|
|
|
* **系统栏涂成全透明 `#00000000`**,再让窗口底色 = 页面背景色透出来。
|
|
|
|
|
* 这样顶部通知栏的颜色天然等于"它下面那一块"的颜色 —— 就是用户要的
|
|
|
|
|
* 「上面那一块和通知栏完全融为一体」。
|
2026-10-11 23:20:49 +08:00
|
|
|
*
|
2026-10-12 08:43:32 +08:00
|
|
|
* ⚠️ 曾经的做法是把状态栏设成某个具体色(先设页面底色、又改成顶栏底色),
|
|
|
|
|
* 结果都不对:状态栏是**独立于页面**的一层,给它写死颜色就必然和它下面的
|
|
|
|
|
* 内容割裂(顶栏是白的、状态栏是米色的,看着就是"没对齐")。
|
|
|
|
|
* 透明才是正解 —— 它不需要知道下面是白还是纸色。
|
|
|
|
|
*
|
|
|
|
|
* ⚠️ `setWindowBackgroundColor` 对 phone/tablet 会报 unavailable(编译器告警),
|
|
|
|
|
* 但**这里必须调**:透明系统栏要透出的就是它。告警可忽略(SyncCalendar 同样这么用)。
|
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 08:43:32 +08:00
|
|
|
const pageBg: string = this.themeColor($r('app.color.page_bg').id, '#F7F3EA');
|
|
|
|
|
// 前景(状态栏里的时间/电量)按页面底色亮度取反,深色模式自动变浅色字
|
|
|
|
|
const fg: string = this.isDarkBg(pageBg) ? '#FFFFFF' : '#2C231D';
|
2026-10-11 22:07:48 +08:00
|
|
|
try {
|
|
|
|
|
win.setWindowSystemBarProperties({
|
2026-10-12 08:43:32 +08:00
|
|
|
statusBarColor: '#00000000',
|
|
|
|
|
statusBarContentColor: fg,
|
|
|
|
|
navigationBarColor: '#00000000',
|
|
|
|
|
navigationBarContentColor: fg
|
2026-10-11 22:07:48 +08:00
|
|
|
});
|
2026-10-12 08:43:32 +08:00
|
|
|
// 窗口底色 = 页面背景色,供透明系统栏透出
|
|
|
|
|
win.setWindowBackgroundColor(pageBg);
|
2026-10-11 22:07:48 +08:00
|
|
|
} catch (err) {
|
|
|
|
|
hilog.error(DOMAIN, 'FamilyBook', 'setSystemBarProperties failed: %{public}s',
|
|
|
|
|
JSON.stringify(err));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|