1/** 2 * Copyright (c) 2021-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 { ILayoutConfig } from '@ohos/common'; 17import { CommonConstants } from '@ohos/common'; 18import { Log } from '@ohos/common'; 19 20/** 21 * Desktop Dock function layout configuration 22 */ 23export class SmartDockLayoutConfig extends ILayoutConfig { 24 /** 25 * Dock Feature Layout Configuration Index 26 */ 27 static SMART_DOCK_LAYOUT_INFO = 'SmartDockLayoutInfo'; 28 29 /** 30 * Dock function layout data 31 */ 32 protected mDockLayoutInfo: any = []; 33 34 protected constructor() { 35 super(); 36 } 37 38 /** 39 * Get an instance of the workspace function layout configuration 40 */ 41 static getInstance(): SmartDockLayoutConfig { 42 if (globalThis.SmartDockLayoutConfig == null) { 43 globalThis.SmartDockLayoutConfig = new SmartDockLayoutConfig(); 44 globalThis.SmartDockLayoutConfig.initConfig(); 45 } 46 return globalThis.SmartDockLayoutConfig; 47 } 48 49 initConfig(): void { 50 const config = this.loadPersistConfig(); 51 this.mDockLayoutInfo = config; 52 } 53 54 getConfigLevel(): string { 55 return CommonConstants.LAYOUT_CONFIG_LEVEL_COMMON; 56 } 57 58 getConfigType(): number { 59 return CommonConstants.LAYOUT_CONFIG_TYPE_FUNCTION; 60 } 61 62 getConfigName(): string { 63 return SmartDockLayoutConfig.SMART_DOCK_LAYOUT_INFO; 64 } 65 66 protected getPersistConfigJson(): string { 67 return JSON.stringify(this.mDockLayoutInfo); 68 } 69 70 /** 71 * Update dock layout data 72 * 73 * @params gridLayoutInfo:dock layout data 74 */ 75 updateDockLayoutInfo(dockLayoutInfo: object): void { 76 this.mDockLayoutInfo = dockLayoutInfo; 77 super.persistConfig(); 78 } 79 80 /** 81 * Get dock layout data 82 * 83 * @return dock layout data 84 */ 85 getDockLayoutInfo(): any { 86 return this.mDockLayoutInfo; 87 } 88 89 /** 90 * load configuration 91 */ 92 loadPersistConfig(): any { 93 let defaultConfig = super.loadPersistConfig(); 94 const configFromFile = this.loadPersistConfigFromFile(); 95 if (configFromFile) { 96 try { 97 defaultConfig = JSON.parse(configFromFile); 98 } catch (err) { 99 Log.showInfo('SmartDockLayoutConfig', `configFromFile cannot use json: ${defaultConfig}`); 100 } 101 } 102 return defaultConfig; 103 } 104} 105