harmony-utils之WindowUtil,窗口相關(guān)工具類
harmony-utils 簡(jiǎn)介與說(shuō)明
[harmony-utils] 一款功能豐富且極易上手的HarmonyOS工具庫(kù),借助眾多實(shí)用工具類,致力于助力開(kāi)發(fā)者迅速構(gòu)建鴻蒙應(yīng)用。其封裝的工具涵蓋了APP、設(shè)備、屏幕、授權(quán)、通知、線程間通信、彈框、吐司、生物認(rèn)證、用戶首選項(xiàng)、拍照、相冊(cè)、掃碼、文件、日志,異常捕獲、字符、字符串、數(shù)字、集合、日期、隨機(jī)、base64、加密、解密、JSON等一系列的功能和操作,能夠滿足各種不同的開(kāi)發(fā)需求。
[picker_utils] 是harmony-utils拆分出來(lái)的一個(gè)子庫(kù),包含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 設(shè)置窗口的顯示方向?qū)傩?/h5>WindowUtil.setPreferredOrientation(window.Orientation.LANDSCAPE).then(() = > {
ToastUtil.showToast(`設(shè)置成功!`)
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
getPreferredOrientation 獲取窗口的顯示方向?qū)傩?,主窗口調(diào)用
let orientation = WindowUtil.getPreferredOrientation();
DialogHelper.showToast(`窗口屏幕方向:${orientation}`);
setWindowPrivacyMode 設(shè)置窗口是否為隱私模式。設(shè)置為隱私模式的窗口,窗口內(nèi)容將無(wú)法被截屏或錄屏
WindowUtil.setWindowPrivacyMode(true).then(() = > {
ToastUtil.showToast("您已設(shè)置隱私模式,禁止截屏、錄像");
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
isPrivacyMode 窗口是否隱私模式,默認(rèn)主窗口
let isPrivacyMode = WindowUtil.isPrivacyMode();
ToastUtil.showToast(`窗口是否隱私模式:${isPrivacyMode}`);
setWindowLayoutFullScreen 設(shè)置窗口的布局是否為沉浸式布局(該沉浸式布局狀態(tài)欄、導(dǎo)航欄仍然顯示)
WindowUtil.setWindowLayoutFullScreen(true).then(() = > {
ToastUtil.showToast(`沉浸式布局已設(shè)置成功!`);
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
isLayoutFullScreen 判斷窗口是否為沉浸式,默認(rèn)主窗口
let isLayoutFullScreen = WindowUtil.isLayoutFullScreen();
ToastUtil.showToast(`窗口是否為沉浸式:${isLayoutFullScreen}`);
setWindowSystemBarProperties 設(shè)置主窗口三鍵導(dǎo)航欄、狀態(tài)欄的屬性
WindowUtil.setWindowSystemBarProperties({
statusBarColor: '#F00FF0',
statusBarContentColor: '#0FF00F',
isStatusBarLightIcon: true,
navigationBarColor: '#F06060',
navigationBarContentColor: "#0606F0",
isNavigationBarLightIcon: true
}).then(() = > {
ToastUtil.showToast("設(shè)置成功!");
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
getWindowSystemBarProperties 獲取主窗口三鍵導(dǎo)航欄、狀態(tài)欄的屬性
let properties = WindowUtil.getWindowSystemBarProperties();
let jsonStr = JSON.stringify(properties, null, 2);
setImmersiveModeEnabledState 設(shè)置當(dāng)前窗口是否開(kāi)啟沉浸式布局,該調(diào)用不會(huì)改變窗口模式和窗口大小
WindowUtil.setImmersiveModeEnabledState(true);
getImmersiveModeEnabledState 查詢當(dāng)前窗口是否已經(jīng)開(kāi)啟沉浸式布局
let enabled = WindowUtil.getImmersiveModeEnabledState();
ToastUtil.showToast(`是否開(kāi)啟沉浸式布局:${enabled}`);
setWindowGrayScale 設(shè)置窗口灰階。該接口需要在調(diào)用loadContent()或setUIContent()使窗口加載頁(yè)面內(nèi)容后調(diào)用。
WindowUtil.setWindowGrayScale(1.0);
setWindowBackgroundColor 設(shè)置窗口的背景色。Stage模型下,該接口需要在loadContent()或setUIContent()調(diào)用生效后使用
WindowUtil.setWindowBackgroundColor('#9932CC');
ToastUtil.showToast("設(shè)置背景色成功!");
setWindowSystemBarEnable 設(shè)置主窗口三鍵導(dǎo)航欄、狀態(tài)欄、底部導(dǎo)航條的可見(jiàn)模式,狀態(tài)欄與底部導(dǎo)航條通過(guò)status控制、三鍵導(dǎo)航欄通過(guò)navigation控制
WindowUtil.setWindowSystemBarEnable(['status', 'navigation']).then(() = > {
ToastUtil.showToast(`設(shè)置成功!`);
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
setSpecificSystemBarEnabled 設(shè)置主窗口三鍵導(dǎo)航欄、狀態(tài)欄、底部導(dǎo)航條的顯示和隱藏
WindowUtil.setSpecificSystemBarEnabled('navigationIndicator', true).then(() = > {
ToastUtil.showToast(`設(shè)置成功!`);
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
setWindowKeepScreenOn 設(shè)置屏幕是否為常亮狀態(tài)
WindowUtil.setWindowKeepScreenOn(true).then(() = > {
ToastUtil.showToast("你已設(shè)置常亮");
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
isKeepScreenOn 屏幕是否常亮
let isKeepScreenOn = WindowUtil.isKeepScreenOn();
ToastUtil.showToast(`屏幕是否常亮:${isKeepScreenOn}`);
setWindowBrightness 設(shè)置屏幕亮度值
WindowUtil.setWindowBrightness(0.7).then(() = > {
ToastUtil.showToast(`您已設(shè)置亮度!`);
}).catch((err: BusinessError) = > {
LogUtil.error(`異常信息-code: ${err.code} - msg: ${err.message}`)
});
getBrightness 獲取屏幕亮度。該參數(shù)為浮點(diǎn)數(shù),可設(shè)置的亮度范圍為[0.0, 1.0],其取1.0時(shí)表示最大亮度值。如果窗口沒(méi)有設(shè)置亮度值,表示亮度跟隨系統(tǒng),此時(shí)獲取到的亮度值為-1
let brightness = WindowUtil.getBrightness();
ToastUtil.showToast(`屏幕亮度:${brightness}`);
setWindowFocusable 設(shè)置使用點(diǎn)擊或其他方式使該窗口獲焦的場(chǎng)景時(shí),該窗口是否支持窗口焦點(diǎn)從點(diǎn)擊前的獲焦窗口切換到該窗口
WindowUtil.setWindowFocusable(true).then(() = > {
ToastUtil.showToast("設(shè)置成功啦^·^");
}).catch((err: BusinessError) = > {
ToastUtil.showToast("設(shè)置失敗!");
});
isFocusable 窗口是否可聚焦,默認(rèn)主窗口
let isFocusable = WindowUtil.isFocusable();
ToastUtil.showToast(`窗口是否可聚焦:${isFocusable}`);
setWindowTouchable 設(shè)置窗口是否為可觸狀態(tài)
WindowUtil.setWindowTouchable(true).then(() = > {
ToastUtil.showToast("設(shè)置成功啦^·^");
}).catch((err: BusinessError) = > {
ToastUtil.showToast("設(shè)置失??!");
});
isTouchable 窗口是否可觸摸,默認(rèn)主窗口
let isTouchable = WindowUtil.isTouchable();
ToastUtil.showToast(`窗口是否可觸摸:${isTouchable}`);
getWindowProperties 獲取當(dāng)前窗口的屬性,默認(rèn)主窗口
let properties = WindowUtil.getWindowProperties();
let jsonStr = `${JSON.stringify(properties, null, 2)}`;
getWindowAvoidArea 獲取當(dāng)前應(yīng)用窗口內(nèi)容規(guī)避的區(qū)域。如系統(tǒng)欄區(qū)域、劉海屏區(qū)域、手勢(shì)區(qū)域、軟鍵盤區(qū)域等與窗口內(nèi)容重疊時(shí),需要窗口內(nèi)容避讓的區(qū)域
let area = WindowUtil.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
let jsonStr = `${JSON.stringify(area, null, 2)}`;
getWindowType 獲取窗口類型,默認(rèn)主窗口
let windowType = WindowUtil.getWindowType();
getWindowStatus 獲取當(dāng)前應(yīng)用窗口的模式
let status = WindowUtil.getWindowStatus();
isFullScreen 判斷窗口是否全屏,默認(rèn)主窗口
let isFullScreen = WindowUtil.isFullScreen();
isFocused 判斷當(dāng)前窗口是否已獲焦
let isFocused = WindowUtil.isFocused();
isTransparent 窗口是否透明,默認(rèn)主窗口
let isTransparent = WindowUtil.isTransparent();
isWindowShowing 判斷當(dāng)前窗口是否已顯示,默認(rèn)主窗口
let isWindowShowing = WindowUtil.isWindowShowing();
isWindowSupportWideGamut 判斷當(dāng)前窗口是否支持廣色域模式,,默認(rèn)主窗口
let isWindowSupportWideGamut = await WindowUtil.isWindowSupportWideGamut();
setDialogBackGestureEnabled 設(shè)置模態(tài)窗口是否響應(yīng)手勢(shì)返回事件,非模態(tài)窗口調(diào)用返回錯(cuò)誤碼
WindowUtil.setDialogBackGestureEnabled(true).then(() = > {
ToastUtil.showToast("設(shè)置成功啦^·^");
}).catch((err: BusinessError) = > {
ToastUtil.showToast("設(shè)置失??!");
});
setGestureBackEnabled 設(shè)置當(dāng)前窗口是否禁用返回手勢(shì)功能,僅主窗全屏模式下生效,2in1設(shè)備下不生效。
let isGestureBack = WindowUtil.isGestureBackEnabled();
WindowUtil.setGestureBackEnabled(!isGestureBack).then(() = > {
ToastUtil.showToast("設(shè)置成功啦^·^");
}).catch((err: BusinessError) = > {
ToastUtil.showToast("設(shè)置失??!");
});
isGestureBackEnabled 獲取當(dāng)前窗口是否禁用返回手勢(shì)功能,僅主窗全屏模式下生效,2in1設(shè)備不生效。
let isGestureBack = WindowUtil.isGestureBackEnabled();
ToastUtil.showToast(`當(dāng)前窗口是否禁用返回:${isGestureBack}`);
創(chuàng)作不易,請(qǐng)給童長(zhǎng)老點(diǎn)贊
WindowUtil.setPreferredOrientation(window.Orientation.LANDSCAPE).then(() = > {
ToastUtil.showToast(`設(shè)置成功!`)
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
let orientation = WindowUtil.getPreferredOrientation();
DialogHelper.showToast(`窗口屏幕方向:${orientation}`);
WindowUtil.setWindowPrivacyMode(true).then(() = > {
ToastUtil.showToast("您已設(shè)置隱私模式,禁止截屏、錄像");
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
let isPrivacyMode = WindowUtil.isPrivacyMode();
ToastUtil.showToast(`窗口是否隱私模式:${isPrivacyMode}`);
WindowUtil.setWindowLayoutFullScreen(true).then(() = > {
ToastUtil.showToast(`沉浸式布局已設(shè)置成功!`);
}).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("設(shè)置成功!");
}).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(`是否開(kāi)啟沉浸式布局:${enabled}`);
WindowUtil.setWindowGrayScale(1.0);
WindowUtil.setWindowBackgroundColor('#9932CC');
ToastUtil.showToast("設(shè)置背景色成功!");
WindowUtil.setWindowSystemBarEnable(['status', 'navigation']).then(() = > {
ToastUtil.showToast(`設(shè)置成功!`);
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
WindowUtil.setSpecificSystemBarEnabled('navigationIndicator', true).then(() = > {
ToastUtil.showToast(`設(shè)置成功!`);
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
WindowUtil.setWindowKeepScreenOn(true).then(() = > {
ToastUtil.showToast("你已設(shè)置常亮");
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
let isKeepScreenOn = WindowUtil.isKeepScreenOn();
ToastUtil.showToast(`屏幕是否常亮:${isKeepScreenOn}`);
WindowUtil.setWindowBrightness(0.7).then(() = > {
ToastUtil.showToast(`您已設(shè)置亮度!`);
}).catch((err: BusinessError) = > {
LogUtil.error(`異常信息-code: ${err.code} - msg: ${err.message}`)
});
let brightness = WindowUtil.getBrightness();
ToastUtil.showToast(`屏幕亮度:${brightness}`);
WindowUtil.setWindowFocusable(true).then(() = > {
ToastUtil.showToast("設(shè)置成功啦^·^");
}).catch((err: BusinessError) = > {
ToastUtil.showToast("設(shè)置失敗!");
});
let isFocusable = WindowUtil.isFocusable();
ToastUtil.showToast(`窗口是否可聚焦:${isFocusable}`);
WindowUtil.setWindowTouchable(true).then(() = > {
ToastUtil.showToast("設(shè)置成功啦^·^");
}).catch((err: BusinessError) = > {
ToastUtil.showToast("設(shè)置失??!");
});
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("設(shè)置成功啦^·^");
}).catch((err: BusinessError) = > {
ToastUtil.showToast("設(shè)置失??!");
});
let isGestureBack = WindowUtil.isGestureBackEnabled();
WindowUtil.setGestureBackEnabled(!isGestureBack).then(() = > {
ToastUtil.showToast("設(shè)置成功啦^·^");
}).catch((err: BusinessError) = > {
ToastUtil.showToast("設(shè)置失??!");
});
let isGestureBack = WindowUtil.isGestureBackEnabled();
ToastUtil.showToast(`當(dāng)前窗口是否禁用返回:${isGestureBack}`);
審核編輯 黃宇
-
Harmony
+關(guān)注
關(guān)注
0文章
108瀏覽量
3017
發(fā)布評(píng)論請(qǐng)先 登錄
評(píng)論