• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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 { PhotoEditMode } from '../base/PhotoEditType';
17import {
18  BigDataConstants,
19  BroadCast,
20  BroadCastConstants,
21  BroadCastManager,
22  Constants,
23  Log,
24  ReportToBigDataUtil,
25  UiUtil
26} from '@ohos/common';
27import { PhotoEditorManager } from '../PhotoEditorManager';
28import { PhotoEditCrop } from '../crop/PhotoEditCrop';
29import router from '@ohos.router';
30
31const TAG: string = 'editor_TitleBar';
32
33@Component
34export struct TitleBar {
35  @Consume isRedo: boolean;
36  @Consume isUndo: boolean;
37  @State name: Resource = undefined;
38  @State isPcStyle: boolean = true;
39  @Consume('selected') selectedMode: number;
40  @Consume broadCast: BroadCast;
41  @Consume isVerticalScreen: boolean;
42  @State isImmersive: boolean = false;
43  @Consume editorManager: PhotoEditorManager;
44  @Consume cropEdit: PhotoEditCrop;
45  private appBroadCast: BroadCast = BroadCastManager.getInstance().getBroadCast();
46  private newImageId: number = -1;
47  private immersiveClick: Function = undefined;
48  private onBackAfterSaveComplete: Function = undefined;
49
50  saveAsNewCallback() {
51    Log.debug(TAG, 'saveAsNewCallback called');
52    let msg = {
53      'SAVE_TYPE': BigDataConstants.SAVE_AS_NEW
54    }
55    ReportToBigDataUtil.report(BigDataConstants.PHOTO_EDIT_SAVE_ID, msg);
56    PhotoEditorManager.getInstance().save(false, this.saveImageCallback.bind(this));
57  }
58
59  replaceOriginalCallback() {
60    Log.debug(TAG, 'replaceOriginalCallback called');
61    let msg = {
62      'SAVE_TYPE': BigDataConstants.SAVE_REPLACE
63    }
64    ReportToBigDataUtil.report(BigDataConstants.PHOTO_EDIT_SAVE_ID, msg);
65    PhotoEditorManager.getInstance().save(true, this.saveImageCallback.bind(this));
66  }
67
68  discardCallback() {
69    Log.debug(TAG, 'discardCallback called');
70  }
71
72  saveImageCallback(uri: string) {
73    Log.debug(TAG, `saveImageCallback uri = ${uri}`);
74    if (uri == undefined || uri == null || uri.length == 0) {
75      this.broadCast.emit(BroadCastConstants.EXIT_SAVE_PROGRESS_CLOSE, []);
76      UiUtil.showToast($r('app.string.edit_photo_save_fail'));
77      router.back();
78    } else {
79      AppStorage.SetOrCreate(BroadCastConstants.PHOTO_EDIT_SAVE_URI, uri);
80      this.appBroadCast.emit(BroadCastConstants.PHOTO_BROWSER_ACTIVE, [true, Constants.PHOTO_TRANSITION_EDIT]);
81    }
82  }
83
84  backAfterSaveComplete() {
85    Log.debug(TAG, `backAfterSaveComplete called`);
86    this.appBroadCast.emit(BroadCastConstants.PHOTO_BROWSER_ACTIVE, [false, Constants.PHOTO_TRANSITION_EDIT]);
87    this.broadCast.emit(BroadCastConstants.EXIT_SAVE_PROGRESS_CLOSE, []);
88    AppStorage.SetOrCreate(BroadCastConstants.PHOTO_EDIT_SAVE_URI, "");
89    router.back();
90  }
91
92  aboutToAppear() {
93    this.immersiveClick = this.immersive.bind(this);
94    this.broadCast.on(Constants.IS_IMMERSIVE, this.immersiveClick);
95    this.onBackAfterSaveComplete = this.backAfterSaveComplete.bind(this);
96    this.appBroadCast.on(BroadCastConstants.PHOTO_EDIT_SAVE_COMPLETE, this.onBackAfterSaveComplete);
97  }
98
99  aboutToDisappear() {
100    this.broadCast.off(Constants.IS_IMMERSIVE, this.immersiveClick);
101    this.appBroadCast.off(BroadCastConstants.PHOTO_EDIT_SAVE_COMPLETE, this.onBackAfterSaveComplete);
102  }
103
104  immersive(isImmersive: boolean) {
105    this.isImmersive = isImmersive;
106  }
107
108  onBackClicked() {
109    Log.debug(TAG, 'back clicked');
110    if (this.isRedo || this.isUndo) {
111      this.broadCast.emit(BroadCastConstants.SHOW_EDIT_EXIT_PHOTO_DIALOG, [this.discardCallback.bind(this)]);
112    } else if (this.selectedMode == PhotoEditMode.EDIT_MODE_CROP && this.cropEdit.couldReset()) {
113      this.broadCast.emit(BroadCastConstants.SHOW_EDIT_EXIT_PHOTO_DIALOG, [this.discardCallback.bind(this)]);
114    } else {
115      router.back();
116    }
117  }
118
119  onSaveClicked() {
120    Log.info(TAG, 'save clicked');
121    if (this.isRedo || this.isRedo) {
122      this.broadCast.emit(BroadCastConstants.SHOW_SAVE_PHOTO_DIALOG,
123        [this.saveAsNewCallback.bind(this), this.replaceOriginalCallback.bind(this)]);
124    } else if (this.selectedMode == PhotoEditMode.EDIT_MODE_CROP && this.cropEdit.couldReset()) {
125      this.broadCast.emit(BroadCastConstants.SHOW_SAVE_PHOTO_DIALOG,
126        [this.saveAsNewCallback.bind(this), this.replaceOriginalCallback.bind(this)]);
127    } else {
128      router.back();
129    }
130  }
131
132  build() {
133    if (!this.isImmersive) {
134      Row() {
135        Column() {
136          if (this.selectedMode == PhotoEditMode.EDIT_MODE_CROP) {
137            Row() {
138              Flex({
139                direction: FlexDirection.Column,
140                justifyContent: FlexAlign.Center,
141                alignItems: ItemAlign.Center
142              }) {
143                Image($r('app.media.ic_gallery_public_back'))
144                  .key('PhotoEditBackButton')
145                  .width($r('app.float.ic_size_default'))
146                  .height($r('app.float.ic_size_default'))
147                  .fillColor($r('app.color.default_white_color'))
148              }
149              .height($r('app.float.buttonWithoutText'))
150              .width($r('app.float.buttonWithoutText'))
151              .margin({
152                left: $r('app.float.bottom_bar_padding'),
153                right: $r('app.float.adjust_text_margin_left')
154              })
155              .onClick(() => {
156                this.onBackClicked()
157              })
158
159              Row() {
160                Text(this.name)
161                  .fontSize($r('sys.float.ohos_id_text_size_headline8'))
162                  .fontWeight(FontWeight.Medium)
163                  .fontColor($r('app.color.default_white_color'))
164                  .margin({
165                    left: $r('app.float.adjust_text_margin_bottom'),
166                    right: $r('app.float.adjust_text_margin_left')
167                  })
168              }
169            }
170          }
171        }
172        .alignItems(HorizontalAlign.Start)
173        .width('50%')
174
175        Column() {
176          Row() {
177            if (this.selectedMode == PhotoEditMode.EDIT_MODE_CROP) {
178              Flex({
179                direction: FlexDirection.Column,
180                justifyContent: FlexAlign.Center,
181                alignItems: ItemAlign.Center
182              }) {
183                Image(this.isPcStyle ? $r('app.media.ic_gallery_public_ok') : $r('app.media.ic_gallery_public_save'))
184                  .key('PhotoEditSaveButton')
185                  .width($r('app.float.ic_size_default'))
186                  .height($r('app.float.ic_size_default'))
187                  .fillColor($r('app.color.default_white_color'))
188              }
189              .height($r('app.float.buttonWithoutText'))
190              .width($r('app.float.buttonWithoutText'))
191              .margin({ right: $r('app.float.actionbar_first_icon_margin') })
192              .onClick(() => {
193                this.onSaveClicked()
194              })
195              .zIndex(100)
196            }
197          }
198        }
199        .alignItems(HorizontalAlign.End)
200        .width('50%')
201      }
202    }
203  }
204}