Skip to content

Commit d9cc8e7

Browse files
committed
fix: use LocalStorage.getShared() so pages receive xcomponentId from WebAbility
1 parent 2296518 commit d9cc8e7

2 files changed

Lines changed: 38 additions & 12 deletions

File tree

‎entry/src/main/ets/pages/Index.ets‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,30 @@ import { WebWindow } from 'web_engine';
1111
import { NativeContext } from 'web_engine/src/main/ets/interface/CommonInterface';
1212
import JsBindingUtils from 'web_engine/src/main/ets/utils/JsBindingUtils';
1313
import { ContextType } from 'web_engine/src/main/ets/common/Constants';
14+
import { uiObserver } from '@kit.ArkUI';
1415

15-
@Entry
16+
let storage = LocalStorage.getShared();
17+
@Entry(storage)
1618
@Component
1719
struct Index {
1820
@LocalStorageLink('xcomponentId') xComponentId: string = '';
1921
@LocalStorageProp('statusBarHeight') statusBarHeight: number = 0;
22+
@State density: number = 0;
2023
private nativeContext: NativeContext =
2124
JsBindingUtils.getNativeContext(ContextType.kMainProcess);
2225

2326
aboutToAppear(): void {
24-
// WebWindow and the native context are managed by web_engine.
25-
// No manual initialization needed here.
27+
uiObserver.on('densityUpdate', this.getUIContext(), (info: uiObserver.DensityInfo) => {
28+
this.density = info.density;
29+
});
30+
}
31+
32+
aboutToDisappear(): void {
33+
uiObserver.off('densityUpdate', this.getUIContext());
2634
}
2735

2836
onBackPress(): boolean {
29-
if (this.nativeContext) {
30-
this.nativeContext.OnBackToLastPage(this.xComponentId);
31-
}
37+
this.nativeContext.OnBackToLastPage(this.xComponentId);
3238
return true;
3339
}
3440

@@ -38,5 +44,12 @@ struct Index {
3844
}
3945
.width('100%')
4046
.height('100%')
47+
.padding({
48+
top: this.getStatusBarHeight(),
49+
})
50+
}
51+
52+
getStatusBarHeight(): number {
53+
return this.getUIContext().px2vp(this.statusBarHeight);
4154
}
4255
}

‎entry/src/main/ets/pages/NodeHandleWindow.ets‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,30 @@ import { WebNodeHandleWindow } from 'web_engine';
1515
import { NativeContext } from 'web_engine/src/main/ets/interface/CommonInterface';
1616
import JsBindingUtils from 'web_engine/src/main/ets/utils/JsBindingUtils';
1717
import { ContextType } from 'web_engine/src/main/ets/common/Constants';
18+
import { uiObserver } from '@kit.ArkUI';
1819

19-
@Entry
20+
let storage = LocalStorage.getShared();
21+
@Entry(storage)
2022
@Component
2123
struct NodeHandleWindow {
2224
@LocalStorageLink('xcomponentId') xComponentId: string = '';
2325
@LocalStorageProp('statusBarHeight') statusBarHeight: number = 0;
26+
@State density: number = 0;
2427
private nativeContext: NativeContext =
2528
JsBindingUtils.getNativeContext(ContextType.kMainProcess);
2629

2730
aboutToAppear(): void {
28-
// WebNodeHandleWindow and the native context are managed by web_engine.
29-
// No manual initialization needed here.
31+
uiObserver.on('densityUpdate', this.getUIContext(), (info: uiObserver.DensityInfo) => {
32+
this.density = info.density;
33+
});
34+
}
35+
36+
aboutToDisappear(): void {
37+
uiObserver.off('densityUpdate', this.getUIContext());
3038
}
3139

3240
onBackPress(): boolean {
33-
if (this.nativeContext) {
34-
this.nativeContext.OnBackToLastPage(this.xComponentId);
35-
}
41+
this.nativeContext.OnBackToLastPage(this.xComponentId);
3642
return true;
3743
}
3844

@@ -42,5 +48,12 @@ struct NodeHandleWindow {
4248
}
4349
.width('100%')
4450
.height('100%')
51+
.padding({
52+
top: this.getStatusBarHeight(),
53+
})
54+
}
55+
56+
getStatusBarHeight(): number {
57+
return this.getUIContext().px2vp(this.statusBarHeight);
4558
}
4659
}

0 commit comments

Comments
 (0)