From c542f3d0aec07ff0c46f725d84360c9674c855f8 Mon Sep 17 00:00:00 2001 From: handongxun Date: Tue, 18 Aug 2020 11:13:00 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E6=96=B0=E5=A2=9Euni.onThemeChange?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=EF=BC=8C=E4=BF=9D=E7=95=99=E6=97=A7=E7=89=88?= =?UTF-8?q?=E6=9C=ACuni.onUIStyleChange,call=20app=20onThemeChange?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/apis.js | 3 ++- lib/modules.json | 1 + src/core/service/api/device/theme.js | 11 +++++++++++ src/platforms/app-plus/service/framework/app.js | 14 +++++++++++--- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/apis.js b/lib/apis.js index ca14f0fe..0ac63a6a 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -119,7 +119,8 @@ const device = [ 'stopBeaconDiscovery', 'checkIsSupportSoterAuthentication', 'checkIsSoterEnrolledInDevice', - 'startSoterAuthentication', + 'startSoterAuthentication', + 'onThemeChange', 'onUIStyleChange' ] diff --git a/lib/modules.json b/lib/modules.json index 740b9fa7..b9457b67 100644 --- a/lib/modules.json +++ b/lib/modules.json @@ -121,6 +121,7 @@ "uni.getBeacons": true, "uni.startBeaconDiscovery": true, "uni.stopBeaconDiscovery": true, + "uni.onThemeChange": true, "uni.onUIStyleChange": true } }, { diff --git a/src/core/service/api/device/theme.js b/src/core/service/api/device/theme.js index 2c1adb6e..575d854f 100644 --- a/src/core/service/api/device/theme.js +++ b/src/core/service/api/device/theme.js @@ -8,6 +8,17 @@ import { const callbacks = [] +onMethod('onThemeChange', function (res) { + callbacks.forEach(callbackId => { + invoke(callbackId, res) + }) +}) + +export function onThemeChange (callbackId) { + callbacks.push(callbackId) +} + +// 旧版本 API,后期文档更新后考虑移除 onMethod('onUIStyleChange', function (res) { callbacks.forEach(callbackId => { invoke(callbackId, res) diff --git a/src/platforms/app-plus/service/framework/app.js b/src/platforms/app-plus/service/framework/app.js index 9a59f2f9..755aa1e1 100644 --- a/src/platforms/app-plus/service/framework/app.js +++ b/src/platforms/app-plus/service/framework/app.js @@ -95,9 +95,17 @@ function initGlobalListeners () { }) }) - globalEvent.addEventListener('uistylechange', function (event) { - publish('onUIStyleChange', { - style: event.uistyle + globalEvent.addEventListener('uistylechange', function (event) { + const args = { + theme: event.uistyle + } + + callAppHook(appCtx, 'onThemeChange', args) + publish('onThemeChange', args) + + // 兼容旧版本 API + publish('onUIStyleChange', { + style: event.uistyle }) }) -- GitLab