harmony-utils之WindowUtil,窗口相关工具类
harmony-utils 简介与说明
harmony-utils 一款功能丰富且极易上手的HarmonyOS工具库,借助众多实用工具类,致力于助力开发者迅速构建鸿蒙应用。其封装的工具涵盖了APP、设备、屏幕、授权、通知、线程间通信、弹框、吐司、生物认证、用户首选项、拍照、相册、扫码、文件、日志,异常捕获、字符、字符串、数字、集合、日期、随机、base64、加密、解密、JSON等一系列的功能和操作,能够满足各种不同的开发需求。
picker_utils 是harmony-utils拆分出来的一个子库,包含PickerUtil、PhotoHelper、ScanUtil。
下载安装ohpm i @pura/harmony-utilsohpm i @pura/picker_utils
//全局初始化方法,在UIAbility的onCreate方法中初始化 AppUtil.init()
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
AppUtil.init(this.context);
}
API方法与使用
setPreferredOrientation 设置窗口的显示方向属性
WindowUtil.setPreferredOrientation(window.Orientation.LANDSCAPE).then(() => {
ToastUtil.showToast(`设置成功!`)
}).catch((err: BusinessError) => {
LogUtil.error(err);
});
getPreferredOrientation 获取窗口的显示方向属性,主窗口调用
let orientation = WindowUtil.getPreferredOrientation();
DialogHelper.showToast(`窗口屏幕方向:${orientation}`);
setWindowPrivacyMode 设置窗口是否为隐私模式。设置为隐私模式的窗口,窗口内容将无法被截屏或录屏
WindowUtil.setWindowPrivacyMode(true).then(() => {
ToastUtil.showToast("您已设置隐私模式,禁止截屏、录像");
}).catch((err: BusinessError) => {
LogUtil.error(err);
});
isPrivacyMode 窗口是否隐私模式,默认主窗口
let isPrivacyMode = WindowUtil.isPrivacyMode();
ToastUtil.showToast(`窗口是否隐私模式:${isPrivacyMode}`);
setWindowLayoutFullScreen 设置窗口的布局是否为沉浸式布局(该沉浸式布局状态栏、导航栏仍然显示)
WindowUtil.setWindowLayoutFullScreen(true).then(() => {
ToastUtil.showToast(`沉浸式布局已设置成功!`);
}).catch((err: BusinessError) => {
LogUtil.error(err);
});
isLayoutFullScreen 判断窗口是否为沉浸式,默认主窗口
let isLayoutFullScreen = WindowUtil.isLayoutFullScreen();
ToastUtil.showToast(`窗口是否为沉浸式:${isLayoutFullScreen}`);
setWindowSystemBarProperties 设置主窗口三键导航栏、状态栏的属性
WindowUtil.setWindowSystemBarProperties({
statusBarColor: '#F00FF0',
statusBarContentColor: '#0FF00F',
isStatusBarLightIcon: true,
navigationBarColor: '#F06060',
navigationBarContentColor: "#0606F0",
isNavigationBarLightIcon: true
}).then(() => {
ToastUtil.showToast("设置成功!");
}).catch((err: BusinessError) => {
LogUtil.error(err);
});
getWindowSystemBarProperties 获取主窗口三键导航栏、状态栏的属性
let properties = WindowUtil.getWindowSystemBarProperties();
let jsonStr = JSON.stringify(properties, null, 2);
setImmersiveModeEnabledState 设置当前窗口是否开启沉浸式布局,该调用不会改变窗口模式和窗口大小
WindowUtil.setImmersiveModeEnabledState(true);
getImmersiveModeEnabledState 查询当前窗口是否已经开启沉浸式布局
let enabled = WindowUtil.getImmersiveModeEnabledState();
ToastUtil.showToast(`是否开启沉浸式布局:${enabled}`);
setWindowGrayScale 设置窗口灰阶。该接口需要在调用loadContent()或setUIContent()使窗口加载页面内容后调用。
WindowUtil.setWindowGrayScale(1.0);
setWindowBackgroundColor 设置窗口的背景色。Stage模型下,该接口需要在loadContent()或setUIContent()调用生效后使用
WindowUtil.setWindowBackgroundColor('#9932CC');
ToastUtil.showToast("设置背景色成功!");
setWindowSystemBarEnable 设置主窗口三键导航栏、状态栏、底部导航条的可见模式,状态栏与底部导航条通过status控制、三键导航栏通过navigation控制
WindowUtil.setWindowSystemBarEnable(['status', 'navigation']).then(() => {
ToastUtil.showToast(`设置成功!`);
}).catch((err: BusinessError) => {
LogUtil.error(err);
});
setSpecificSystemBarEnabled 设置主窗口三键导航栏、状态栏、底部导航条的显示和隐藏
WindowUtil.setSpecificSystemBarEnabled('navigationIndicator', true).then(() => {
ToastUtil.showToast(`设置成功!`);
}).catch((err: BusinessError) => {
LogUtil.error(err);
});
setWindowKeepScreenOn 设置屏幕是否为常亮状态
WindowUtil.setWindowKeepScreenOn(true).then(() => {
ToastUtil.showToast("你已设置常亮");
}).catch((err: BusinessError) => {
LogUtil.error(err);
});
isKeepScreenOn 屏幕是否常亮
let isKeepScreenOn = WindowUtil.isKeepScreenOn();
ToastUtil.showToast(`屏幕是否常亮:${isKeepScreenOn}`);
setWindowBrightness 设置屏幕亮度值
WindowUtil.setWindowBrightness(0.7).then(() => {
ToastUtil.showToast(`您已设置亮度!`);
}).catch((err: BusinessError) => {
LogUtil.error(`异常信息-code: ${err.code} - msg: ${err.message}`)
});
getBrightness 获取屏幕亮度。该参数为浮点数,可设置的亮度范围为[0.0, 1.0],其取1.0时表示最大亮度值。如果窗口没有设置亮度值,表示亮度跟随系统,此时获取到的亮度值为-1
let brightness = WindowUtil.getBrightness();
ToastUtil.showToast(`屏幕亮度:${brightness}`);
setWindowFocusable 设置使用点击或其他方式使该窗口获焦的场景时,该窗口是否支持窗口焦点从点击前的获焦窗口切换到该窗口
WindowUtil.setWindowFocusable(true).then(() => {
ToastUtil.showToast("设置成功啦^·^");
}).catch((err: BusinessError) => {
ToastUtil.showToast("设置失败!");
});
isFocusable 窗口是否可聚焦,默认主窗口
let isFocusable = WindowUtil.isFocusable();
ToastUtil.showToast(`窗口是否可聚焦:${isFocusable}`);
setWindowTouchable 设置窗口是否为可触状态
WindowUtil.setWindowTouchable(true).then(() => {
ToastUtil.showToast("设置成功啦^·^");
}).catch((err: BusinessError) => {
ToastUtil.showToast("设置失败!");
});
isTouchable 窗口是否可触摸,默认主窗口
let isTouchable = WindowUtil.isTouchable();
ToastUtil.showToast(`窗口是否可触摸:${isTouchable}`);
getWindowProperties 获取当前窗口的属性,默认主窗口
let properties = WindowUtil.getWindowProperties();
let jsonStr = `${JSON.stringify(properties, null, 2)}`;
getWindowAvoidArea 获取当前应用窗口内容规避的区域。如系统栏区域、刘海屏区域、手势区域、软键盘区域等与窗口内容重叠时,需要窗口内容避让的区域
let area = WindowUtil.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
let jsonStr = `${JSON.stringify(area, null, 2)}`;
getWindowType 获取窗口类型,默认主窗口
let windowType = WindowUtil.getWindowType();
getWindowStatus 获取当前应用窗口的模式
let status = WindowUtil.getWindowStatus();
isFullScreen 判断窗口是否全屏,默认主窗口
let isFullScreen = WindowUtil.isFullScreen();
isFocused 判断当前窗口是否已获焦
let isFocused = WindowUtil.isFocused();
isTransparent 窗口是否透明,默认主窗口
let isTransparent = WindowUtil.isTransparent();
isWindowShowing 判断当前窗口是否已显示,默认主窗口
let isWindowShowing = WindowUtil.isWindowShowing();
isWindowSupportWideGamut 判断当前窗口是否支持广色域模式,,默认主窗口
let isWindowSupportWideGamut = await WindowUtil.isWindowSupportWideGamut();
setDialogBackGestureEnabled 设置模态窗口是否响应手势返回事件,非模态窗口调用返回错误码
WindowUtil.setDialogBackGestureEnabled(true).then(() => {
ToastUtil.showToast("设置成功啦^·^");
}).catch((err: BusinessError) => {
ToastUtil.showToast("设置失败!");
});
setGestureBackEnabled 设置当前窗口是否禁用返回手势功能,仅主窗全屏模式下生效,2in1设备下不生效。
let isGestureBack = WindowUtil.isGestureBackEnabled();
WindowUtil.setGestureBackEnabled(!isGestureBack).then(() => {
ToastUtil.showToast("设置成功啦^·^");
}).catch((err: BusinessError) => {
ToastUtil.showToast("设置失败!");
});
isGestureBackEnabled 获取当前窗口是否禁用返回手势功能,仅主窗全屏模式下生效,2in1设备不生效。
let isGestureBack = WindowUtil.isGestureBackEnabled();
ToastUtil.showToast(`当前窗口是否禁用返回:${isGestureBack}`);
示例代码
import {
router, window } from '@kit.ArkUI';
import {
AppUtil, LogUtil, ToastUtil, WindowUtil } from '@pura/harmony-utils';
import {
DescribeBean } from '../../model/DescribeBean';
import {
BusinessError } from '@kit.BasicServicesKit';
import {
MockSetup } from '@ohos/hamock';
import {
TitleBarView } from '../../component/TitleBarView';
import {
DialogHelper } from '@pura/harmony-dialog';
import {
Utils } from '../../utils/Utils';
import {
JSON } from '@kit.ArkTS';
/**
* 窗口相关工具类
*/
@Entry
@Component
struct Index {
private scroller: Scroller = new Scroller();
@State describe: DescribeBean = router.getParams() as DescribeBean;
@State privacyMode: boolean = false; //是否隐私模式
@State isLayoutFullScreen: boolean = false; //是否沉浸式布局
@State blImmersive: boolean = false; //是否开启沉浸式布局
@State blGrayScale: boolean = false; //是否灰阶
@State blFullScreen: boolean = false; //是否全屏
@State isKeepScreenOn: boolean = false; //屏幕是否为常亮状态
@State bright: number = 0.2; //屏幕亮度值
@State blFocusable: boolean = false; //窗口是否可聚焦
@State blTouchable: boolean = false; //窗口是否可触摸
@State blD1: boolean = false; //模态窗口是否响应手势返回事件
@MockSetup
mock() {
this.describe = new DescribeBean("AppUtil", "窗口相关工具类");
}
build() {
Column() {
TitleBarView({
describe: this.describe })
Divider()
Scroll(this.scroller) {
Column({
space: 5 }) {
Button("setPreferredOrientation()")
.btnStyle()
.onClick(() => {
WindowUtil.setPreferredOrientation(AppUtil.isLandscape() ? window.Orientation.PORTRAIT : window.Orientation.LANDSCAPE)
.then(() => {
ToastUtil.showToast(`设置成功!`)
}).catch((err: BusinessError) => {
LogUtil.error(err);
});
})
Button("getPreferredOrientation()")
.btnStyle()
.onClick(() => {
let orientation = WindowUtil.getPreferredOrientation();
DialogHelper.showToast(`窗口屏幕方向:${
orientation}`);
})
Button("setWindowPrivacyMode()")
.btnStyle()
.onClick(() => {
this.privacyMode = this.privacyMode ? false : true;
WindowUtil.setWindowPrivacyMode(this.privacyMode).then(() => {
ToastUtil.showToast(`${
this.privacyMode ? "您已设置隐私模式,禁止截屏、录像" : "您已取消隐私模式"}`);
}).catch((err: BusinessError) => {
LogUtil.error(err);
});
})
Button("isPrivacyMode()")
.btnStyle()
.onClick(() => {
let isPrivacyMode = WindowUtil.isPrivacyMode();
ToastUtil.showToast(`窗口是否隐私模式:${
isPrivacyMode}`);
})
Button("setWindowLayoutFullScreen()")
.btnStyle()
.onClick(() => {
this.isLayoutFullScreen = this.isLayoutFullScreen ? false : true;
WindowUtil.setWindowLayoutFullScreen(this.isLayoutFullScreen).then(() => {
ToastUtil.showToast(`沉浸式布局已设置成功!`);
}).catch((err: BusinessError) => {
LogUtil.error(err);
});
})
Button("isLayoutFullScreen()")
.btnStyle()
.onClick(() => {
let isLayoutFullScreen = WindowUtil.isLayoutFullScreen();
ToastUtil.showToast(`窗口是否为沉浸式:${
isLayoutFullScreen}`);
})
Button("setWindowSystemBarProperties()")
.btnStyle()
.onClick(() => {
WindowUtil.setWindowSystemBarProperties({
statusBarColor: '#F00FF0',
statusBarContentColor: '#0FF00F',
isStatusBarLightIcon: true,
navigationBarColor: '#F06060',
navigationBarContentColor: "#0606F0",
isNavigationBarLightIcon: true
}).then(() => {
ToastUtil.showToast("设置成功!");
}).catch((err: BusinessError) => {
LogUtil.error(err);
});
})
Button("getWindowSystemBarProperties()")
.btnStyle()
.onClick(() => {
let properties = WindowUtil.getWindowSystemBarProperties();
let jsonStr = JSON.stringify(properties, null, 2);
Utils.showSheetText(jsonStr);
})
Button("setImmersiveModeEnabledState()")
.btnStyle()
.onClick(() => {
this.blImmersive = this.blImmersive ? false : true;
WindowUtil.setImmersiveModeEnabledState(this.blImmersive);
ToastUtil.showToast("设置成功!");
})
Button("getImmersiveModeEnabledState()")
.btnStyle()
.onClick(() => {
let enabled = WindowUtil.getImmersiveModeEnabledState();
ToastUtil.showToast(`是否开启沉浸式布局:${
enabled}`);
})
Button("setWindowGrayScale()")
.btnStyle()
.onClick(() => {
this.blGrayScale = !this.blGrayScale;
let grayScale = this.blGrayScale ? 1.0 : 0;
WindowUtil.setWindowGrayScale(grayScale);
ToastUtil.showToast(`窗口灰阶:${
grayScale}`);
})
Button("setWindowBackgroundColor()")
.btnStyle()
.onClick(() => {
WindowUtil.setWindowBackgroundColor('#9932CC');
ToastUtil.showToast("设置背景色成功!");
})
Button("setWindowSystemBarEnable()")
.btnStyle()
.onClick(() => {
this.blFullScreen = !this.blFullScreen;
WindowUtil.setWindowSystemBarEnable(this.blFullScreen ? [] : ['status', 'navigation']).then(() => {
ToastUtil.showToast(`设置成功!`);
}).catch((err: BusinessError) => {
LogUtil.error(err);
});
})
Button("setSpecificSystemBarEnabled()")
.btnStyle()
.onClick(() => {
let name: window.SpecificSystemBar = this.blFullScreen ? 'status' : 'navigationIndicator';
WindowUtil.setSpecificSystemBarEnabled(name, this.blFullScreen).then(() => {
ToastUtil.showToast(`设置成功!`);
}).catch((err: BusinessError) => {
LogUtil.error(err);
});
})
Button("setWindowKeepScreenOn()")
.btnStyle()
.onClick(() => {
this.isKeepScreenOn = this.isKeepScreenOn ? false : true;
WindowUtil.setWindowKeepScreenOn(this.isKeepScreenOn).then(() => {
ToastUtil.showToast(`${
this.isKeepScreenOn ? "你已设置常亮" : "你已取消常亮"}`);
}).catch((err: BusinessError) => {
LogUtil.error(err);
});
})
Button("isKeepScreenOn()")
.btnStyle()
.onClick(() => {
let isKeepScreenOn = WindowUtil.isKeepScreenOn();
ToastUtil.showToast(`屏幕是否常亮:${
isKeepScreenOn}`);
})
Button("setWindowBrightness()")
.btnStyle()
.onClick(() => {
this.bright = this.bright === 1.0 ? 0.2 : 1.0;
WindowUtil.setWindowBrightness(this.bright).then(() => {
ToastUtil.showToast(`您已设置亮度: ${
this.bright}`);
}).catch((err: BusinessError) => {
LogUtil.error(`异常信息-code: ${
err.code} - msg: ${
err.message}`)
});
})
Button("getBrightness()")
.btnStyle()
.onClick(() => {
let brightness = WindowUtil.getBrightness();
ToastUtil.showToast(`屏幕亮度:${
brightness}`);
})
Button("setWindowFocusable()")
.btnStyle()
.onClick(() => {
this.blFocusable = !this.blFocusable;
WindowUtil.setWindowFocusable(this.blFocusable).then(() => {
ToastUtil.showToast("设置成功啦^·^");
}).catch((err: BusinessError) => {
ToastUtil.showToast("设置失败!");
});
})
Button("isFocusable()")
.btnStyle()
.onClick(() => {
let isFocusable = WindowUtil.isFocusable();
ToastUtil.showToast(`窗口是否可聚焦:${
isFocusable}`);
})
Button("setWindowTouchable()")
.btnStyle()
.onClick(() => {
this.blTouchable = !this.blTouchable;
WindowUtil.setWindowTouchable(this.blTouchable).then(() => {
ToastUtil.showToast("设置成功啦^·^");
}).catch((err: BusinessError) => {
ToastUtil.showToast("设置失败!");
});
})
Button("isTouchable()")
.btnStyle()
.onClick(() => {
let isTouchable = WindowUtil.isTouchable();
ToastUtil.showToast(`窗口是否可触摸:${
isTouchable}`);
})
Button("getWindowProperties()")
.btnStyle()
.onClick(() => {
let properties = WindowUtil.getWindowProperties();
let jsonStr = `${
JSON.stringify(properties, null, 2)}`;
Utils.showSheetText(jsonStr);
})
Button("getWindowAvoidArea()")
.btnStyle()
.onClick(() => {
let area = WindowUtil.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
let jsonStr = `${
JSON.stringify(area, null, 2)}`;
Utils.showSheetText(jsonStr);
})
Button("getWindowType()\ngetWindowStatus()\nisTransparent()\nisFullScreen()\nisTransparent()\nisWindowShowing()\nisWindowSupportWideGamut()")
.labelStyle({
maxLines: 8 })
.type(ButtonType.Normal)
.borderRadius(10)
.padding({
top: 10, bottom: 10 })
.btnStyle()
.onClick(async () => {
let windowType = WindowUtil.getWindowType();
let status = WindowUtil.getWindowStatus();
let isFocused = WindowUtil.isFocused();
let isFullScreen = WindowUtil.isFullScreen();
let isTransparent = WindowUtil.isTransparent();
let isWindowShowing = WindowUtil.isWindowShowing();
let isWindowSupportWideGamut = await WindowUtil.isWindowSupportWideGamut();
let textStr = `windowType: ${
windowType}\nWindowStatus: ${
status}\nisFocused: ${
isFocused}\nisFullScreen: ${
isFullScreen}`;
textStr = textStr + `\nisTransparent: ${
isTransparent}\nisWindowShowing: ${
isWindowShowing}\nisWindowSupportWideGamut: ${
isWindowSupportWideGamut}`;
Utils.showSheetText(textStr);
})
Button("setDialogBackGestureEnabled()")
.btnStyle()
.onClick(() => {
this.blD1 = !this.blD1;
WindowUtil.setDialogBackGestureEnabled(this.blD1).then(() => {
ToastUtil.showToast("设置成功啦^·^");
}).catch((err: BusinessError) => {
ToastUtil.showToast("设置失败!");
});
})
Button("setGestureBackEnabled()")
.btnStyle()
.onClick(() => {
let isGestureBack = WindowUtil.isGestureBackEnabled();
WindowUtil.setGestureBackEnabled(!isGestureBack).then(() => {
ToastUtil.showToast("设置成功啦^·^");
}).catch((err: BusinessError) => {
ToastUtil.showToast("设置失败!");
});
})
Button("isGestureBackEnabled()")
.btnStyle()
.onClick(() => {
let isGestureBack = WindowUtil.isGestureBackEnabled();
ToastUtil.showToast(`当前窗口是否禁用返回:${
isGestureBack}`);
})
}
.margin({
top: 5, bottom: 5 })
}
.layoutWeight(1)
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Start)
.backgroundColor($r('app.color.main_background'))
}
}
@Styles
function btnStyle() {
.width('90%')
.margin({
top: 10, bottom: 5 })
}
创作不易,请给童长老点赞👍
https://github.com/787107497/harmony-utils
https://gitee.com/tongyuyan/harmony-utils
OpenHarmony三方库
童长老CSDN博客