harmony-utils之WindowUtil,窗口相关工具类 电子说
[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);
}
WindowUtil.setPreferredOrientation(window.Orientation.LANDSCAPE).then(() = > {
ToastUtil.showToast(`设置成功!`)
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
let orientation = WindowUtil.getPreferredOrientation();
DialogHelper.showToast(`窗口屏幕方向:${orientation}`);
WindowUtil.setWindowPrivacyMode(true).then(() = > {
ToastUtil.showToast("您已设置隐私模式,禁止截屏、录像");
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
let isPrivacyMode = WindowUtil.isPrivacyMode();
ToastUtil.showToast(`窗口是否隐私模式:${isPrivacyMode}`);
WindowUtil.setWindowLayoutFullScreen(true).then(() = > {
ToastUtil.showToast(`沉浸式布局已设置成功!`);
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
let isLayoutFullScreen = WindowUtil.isLayoutFullScreen();
ToastUtil.showToast(`窗口是否为沉浸式:${isLayoutFullScreen}`);
WindowUtil.setWindowSystemBarProperties({
statusBarColor: '#F00FF0',
statusBarContentColor: '#0FF00F',
isStatusBarLightIcon: true,
navigationBarColor: '#F06060',
navigationBarContentColor: "#0606F0",
isNavigationBarLightIcon: true
}).then(() = > {
ToastUtil.showToast("设置成功!");
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
let properties = WindowUtil.getWindowSystemBarProperties();
let jsonStr = JSON.stringify(properties, null, 2);
WindowUtil.setImmersiveModeEnabledState(true);
let enabled = WindowUtil.getImmersiveModeEnabledState();
ToastUtil.showToast(`是否开启沉浸式布局:${enabled}`);
WindowUtil.setWindowGrayScale(1.0);
WindowUtil.setWindowBackgroundColor('#9932CC');
ToastUtil.showToast("设置背景色成功!");
WindowUtil.setWindowSystemBarEnable(['status', 'navigation']).then(() = > {
ToastUtil.showToast(`设置成功!`);
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
WindowUtil.setSpecificSystemBarEnabled('navigationIndicator', true).then(() = > {
ToastUtil.showToast(`设置成功!`);
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
WindowUtil.setWindowKeepScreenOn(true).then(() = > {
ToastUtil.showToast("你已设置常亮");
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
let isKeepScreenOn = WindowUtil.isKeepScreenOn();
ToastUtil.showToast(`屏幕是否常亮:${isKeepScreenOn}`);
WindowUtil.setWindowBrightness(0.7).then(() = > {
ToastUtil.showToast(`您已设置亮度!`);
}).catch((err: BusinessError) = > {
LogUtil.error(`异常信息-code: ${err.code} - msg: ${err.message}`)
});
let brightness = WindowUtil.getBrightness();
ToastUtil.showToast(`屏幕亮度:${brightness}`);
WindowUtil.setWindowFocusable(true).then(() = > {
ToastUtil.showToast("设置成功啦^·^");
}).catch((err: BusinessError) = > {
ToastUtil.showToast("设置失败!");
});
let isFocusable = WindowUtil.isFocusable();
ToastUtil.showToast(`窗口是否可聚焦:${isFocusable}`);
WindowUtil.setWindowTouchable(true).then(() = > {
ToastUtil.showToast("设置成功啦^·^");
}).catch((err: BusinessError) = > {
ToastUtil.showToast("设置失败!");
});
let isTouchable = WindowUtil.isTouchable();
ToastUtil.showToast(`窗口是否可触摸:${isTouchable}`);
let properties = WindowUtil.getWindowProperties();
let jsonStr = `${JSON.stringify(properties, null, 2)}`;
let area = WindowUtil.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
let jsonStr = `${JSON.stringify(area, null, 2)}`;
let windowType = WindowUtil.getWindowType();
let status = WindowUtil.getWindowStatus();
let isFullScreen = WindowUtil.isFullScreen();
let isFocused = WindowUtil.isFocused();
let isTransparent = WindowUtil.isTransparent();
let isWindowShowing = WindowUtil.isWindowShowing();
let isWindowSupportWideGamut = await WindowUtil.isWindowSupportWideGamut();
WindowUtil.setDialogBackGestureEnabled(true).then(() = > {
ToastUtil.showToast("设置成功啦^·^");
}).catch((err: BusinessError) = > {
ToastUtil.showToast("设置失败!");
});
let isGestureBack = WindowUtil.isGestureBackEnabled();
WindowUtil.setGestureBackEnabled(!isGestureBack).then(() = > {
ToastUtil.showToast("设置成功啦^·^");
}).catch((err: BusinessError) = > {
ToastUtil.showToast("设置失败!");
});
let isGestureBack = WindowUtil.isGestureBackEnabled();
ToastUtil.showToast(`当前窗口是否禁用返回:${isGestureBack}`);
审核编辑 黄宇
全部0条评论
快来发表一下你的评论吧 !