HarmonyOSBaseLibs/src/main/ets/view/SafeView.ets

224 行
6.7 KiB
Plaintext

2024-10-16 11:56:35 +08:00
import { WindowHelper } from '../utils/WindowHelper';
import { inputMethod } from '@kit.IMEKit';
2024-10-15 20:27:59 +08:00
2024-10-16 11:56:35 +08:00
export interface TitleBarBtn {
text?: string | Resource;
img?: PixelMap | ResourceStr | DrawableDescriptor;
color?: string | Resource;
onClick: () => void
}
2024-10-15 20:27:59 +08:00
2024-10-15 18:47:21 +08:00
@Preview
@Component
export struct SafeView {
2024-10-20 15:05:08 +08:00
pageInfos?: NavPathStack
2024-10-17 18:39:31 +08:00
@Prop isLoading: boolean
2024-10-16 11:56:35 +08:00
// 是不是沉浸式
@Prop isImmersive: boolean
// 设置导航栏标题
@Prop titleText: ResourceStr
// 设置导航栏背景色
2024-10-17 18:39:31 +08:00
@Prop backgroundColorNav: ResourceColor
// 设置导航栏背景色
@Prop backgroundColorView: ResourceColor
2024-10-16 11:56:35 +08:00
// 设置导航栏背景不透明度
// 元素的不透明度,取值范围为0到1,1表示不透明,0表示完全透明, 达到隐藏组件效果,但是在布局中占位。
@Prop opacitys: number | Resource
2024-10-17 14:28:53 +08:00
// 是否显示返回按钮
@Prop hideBack: boolean
2024-10-17 14:38:26 +08:00
/**
* 是否显示左侧角标,存在onClickLeft时生效
*/
2024-10-17 14:28:53 +08:00
@Prop showBadgeLeft: boolean;
2024-10-17 14:38:26 +08:00
/**
* 是否显示右侧角标,存在onClickLeft时生效
*/
2024-10-17 14:28:53 +08:00
@Prop showBadgeRight: boolean;
// 设置返回按钮事件,如果hideBack为true,该事件无效
2024-10-16 11:56:35 +08:00
onBackEvent?: () => void = undefined
// 设置返回按钮事件
onClickLeft?: TitleBarBtn
onClickRight?: TitleBarBtn
2024-10-15 18:47:21 +08:00
@Builder
doNothingBuilder() {
}
@BuilderParam customBuilderParam: () => void = this.doNothingBuilder
build() {
2024-10-15 20:27:59 +08:00
NavDestination() {
Stack() {
Column() {
2024-10-16 11:56:35 +08:00
this.customBuilderParam()
}
.padding({
top: this.isImmersive ? 0 : WindowHelper.topRectHeight + 44
})
.align(Alignment.Top)
.width('100%')
.height('100%')
Row()
.height(WindowHelper.topRectHeight)
.width('100%')
2024-10-17 18:39:31 +08:00
.backgroundColor(this.backgroundColorNav ?? '#ffffff')
2024-10-16 11:56:35 +08:00
.opacity(undefined === this.opacitys ? 1 : this.opacitys > 1 ? 1 : this.opacitys < 0 ? 0 : this.opacitys)
Row() {
2024-10-15 20:27:59 +08:00
2024-10-16 11:56:35 +08:00
Row() {
2024-10-15 20:27:59 +08:00
Image($r("app.media.base_back"))
.height(15)
2024-10-16 11:56:35 +08:00
.objectFit(ImageFit.Contain)
2024-10-15 20:27:59 +08:00
.onClick(() => {
2024-10-17 14:28:53 +08:00
if (this.onBackEvent && !this.hideBack) {
2024-10-16 11:56:35 +08:00
this.onBackEvent()
2024-10-15 20:27:59 +08:00
} else if (this.pageInfos) {
this.pageInfos.pop()
}
2024-10-17 14:28:53 +08:00
}).visibility(!this.hideBack ? Visibility.Visible : Visibility.None)
2024-10-16 11:56:35 +08:00
}.width(110)
2024-10-15 20:27:59 +08:00
2024-10-16 15:58:19 +08:00
Text(`${this.titleText}`)
2024-10-16 11:56:35 +08:00
.maxLines(1)
2024-10-17 18:05:50 +08:00
.fontColor('#11102C')
.fontSize(16)
.fontWeight(FontWeight.Medium)
2024-10-16 11:56:35 +08:00
.ellipsisMode(EllipsisMode.CENTER)
.textOverflow({
overflow: TextOverflow.Ellipsis
})
.constraintSize({
maxWidth: 120
})
2024-10-15 20:27:59 +08:00
2024-10-16 11:56:35 +08:00
Row() {
if (!this.onClickLeft?.img && this.onClickLeft) {
2024-10-17 14:36:48 +08:00
Badge({
2024-10-20 15:05:08 +08:00
count: this.showBadgeLeft ? 1 : 0,
style: {
badgeSize: 6,
2024-10-17 14:36:48 +08:00
badgeColor: Color.Red
}
2024-10-20 15:05:08 +08:00
}) {
2024-10-17 14:36:48 +08:00
Text(`${this.onClickLeft?.text}` ?? '确定')
.fontColor(this.onClickLeft?.color ?? '#17171A')
.onClick(() => {
this.onClickLeft?.onClick && this.onClickLeft?.onClick()
})
}
2024-10-16 11:56:35 +08:00
}
2024-10-17 14:36:48 +08:00
if (this.onClickLeft?.img && this.onClickLeft) {
Badge({
2024-10-20 15:05:08 +08:00
count: this.showBadgeLeft ? 1 : 0,
style: {
badgeSize: 6,
2024-10-17 14:36:48 +08:00
badgeColor: '#FF6500',
color: '#FF6500'
}
2024-10-20 15:05:08 +08:00
}) {
2024-10-17 14:36:48 +08:00
Image(this.onClickLeft.img)
.onClick(() => {
this.onClickLeft?.onClick && this.onClickLeft?.onClick()
})
.objectFit(ImageFit.Contain)
.width(20)
.height(20)
}
2024-10-16 11:56:35 +08:00
}
if (!this.onClickRight?.img && this.onClickRight) {
2024-10-17 14:36:48 +08:00
Badge({
2024-10-20 15:05:08 +08:00
count: this.showBadgeRight ? 1 : 0,
style: {
badgeSize: 6,
2024-10-17 14:36:48 +08:00
badgeColor: '#FF6500',
color: '#FF6500'
}
2024-10-20 15:05:08 +08:00
}) {
2024-10-17 14:36:48 +08:00
Text(this.onClickRight?.text ?? '确定')
.fontColor(this.onClickRight?.color ?? '#17171A')
.onClick(() => {
this.onClickRight?.onClick && this.onClickRight?.onClick()
})
.margin({
left: 10
})
}
2024-10-16 11:56:35 +08:00
}
2024-10-17 14:36:48 +08:00
if (this.onClickRight?.img && this.onClickRight) {
Badge({
2024-10-20 15:05:08 +08:00
count: this.showBadgeRight ? 1 : 0,
style: {
badgeSize: 6,
2024-10-17 14:36:48 +08:00
badgeColor: '#FF6500',
color: '#FF6500'
}
2024-10-20 15:05:08 +08:00
}) {
2024-10-17 14:36:48 +08:00
Image(this.onClickRight.img)
.onClick(() => {
this.onClickRight?.onClick && this.onClickRight?.onClick()
})
.objectFit(ImageFit.Contain)
.width(20)
.height(20)
.margin({
left: 10
})
}
2024-10-15 20:27:59 +08:00
}
2024-10-16 11:56:35 +08:00
}.width(110)
.justifyContent(FlexAlign.End)
2024-10-15 20:27:59 +08:00
}
2024-10-17 18:39:31 +08:00
.backgroundColor(this.backgroundColorNav ?? '#ffffff')
2024-10-16 11:56:35 +08:00
.opacity(undefined === this.opacitys ? 1 : this.opacitys > 1 ? 1 : this.opacitys < 0 ? 0 : this.opacitys)
.justifyContent(FlexAlign.SpaceBetween)
.height(44)
.padding({
left: 15,
right: 15
})
2024-10-15 20:27:59 +08:00
.width('100%')
2024-10-16 11:56:35 +08:00
.margin({
top: WindowHelper.topRectHeight
})
2024-10-15 20:27:59 +08:00
Column() {
LoadingProgress()
.color(Color.White)
.width(80).height(80)
Text('努力加载中..')
.fontSize(16)
.fontColor(Color.White)
}
.visibility(this.isLoading ? Visibility.Visible : Visibility.None)
.width('100%')
.height('100%')
.backgroundColor('#40000000')
.justifyContent(FlexAlign.Center)
2024-10-15 18:47:21 +08:00
}
2024-10-16 11:56:35 +08:00
.align(Alignment.Top)
.width('100%')
.height('100%')
}
.onClick(() => {
2024-10-15 20:27:59 +08:00
inputMethod.getController().stopInputSession()
})
.hideTitleBar(true)
.width('100%')
.height('100%')
2024-10-20 15:05:08 +08:00
.backgroundColor(this.backgroundColorView ?? '#ffffff')
2024-10-16 11:56:35 +08:00
.onBackPressed(() => {
if (this.onBackEvent) {
this.onBackEvent()
2024-10-20 15:05:08 +08:00
return true
2024-10-16 11:56:35 +08:00
} else if (this.pageInfos) {
this.pageInfos.pop()
2024-10-20 15:05:08 +08:00
return true
} else {
return false
2024-10-16 11:56:35 +08:00
}
})
2024-10-15 18:47:21 +08:00
}
}