1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import display from '@ohos.display' 17import Logger from '../model/Logger' 18import { ShowInfo } from '../common/ShowInfo' 19import { DisplayModel } from '../model/DisplayModel' 20 21const TAG: string = '[DisplayDevice]' 22 23@Component 24export struct DisplayDevice { 25 @State devices: number[] = [] 26 private screenIds: number[] = [] 27 private allScreens: display.Display[] = [] 28 private defaultScreens: display.Display = undefined 29 private defaultOrAllScreen: boolean = false 30 private displayModel: DisplayModel = new DisplayModel() 31 dialogController: CustomDialogController = new CustomDialogController({ 32 builder: ShowInfo({ 33 defaultScreens: this.defaultScreens, 34 allScreens: this.allScreens, 35 defaultOrAllScreen: this.defaultOrAllScreen 36 }), 37 autoCancel: true, 38 alignment: DialogAlignment.Default 39 }) 40 41 addWatch() { 42 display.on('add', (data) => { 43 if (this.devices.length < 3) { 44 this.devices.push(this.screenIds[this.screenIds.length - 1]) 45 Logger.info(TAG, `this push devices = ${JSON.stringify(this.devices)} this id = ${data}`) 46 } else { 47 Logger.info(TAG, `this screen's number is exceed three`) 48 } 49 }) 50 } 51 52 removeWatch() { 53 display.on('remove', (result) => { 54 this.devices.pop() 55 this.screenIds.pop() 56 Logger.info(TAG, `this pop devices = ${JSON.stringify(this.devices)} this screenIds = ${JSON.stringify(this.screenIds)} result = ${result}`) 57 }) 58 } 59 60 aboutToAppear() { 61 this.addWatch() 62 this.removeWatch() 63 } 64 65 async getDefaultScreen() { 66 this.defaultOrAllScreen = true 67 this.defaultScreens = await display.getDefaultDisplay() 68 Logger.info(TAG, `This getDefaultScreen = ${JSON.stringify(this.defaultScreens)}`) 69 } 70 71 async getAllScreen() { 72 this.defaultOrAllScreen = false 73 this.allScreens = await display.getAllDisplay() 74 Logger.info(TAG, `This getAllScreen = ${JSON.stringify(this.allScreens)}`) 75 } 76 77 build() { 78 Column() { 79 Row() { 80 Rect({ width: '34%', height: '17%' }) 81 .margin({ top: '10%', left: 4, right: 4, bottom: 4 }) 82 .key('bigRect') 83 .fill('#5dbbff') 84 .onClick(async () => { 85 await this.getDefaultScreen() 86 Logger.info(TAG, `defaultOrAllScreen = ${this.defaultOrAllScreen}`) 87 this.dialogController.open() 88 }) 89 90 91 ForEach(this.devices, (item, index) => { 92 Rect({ width: '18%', height: '9%' }) 93 .margin({ top: '10%', left: 4, right: 4, bottom: 4 }) 94 .fill('#5dbbff') 95 .key('smallRect' + (index + 1)) 96 .onClick(async () => { 97 await this.getAllScreen() 98 Logger.info(TAG, `defaultOrAllScreen = ${this.defaultOrAllScreen}`) 99 this.dialogController.open() 100 }) 101 }, item => item.toString()) 102 } 103 .margin({ top: 10, bottom: '8%' }) 104 105 Button($r('app.string.add_screen')) 106 .width('58%') 107 .height('6%') 108 .key('addScreen') 109 .fontSize(18) 110 .margin({ top: 10, left: 4, right: 4, bottom: '8%' }) 111 .onClick(async () => { 112 let result = await this.displayModel.createVirtualScreen(this.screenIds.length) 113 Logger.info(TAG, `this createVirtualScreen screen id = ${JSON.stringify(this.screenIds)}`) 114 this.screenIds.push(result.id) 115 }) 116 117 Button($r('app.string.remove_screen')) 118 .width('58%') 119 .height('6%') 120 .key('removeScreen') 121 .fontSize(18) 122 .margin({ top: 10, left: 4, right: 4, bottom: 4 }) 123 .onClick(async () => { 124 await this.displayModel.destroyVirtualScreen(this.screenIds[this.screenIds.length - 1]) 125 Logger.info(TAG, `this after destroyVirtualScreen screen id = ${JSON.stringify(this.screenIds)}`) 126 }) 127 } 128 } 129} 130