1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import router from '@ohos.router';
17import { LogUtils } from '../util/LogUtils';
18
19/**
20 * titleBar
21 *
22 * @since 2022-06-06
23 */
24@Component
25export struct TitleBar {
26  private static readonly TAG = 'TitleBar';
27  private title: string | Resource;
28  private onBack?: () => boolean;
29
30  build() {
31    Flex({ justifyContent: FlexAlign.SpaceBetween }) {
32      Row() {
33        Row() {
34          Image($r('app.media.back'))
35            .width($r('app.float.title_bar_icon_width'))
36            .height($r('app.float.title_bar_icon_height'))
37            .objectFit(ImageFit.Contain)
38        }.height($r('app.float.title_bar_height'))
39        .margin({ left: $r('app.float.title_bar_icon_margin_left') })
40        .onClick(() => {
41          let isCanNotBack = this.onBack?.();
42          if (isCanNotBack) {
43            LogUtils.info(TitleBar.TAG, 'can not router back');
44            return;
45          }
46          LogUtils.info(TitleBar.TAG, 'terminateSelf.');
47          router.back();
48        })
49
50        Text(this.title)
51          .fontSize($r('app.float.text_size_title_bar'))
52          .fontColor(Color.Black)
53          .margin({ left: $r('app.float.title_bar_text_margin_left') })
54          .fontWeight(FontWeight.Bold)
55      }.flexGrow(1)
56      .height($r('app.float.title_bar_height'))
57    }
58  }
59}
60