1/* 2 * Copyright (c) 2021-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 window from '@ohos.window'; 17import prompt from '@ohos.prompt'; 18import { getResourceString } from './Tools'; 19import { MILLISECOND, FILENAME_REGEXP } from '../constants/Constant'; 20import Logger from '../log/Logger'; 21import type { BusinessError } from '@ohos.base'; 22 23const TAG = 'commonUtil'; 24 25export const toast = (text: string | Resource, time = MILLISECOND.ONE_SECOND) => { 26 let resourceString: string | undefined = undefined; 27 if (typeof text !== 'string') { 28 resourceString = getResourceString(text); 29 } else { 30 resourceString = text; 31 } 32 prompt.showToast({ message: resourceString, duration: time }); 33} 34 35/** 36 * 设置导航栏、状态栏背景、文字颜色 37 * 38 * @param navigationBarColor 导航栏背景颜色 39 * @param statusBarColor 状态栏背景颜色 40 * @param navigationBarContentColor 导航栏文字颜色 41 * @param statusBarContentColor 状态栏文字颜色 42 */ 43export const setSystemBar = async (navigationBarColor?: string, statusBarColor?: string, 44 navigationBarContentColor?: string, statusBarContentColor?: string) => { 45 let w = await window.getTopWindow(getContext()); 46 await w.setSystemBarProperties({ 47 navigationBarColor, 48 statusBarColor, 49 navigationBarContentColor, 50 statusBarContentColor 51 }); 52} 53 54/** 55 * 设置沉浸式 56 * 57 * @param isImmersion 是否沉浸式 58 */ 59export const setImmersion = (isImmersion: boolean) => { 60 let TAG = 'setImmersion'; 61 if (!globalThis.windowClass) { 62 return; 63 } 64 globalThis.windowClass.setFullScreen(isImmersion, (err: BusinessError) => { 65 if (err.code) { 66 Logger.e(TAG, 'Failed to enable the full-screen mode. Cause:' + JSON.stringify(err)); 67 return; 68 } 69 }) 70} 71 72/** 73 * 校验文件名合法性 74 * 75 * @param fileName 文件名 76 * @return 是否合法 77 */ 78export function isValidFileName(fileName: string): boolean { 79 return FILENAME_REGEXP.test(fileName); 80} 81 82