TitleBar.ets 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. @Component
  2. export struct TitleBar {
  3. @Link model: TitleBar.Model;
  4. @BuilderParam customLeftView?: CustomBuilder;
  5. @BuilderParam customCenterView?: CustomBuilder;
  6. @BuilderParam customRightView?: CustomBuilder;
  7. private multipleTitleSwiperController: SwiperController = new SwiperController();
  8. private multipleTitleDataSource: TitleBar.MultipleTitleDataSource = new TitleBar.MultipleTitleDataSource(this.model.multipleTitleList);
  9. @State opacityItem:number = 1
  10. /**
  11. * 标题栏两边普通布局
  12. * @param type
  13. */
  14. @Builder normalBothSidesLayout(type: TitleBar.BothSidesLayoutType) {
  15. Row() {
  16. // 图标在上方 文字在下方
  17. if (this.model.leftIconGravity == TitleBar.IconGravity.TOP) {
  18. Column({
  19. space: type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleSpace : this.model.rightTitleSpace
  20. }) {
  21. if ((type == TitleBar.BothSidesLayoutType.LEFT && this.model.leftIcon)
  22. || (type == TitleBar.BothSidesLayoutType.RIGHT && this.model.rightIcon)) {
  23. Image(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIcon : this.model.rightIcon)
  24. .objectFit(ImageFit.Auto)
  25. .opacity(type == TitleBar.BothSidesLayoutType.LEFT ?this.model.opacityLeft:1)
  26. .width(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconWidth : this.model.rightIconWidth)
  27. .height(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconHeight : this.model.rightIconHeight)
  28. .padding(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconPadding : this.model.rightIconPadding)
  29. }
  30. Text(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleName : this.model.rightTitleName)
  31. .maxLines(1)
  32. .fontSize(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontSize : this.model.rightTitleFontSize)
  33. .fontWeight(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontWeight : this.model.rightTitleFontWeight)
  34. .fontColor(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontColor : this.model.rightTitleFontColor)
  35. }
  36. }
  37. // 图标在下方 文字在上方
  38. else if (this.model.leftIconGravity == TitleBar.IconGravity.BOTTOM) {
  39. Column({
  40. space: type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleSpace : this.model.rightTitleSpace
  41. }) {
  42. Text(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleName : this.model.rightTitleName)
  43. .maxLines(1)
  44. .fontSize(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontSize : this.model.rightTitleFontSize)
  45. .fontWeight(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontWeight : this.model.rightTitleFontWeight)
  46. .fontColor(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontColor : this.model.rightTitleFontColor)
  47. if ((type == TitleBar.BothSidesLayoutType.LEFT && this.model.leftIcon)
  48. || (type == TitleBar.BothSidesLayoutType.RIGHT && this.model.rightIcon)) {
  49. Image(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIcon : this.model.rightIcon)
  50. .objectFit(ImageFit.Auto)
  51. .opacity(type == TitleBar.BothSidesLayoutType.LEFT ?this.model.opacityLeft:1)
  52. .width(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconWidth : this.model.rightIconWidth)
  53. .height(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconHeight : this.model.rightIconHeight)
  54. .padding(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconPadding : this.model.rightIconPadding)
  55. }
  56. }
  57. }
  58. // 图标在左方 文字在右方
  59. else if (this.model.leftIconGravity == TitleBar.IconGravity.LEFT) {
  60. Row({
  61. space: type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleSpace : this.model.rightTitleSpace
  62. }) {
  63. if ((type == TitleBar.BothSidesLayoutType.LEFT && this.model.leftIcon)
  64. || (type == TitleBar.BothSidesLayoutType.RIGHT && this.model.rightIcon)) {
  65. Image(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIcon : this.model.rightIcon)
  66. .objectFit(ImageFit.Auto)
  67. .opacity(type == TitleBar.BothSidesLayoutType.LEFT ?this.model.opacityLeft:1)
  68. .width(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconWidth : this.model.rightIconWidth)
  69. .height(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconHeight : this.model.rightIconHeight)
  70. .padding(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconPadding : this.model.rightIconPadding)
  71. }
  72. Text(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleName : this.model.rightTitleName)
  73. .maxLines(1)
  74. .fontSize(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontSize : this.model.rightTitleFontSize)
  75. .fontWeight(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontWeight : this.model.rightTitleFontWeight)
  76. .fontColor(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontColor : this.model.rightTitleFontColor)
  77. }
  78. }
  79. // 图标右方 文字在左方
  80. else if (this.model.leftIconGravity == TitleBar.IconGravity.RIGHT) {
  81. Row({
  82. space: type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleSpace : this.model.rightTitleSpace
  83. }) {
  84. Text(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleName : this.model.rightTitleName)
  85. .maxLines(1)
  86. .fontSize(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontSize : this.model.rightTitleFontSize)
  87. .fontWeight(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontWeight : this.model.rightTitleFontWeight)
  88. .fontColor(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleFontColor : this.model.rightTitleFontColor)
  89. if ((type == TitleBar.BothSidesLayoutType.LEFT && this.model.leftIcon)
  90. || (type == TitleBar.BothSidesLayoutType.RIGHT && this.model.rightIcon)) {
  91. Image(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIcon : this.model.rightIcon)
  92. .objectFit(ImageFit.Auto)
  93. .opacity(type == TitleBar.BothSidesLayoutType.LEFT ?this.model.opacityLeft:1)
  94. .width(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconWidth : this.model.rightIconWidth)
  95. .height(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconHeight : this.model.rightIconHeight)
  96. .padding(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftIconPadding : this.model.rightIconPadding)
  97. }
  98. }
  99. }
  100. }
  101. .justifyContent(FlexAlign.Center)
  102. .alignItems(VerticalAlign.Center)
  103. .constraintSize({
  104. minWidth: this.model.titleBarMinHeight,
  105. minHeight: this.model.titleBarMinHeight,
  106. })
  107. .opacity(this.opacityItem) // 绑定透明度
  108. .padding(this.model.titleBarNormalPadding)
  109. .stateStyles({
  110. normal: {
  111. .backgroundColor(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleStateNormalStyleColor : this.model.rightTitleStateNormalStyleColor)
  112. },
  113. pressed: {
  114. .backgroundColor(type == TitleBar.BothSidesLayoutType.LEFT ? this.model.leftTitleStatePressedStyleColor : this.model.rightTitleStatePressedStyleColor)
  115. }
  116. })
  117. .onClick(() => {
  118. type == TitleBar.BothSidesLayoutType.LEFT ? this.model.onLeftClickListener() : this.model.onRightClickListener();
  119. })
  120. }
  121. /**
  122. * 标题布局
  123. */
  124. @Builder normalCenterLayout() {
  125. Column({ space: this.model.titleSpace }) {
  126. // 主标题
  127. // 跑马灯
  128. if (this.model.titleTextOverflowType == TitleBar.TitleTextOverflowType.MARQUEE) {
  129. Marquee({
  130. start: true,
  131. src: this.model.titleName
  132. })
  133. // .opacity(this.model.opacityLeft)
  134. .fontSize(this.model.titleFontSize)
  135. .fontColor(this.model.titleFontColor)
  136. .fontWeight(this.model.titleFontWeight)
  137. }
  138. // 垂直跑马灯多文本动态显示效果
  139. else if (this.model.titleTextOverflowType == TitleBar.TitleTextOverflowType.MULTIPLE) {
  140. Swiper(this.multipleTitleSwiperController) {
  141. LazyForEach(this.multipleTitleDataSource, (item: string) => {
  142. Text(item)
  143. .maxLines(1)
  144. .textAlign(this.model.multipleTitleTextAlign)
  145. .fontSize(this.model.multipleTitleFontSize)
  146. .fontColor(this.model.multipleTitleFontColor)
  147. .fontWeight(this.model.multipleTitleFontWeight)
  148. .textOverflow({ overflow: this.textOverflowConvert(this.model.multipleTitleTextOverflowType) })
  149. .fontStyle(this.model.multipleTitleTextStyle)
  150. .width('100%')
  151. }, (item: string) => item)
  152. }
  153. .autoPlay(true)
  154. .interval(this.model.multipleTitlePlayInterval)
  155. .indicator(false)
  156. .loop(true)
  157. .duration(this.model.multipleTitlePlayDuration)
  158. .itemSpace(this.model.multipleTitlePlayItemSpace)
  159. .vertical(true)
  160. .curve(Curve.Linear)
  161. }
  162. // 普通文本
  163. else {
  164. Text(this.model.titleName)
  165. .maxLines(1)
  166. // .opacity(this.model.opacityLeft)
  167. .fontSize(this.model.titleFontSize)
  168. .fontColor(this.model.titleFontColor)
  169. .fontWeight(this.model.titleFontWeight)
  170. .textOverflow({ overflow: this.textOverflowConvert(this.model.titleTextOverflowType) })
  171. .fontStyle(this.model.titleTextStyle)
  172. }
  173. // 副标题
  174. if (this.model.subTitleName.length != 0) {
  175. if (this.model.subTitleTextOverflowType == TitleBar.TitleTextOverflowType.MARQUEE) {
  176. Marquee({
  177. start: true,
  178. src: this.model.subTitleName
  179. })
  180. .fontSize(this.model.subTitleFontSize)
  181. .fontColor(this.model.subTitleFontColor)
  182. .fontWeight(this.model.subTitleFontWeight)
  183. } else {
  184. Text(this.model.subTitleName)
  185. .maxLines(1)
  186. .fontSize(this.model.subTitleFontSize)
  187. .fontColor(this.model.subTitleFontColor)
  188. .fontWeight(this.model.subTitleFontWeight)
  189. .textOverflow({ overflow: this.textOverflowConvert(this.model.subTitleTextOverflowType) })
  190. .fontStyle(this.model.subTitleTextStyle)
  191. }
  192. }
  193. }
  194. .padding(this.model.titleBarNormalPadding)
  195. .layoutWeight(1)
  196. .onClick(() => {
  197. this.model.onTitleClickListener();
  198. })
  199. }
  200. @Builder blankLayout() {
  201. Row() {
  202. Blank()
  203. }
  204. .constraintSize({
  205. minWidth: this.model.titleBarMinHeight,
  206. minHeight: this.model.titleBarMinHeight,
  207. })
  208. }
  209. textOverflowConvert(type: TitleBar.TitleTextOverflowType): TextOverflow {
  210. if (type == TitleBar.TitleTextOverflowType.NONE) {
  211. return TextOverflow.None;
  212. } else if (type == TitleBar.TitleTextOverflowType.CLIP) {
  213. return TextOverflow.Clip;
  214. } else if (type == TitleBar.TitleTextOverflowType.ELLIPSIS) {
  215. return TextOverflow.Ellipsis;
  216. } else {
  217. return TextOverflow.None;
  218. }
  219. }
  220. aboutToAppear() {
  221. this.model.showLeftSideLayout = !(this.model.leftTitleName.length == 0 && this.model.leftIcon == null);
  222. this.model.showRightSideLayout = !(this.model.rightTitleName.length == 0 && this.model.rightIcon == null);
  223. }
  224. build() {
  225. Row() {
  226. // 左边布局
  227. if (this.customLeftView) {
  228. this.customLeftView()
  229. } else {
  230. if (this.model.showLeftSideLayout) {
  231. this.normalBothSidesLayout(TitleBar.BothSidesLayoutType.LEFT)
  232. } else {
  233. this.blankLayout()
  234. }
  235. }
  236. // 中间布局
  237. if (this.customCenterView) {
  238. this.customCenterView()
  239. } else {
  240. this.normalCenterLayout()
  241. }
  242. // 右边布局
  243. if (this.customRightView) {
  244. this.customRightView()
  245. } else {
  246. if (this.model.showRightSideLayout) {
  247. this.normalBothSidesLayout(TitleBar.BothSidesLayoutType.RIGHT)
  248. } else {
  249. this.blankLayout()
  250. }
  251. }
  252. }
  253. .alignItems(VerticalAlign.Center)
  254. .backgroundColor(this.model.titleBarBackground)
  255. .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP])
  256. .width('100%')
  257. .constraintSize({
  258. minHeight: this.model.titleBarMinHeight,
  259. })
  260. .border({
  261. width: {
  262. bottom: this.model.titleBarBottomLineHeight
  263. },
  264. color: {
  265. bottom: this.model.titleBarBottomLineColor
  266. }
  267. })
  268. .linearGradient({
  269. angle: this.model.titleBarLinearGradient.angle,
  270. direction: this.model.titleBarLinearGradient.direction,
  271. colors: this.model.titleBarLinearGradient.colors,
  272. repeating: this.model.titleBarLinearGradient.repeating
  273. })
  274. }
  275. }
  276. export namespace TitleBar {
  277. /**
  278. * 标题栏样式
  279. */
  280. export enum BarStyle {
  281. // 日间主题样式
  282. LIGHT = 1,
  283. // 夜间主题样式
  284. NIGHT = 2,
  285. // 透明主题样式实现
  286. TRANSPARENT = 3,
  287. // 渐变色样式实现
  288. LINEARGRADIENT = 4,
  289. }
  290. /**
  291. * 标题文本超长时处理显示策略
  292. */
  293. export enum TitleTextOverflowType {
  294. // 文本超长时不进行裁剪。
  295. NONE = 0,
  296. // 文本超长时显示不下的文本用省略号代替。
  297. ELLIPSIS = 1,
  298. // 文本超长时进行裁剪显示。
  299. CLIP = 2,
  300. // 文本超长时跑马灯显示
  301. MARQUEE = 3,
  302. // 垂直跑马灯多文本动态显示效果
  303. MULTIPLE = 4,
  304. }
  305. /**
  306. * 左右两边的操作布局类型 左 右
  307. */
  308. export enum BothSidesLayoutType {
  309. LEFT = 1,
  310. RIGHT = 2,
  311. }
  312. /**
  313. * 左右图标位置 上 下 左 右
  314. */
  315. export enum IconGravity {
  316. TOP = 1,
  317. BOTTOM = 2,
  318. LEFT = 3,
  319. RIGHT = 4,
  320. }
  321. export class Model {
  322. opacityLeft: number = 1; // 控制透明度的状态变量
  323. // 标题栏 Padding
  324. titleBarNormalPadding: number | Resource = 5;
  325. // 标题栏最小高度
  326. titleBarMinHeight: number | Resource = 55;
  327. // 标题栏样式
  328. titleBarStyle: BarStyle = BarStyle.LIGHT;
  329. // 标题栏背景色
  330. titleBarBackground: ResourceColor = Color.White;
  331. // 标题栏底部线条颜色
  332. titleBarBottomLineColor: ResourceColor = 0xFFECECEC;
  333. // 标题栏底部线条高度
  334. titleBarBottomLineHeight: number | Resource = 0.35;
  335. // 标题栏渐变色
  336. titleBarLinearGradient: TitleBarLinearGradient = new TitleBarLinearGradient();
  337. /***************************************************************
  338. * 中间标题
  339. ***************************************************************/
  340. // 布局项间距
  341. titleSpace: string | number = 2;
  342. // 标题名称
  343. titleName: string = "";
  344. // 标题颜色
  345. titleFontColor: ResourceColor = Color.Black;
  346. // 标题大小
  347. titleFontSize: number | string | Resource = 16;
  348. // 标题文字粗细
  349. titleFontWeight: number | FontWeight | string = FontWeight.Normal;
  350. // 标题文本超长时处理显示策略
  351. titleTextOverflowType: TitleTextOverflowType = TitleTextOverflowType.ELLIPSIS;
  352. // 字体样式设置 Normal 标准 Italic 斜体
  353. titleTextStyle: FontStyle = FontStyle.Normal;
  354. // 子标题名称
  355. subTitleName: string = "";
  356. // 子标题颜色
  357. subTitleFontColor: ResourceColor = Color.Black;
  358. // 子标题大小
  359. subTitleFontSize: number | string | Resource = 12;
  360. // 子标题文字粗细
  361. subTitleFontWeight: number | FontWeight | string = FontWeight.Normal;
  362. // 子标题文本超长时处理显示策略
  363. subTitleTextOverflowType: TitleTextOverflowType = TitleTextOverflowType.ELLIPSIS;
  364. // 子标题字体样式设置 Normal 标准 Italic 斜体
  365. subTitleTextStyle: FontStyle = FontStyle.Normal;
  366. // 标题栏点击回调
  367. onTitleClickListener: () => void = () => {
  368. };
  369. /***************************************************************
  370. * 垂直跑马灯多文本动态显示效果
  371. ***************************************************************/
  372. multipleTitleList: string[] = [''];
  373. // 垂直跑马灯多文本动态显示标题颜色
  374. multipleTitleFontColor: ResourceColor = Color.Black;
  375. // 垂直跑马灯多文本动态显示标题大小
  376. multipleTitleFontSize: number | string | Resource = 18;
  377. // 垂直跑马灯多文本动态显示标题文字粗细
  378. multipleTitleFontWeight: number | FontWeight | string = FontWeight.Normal;
  379. // 垂直跑马灯多文本动态显示标题文本超长时处理显示策略
  380. multipleTitleTextOverflowType: TitleTextOverflowType = TitleTextOverflowType.ELLIPSIS;
  381. // 垂直跑马灯多文本动态显示字体样式设置 Normal 标准 Italic 斜体
  382. multipleTitleTextStyle: FontStyle = FontStyle.Normal;
  383. // 垂直跑马灯多文本动态显示文字显示位置
  384. multipleTitleTextAlign: TextAlign = TextAlign.Start;
  385. // 垂直跑马灯多文本动态显示文字播放 Interval 间隔
  386. multipleTitlePlayInterval: number = 2000;
  387. // 垂直跑马灯多文本动态显示文字播放 Duration
  388. multipleTitlePlayDuration: number = 2000;
  389. // 垂直跑马灯多文本动态显示文字播放 ItemSpace
  390. multipleTitlePlayItemSpace: number = 5;
  391. /***************************************************************
  392. * 左边按钮
  393. ***************************************************************/
  394. // 左边布局项间距
  395. leftTitleSpace: string | number = 2;
  396. // 左边标题多态样式 Normal
  397. leftTitleStateNormalStyleColor: ResourceColor = Color.White;
  398. // 左边标题多态样式 Pressed
  399. leftTitleStatePressedStyleColor: ResourceColor = Color.Grey;
  400. // 显示左边布局
  401. showLeftSideLayout: boolean = true;
  402. // 左边背景色
  403. leftTitleBackground: ResourceColor = Color.White;
  404. // 左边按钮标题名称
  405. leftTitleName: string = "";
  406. // 左边按钮标题颜色
  407. leftTitleFontColor: ResourceColor = Color.Black;
  408. // 左边按钮标题文字大小
  409. leftTitleFontSize: number | string | Resource = 16;
  410. // 左边按钮标题文字粗细
  411. leftTitleFontWeight: number | FontWeight | string = FontWeight.Normal;
  412. // 左边按钮图标
  413. leftIcon: string | PixelMap | Resource | null = $r('app.media.left_back_black');
  414. // 左边按钮图标宽度
  415. leftIconWidth: number | string | Resource = 20;
  416. // 左边按钮图标高度
  417. leftIconHeight: number | string | Resource = 20;
  418. // 左边按钮图标 Padding
  419. leftIconPadding: Padding | Length = 0;
  420. // 左边按钮图标位置
  421. leftIconGravity: IconGravity = IconGravity.LEFT;
  422. // 左边点击回调
  423. onLeftClickListener: () => void = () => {
  424. };
  425. /***************************************************************
  426. * 右边按钮
  427. ***************************************************************/
  428. // 右边布局项间距
  429. rightTitleSpace: string | number = 2;
  430. // 右边标题多态样式 Normal
  431. rightTitleStateNormalStyleColor: ResourceColor = Color.Black;
  432. // 右边标题多态样式 Pressed
  433. rightTitleStatePressedStyleColor: ResourceColor = Color.Grey;
  434. // 显示右边布局
  435. showRightSideLayout: boolean = true;
  436. // 右边背景色
  437. rightTitleBackground: ResourceColor = Color.Blue;
  438. // 右边按钮标题名称
  439. rightTitleName: string = "";
  440. // 右边按钮标题颜色
  441. rightTitleFontColor: ResourceColor = Color.Black;
  442. // 右边按钮标题文字大小
  443. rightTitleFontSize: number | string | Resource = 16;
  444. // 右边按钮标题文字粗细
  445. rightTitleFontWeight: number | FontWeight | string = FontWeight.Normal;
  446. // 右边按钮图标
  447. rightIcon: string | PixelMap | Resource | null = null;
  448. // 右边按钮图标宽度
  449. rightIconWidth: number | string | Resource = 20;
  450. // 右边按钮图标高度
  451. rightIconHeight: number | string | Resource = 20;
  452. // 右边按钮图标 Padding
  453. rightIconPadding: Padding | Length = 0;
  454. // 右边按钮图标位置
  455. rightIconGravity: IconGravity = IconGravity.LEFT;
  456. // 右边点击回调
  457. onRightClickListener: () => void = () => {
  458. };
  459. constructor(isDark?:boolean) {
  460. this.barStyleTransform(isDark?BarStyle.NIGHT:BarStyle.LIGHT);
  461. }
  462. setTitleBarNormalPadding(value: number | Resource): Model {
  463. this.titleBarNormalPadding = value;
  464. return this;
  465. }
  466. setTitleBarMinHeight(value: number | Resource): Model {
  467. this.titleBarMinHeight = value;
  468. return this;
  469. }
  470. setTitleBarStyle(value: BarStyle): Model {
  471. this.titleBarStyle = value;
  472. this.barStyleTransform(value);
  473. return this;
  474. }
  475. setTitleBarBackground(value: ResourceColor): Model {
  476. this.titleBarBackground = value;
  477. console.log('Heanup: TitleBar背景色:'+JSON.stringify(value))
  478. return this;
  479. }
  480. setTitleBarBottomLineHeight(value: number | Resource): Model {
  481. this.titleBarBottomLineHeight = value;
  482. return this;
  483. }
  484. setTitleBarBottomLineColor(value: ResourceColor): Model {
  485. this.titleBarBottomLineColor = value;
  486. return this;
  487. }
  488. setShowTitleBarBottomLine(value: boolean): Model {
  489. this.titleBarBottomLineHeight = value ? 0.35 : 0;
  490. return this;
  491. }
  492. setTitleBarLinearGradient(value: TitleBarLinearGradient): Model {
  493. this.titleBarLinearGradient = value;
  494. return this;
  495. }
  496. /***************************************************************
  497. * 中间标题
  498. ***************************************************************/
  499. setTitleSpace(value: string | number): Model {
  500. this.titleSpace = value;
  501. return this;
  502. }
  503. setTitleName(value: string): Model {
  504. this.titleName = value;
  505. return this;
  506. }
  507. setTitleFontColor(value: ResourceColor): Model {
  508. this.titleFontColor = value;
  509. return this;
  510. }
  511. setTitleFontSize(value: number | string | Resource): Model {
  512. this.titleFontSize = value;
  513. return this;
  514. }
  515. setTitleFontWeight(value: number | FontWeight | string): Model {
  516. this.titleFontWeight = value;
  517. return this;
  518. }
  519. setTitleTextOverflowType(value: TitleTextOverflowType): Model {
  520. this.titleTextOverflowType = value;
  521. return this;
  522. }
  523. setTitleTextStyle(value: FontStyle): Model {
  524. this.titleTextStyle = value;
  525. return this;
  526. }
  527. setSubTitleName(value: string): Model {
  528. this.subTitleName = value;
  529. return this;
  530. }
  531. setSubTitleFontColor(value: ResourceColor): Model {
  532. this.subTitleFontColor = value;
  533. return this;
  534. }
  535. setSubTitleFontSize(value: number | string | Resource): Model {
  536. this.subTitleFontSize = value;
  537. return this;
  538. }
  539. setSubTitleFontWeight(value: number | FontWeight | string): Model {
  540. this.subTitleFontWeight = value;
  541. return this;
  542. }
  543. setSubTitleTextOverflowType(value: TitleTextOverflowType): Model {
  544. this.subTitleTextOverflowType = value;
  545. return this;
  546. }
  547. setSubTitleTextStyle(value: FontStyle): Model {
  548. this.subTitleTextStyle = value;
  549. return this;
  550. }
  551. setOnTitleClickListener(callback: () => void): Model {
  552. this.onTitleClickListener = callback
  553. return this
  554. }
  555. /***************************************************************
  556. * 垂直跑马灯多文本动态显示效果
  557. ***************************************************************/
  558. setMultipleTitleList(value: string[]): Model {
  559. this.multipleTitleList = value;
  560. return this;
  561. }
  562. setMultipleTitleFontColor(value: ResourceColor): Model {
  563. this.multipleTitleFontColor = value;
  564. return this;
  565. }
  566. setMultipleTitleFontSize(value: number | string | Resource): Model {
  567. this.multipleTitleFontSize = value;
  568. return this;
  569. }
  570. setMultipleTitleFontWeight(value: number | FontWeight | string): Model {
  571. this.multipleTitleFontWeight = value;
  572. return this;
  573. }
  574. setMultipleTitleTextOverflowType(value: TitleTextOverflowType): Model {
  575. this.multipleTitleTextOverflowType = value;
  576. return this;
  577. }
  578. setMultipleTitleTextStyle(value: FontStyle): Model {
  579. this.multipleTitleTextStyle = value;
  580. return this;
  581. }
  582. setMultipleTitleTextAlign(value: TextAlign): Model {
  583. this.multipleTitleTextAlign = value;
  584. return this;
  585. }
  586. setMultipleTitlePlayInterval(value: number): Model {
  587. this.multipleTitlePlayInterval = value;
  588. return this;
  589. }
  590. setMultipleTitlePlayDuration(value: number): Model {
  591. this.multipleTitlePlayDuration = value;
  592. return this;
  593. }
  594. setMultipleTitlePlayItemSpace(value: number): Model {
  595. this.multipleTitlePlayItemSpace = value;
  596. return this;
  597. }
  598. /***************************************************************
  599. * 左边按钮
  600. ***************************************************************/
  601. setLeftTitleStateNormalStyleColor(value: ResourceColor): Model {
  602. this.leftTitleStateNormalStyleColor = value;
  603. return this;
  604. }
  605. setLeftTitleStatePressedStyleColor(value: ResourceColor): Model {
  606. this.leftTitleStatePressedStyleColor = value;
  607. return this;
  608. }
  609. setLeftTitleSpace(value: string | number): Model {
  610. this.leftTitleSpace = value;
  611. return this;
  612. }
  613. setShowLeftSideLayout(value: boolean): Model {
  614. this.showLeftSideLayout = value;
  615. return this;
  616. }
  617. setLeftTitleName(value: string): Model {
  618. this.leftTitleName = value;
  619. return this;
  620. }
  621. setLeftTitleFontColor(value: ResourceColor): Model {
  622. this.leftTitleFontColor = value;
  623. return this;
  624. }
  625. setLeftTitleFontSize(value: number | string | Resource): Model {
  626. this.leftTitleFontSize = value;
  627. return this;
  628. }
  629. setLeftTitleFontWeight(value: number | FontWeight | string): Model {
  630. this.leftTitleFontWeight = value;
  631. return this;
  632. }
  633. setLeftIcon(value: string | PixelMap | Resource | null): Model {
  634. this.leftIcon = value;
  635. return this;
  636. }
  637. setLeftIconMain(value: string | PixelMap | Resource | null): Model {
  638. animateTo({ duration: 200,curve:'EaseInOut' }, () => { this.opacityLeft = 0.4; });
  639. setTimeout(() => {
  640. this.leftIcon = value;
  641. animateTo({ duration: 200,curve:'EaseInOut' }, () => { this.opacityLeft = 1; });
  642. }, 200);
  643. return this;
  644. }
  645. setLeftIconMainNoAnimate(value: string | PixelMap | Resource | null): Model {
  646. this.leftIcon = value;
  647. return this;
  648. }
  649. setLeftIconWidth(value: number | string | Resource): Model {
  650. this.leftIconWidth = value;
  651. return this;
  652. }
  653. setLeftIconHeight(value: number | string | Resource): Model {
  654. this.leftIconHeight = value;
  655. return this;
  656. }
  657. setLeftIconPadding(value: Padding | Length): Model {
  658. this.leftIconPadding = value;
  659. return this;
  660. }
  661. setLeftIconGravity(value: IconGravity): Model {
  662. this.leftIconGravity = value;
  663. return this;
  664. }
  665. setOnLeftClickListener(callback: () => void): Model {
  666. this.onLeftClickListener = callback
  667. return this
  668. }
  669. setLeftTitleBackground(value: ResourceColor): Model {
  670. this.leftTitleBackground = value;
  671. return this;
  672. }
  673. /***************************************************************
  674. * 右边按钮
  675. ***************************************************************/
  676. setRightTitleStateNormalStyleColor(value: ResourceColor): Model {
  677. this.rightTitleStateNormalStyleColor = value;
  678. return this;
  679. }
  680. setRightTitleStatePressedStyleColor(value: ResourceColor): Model {
  681. this.rightTitleStatePressedStyleColor = value;
  682. return this;
  683. }
  684. setRightTitleSpace(value: string | number): Model {
  685. this.rightTitleSpace = value;
  686. return this;
  687. }
  688. setShowRightSideLayout(value: boolean): Model {
  689. this.showRightSideLayout = value;
  690. return this;
  691. }
  692. setRightTitleName(value: string): Model {
  693. this.rightTitleName = value;
  694. return this;
  695. }
  696. setRightTitleFontColor(value: ResourceColor): Model {
  697. this.rightTitleFontColor = value;
  698. return this;
  699. }
  700. setRightTitleFontSize(value: number | string | Resource): Model {
  701. this.rightTitleFontSize = value;
  702. return this;
  703. }
  704. setRightTitleFontWeight(value: number | FontWeight | string): Model {
  705. this.rightTitleFontWeight = value;
  706. return this;
  707. }
  708. setRightIcon(value: string | PixelMap | Resource | null): Model {
  709. this.rightIcon = value;
  710. return this;
  711. }
  712. setRightIconWidth(value: number | string | Resource): Model {
  713. this.rightIconWidth = value;
  714. return this;
  715. }
  716. setRightIconHeight(value: number | string | Resource): Model {
  717. this.rightIconHeight = value;
  718. return this;
  719. }
  720. setRightIconPadding(value: Padding | Length): Model {
  721. this.rightIconPadding = value;
  722. return this;
  723. }
  724. setRightIconGravity(value: IconGravity): Model {
  725. this.rightIconGravity = value;
  726. return this;
  727. }
  728. setOnRightClickListener(callback: () => void): Model {
  729. this.onRightClickListener = callback
  730. return this
  731. }
  732. setRightTitleBackground(value: ResourceColor): Model {
  733. this.rightTitleBackground = value;
  734. return this;
  735. }
  736. /**
  737. * 标题样式转换
  738. * @param value
  739. */
  740. private barStyleTransform(value: BarStyle) {
  741. this.setTitleBarBackground($r('app.color.title_bar_bg'));
  742. this.setTitleFontColor($r('app.color.title_text'));
  743. this.setTitleBarBottomLineColor($r('app.color.title_bar_bottom_line'));
  744. this.setLeftTitleStateNormalStyleColor($r('app.color.title_bar_bg'));
  745. this.setLeftTitleStatePressedStyleColor($r('app.color.title_bar_pressed'));
  746. this.setLeftIcon($r('app.media.left_back_black'));
  747. this.setLeftTitleFontColor($r('app.color.title_left_text'));
  748. this.setRightTitleStateNormalStyleColor($r('app.color.title_bar_bg'));
  749. this.setRightTitleStatePressedStyleColor($r('app.color.title_bar_pressed'));
  750. this.setRightTitleFontColor($r('app.color.title_right_text'));
  751. }
  752. }
  753. export class TitleBarLinearGradient {
  754. angle?: number | string;
  755. direction?: GradientDirection;
  756. colors?: Array<[
  757. ResourceColor,
  758. number
  759. ]>;
  760. repeating?: boolean;
  761. }
  762. export class MultipleTitleDataSource implements IDataSource {
  763. private multipleTitleList: string[] = [];
  764. private listener?: DataChangeListener;
  765. constructor(list: string[]) {
  766. this.multipleTitleList = list;
  767. }
  768. totalCount(): number {
  769. return this.multipleTitleList.length;
  770. }
  771. getData(index: number): Object {
  772. return this.multipleTitleList[index];
  773. }
  774. registerDataChangeListener(listener: DataChangeListener): void {
  775. this.listener = listener;
  776. }
  777. unregisterDataChangeListener() {
  778. this.listener = undefined;
  779. }
  780. }
  781. }
  782. export default TitleBar