1 Star 0 Fork 0

雷琦玮/HarmonyOS_Winsleikit

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
WindowHome.ets 6.22 KB
一键复制 编辑 原始数据 按行查看 历史
雷琦玮 提交于 2025-04-16 15:38 +08:00 . 完善Demo
import { WinsleiLogManager } from '@winslei/winsleilog'
import { MenuInfo } from '../../Common/model/MenuInfo'
import window from '@ohos.window'
import { AppStorageNameConstant } from '../../Common/constants/AppStorageNameConstant'
import settings from '@ohos.settings';
import { BusinessError } from '@ohos.base'
import { Test1 } from '@winslei/winsleihsp'
const TAG = 'WindowHome'
@Entry
@Component
struct WindowHome {
scroller: Scroller = new Scroller()
@State focusIndex: number = -1
@State menus: MenuInfo[] = []
private windowStage: window.WindowStage | undefined = AppStorage.get(AppStorageNameConstant.ENTRY_WINDOW_STAGE)
private context: Context = getContext(this)
aboutToDisappear() {
window.getLastWindow(getContext(this)).then((win) => {
win.setWindowLayoutFullScreen(false).then(() => {
win.setWindowSystemBarEnable(['status', 'navigation']).then(()=> {
})
})
})
}
aboutToAppear() {
window.getLastWindow(getContext(this)).then((win) => {
win.setWindowLayoutFullScreen(true).then(() => {
win.setWindowSystemBarEnable(['navigation']).then(()=> {
})
// win.setSpecificSystemBarEnabled('navigationIndicator', true).then(()=> {
//
// })
})
})
let menuInfo1 = new MenuInfo()
menuInfo1.title = '设置屏幕常亮'
let menuInfo2 = new MenuInfo()
menuInfo2.title = '获取主窗口屏幕亮度'
let menuInfo3 = new MenuInfo()
menuInfo3.title = '设置主窗口亮度'
let menuInfo4 = new MenuInfo()
menuInfo4.title = '获取系统屏幕亮度'
let menuInfo5 = new MenuInfo()
menuInfo5.title = '设置系统屏幕亮度'
let menuInfo6 = new MenuInfo()
menuInfo6.title = '创建子窗口并加载hsp页面'
this.menus.push(menuInfo1, menuInfo2, menuInfo3, menuInfo4, menuInfo5, menuInfo6)
}
build() {
Grid() {
ForEach(this.menus, (menuInfo: MenuInfo, index: number) => {
GridItem() {
Text(menuInfo.title)
.fontSize(30)
.fontColor(Color.White)
.backgroundColor('#1affffff')
.width('100%')
.height('100%')
.textAlign(TextAlign.Center)
.border({
style: BorderStyle.Solid,
width: this.focusIndex == index ? 4 : 0,
color: Color.White
})
.focusable(true)
.defaultFocus(index == 0)
.onFocus(() => {
WinsleiLogManager.hilogIWithTag(TAG, JSON.stringify(menuInfo))
this.focusIndex = index
})
.onClick((event?: ClickEvent) => {
switch(menuInfo.title) {
case '设置屏幕常亮':
this.windowStage?.getMainWindow().then(win => {
win.setWindowKeepScreenOn(true).then(() => {
WinsleiLogManager.hilogIWithTag(TAG, 'onClick, setWindowKeepScreenOn success')
})
})
break
case '获取主窗口屏幕亮度':
this.windowStage?.getMainWindow().then(win => {
let winPro = win.getWindowProperties()
WinsleiLogManager.hilogIWithTag(TAG, 'onClick, getWindowProperties. winPro:' + JSON.stringify(winPro))
})
break
case '设置主窗口亮度':
this.windowStage?.getMainWindow().then(win => {
win.setWindowBrightness(0.8).then(() => {
WinsleiLogManager.hilogIWithTag(TAG, 'onClick, setWindowBrightness success')
})
})
break
case '获取系统屏幕亮度':
// 区间为0-255,通过 获取到的值/255 即可得到 默认主窗口屏幕亮度
let brightness = settings.getValueSync(this.context, settings.display.SCREEN_BRIGHTNESS_STATUS, settings.domainName.DEVICE_SHARED)
WinsleiLogManager.hilogIWithTag(TAG, 'onClick, getValueSync. brightness:' + JSON.stringify(brightness))
break
case '设置系统屏幕亮度':
// 三方应用无权限
settings.setValueSync(this.context, settings.display.SCREEN_BRIGHTNESS_STATUS, '30', settings.domainName.DEVICE_SHARED)
WinsleiLogManager.hilogIWithTag(TAG, 'onClick, setValueSync brightness success')
break
case '创建子窗口并加载hsp页面':
let windowClass: window.Window | undefined = undefined;
let config: window.Configuration = {
name: "mySubWindow",
windowType: window.WindowType.TYPE_DIALOG,
ctx: this.context // context对象也可在组件中通过getContext()方法获取
};
try {
window.createWindow(config, (err: BusinessError, data) => {
const errCode: number = err.code;
if (errCode) {
console.error(`Failed to create the window. Cause code: ${err.code}, message: ${err.message}`);
return;
}
windowClass = data;
console.info('Succeeded in creating the window. Data: ' + JSON.stringify(data));
windowClass.resize(500, 1000).then(() => {
data.setUIContent('@bundle:com.winslei.winsleikit/WinsleiHSP/ets/pages/Test1', () => {
data.showWindow(() => {
})
})
});
});
} catch (exception) {
console.error(`Failed to create the window. Cause code: ${exception.code}, message: ${exception.message}`);
}
break
}
// AppStorage.setOrCreate('showGlobalCustomDialog', true)
})
}
})
}
.columnsTemplate('1fr 1fr 1fr')
.rowsTemplate('1fr 1fr 1fr')
.columnsGap(10)
.rowsGap(10)
.width('100%')
.backgroundColor('#304353')
.height('100%')
.padding(20)
.focusable(true)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/winslei/harmony-os_-winsleikit.git
git@gitee.com:winslei/harmony-os_-winsleikit.git
winslei
harmony-os_-winsleikit
HarmonyOS_Winsleikit
master

搜索帮助