写点什么

harmony-utils 之 WindowUtil,窗口相关工具类

  • 2025-06-26
    安徽
  • 本文字数:3751 字

    阅读完需:约 12 分钟

harmony-utils 之 WindowUtil,窗口相关工具类

harmony-utils 简介与说明



harmony-utils 一款功能丰富且极易上手的 HarmonyOS 工具库,借助众多实用工具类,致力于助力开发者迅速构建鸿蒙应用。其封装的工具涵盖了 APP、设备、屏幕、授权、通知、线程间通信、弹框、吐司、生物认证、用户首选项、拍照、相册、扫码、文件、日志,异常捕获、字符、字符串、数字、集合、日期、随机、base64、加密、解密、JSON 等一系列的功能和操作,能够满足各种不同的开发需求。


picker_utils 是 harmony-utils 拆分出来的一个子库,包含 PickerUtil、PhotoHelper、ScanUtil。


下载安装


ohpm i @pura/harmony-utils


ohpm 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 设备下不生效。<API13+>
let isGestureBack = WindowUtil.isGestureBackEnabled();WindowUtil.setGestureBackEnabled(!isGestureBack).then(() => {  ToastUtil.showToast("设置成功啦^·^");}).catch((err: BusinessError) => {  ToastUtil.showToast("设置失败!");});
复制代码
isGestureBackEnabled 获取当前窗口是否禁用返回手势功能,仅主窗全屏模式下生效,2in1 设备不生效。<API13+>
 let isGestureBack = WindowUtil.isGestureBackEnabled(); ToastUtil.showToast(`当前窗口是否禁用返回:${isGestureBack}`);
复制代码

创作不易,请给童长老点赞👍



https://github.com/787107497/harmony-utils


https://gitee.com/tongyuyan/harmony-utils


OpenHarmony三方库


童长老CSDN博客

用户头像

还未添加个人签名 2025-05-14 加入

还未添加个人简介

评论

发布
暂无评论
harmony-utils之WindowUtil,窗口相关工具类_HarmonyOS_桃花镇童长老_InfoQ写作社区