1e484b35bSopenharmony_ci/* 2e484b35bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3e484b35bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4e484b35bSopenharmony_ci * you may not use this file except in compliance with the License. 5e484b35bSopenharmony_ci * You may obtain a copy of the License at 6e484b35bSopenharmony_ci * 7e484b35bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8e484b35bSopenharmony_ci * 9e484b35bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10e484b35bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11e484b35bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12e484b35bSopenharmony_ci * See the License for the specific language governing permissions and 13e484b35bSopenharmony_ci * limitations under the License. 14e484b35bSopenharmony_ci */ 15e484b35bSopenharmony_ci 16e484b35bSopenharmony_ciimport { 17e484b35bSopenharmony_ci getValue, 18e484b35bSopenharmony_ci Log 19e484b35bSopenharmony_ci} from '../../../utils/index'; 20e484b35bSopenharmony_ci 21e484b35bSopenharmony_ciinterface dpiConstructor { 22e484b35bSopenharmony_ci new(options: object): Dpi; 23e484b35bSopenharmony_ci} 24e484b35bSopenharmony_ci 25e484b35bSopenharmony_ciexport interface DPIInterface { 26e484b35bSopenharmony_ci instance?: { dpi: dpiConstructor }; 27e484b35bSopenharmony_ci} 28e484b35bSopenharmony_ci 29e484b35bSopenharmony_ciinterface VMInterface { 30e484b35bSopenharmony_ci $r: Function; 31e484b35bSopenharmony_ci _dpi: Function; 32e484b35bSopenharmony_ci} 33e484b35bSopenharmony_ci 34e484b35bSopenharmony_ciconst instances = {}; 35e484b35bSopenharmony_ci 36e484b35bSopenharmony_ci/** 37e484b35bSopenharmony_ci * This class provides multi-resolution display support. 38e484b35bSopenharmony_ci */ 39e484b35bSopenharmony_ciclass Dpi { 40e484b35bSopenharmony_ci public image: object; 41e484b35bSopenharmony_ci 42e484b35bSopenharmony_ci constructor(options) { 43e484b35bSopenharmony_ci this.image = options.images; 44e484b35bSopenharmony_ci } 45e484b35bSopenharmony_ci 46e484b35bSopenharmony_ci /** 47e484b35bSopenharmony_ci * Check the value of the key in images. 48e484b35bSopenharmony_ci * @param {string} path - Source path. 49e484b35bSopenharmony_ci * @return {Object | string} The value of the key if found. 50e484b35bSopenharmony_ci */ 51e484b35bSopenharmony_ci public $r(path: string): object | string { 52e484b35bSopenharmony_ci if (typeof path !== 'string') { 53e484b35bSopenharmony_ci Log.warn(`Invalid parameter type: The type of 'path' should be string, not ${typeof path}.`); 54e484b35bSopenharmony_ci return; 55e484b35bSopenharmony_ci } 56e484b35bSopenharmony_ci const images = this.image; 57e484b35bSopenharmony_ci let res; 58e484b35bSopenharmony_ci for (const index in images) { 59e484b35bSopenharmony_ci res = getValue(path, images[index]); 60e484b35bSopenharmony_ci if (res) { 61e484b35bSopenharmony_ci return res; 62e484b35bSopenharmony_ci } 63e484b35bSopenharmony_ci } 64e484b35bSopenharmony_ci return path; 65e484b35bSopenharmony_ci } 66e484b35bSopenharmony_ci 67e484b35bSopenharmony_ci /** 68e484b35bSopenharmony_ci * Extend _dpi to Vm. 69e484b35bSopenharmony_ci * @param {VMInterface} Vm - The Vm. 70e484b35bSopenharmony_ci */ 71e484b35bSopenharmony_ci public extend(Vm: VMInterface): void { 72e484b35bSopenharmony_ci Object.defineProperty(Vm, '_dpi', { 73e484b35bSopenharmony_ci configurable: true, 74e484b35bSopenharmony_ci enumerable: true, 75e484b35bSopenharmony_ci get: function proxyGetter() { 76e484b35bSopenharmony_ci return this.dpi ? this.dpi : global.aceapp.dpi; 77e484b35bSopenharmony_ci } 78e484b35bSopenharmony_ci }); 79e484b35bSopenharmony_ci Vm.$r = function(key: string): string { 80e484b35bSopenharmony_ci const dpi = this._dpi; 81e484b35bSopenharmony_ci return dpi.$r(key); 82e484b35bSopenharmony_ci }; 83e484b35bSopenharmony_ci } 84e484b35bSopenharmony_ci} 85e484b35bSopenharmony_ci 86e484b35bSopenharmony_ci/** 87e484b35bSopenharmony_ci * Init the dpi object. 88e484b35bSopenharmony_ci */ 89e484b35bSopenharmony_ciexport default { 90e484b35bSopenharmony_ci create: (id: number): DPIInterface => { 91e484b35bSopenharmony_ci instances[id] = []; 92e484b35bSopenharmony_ci if (typeof global.dpi === 'function') { 93e484b35bSopenharmony_ci return {}; 94e484b35bSopenharmony_ci } 95e484b35bSopenharmony_ci const dpiObject = { 96e484b35bSopenharmony_ci dpi: class extends Dpi { 97e484b35bSopenharmony_ci constructor(options) { 98e484b35bSopenharmony_ci super(options); 99e484b35bSopenharmony_ci instances[id].push(this); 100e484b35bSopenharmony_ci } 101e484b35bSopenharmony_ci } 102e484b35bSopenharmony_ci }; 103e484b35bSopenharmony_ci return { 104e484b35bSopenharmony_ci instance: dpiObject 105e484b35bSopenharmony_ci }; 106e484b35bSopenharmony_ci }, 107e484b35bSopenharmony_ci destroy: (id: number): void => { 108e484b35bSopenharmony_ci delete instances[id]; 109e484b35bSopenharmony_ci } 110e484b35bSopenharmony_ci}; 111