添加横竖屏切换及监听
This commit is contained in:
parent
a8af14eb3c
commit
5e4df7785f
@ -1,40 +1,48 @@
|
|||||||
import { ComponentConst, ScreenUtil, TitleBar } from '@devwiki/common_ui';
|
import { ComponentConst, ScreenUtil, TitleBar } from '@devwiki/common_ui';
|
||||||
import { TitleBarMenuType } from '@devwiki/common_ui/src/main/ets/component/TitleBar';
|
import { TitleBarMenuType } from '@devwiki/common_ui/src/main/ets/component/TitleBar';
|
||||||
import { router } from '@kit.ArkUI';
|
|
||||||
import { PageRouter } from './PageRouter';
|
|
||||||
import { WebPage } from './WebPage';
|
import { WebPage } from './WebPage';
|
||||||
|
import { window } from '@kit.ArkUI';
|
||||||
|
|
||||||
@CustomDialog
|
@CustomDialog
|
||||||
struct WebViewDialog {
|
struct WebViewDialog {
|
||||||
|
|
||||||
aboutToAppear(): void {
|
showHeight: Length = '50%';
|
||||||
ScreenUtil.getInstance().initScreenSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
dialogController: CustomDialogController = new CustomDialogController({
|
dialogController: CustomDialogController = new CustomDialogController({
|
||||||
builder: WebViewDialog({})
|
builder: WebViewDialog({})
|
||||||
});
|
});
|
||||||
|
|
||||||
build() {
|
build() {
|
||||||
WebPage().height('100%').width('50%')
|
WebPage({onTitleBarLeftClick: ()=> {
|
||||||
|
this.dialogController.close();
|
||||||
|
}}).height(this.showHeight).width('100%')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Entry
|
@Entry
|
||||||
@Component
|
@Component
|
||||||
struct Index {
|
struct Index {
|
||||||
|
|
||||||
@State title: string = "Home";
|
@State title: string = "Home";
|
||||||
|
@StorageLink(ScreenUtil.isPortraitKey)isPortrait: boolean = true;
|
||||||
|
|
||||||
dialogController: CustomDialogController = new CustomDialogController({
|
dialogController: CustomDialogController = new CustomDialogController({
|
||||||
builder: WebViewDialog(),
|
builder: WebViewDialog({showHeight: this.isPortrait ? '95%' : '50%'}),
|
||||||
alignment: DialogAlignment.CenterEnd,
|
alignment: DialogAlignment.BottomEnd,
|
||||||
customStyle: true
|
customStyle: true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
aboutToAppear(): void {
|
||||||
|
ScreenUtil.getInstance().initScreenSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
onPageShow(): void {
|
||||||
|
ScreenUtil.getInstance().setPreferredOrientation(window.Orientation.AUTO_ROTATION_PORTRAIT);
|
||||||
|
}
|
||||||
|
|
||||||
onTitleBarLeftClick(_event: ClickEvent) {
|
onTitleBarLeftClick(_event: ClickEvent) {
|
||||||
this.getUIContext().getRouter().back();
|
this.getUIContext().getRouter().back();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
onButtonClicked() {
|
onButtonClicked() {
|
||||||
this.dialogController.open();
|
this.dialogController.open();
|
||||||
|
|
||||||
@ -72,7 +80,7 @@ struct Index {
|
|||||||
|
|
||||||
GridItem() {
|
GridItem() {
|
||||||
Button("WebPage").type(ButtonType.Capsule).height(32).width(80)
|
Button("WebPage").type(ButtonType.Capsule).height(32).width(80)
|
||||||
.onClick(() => { });
|
.onClick(() => { this.dialogController.open() });
|
||||||
}.rowStart(0).rowEnd(0).columnStart(1).columnEnd(1)
|
}.rowStart(0).rowEnd(0).columnStart(1).columnEnd(1)
|
||||||
|
|
||||||
GridItem() {
|
GridItem() {
|
||||||
|
@ -11,9 +11,7 @@ export struct WebPage {
|
|||||||
this.webViewController = new WebPageController(this.viewModel);
|
this.webViewController = new WebPageController(this.viewModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
onTitleBarLeftClick(event: ClickEvent) {
|
onTitleBarLeftClick?: Function;
|
||||||
this.getUIContext().getRouter().back();
|
|
||||||
}
|
|
||||||
|
|
||||||
onTitleBarRightClick(event: ClickEvent) {
|
onTitleBarRightClick(event: ClickEvent) {
|
||||||
this.webViewController?.refresh();
|
this.webViewController?.refresh();
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import window from '@ohos.window';
|
import window from '@ohos.window';
|
||||||
|
import mediaquery from '@ohos.mediaquery';
|
||||||
|
|
||||||
export class ScreenUtil {
|
export class ScreenUtil {
|
||||||
// TODO: 工具待抽离, key统一定义
|
// TODO: 工具待抽离, key统一定义
|
||||||
@ -14,6 +15,9 @@ export class ScreenUtil {
|
|||||||
public layoutHeight: number = 0
|
public layoutHeight: number = 0
|
||||||
|
|
||||||
|
|
||||||
|
private landscapeListener: mediaquery.MediaQueryListener = mediaquery.matchMediaSync('(orientation: landscape)');
|
||||||
|
private portraitListener: mediaquery.MediaQueryListener = mediaquery.matchMediaSync('(orientation: portrait)');
|
||||||
|
|
||||||
public static getInstance(): ScreenUtil {
|
public static getInstance(): ScreenUtil {
|
||||||
if (!ScreenUtil.instance) {
|
if (!ScreenUtil.instance) {
|
||||||
ScreenUtil.instance = new ScreenUtil();
|
ScreenUtil.instance = new ScreenUtil();
|
||||||
@ -22,6 +26,12 @@ export class ScreenUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
initScreenSize(): void {
|
initScreenSize(): void {
|
||||||
|
this.landscapeListener.on('change', ()=> {
|
||||||
|
AppStorage.setOrCreate(ScreenUtil.isPortraitKey, false)
|
||||||
|
});
|
||||||
|
this.portraitListener.on('change', ()=> {
|
||||||
|
AppStorage.setOrCreate(ScreenUtil.isPortraitKey, true)
|
||||||
|
})
|
||||||
window.getLastWindow(getContext(this))
|
window.getLastWindow(getContext(this))
|
||||||
.then((windowClass: window.Window) => {
|
.then((windowClass: window.Window) => {
|
||||||
let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; // 以导航条避让为例
|
let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; // 以导航条避让为例
|
||||||
@ -43,7 +53,6 @@ export class ScreenUtil {
|
|||||||
window.getLastWindow(getContext(this))
|
window.getLastWindow(getContext(this))
|
||||||
.then((windowClass: window.Window) => {
|
.then((windowClass: window.Window) => {
|
||||||
windowClass.setPreferredOrientation(orientation);
|
windowClass.setPreferredOrientation(orientation);
|
||||||
AppStorage.setOrCreate(ScreenUtil.isPortraitKey, orientation == window.Orientation.PORTRAIT || orientation == window.Orientation.PORTRAIT_INVERTED);
|
|
||||||
})
|
})
|
||||||
.catch((error: Error) => {
|
.catch((error: Error) => {
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user