SafeView.ets 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import { WindowHelper } from '../utils/WindowHelper';
  2. import { inputMethod } from '@kit.IMEKit';
  3. export interface TitleBarBtn {
  4. text?: string | Resource;
  5. img?: PixelMap | ResourceStr | DrawableDescriptor;
  6. color?: string | Resource;
  7. onClick: () => void
  8. }
  9. @Preview
  10. @Component
  11. export struct SafeView {
  12. pageInfos?: NavPathStack
  13. @Prop isLoading: boolean
  14. // 是不是沉浸式
  15. @Prop isImmersive: boolean
  16. // 设置导航栏标题
  17. @Prop titleText: ResourceStr
  18. // 设置导航栏标题颜色
  19. @Prop titleColor: ResourceColor
  20. // 设置导航栏背景色
  21. @Prop backgroundColorNav: ResourceColor
  22. // 设置导航栏背景色
  23. @Prop backgroundColorView: ResourceColor
  24. // 设置导航栏背景不透明度
  25. // 元素的不透明度,取值范围为0到1,1表示不透明,0表示完全透明, 达到隐藏组件效果,但是在布局中占位。
  26. @Prop opacitys: number | Resource
  27. // 是否显示返回按钮
  28. @Prop hideBack: boolean
  29. // 是否显示返回按钮
  30. @Prop hideNavBar: boolean
  31. /**
  32. * 是否显示左侧角标,存在onClickLeft时生效
  33. */
  34. @Prop showBadgeLeft: boolean;
  35. /**
  36. * 是否显示右侧角标,存在onClickLeft时生效
  37. */
  38. @Prop showBadgeRight: boolean;
  39. // 设置返回按钮事件,如果hideBack为true,该事件无效
  40. onBackEvent?: () => void = undefined
  41. // 设置返回按钮事件
  42. onClickLeft?: TitleBarBtn
  43. onClickRight?: TitleBarBtn
  44. @Builder
  45. doNothingBuilder() {
  46. }
  47. @BuilderParam customBuilderParam: () => void = this.doNothingBuilder
  48. build() {
  49. NavDestination() {
  50. Stack() {
  51. Column() {
  52. this.customBuilderParam()
  53. }
  54. .padding({
  55. top: this.isImmersive ? 0 : WindowHelper.topRectHeight + 44
  56. })
  57. .align(Alignment.Top)
  58. .width('100%')
  59. .height('100%')
  60. if (!this.hideNavBar) {
  61. Row()
  62. .height(WindowHelper.topRectHeight)
  63. .width('100%')
  64. .backgroundColor(this.backgroundColorNav ?? '#ffffff')
  65. .opacity(undefined === this.opacitys ? 1 : this.opacitys > 1 ? 1 : this.opacitys < 0 ? 0 : this.opacitys)
  66. Row() {
  67. Row() {
  68. Image($r("app.media.base_back"))
  69. .height(15)
  70. .objectFit(ImageFit.Contain).visibility(!this.hideBack ? Visibility.Visible : Visibility.None)
  71. }.width(110)
  72. .onClick(() => {
  73. if (this.hideBack) {
  74. return
  75. }
  76. if (this.onBackEvent && !this.hideBack) {
  77. this.onBackEvent()
  78. } else if (this.pageInfos) {
  79. this.pageInfos.pop()
  80. }
  81. })
  82. Text(this.titleText ?? '')
  83. .maxLines(1)
  84. .fontColor(this.titleColor ?? '#11102C')
  85. .fontSize(16)
  86. .fontWeight(FontWeight.Medium)
  87. .ellipsisMode(EllipsisMode.CENTER)
  88. .textOverflow({
  89. overflow: TextOverflow.Ellipsis
  90. })
  91. .constraintSize({
  92. maxWidth: 200
  93. })
  94. Row() {
  95. if (!this.onClickLeft?.img && this.onClickLeft) {
  96. Badge({
  97. count: this.showBadgeLeft ? 1 : 0,
  98. style: {
  99. badgeSize: 6,
  100. badgeColor: Color.Red
  101. }
  102. }) {
  103. Text(`${this.onClickLeft?.text}` ?? '确定')
  104. .fontColor(this.onClickLeft?.color ?? '#17171A')
  105. .onClick(() => {
  106. this.onClickLeft?.onClick && this.onClickLeft?.onClick()
  107. })
  108. }
  109. }
  110. if (this.onClickLeft?.img && this.onClickLeft) {
  111. Badge({
  112. count: this.showBadgeLeft ? 1 : 0,
  113. style: {
  114. badgeSize: 6,
  115. badgeColor: '#FF6500',
  116. color: '#FF6500'
  117. }
  118. }) {
  119. Image(this.onClickLeft.img)
  120. .onClick(() => {
  121. this.onClickLeft?.onClick && this.onClickLeft?.onClick()
  122. })
  123. .objectFit(ImageFit.Contain)
  124. .width(20)
  125. .height(20)
  126. }
  127. }
  128. if (!this.onClickRight?.img && this.onClickRight) {
  129. Badge({
  130. count: this.showBadgeRight ? 1 : 0,
  131. style: {
  132. badgeSize: 6,
  133. badgeColor: '#FF6500',
  134. color: '#FF6500'
  135. }
  136. }) {
  137. Text(this.onClickRight?.text ?? '确定')
  138. .fontColor(this.onClickRight?.color ?? '#17171A')
  139. .onClick(() => {
  140. this.onClickRight?.onClick && this.onClickRight?.onClick()
  141. })
  142. .margin({
  143. left: 10
  144. })
  145. }
  146. }
  147. if (this.onClickRight?.img && this.onClickRight) {
  148. Badge({
  149. count: this.showBadgeRight ? 1 : 0,
  150. style: {
  151. badgeSize: 6,
  152. badgeColor: '#FF6500',
  153. color: '#FF6500'
  154. }
  155. }) {
  156. Image(this.onClickRight.img)
  157. .onClick(() => {
  158. this.onClickRight?.onClick && this.onClickRight?.onClick()
  159. })
  160. .objectFit(ImageFit.Contain)
  161. .width(20)
  162. .height(20)
  163. .margin({
  164. left: 10
  165. })
  166. }
  167. }
  168. }.width(110)
  169. .justifyContent(FlexAlign.End)
  170. }
  171. .backgroundColor(this.backgroundColorNav ?? '#ffffff')
  172. .opacity(undefined === this.opacitys ? 1 : this.opacitys > 1 ? 1 : this.opacitys < 0 ? 0 : this.opacitys)
  173. .justifyContent(FlexAlign.SpaceBetween)
  174. .height(44)
  175. .padding({
  176. left: 15,
  177. right: 15
  178. })
  179. .width('100%')
  180. .margin({
  181. top: WindowHelper.topRectHeight
  182. })
  183. }
  184. Column() {
  185. LoadingProgress()
  186. .color(Color.White)
  187. .width(80).height(80)
  188. Text('努力加载中..')
  189. .fontSize(16)
  190. .fontColor(Color.White)
  191. }
  192. .visibility(this.isLoading ? Visibility.Visible : Visibility.None)
  193. .width('100%')
  194. .height('100%')
  195. .backgroundColor('#40000000')
  196. .justifyContent(FlexAlign.Center)
  197. }
  198. .align(Alignment.Top)
  199. .width('100%')
  200. .height('100%')
  201. }
  202. .onClick(() => {
  203. inputMethod.getController().stopInputSession()
  204. })
  205. .hideTitleBar(true)
  206. .width('100%')
  207. .height('100%')
  208. .backgroundColor(this.backgroundColorView ?? '#ffffff')
  209. .onBackPressed(() => {
  210. if (this.onBackEvent) {
  211. this.onBackEvent()
  212. return true
  213. } else if (this.pageInfos) {
  214. this.pageInfos.pop()
  215. return true
  216. } else {
  217. return false
  218. }
  219. })
  220. }
  221. }