From f072bbfe19acb4c3779981de74846e9ed37c737b Mon Sep 17 00:00:00 2001 From: yuanxinying Date: Tue, 28 Feb 2023 22:10:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0js-apis-pointer=E5=92=8Cpoint?= =?UTF-8?q?erstyle-guidelines=E6=96=87=E6=A1=A3=E7=A4=BA=E4=BE=8B=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=B8=AD=E8=8E=B7=E5=8F=96windowId=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95=20Signed-off-by:=20yuanxinying=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../device/pointerstyle-guidelines.md | 69 +++++----- .../reference/apis/js-apis-pointer.md | 128 ++++++++++-------- 2 files changed, 105 insertions(+), 92 deletions(-) diff --git a/zh-cn/application-dev/device/pointerstyle-guidelines.md b/zh-cn/application-dev/device/pointerstyle-guidelines.md index 997f375bd7..0f3d451f1a 100644 --- a/zh-cn/application-dev/device/pointerstyle-guidelines.md +++ b/zh-cn/application-dev/device/pointerstyle-guidelines.md @@ -77,43 +77,48 @@ try { 5. 设置鼠标光标样式为默认样式。 ```js +import pointer from '@ohos.multimodalInput.pointer'; import window from '@ohos.window'; // 1.开发者使能取色功能 // 2.调用窗口实例获取对应的窗口id -window.getTopWindow((error, windowClass) => { - windowClass.getProperties((error, data) => { - var windowId = data.id; - if (windowId < 0) { - console.log(`Invalid windowId`); - return; - } - try { - // 3.设置鼠标光标样式为取色器样式 - pointer.setPointerStyle(windowId, pointer.PointerStyle.COLOR_SUCKER).then(() => { - console.log(`Successfully set mouse pointer style`); - }); - } catch (error) { - console.log(`Failed to set the pointer style, error=${JSON.stringify(error)}, msg=${JSON.stringify(message)}`); - } - }); +window.getLastWindow(this.context, (error, windowClass) => { + if (error.code) { + console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(error)); + return; + } + var windowId = windowClass.getWindowProperties().id; + if (windowId < 0) { + console.log(`Invalid windowId`); + return; + } + try { + // 3.设置鼠标光标样式为取色器样式 + pointer.setPointerStyle(windowId, pointer.PointerStyle.COLOR_SUCKER).then(() => { + console.log(`Successfully set mouse pointer style`); + }); + } catch (error) { + console.log(`Failed to set the pointer style, error=${JSON.stringify(error)}, msg=${JSON.stringify(`message`)}`); + } }); // 4.取色结束 -window.getTopWindow((error, windowClass) => { - windowClass.getProperties((error, data) => { - var windowId = data.id; - if (windowId < 0) { - console.log(`Invalid windowId`); - return; - } - try { - // 5.设置鼠标光标样式为默认样式 - pointer.setPointerStyle(windowId, pointer.PointerStyle.DEFAULT).then(() => { - console.log(`Successfully set mouse pointer style`); - }); - } catch (error) { - console.log(`Failed to set the pointer style, error=${JSON.stringify(error)}, msg=${JSON.stringify(message)}`); - } - }); +window.getLastWindow(this.context, (error, windowClass) => { + if (error.code) { + console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(error)); + return; + } + var windowId = windowClass.getWindowProperties().id; + if (windowId < 0) { + console.log(`Invalid windowId`); + return; + } + try { + // 5.设置鼠标光标样式为默认样式 + pointer.setPointerStyle(windowId, pointer.PointerStyle.DEFAULT).then(() => { + console.log(`Successfully set mouse pointer style`); + }); + } catch (error) { + console.log(`Failed to set the pointer style, error=${JSON.stringify(error)}, msg=${JSON.stringify(`message`)}`); + } }); ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-pointer.md b/zh-cn/application-dev/reference/apis/js-apis-pointer.md index c9f96bd2a7..d4fdde6403 100755 --- a/zh-cn/application-dev/reference/apis/js-apis-pointer.md +++ b/zh-cn/application-dev/reference/apis/js-apis-pointer.md @@ -278,21 +278,23 @@ getPointerStyle(windowId: number, callback: AsyncCallback<PointerStyle>): ```js import window from '@ohos.window'; -window.getTopWindow((error, win) => { - win.getWindowProperties((error, properties) => { - let windowId = properties.id; - if (windowId < 0) { - console.log(`Invalid windowId`); - return; - } - try { - pointer.getPointerStyle(windowId, (error, style) => { - console.log(`Get pointer style success, style: ${JSON.stringify(style)}`); - }); - } catch (error) { - console.log(`Get pointer style failed, error: ${JSON.stringify(error, [`code`, `message`])}`); - } - }); +window.getLastWindow(this.context, (error, win) => { + if (error.code) { + console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(error)); + return; + } + let windowId = win.getWindowProperties().id; + if (windowId < 0) { + console.log(`Invalid windowId`); + return; + } + try { + pointer.getPointerStyle(windowId, (error, style) => { + console.log(`Get pointer style success, style: ${JSON.stringify(style)}`); + }); + } catch (error) { + console.log(`Get pointer style failed, error: ${JSON.stringify(error, [`code`, `message`])}`); + } }); ``` @@ -321,21 +323,23 @@ getPointerStyle(windowId: number): Promise<PointerStyle> ```js import window from '@ohos.window'; -window.getTopWindow((error, win) => { - win.getWindowProperties((error, properties) => { - let windowId = properties.id; - if (windowId < 0) { - console.log(`Invalid windowId`); - return; - } - try { - pointer.getPointerStyle(windowId).then((style) => { - console.log(`Get pointer style success, style: ${JSON.stringify(style)}`); - }); - } catch (error) { - console.log(`Get pointer style failed, error: ${JSON.stringify(error, [`code`, `message`])}`); - } - }); +window.getLastWindow(this.context, (error, win) => { + if (error.code) { + console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(error)); + return; + } + let windowId = win.getWindowProperties().id; + if (windowId < 0) { + console.log(`Invalid windowId`); + return; + } + try { + pointer.getPointerStyle(windowId).then((style) => { + console.log(`Get pointer style success, style: ${JSON.stringify(style)}`); + }); + } catch (error) { + console.log(`Get pointer style failed, error: ${JSON.stringify(error, [`code`, `message`])}`); + } }); ``` @@ -360,21 +364,23 @@ setPointerStyle(windowId: number, pointerStyle: PointerStyle, callback: AsyncCal ```js import window from '@ohos.window'; -window.getTopWindow((error, win) => { - win.getWindowProperties((error, properties) => { - let windowId = properties.id; - if (windowId < 0) { - console.log(`Invalid windowId`); - return; - } - try { - pointer.setPointerStyle(windowId, pointer.PointerStyle.CROSS, error => { - console.log(`Set pointer style success`); - }); - } catch (error) { - console.log(`Set pointer style failed, error: ${JSON.stringify(error, [`code`, `message`])}`); - } - }); +window.getLastWindow(this.context, (error, win) => { + if (error.code) { + console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(error)); + return; + } + let windowId = win.getWindowProperties().id; + if (windowId < 0) { + console.log(`Invalid windowId`); + return; + } + try { + pointer.setPointerStyle(windowId, pointer.PointerStyle.CROSS, error => { + console.log(`Set pointer style success`); + }); + } catch (error) { + console.log(`Set pointer style failed, error: ${JSON.stringify(error, [`code`, `message`])}`); + } }); ``` ## pointer.setPointerStyle9+ @@ -398,21 +404,23 @@ setPointerStyle(windowId: number, pointerStyle: PointerStyle): Promise<void&g ```js import window from '@ohos.window'; -window.getTopWindow((error, win) => { - win.getWindowProperties((error, properties) => { - let windowId = properties.id; - if (windowId < 0) { - console.log(`Invalid windowId`); - return; - } - try { - pointer.setPointerStyle(windowId, pointer.PointerStyle.CROSS).then(() => { - console.log(`Set pointer style success`); - }); - } catch (error) { - console.log(`Set pointer style failed, error: ${JSON.stringify(error, [`code`, `message`])}`); - } - }); +window.getLastWindow(this.context, (error, win) => { + if (error.code) { + console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(error)); + return; + } + let windowId = win.getWindowProperties().id; + if (windowId < 0) { + console.log(`Invalid windowId`); + return; + } + try { + pointer.setPointerStyle(windowId, pointer.PointerStyle.CROSS).then(() => { + console.log(`Set pointer style success`); + }); + } catch (error) { + console.log(`Set pointer style failed, error: ${JSON.stringify(error, [`code`, `message`])}`); + } }); ``` ## PointerStyle9+ -- GitLab