1/* 2 * Copyright (c) 2022-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 { 18 Action, 19 BigDataConstants, 20 BroadCast, 21 BroadCastConstants, 22 BroadCastManager, 23 Constants, 24 Log, 25 MediaItem, 26 ReportToBigDataUtil, 27 ScreenManager, 28 UiUtil 29} from '@ohos/common'; 30import { CustomDialogView } from '@ohos/common/CommonComponents'; 31import { PhotoEditCrop, PhotoEditMode, PhotoEditorManager } from '@ohos/editor'; 32import { CropImageShow, MainMenuBar, TitleBar } from '@ohos/editor/EditorComponnts'; 33 34const TAG: string = 'EditMain'; 35 36@Entry 37@Component 38export struct EditMain { 39 @Provide editorManager: PhotoEditorManager = PhotoEditorManager.getInstance(); 40 @Provide('compare') isCompare: boolean = false; 41 @Provide('angle') rotateAngle: number = Constants.NUMBER_0; 42 @Provide isCropReset: boolean = false; 43 @Provide isBigTextShow: boolean = false; 44 @Provide bigText: string = ''; 45 @Provide('scale') imageScale: number = Constants.NUMBER_0; 46 @Provide('barSize') barScale: Resource = $r('app.float.menuBar_edit'); 47 @Provide('selected') selectedMode: number = PhotoEditMode.EDIT_MODE_CROP; 48 @Provide broadCast: BroadCast = new BroadCast(); 49 @Provide('verticalScreen') isVerticalScreen: boolean = !ScreenManager.getInstance().isHorizontal(); 50 @Provide screenWidth: number = Constants.NUMBER_0; 51 @Provide screenHeight: number = Constants.NUMBER_0; 52 @Provide cropEdit: PhotoEditCrop | undefined = undefined; 53 @Provide isRedo: boolean = false; 54 @Provide isUndo: boolean = false; 55 @Provide isMagnifier: boolean = false; 56 @Provide mainBottomSize: number = Constants.BOTTOM_TOOL_BAR_SIZE; 57 @Provide titleSize: number = Constants.TOP_BAR_SIZE; 58 @Provide filterBottomSize: number = Constants.FILTER_BOTTOM_TOOL_BAR_SIZE; 59 @Provide moreMenuList: Array<Action> = new Array<Action>(); 60 @StorageLink('leftBlank') leftBlank: number[] 61 = [ 62 Constants.NUMBER_0, 63 ScreenManager.getInstance().getStatusBarHeight(), 64 Constants.NUMBER_0, 65 ScreenManager.getInstance().getNaviBarHeight() 66 ]; 67 @State imageScaleWidth: number = Constants.NUMBER_0; 68 appEventBus: BroadCast = BroadCastManager.getInstance().getBroadCast(); 69 @StorageLink('currentBreakpoint') @Watch('updateIsVerticalScreen') currentBreakpoint: string = Constants.BREAKPOINT_MD; 70 private mediaItem: MediaItem = AppStorage.get<MediaItem>('EditorMediaItem') as MediaItem; 71 private screenManager = ScreenManager.getInstance(); 72 private isLoadFailed: boolean = false; 73 private albumUri: string = AppStorage.get<string>('EditorAlbumUri') as string; 74 private onWindowSizeChangeCallBack: Function = (): void => this.updateIsVerticalScreen(); 75 76 updateIsVerticalScreen(): void { 77 if (this.currentBreakpoint == Constants.BREAKPOINT_LG) { 78 this.isVerticalScreen = false; 79 this.screenHeight = Math.ceil(this.screenManager.getWinHeight()) - this.leftBlank[3] - Constants.ActionBarHeight; 80 this.screenWidth = Math.ceil(this.screenManager.getWinWidth()) - Constants.HORIZONTAL_RESET_BUTTON_WIDTH - 81 Constants.HORIZONTAL_RESET_BUTTON_MARGIN - Constants.HORIZONTAL_CROP_STYLE_BAR_HEIGHT - 82 Constants.HORIZONTAL_RULER_COMPONENT_WIDTH - Constants.HORIZONTAL_TOOL_BAR_WIDTH; 83 } else { 84 this.isVerticalScreen = true; 85 this.screenHeight = Math.ceil(this.screenManager.getWinHeight()) - this.leftBlank[3] - 86 Constants.ActionBarHeight - Constants.PUNCH_HOLE_HEIGHT - 87 Constants.VERTICAL_RESET_BUTTON_HEIGHT - Constants.VERTICAL_RESET_BUTTON_MARGIN - 88 Constants.VERTICAL_CROP_STYLE_BAR_HEIGHT - Constants.VERTICAL_RULER_COMPONENT_HEIGHT - 89 Constants.VERTICAL_TOOL_BAR_HEIGHT; 90 this.screenWidth = Math.ceil(this.screenManager.getWinWidth()); 91 } 92 this.imageScale = this.isVerticalScreen 93 ? (this.screenWidth - this.titleSize - this.mainBottomSize) : (this.screenWidth - this.titleSize); 94 this.imageScaleWidth = this.isVerticalScreen ? this.screenHeight : (this.screenHeight - this.mainBottomSize); 95 } 96 97 onBackPress(): boolean { 98 if (this.isCropReset) { 99 this.broadCast.emit(BroadCastConstants.SHOW_EDIT_EXIT_PHOTO_DIALOG, [(): void => this.discardCallback()]); 100 } else { 101 router.back(); 102 } 103 return true; 104 } 105 106 discardCallback(): void { 107 Log.debug(TAG, 'discardCallback called'); 108 } 109 110 loadFailedCallback(): void { 111 this.isLoadFailed = true; 112 } 113 114 aboutToAppear(): void { 115 Log.debug(TAG, 'EditMain init start'); 116 ScreenManager.getInstance().setSystemUi(false); 117 if (this.mediaItem) { 118 this.isLoadFailed = false; 119 this.editorManager.initialize( 120 this.mediaItem, this.albumUri, PhotoEditMode.EDIT_MODE_CROP, (): void => this.loadFailedCallback()); 121 } 122 this.cropEdit = this.editorManager.getPhotoEditBaseInstance(PhotoEditMode.EDIT_MODE_CROP) as PhotoEditCrop; 123 ScreenManager.getInstance().on(ScreenManager.ON_WIN_SIZE_CHANGED, this.onWindowSizeChangeCallBack); 124 this.updateIsVerticalScreen(); 125 ReportToBigDataUtil.report(BigDataConstants.ENTER_PHOTO_EDIT_ID, undefined); 126 } 127 128 aboutToDisappear(): void { 129 ScreenManager.getInstance().off(ScreenManager.ON_WIN_SIZE_CHANGED, this.onWindowSizeChangeCallBack); 130 Log.debug(TAG, 'aboutToDisappear'); 131 132 AppStorage.setOrCreate<MediaItem | undefined>('EditorMediaItem', undefined); 133 PhotoEditorManager.getInstance().clear(); 134 ScreenManager.getInstance().setSystemUi(true); 135 } 136 137 onPageShow(): void { 138 this.appEventBus.emit(BroadCastConstants.THIRD_ROUTE_PAGE, []); 139 if (this.isLoadFailed) { 140 router.back(); 141 } 142 } 143 144 build() { 145 // 根布局 146 Flex({ 147 direction: FlexDirection.Column, 148 alignItems: ItemAlign.Center, 149 justifyContent: FlexAlign.Center 150 }) { 151 // ActionBar 152 Row() { 153 TitleBar({ 154 name: $r('app.string.editBar_text'), 155 isPcStyle: false 156 }) 157 } 158 .height(this.isVerticalScreen ? Constants.ActionBarHeight + Constants.PUNCH_HOLE_HEIGHT : Constants.ActionBarHeight) 159 .width(Constants.PERCENT_100) 160 .backgroundColor($r('app.color.transparent')) 161 .padding({ top: this.isVerticalScreen ? Constants.PUNCH_HOLE_HEIGHT : Constants.NUMBER_0 }) 162 163 Flex({ 164 direction: this.isVerticalScreen ? FlexDirection.Column : FlexDirection.Row, 165 alignItems: ItemAlign.Center, 166 justifyContent: this.isVerticalScreen ? FlexAlign.Center : FlexAlign.Start 167 }) { 168 // 图片编辑区域与菜单列 169 if (this.selectedMode == PhotoEditMode.EDIT_MODE_CROP) { 170 CropImageShow() 171 } 172 173 if (!this.isVerticalScreen) { 174 // 菜单工具栏 175 Column() { 176 if (this.selectedMode == PhotoEditMode.EDIT_MODE_CROP) { 177 MainMenuBar() 178 } 179 } 180 .width(Constants.HORIZONTAL_MAIN_MENU_WIDTH) 181 .height(Constants.PERCENT_100) 182 } else { 183 // 菜单工具栏 184 Row() { 185 if (this.selectedMode == PhotoEditMode.EDIT_MODE_CROP) { 186 MainMenuBar() 187 } 188 } 189 .height(Constants.VERTICAL_MAIN_MENU_BAR_HEIGHT) 190 .width(Constants.PERCENT_100) 191 } 192 193 // 对话框 194 CustomDialogView({ broadCast: $broadCast }) 195 } 196 } 197 .width(Constants.PERCENT_100) 198 .height(Constants.PERCENT_100) 199 .backgroundColor(Color.Black) 200 } 201} 202