146 lines
5.7 KiB
Plaintext
146 lines
5.7 KiB
Plaintext
import { AbilityConstant, Configuration, ConfigurationConstant, EnvironmentCallback,
|
|||
|
|
UIAbility, Want } from '@kit.AbilityKit';
|
||
|
|
import { hilog } from '@kit.PerformanceAnalysisKit';
|
||
|
|
import { window } from '@kit.ArkUI';
|
||
|
|
|
||
|
|
const DOMAIN: number = 0x0000;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 家书 · 应用入口。
|
||
|
|
*
|
||
|
|
* 只做三件事:加载主页面、把状态栏/导航条配色对齐主题、跟随系统深浅色重刷。
|
||
|
|
*
|
||
|
|
* ⚠️ 这里**不申请任何权限**(除 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));
|
||
|
|
}
|
||
|
|
|
||
|
|
// ② 状态栏 / 导航条配色对齐主题,消除顶部和底部的异色条。
|
||
|
|
try {
|
||
|
|
const win: window.Window = windowStage.getMainWindowSync();
|
||
|
|
this.mainWindow = win;
|
||
|
|
// 顺手把窗口引用存进 AppStorage:页面侧要在 aboutToAppear 里**同步**读
|
||
|
|
// 避让区(状态栏高度)来铺安全区,而 window.getLastWindow() 只有异步版。
|
||
|
|
AppStorage.setOrCreate<window.Window>('fbMainWindow', win);
|
||
|
|
this.applySystemBars(win);
|
||
|
|
} catch (err) {
|
||
|
|
hilog.error(DOMAIN, 'FamilyBook', 'applySystemBars failed: %{public}s', JSON.stringify(err));
|
||
|
|
}
|
||
|
|
|
||
|
|
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');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 取当前主题下 page_bg 的**真实取值**(经 dark/ 限定符解析)。
|
||
|
|
*
|
||
|
|
* ⚠️ 必须以「资源系统实际解析出的颜色」为准来刷系统栏 ——
|
||
|
|
* 如果直接读 config.colorMode 判断,会在系统切换的那一帧读到旧值,
|
||
|
|
* 结果状态栏与页面背景对不上。
|
||
|
|
*/
|
||
|
|
private themePageBg(): string {
|
||
|
|
try {
|
||
|
|
const c: number = this.context.resourceManager.getColorSync($r('app.color.page_bg').id) >>> 0;
|
||
|
|
return '#' + c.toString(16).padStart(8, '0');
|
||
|
|
} catch (err) {
|
||
|
|
return '#F7F3EA';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 按背景亮度决定系统栏前景色(浅底用深字,深底用浅字) */
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 状态栏/导航条底色 = 页面背景色,前景按亮度取反 —— 视觉上与页面连成一片 */
|
||
|
|
private applySystemBars(win: window.Window): void {
|
||
|
|
const bg: string = this.themePageBg();
|
||
|
|
const fg: string = this.isDarkBg(bg) ? '#FFFFFF' : '#2C231D';
|
||
|
|
try {
|
||
|
|
win.setWindowSystemBarProperties({
|
||
|
|
statusBarColor: bg,
|
||
|
|
statusBarContentColor: fg,
|
||
|
|
navigationBarColor: bg,
|
||
|
|
navigationBarContentColor: fg
|
||
|
|
});
|
||
|
|
} 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."),
|
||
|
|
// 而页面自身(Index.ets 的安全区 Column)已经用同色铺满整屏,
|
||
|
|
// 窗口底色这层是多余的。留着只会带来一条无意义的告警。
|
||
|
|
}
|
||
|
|
}
|