From cae6333639e796caecc754b67ff8c9043306764f Mon Sep 17 00:00:00 2001 From: xiaozn Date: Thu, 19 Dec 2024 17:05:57 +0800 Subject: [PATCH 1/6] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DmediaQuery?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=9A=84=E6=95=B0=E6=8D=AE=E4=B8=AD=E5=8C=85?= =?UTF-8?q?=E5=90=AB=E7=9A=84displayFeature=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiaozn --- .../flutter/src/main/ets/view/FlutterView.ets | 141 +++++++++++++++++- 1 file changed, 137 insertions(+), 4 deletions(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets index c8b2fd6742..785e7cbd85 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets @@ -27,6 +27,7 @@ import PlatformView, { Params } from '../plugin/platform/PlatformView'; import { JSON } from '@kit.ArkTS'; import { accessibility } from '@kit.AccessibilityKit'; import TextInputPlugin from '../plugin/editing/TextInputPlugin'; +import { browser } from '@kit.MDMKit'; const TAG = "FlutterViewTag"; @@ -47,6 +48,7 @@ export class ViewportMetrics { systemGestureInsetBottom: number = 0; systemGestureInsetLeft: number = 0; physicalTouchSlop: number = -1; + displayFeatures: ArrayList = new ArrayList(); clone(): ViewportMetrics { const copy = new ViewportMetrics(); @@ -66,6 +68,7 @@ export class ViewportMetrics { copy.systemGestureInsetBottom = this.systemGestureInsetBottom; copy.systemGestureInsetLeft = this.systemGestureInsetLeft; copy.physicalTouchSlop = this.physicalTouchSlop; + copy.displayFeatures = this.displayFeatures; return copy; } @@ -89,6 +92,62 @@ export class ViewportMetrics { } } +export class DisplayFeature { + bounds: display.Rect[]; + type: DisplayFeatureType; + state: DisplayFeatureState; + + constructor(bounds: display.Rect[], type: DisplayFeatureType, state: DisplayFeatureState) { + this.bounds = bounds; + this.type = type; + this.state = state; + } + + getBounds(): display.Rect[] { + return this.bounds; + } + + getType(): DisplayFeatureType { + return this.type; + } + + getState(): DisplayFeatureState { + return this.state + } + + setBounds(bounds: display.Rect[]): void { + this.bounds = bounds; + } + + setType(type: DisplayFeatureType): void { + this.type = type; + } + + setState(state: DisplayFeatureState): void { + this.state = state; + } +} + +export enum DisplayFeatureType{ + UNKNOWN = 0, + FOLD = 1, + HINGE = 2, + CUTOUT = 3 +}; + +export enum DisplayFeatureState{ + UNKNOWN = 0, + POSTURE_FLAT = 1, + POSTURE_HALF_OPENED = 2, +}; + +export enum DisplayFoldStatus{ + FOLD_STATUS_UNKNOWN = 0, + FOLD_STATUS_EXPANDED = 1, + FOLD_STATUS_FOLDED = 2, + FOLD_STATUS_HALF_FOLDED = 3 +}; + export class PlatformViewParas { width: number = 0.0; height: number = 0.0; @@ -144,6 +203,7 @@ export class FlutterView { this.id = viewId this.displayInfo = display.getDefaultDisplaySync(); this.viewportMetrics.devicePixelRatio = this.displayInfo?.densityPixels; + this.buildDisplayFeatures(display.getFoldStatus()); this.mainWindow = FlutterManager.getInstance() .getWindowStage(FlutterManager.getInstance().getUIAbility(context)) .getMainWindowSync(); @@ -164,6 +224,12 @@ export class FlutterView { this.gestureAvoidArea = this.mainWindow?.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM_GESTURE); this.keyboardAvoidArea = this.mainWindow?.getWindowAvoidArea(window.AvoidAreaType.TYPE_KEYBOARD); + // 监听折叠状态的改变 + display?.on('foldStatusChange', (data: display.FoldStatus) => { + Log.d(TAG, `Fold status change to ${JSON.stringify(data)}`) + this.buildDisplayFeatures(data); + }) + // Subscribes to display changes. Example: event that the display size is changed. try { display.on("change", ()=>{ @@ -179,6 +245,50 @@ export class FlutterView { } } + private async buildDisplayFeatures(foldStatus: display.FoldStatus) { + let displayFeatures: ArrayList = new ArrayList(); + let bound: display.Rect[]; + let state = 0; + let type = 0; + const displayInfos = display.getDefaultDisplaySync(); + if (display.isFoldable()) { + switch (foldStatus) { + case DisplayFoldStatus.FOLD_STATUS_FOLDED.valueOf(): + type = DisplayFeatureType.HINGE; + state = DisplayFeatureState.UNKNOWN; + break; + case DisplayFoldStatus.FOLD_STATUS_EXPANDED.valueOf(): + type = DisplayFeatureType.FOLD; + state = DisplayFeatureState.POSTURE_FLAT; + break; + case DisplayFoldStatus.FOLD_STATUS_HALF_FOLDED.valueOf(): + type = DisplayFeatureType.FOLD; + state = DisplayFeatureState.POSTURE_HALF_OPENED; + break; + default: + type = DisplayFeatureType.UNKNOWN; + state = DisplayFeatureState.UNKNOWN; + break; + } + const displays = await display.getAllDisplays(); + for (let i = 0; i < displays.length; i++) { + let cutoutInfo = await displays[i].getCutoutInfo(); + bound = cutoutInfo.boundingRects; + displayFeatures.add(new DisplayFeature(bound, type, state)); + } + Log.d(TAG, `FOLD device displayFeatures is : ${JSON.stringify(displayFeatures)}`) + } else { + type = DisplayFeatureType.UNKNOWN; + state = DisplayFeatureState.UNKNOWN; + let infos = await displayInfos?.getCutoutInfo(); + bound = infos.boundingRects; + displayFeatures.add(new DisplayFeature(bound, type, state)); + Log.d(TAG, `UNFold device displayFeatures is : ${JSON.stringify(displayFeatures)}`) + } + this.viewportMetrics.displayFeatures = displayFeatures; + this.updateViewportMetrics(); + } + private windowSizeChangeCallback = (data: window.Size) => { Log.i(TAG, "windowSizeChangeCallback w:" + data.width + ", h:" + data.height); if (this.isAttachedToFlutterEngine()) { @@ -213,7 +323,7 @@ export class FlutterView { Log.i(TAG, "keyboardHeightChangeCallback " + data); this.keyboardAvoidArea.bottomRect.height = data; if (this.isAttachedToFlutterEngine()) { - this.onAreaChange(null); + this.onAreaChange(null); } }; @@ -270,6 +380,9 @@ export class FlutterView { Log.i(TAG, `unsubscribe accessibility state change, result: ${JSON.stringify(data)}`); }); this.mainWindow?.off('keyboardHeightChange', this.keyboardHeightChangeCallback); + display.off('foldStatusChange', (status: display.FoldStatus) => { + Log.d(TAG, `foldStatusChange listner destory`); + }) } catch (e) { Log.e(TAG, "mainWindow off error: " + JSON.stringify(e)); } @@ -444,6 +557,26 @@ export class FlutterView { if (this.isAttachedToFlutterEngine()) { Log.i(TAG, 'updateViewportMetrics devicePixelRatio:' + this.viewportMetrics.devicePixelRatio); + const displayFeatures = this.viewportMetrics.displayFeatures; + let boundCount = 0; + for (let i = 0; i < displayFeatures.length; i++) { + boundCount = boundCount + displayFeatures[i].getBounds().length; + } + let displayFeatureBound: number[] = new Array(boundCount * 4); + let displayFeatureType: number[] = new Array(displayFeatures.length); + let displayFeatureStatus: number[] = new Array(displayFeatures.length); + for (let i = 0; i < displayFeatures.length; i++) { + let singleFeatureBound = displayFeatures[i].getBounds(); + for (let j = 0; j < singleFeatureBound.length; j++) { + displayFeatureBound[4*i + 4*j] = singleFeatureBound[j].left; + displayFeatureBound[4*i + 4*j + 1] = singleFeatureBound[j].top + displayFeatureBound[4*i + 4*j + 2] = singleFeatureBound[j].width; + displayFeatureBound[4*i + 4*j + 3] = singleFeatureBound[j].height; + } + displayFeatureType[i] = displayFeatures[i].getType(); + displayFeatureStatus[i] = displayFeatures[i].getState(); + } + this?.flutterEngine?.getFlutterNapi()?.setViewportMetrics(this.viewportMetrics.devicePixelRatio, this.viewportMetrics.physicalWidth, this.viewportMetrics.physicalHeight, @@ -460,9 +593,9 @@ export class FlutterView { this.viewportMetrics.systemGestureInsetBottom, this.viewportMetrics.systemGestureInsetLeft, this.viewportMetrics.physicalTouchSlop, - new Array(0), - new Array(0), - new Array(0)) + displayFeatureBound, + displayFeatureType, + displayFeatureStatus) return true; } return false; -- Gitee From 5f0693fabec1cf1ededcf16eb8774a799e5a2c69 Mon Sep 17 00:00:00 2001 From: xiaozn Date: Thu, 19 Dec 2024 17:09:18 +0800 Subject: [PATCH 2/6] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DmediaQuery?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=9A=84=E6=95=B0=E6=8D=AE=E4=B8=AD=E5=8C=85?= =?UTF-8?q?=E5=90=AB=E7=9A=84displayFeature=E6=95=B0=E6=8D=AE=EF=BC=8C?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E6=95=88import?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiaozn --- .../flutter_embedding/flutter/src/main/ets/view/FlutterView.ets | 1 - 1 file changed, 1 deletion(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets index 785e7cbd85..c974a8fd92 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets @@ -27,7 +27,6 @@ import PlatformView, { Params } from '../plugin/platform/PlatformView'; import { JSON } from '@kit.ArkTS'; import { accessibility } from '@kit.AccessibilityKit'; import TextInputPlugin from '../plugin/editing/TextInputPlugin'; -import { browser } from '@kit.MDMKit'; const TAG = "FlutterViewTag"; -- Gitee From 0930c1645a435a519aa797ed650326f8f10b91a9 Mon Sep 17 00:00:00 2001 From: xiaozn Date: Mon, 23 Dec 2024 19:27:02 +0800 Subject: [PATCH 3/6] =?UTF-8?q?fix:=20displayFeature=E5=B0=86=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E6=95=B4=E4=B8=AA=E9=A1=B5=E9=9D=A2=E7=9A=84=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=EF=BC=8C=E5=8C=85=E6=8B=AC=E6=8A=98=E5=8F=A0=E5=8C=BA?= =?UTF-8?q?=E5=9F=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiaozn --- .../flutter/src/main/ets/view/FlutterView.ets | 171 +++++++++--------- 1 file changed, 86 insertions(+), 85 deletions(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets index c974a8fd92..84c8751741 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets @@ -27,6 +27,9 @@ import PlatformView, { Params } from '../plugin/platform/PlatformView'; import { JSON } from '@kit.ArkTS'; import { accessibility } from '@kit.AccessibilityKit'; import TextInputPlugin from '../plugin/editing/TextInputPlugin'; +import { cert } from '@kit.DeviceCertificateKit'; +import json from '@ohos.util.json'; +import { data } from '@kit.TelephonyKit'; const TAG = "FlutterViewTag"; @@ -47,7 +50,7 @@ export class ViewportMetrics { systemGestureInsetBottom: number = 0; systemGestureInsetLeft: number = 0; physicalTouchSlop: number = -1; - displayFeatures: ArrayList = new ArrayList(); + displayFeatures: DisplayFeature[] = []; clone(): ViewportMetrics { const copy = new ViewportMetrics(); @@ -92,17 +95,17 @@ export class ViewportMetrics { } export class DisplayFeature { - bounds: display.Rect[]; + bounds: display.Rect; type: DisplayFeatureType; state: DisplayFeatureState; - constructor(bounds: display.Rect[], type: DisplayFeatureType, state: DisplayFeatureState) { + constructor(bounds: display.Rect, type: DisplayFeatureType, state: DisplayFeatureState = DisplayFeatureState.UNKNOWN) { this.bounds = bounds; this.type = type; this.state = state; } - getBounds(): display.Rect[] { + getBounds(): display.Rect { return this.bounds; } @@ -114,7 +117,7 @@ export class DisplayFeature { return this.state } - setBounds(bounds: display.Rect[]): void { + setBounds(bounds: display.Rect): void { this.bounds = bounds; } @@ -196,13 +199,15 @@ export class FlutterView { private gestureAvoidArea: window.AvoidArea; private keyboardAvoidArea: window.AvoidArea; private needSetViewport: boolean = false; + private displayFeaturesCache: Map> + = new Map>(); constructor(viewId: string, context: Context) { this.id = viewId this.displayInfo = display.getDefaultDisplaySync(); this.viewportMetrics.devicePixelRatio = this.displayInfo?.densityPixels; - this.buildDisplayFeatures(display.getFoldStatus()); + this.mainWindow = FlutterManager.getInstance() .getWindowStage(FlutterManager.getInstance().getUIAbility(context)) .getMainWindowSync(); @@ -223,69 +228,77 @@ export class FlutterView { this.gestureAvoidArea = this.mainWindow?.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM_GESTURE); this.keyboardAvoidArea = this.mainWindow?.getWindowAvoidArea(window.AvoidAreaType.TYPE_KEYBOARD); - // 监听折叠状态的改变 - display?.on('foldStatusChange', (data: display.FoldStatus) => { - Log.d(TAG, `Fold status change to ${JSON.stringify(data)}`) - this.buildDisplayFeatures(data); - }) - // Subscribes to display changes. Example: event that the display size is changed. try { - display.on("change", ()=>{ - this.displayInfo = display.getDefaultDisplaySync(); - if (this.viewportMetrics.devicePixelRatio != this.displayInfo?.densityPixels) { - this.viewportMetrics.devicePixelRatio = this.displayInfo?.densityPixels; - Log.i(TAG, "Display on: " + JSON.stringify(this.displayInfo)) - this.updateViewportMetrics() - } - }); + display.on("change", this.displayChangeCallback); } catch (e) { Log.e(TAG, "displayInfo error" + JSON.stringify(e)); } } - private async buildDisplayFeatures(foldStatus: display.FoldStatus) { - let displayFeatures: ArrayList = new ArrayList(); - let bound: display.Rect[]; - let state = 0; - let type = 0; - const displayInfos = display.getDefaultDisplaySync(); - if (display.isFoldable()) { - switch (foldStatus) { - case DisplayFoldStatus.FOLD_STATUS_FOLDED.valueOf(): - type = DisplayFeatureType.HINGE; - state = DisplayFeatureState.UNKNOWN; - break; - case DisplayFoldStatus.FOLD_STATUS_EXPANDED.valueOf(): - type = DisplayFeatureType.FOLD; - state = DisplayFeatureState.POSTURE_FLAT; - break; - case DisplayFoldStatus.FOLD_STATUS_HALF_FOLDED.valueOf(): - type = DisplayFeatureType.FOLD; - state = DisplayFeatureState.POSTURE_HALF_OPENED; - break; - default: - type = DisplayFeatureType.UNKNOWN; - state = DisplayFeatureState.UNKNOWN; - break; + private displayChangeCallback = async (data: number) => { + this.displayInfo = display.getDefaultDisplaySync(); + if (this.viewportMetrics.devicePixelRatio != this.displayInfo?.densityPixels) { + this.viewportMetrics.devicePixelRatio = this.displayInfo?.densityPixels; + let displayFeatures: Array = await this.buildDisplayFeatures(); + Log.i(TAG, "Display on: " + JSON.stringify(this.displayInfo)) + this.viewportMetrics.displayFeatures = displayFeatures; + this.updateViewportMetrics() + } + } + + private async buildDisplayFeatures(): Promise> { + let displayMode: display.FoldDisplayMode = display.getFoldDisplayMode(); + if (this.displayFeaturesCache.has(displayMode)) { + return this.displayFeaturesCache.get(displayMode)!; + } + let displayFeatures: Array = []; + let foldCreaseRegion: display.FoldCreaseRegion = display.getCurrentFoldCreaseRegion(); + if (foldCreaseRegion != undefined) { + let type: DisplayFeatureType = DisplayFeatureType.FOLD; + let state: DisplayFeatureState = DisplayFeatureState.UNKNOWN; + + if (displayMode == display.FoldDisplayMode.FOLD_DISPLAY_MODE_FULL || + displayMode == display.FoldDisplayMode.FOLD_DISPLAY_MODE_COORDINATION) { + state = DisplayFeatureState.POSTURE_FLAT; + } else if (displayMode == display.FoldDisplayMode.FOLD_DISPLAY_MODE_MAIN + || displayMode == display.FoldDisplayMode.FOLD_DISPLAY_MODE_SUB) { + state = DisplayFeatureState.POSTURE_HALF_OPENED; } - const displays = await display.getAllDisplays(); - for (let i = 0; i < displays.length; i++) { - let cutoutInfo = await displays[i].getCutoutInfo(); - bound = cutoutInfo.boundingRects; - displayFeatures.add(new DisplayFeature(bound, type, state)); + foldCreaseRegion.creaseRects.forEach((rect: display.Rect) => { + displayFeatures.push(new DisplayFeature(rect, type, state)); + }); + } + + let cutoutInfo: display.CutoutInfo | undefined = await this.displayInfo?.getCutoutInfo(); + if (cutoutInfo != undefined) { + cutoutInfo.boundingRects.forEach((bounding: display.Rect) => { + displayFeatures.push(new DisplayFeature(bounding, DisplayFeatureType.CUTOUT)); + }); + + let waterFallRect: display.WaterfallDisplayAreaRects = cutoutInfo.waterfallDisplayAreaRects; + if (!this.isEmptyRect(waterFallRect.left)) { + displayFeatures.push(new DisplayFeature(waterFallRect.left, DisplayFeatureType.FOLD)); } - Log.d(TAG, `FOLD device displayFeatures is : ${JSON.stringify(displayFeatures)}`) - } else { - type = DisplayFeatureType.UNKNOWN; - state = DisplayFeatureState.UNKNOWN; - let infos = await displayInfos?.getCutoutInfo(); - bound = infos.boundingRects; - displayFeatures.add(new DisplayFeature(bound, type, state)); - Log.d(TAG, `UNFold device displayFeatures is : ${JSON.stringify(displayFeatures)}`) + if (!this.isEmptyRect(waterFallRect.right)) { + displayFeatures.push(new DisplayFeature(waterFallRect.right, DisplayFeatureType.FOLD)); + } + if (!this.isEmptyRect(waterFallRect.top)) { + displayFeatures.push(new DisplayFeature(waterFallRect.top, DisplayFeatureType.FOLD)); + } + if (!this.isEmptyRect(waterFallRect.bottom)) { + displayFeatures.push(new DisplayFeature(waterFallRect.bottom, DisplayFeatureType.FOLD)); + } + } + this.displayFeaturesCache.set(displayMode, displayFeatures); + return displayFeatures; + } + + private isEmptyRect(rect: display.Rect | null | undefined) { + if (rect == null || rect == undefined) { + return true } - this.viewportMetrics.displayFeatures = displayFeatures; - this.updateViewportMetrics(); + return rect.left == 0 && rect.top ==0 && rect.width == 0 && rect.height == 0; } private windowSizeChangeCallback = (data: window.Size) => { @@ -388,12 +401,7 @@ export class FlutterView { this.mainWindow = null; try { - display.off("change", ()=>{ - this.displayInfo = display.getDefaultDisplaySync(); - this.viewportMetrics.devicePixelRatio = this.displayInfo?.densityPixels; - Log.i(TAG, "Display Info: " + JSON.stringify(this.displayInfo)) - this.updateViewportMetrics() - }); + display.off("change", this.displayChangeCallback); } catch (e) { Log.e(TAG, "displayInfo off error" + JSON.stringify(e)); } @@ -555,27 +563,20 @@ export class FlutterView { private updateViewportMetrics(): boolean { if (this.isAttachedToFlutterEngine()) { Log.i(TAG, 'updateViewportMetrics devicePixelRatio:' + this.viewportMetrics.devicePixelRatio); - - const displayFeatures = this.viewportMetrics.displayFeatures; - let boundCount = 0; - for (let i = 0; i < displayFeatures.length; i++) { - boundCount = boundCount + displayFeatures[i].getBounds().length; - } - let displayFeatureBound: number[] = new Array(boundCount * 4); - let displayFeatureType: number[] = new Array(displayFeatures.length); - let displayFeatureStatus: number[] = new Array(displayFeatures.length); - for (let i = 0; i < displayFeatures.length; i++) { - let singleFeatureBound = displayFeatures[i].getBounds(); - for (let j = 0; j < singleFeatureBound.length; j++) { - displayFeatureBound[4*i + 4*j] = singleFeatureBound[j].left; - displayFeatureBound[4*i + 4*j + 1] = singleFeatureBound[j].top - displayFeatureBound[4*i + 4*j + 2] = singleFeatureBound[j].width; - displayFeatureBound[4*i + 4*j + 3] = singleFeatureBound[j].height; - } - displayFeatureType[i] = displayFeatures[i].getType(); - displayFeatureStatus[i] = displayFeatures[i].getState(); + let disPlayFeature = this.viewportMetrics.displayFeatures; + let displayFeatureBound: number[] = new Array(disPlayFeature.length * 4); + let displayFeatureType: number[] = new Array(disPlayFeature.length); + let displayFeatureStatus: number[] = new Array(disPlayFeature.length); + + for (let i = 0; i < disPlayFeature.length; i++) { + displayFeatureBound[4 * i] = disPlayFeature[i].bounds.left; + displayFeatureBound[4 * i + 1] = disPlayFeature[i].bounds.top; + displayFeatureBound[4 * i + 2] = disPlayFeature[i].bounds.width; + displayFeatureBound[4 * i + 3] = disPlayFeature[i].bounds.height; + displayFeatureType[i] = disPlayFeature[i].type; + displayFeatureStatus[i] = disPlayFeature[i].state; } - + Log.d(TAG, 'update displayFeatureBound is:' + json.stringify(disPlayFeature)); this?.flutterEngine?.getFlutterNapi()?.setViewportMetrics(this.viewportMetrics.devicePixelRatio, this.viewportMetrics.physicalWidth, this.viewportMetrics.physicalHeight, -- Gitee From 1215ad478550277ef4bacd4dca98b73636d003d8 Mon Sep 17 00:00:00 2001 From: xiaozn Date: Mon, 23 Dec 2024 23:39:42 +0800 Subject: [PATCH 4/6] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiaozn --- .../flutter/src/main/ets/view/FlutterView.ets | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets index 84c8751741..5320925562 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + import FlutterEngine from '../embedding/engine/FlutterEngine'; import Log from '../util/Log'; import { DVModel, DVModelChildren, DVModelEvents, DVModelParameters } from './DynamicView/dynamicView'; @@ -27,9 +28,7 @@ import PlatformView, { Params } from '../plugin/platform/PlatformView'; import { JSON } from '@kit.ArkTS'; import { accessibility } from '@kit.AccessibilityKit'; import TextInputPlugin from '../plugin/editing/TextInputPlugin'; -import { cert } from '@kit.DeviceCertificateKit'; import json from '@ohos.util.json'; -import { data } from '@kit.TelephonyKit'; const TAG = "FlutterViewTag"; @@ -202,7 +201,6 @@ export class FlutterView { private displayFeaturesCache: Map> = new Map>(); - constructor(viewId: string, context: Context) { this.id = viewId this.displayInfo = display.getDefaultDisplaySync(); @@ -238,12 +236,18 @@ export class FlutterView { private displayChangeCallback = async (data: number) => { this.displayInfo = display.getDefaultDisplaySync(); - if (this.viewportMetrics.devicePixelRatio != this.displayInfo?.densityPixels) { - this.viewportMetrics.devicePixelRatio = this.displayInfo?.densityPixels; - let displayFeatures: Array = await this.buildDisplayFeatures(); + let displayFeatures: Array = await this.buildDisplayFeatures(); + let devicePixelRatio: number = this.displayInfo?.densityPixels; + let physicalTouchSlop: number = 1.0 * this.displayInfo?.densityPixels; + + if (devicePixelRatio != this.viewportMetrics.devicePixelRatio || + physicalTouchSlop != this.viewportMetrics.physicalTouchSlop || + displayFeatures != this.viewportMetrics.displayFeatures) { + this.viewportMetrics.devicePixelRatio = devicePixelRatio; + this.viewportMetrics.physicalTouchSlop = physicalTouchSlop; Log.i(TAG, "Display on: " + JSON.stringify(this.displayInfo)) this.viewportMetrics.displayFeatures = displayFeatures; - this.updateViewportMetrics() + // this.updateViewportMetrics() } } @@ -392,9 +396,7 @@ export class FlutterView { Log.i(TAG, `unsubscribe accessibility state change, result: ${JSON.stringify(data)}`); }); this.mainWindow?.off('keyboardHeightChange', this.keyboardHeightChangeCallback); - display.off('foldStatusChange', (status: display.FoldStatus) => { - Log.d(TAG, `foldStatusChange listner destory`); - }) + } catch (e) { Log.e(TAG, "mainWindow off error: " + JSON.stringify(e)); } -- Gitee From 2e8c734bf528933c2ec27530591cb1644b1613b3 Mon Sep 17 00:00:00 2001 From: shijie Date: Tue, 24 Dec 2024 16:58:54 +0800 Subject: [PATCH 5/6] =?UTF-8?q?fix:=20=E5=9B=9E=E9=80=80displayFeature?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=9A=84=E8=AE=BE=E5=A4=87=E4=BF=A1=E6=81=AF?= =?UTF-8?q?,=E8=A7=A3=E5=86=B3=E8=BF=94=E5=9B=9E=E6=8A=98=E7=97=95?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E5=AF=BC=E8=87=B4=E5=BC=B9=E7=AA=97=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=E5=81=8F=E9=A1=B6=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiaozn --- .../flutter/src/main/ets/view/FlutterView.ets | 183 +++++++++--------- 1 file changed, 91 insertions(+), 92 deletions(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets index 5320925562..9e8e971fdd 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets @@ -28,7 +28,6 @@ import PlatformView, { Params } from '../plugin/platform/PlatformView'; import { JSON } from '@kit.ArkTS'; import { accessibility } from '@kit.AccessibilityKit'; import TextInputPlugin from '../plugin/editing/TextInputPlugin'; -import json from '@ohos.util.json'; const TAG = "FlutterViewTag"; @@ -49,7 +48,7 @@ export class ViewportMetrics { systemGestureInsetBottom: number = 0; systemGestureInsetLeft: number = 0; physicalTouchSlop: number = -1; - displayFeatures: DisplayFeature[] = []; + displayFeatures: ArrayList = new ArrayList(); clone(): ViewportMetrics { const copy = new ViewportMetrics(); @@ -89,22 +88,23 @@ export class ViewportMetrics { this.systemGestureInsetRight === other.systemGestureInsetRight && this.systemGestureInsetBottom === other.systemGestureInsetBottom && this.systemGestureInsetLeft === other.systemGestureInsetLeft && - this.physicalTouchSlop === other.physicalTouchSlop; + this.physicalTouchSlop === other.physicalTouchSlop && + this.displayFeatures == other.displayFeatures; } } export class DisplayFeature { - bounds: display.Rect; + bounds: display.Rect[]; type: DisplayFeatureType; state: DisplayFeatureState; - constructor(bounds: display.Rect, type: DisplayFeatureType, state: DisplayFeatureState = DisplayFeatureState.UNKNOWN) { + constructor(bounds: display.Rect[], type: DisplayFeatureType, state: DisplayFeatureState) { this.bounds = bounds; this.type = type; this.state = state; } - getBounds(): display.Rect { + getBounds(): display.Rect[] { return this.bounds; } @@ -116,7 +116,7 @@ export class DisplayFeature { return this.state } - setBounds(bounds: display.Rect): void { + setBounds(bounds: display.Rect[]): void { this.bounds = bounds; } @@ -198,14 +198,13 @@ export class FlutterView { private gestureAvoidArea: window.AvoidArea; private keyboardAvoidArea: window.AvoidArea; private needSetViewport: boolean = false; - private displayFeaturesCache: Map> - = new Map>(); + constructor(viewId: string, context: Context) { this.id = viewId this.displayInfo = display.getDefaultDisplaySync(); this.viewportMetrics.devicePixelRatio = this.displayInfo?.densityPixels; - + this.buildDisplayFeatures(display.getFoldStatus()); this.mainWindow = FlutterManager.getInstance() .getWindowStage(FlutterManager.getInstance().getUIAbility(context)) .getMainWindowSync(); @@ -226,83 +225,69 @@ export class FlutterView { this.gestureAvoidArea = this.mainWindow?.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM_GESTURE); this.keyboardAvoidArea = this.mainWindow?.getWindowAvoidArea(window.AvoidAreaType.TYPE_KEYBOARD); + // 监听折叠状态的改变 + display?.on('foldStatusChange', (data: display.FoldStatus) => { + Log.d(TAG, `Fold status change to ${JSON.stringify(data)}`) + this.buildDisplayFeatures(data); + }) + // Subscribes to display changes. Example: event that the display size is changed. try { - display.on("change", this.displayChangeCallback); + display.on("change", ()=>{ + this.displayInfo = display.getDefaultDisplaySync(); + if (this.viewportMetrics.devicePixelRatio != this.displayInfo?.densityPixels) { + this.viewportMetrics.devicePixelRatio = this.displayInfo?.densityPixels; + Log.i(TAG, "Display on: " + JSON.stringify(this.displayInfo)) + this.updateViewportMetrics() + } + }); } catch (e) { Log.e(TAG, "displayInfo error" + JSON.stringify(e)); } } - private displayChangeCallback = async (data: number) => { - this.displayInfo = display.getDefaultDisplaySync(); - let displayFeatures: Array = await this.buildDisplayFeatures(); - let devicePixelRatio: number = this.displayInfo?.densityPixels; - let physicalTouchSlop: number = 1.0 * this.displayInfo?.densityPixels; - - if (devicePixelRatio != this.viewportMetrics.devicePixelRatio || - physicalTouchSlop != this.viewportMetrics.physicalTouchSlop || - displayFeatures != this.viewportMetrics.displayFeatures) { - this.viewportMetrics.devicePixelRatio = devicePixelRatio; - this.viewportMetrics.physicalTouchSlop = physicalTouchSlop; - Log.i(TAG, "Display on: " + JSON.stringify(this.displayInfo)) - this.viewportMetrics.displayFeatures = displayFeatures; - // this.updateViewportMetrics() - } - } - - private async buildDisplayFeatures(): Promise> { - let displayMode: display.FoldDisplayMode = display.getFoldDisplayMode(); - if (this.displayFeaturesCache.has(displayMode)) { - return this.displayFeaturesCache.get(displayMode)!; - } - let displayFeatures: Array = []; - let foldCreaseRegion: display.FoldCreaseRegion = display.getCurrentFoldCreaseRegion(); - if (foldCreaseRegion != undefined) { - let type: DisplayFeatureType = DisplayFeatureType.FOLD; - let state: DisplayFeatureState = DisplayFeatureState.UNKNOWN; - - if (displayMode == display.FoldDisplayMode.FOLD_DISPLAY_MODE_FULL || - displayMode == display.FoldDisplayMode.FOLD_DISPLAY_MODE_COORDINATION) { - state = DisplayFeatureState.POSTURE_FLAT; - } else if (displayMode == display.FoldDisplayMode.FOLD_DISPLAY_MODE_MAIN - || displayMode == display.FoldDisplayMode.FOLD_DISPLAY_MODE_SUB) { - state = DisplayFeatureState.POSTURE_HALF_OPENED; + private async buildDisplayFeatures(foldStatus: display.FoldStatus) { + let displayFeatures: ArrayList = new ArrayList(); + let bound: display.Rect[]; + let state: DisplayFeatureState = DisplayFeatureState.UNKNOWN; + let type: DisplayFeatureType = DisplayFeatureType.FOLD; + const displayInfos = display.getDefaultDisplaySync(); + if (display.isFoldable()) { + switch (foldStatus) { + case display.FoldStatus.FOLD_STATUS_EXPANDED: + type = DisplayFeatureType.FOLD; + state = DisplayFeatureState.POSTURE_FLAT; + break; + case display.FoldStatus.FOLD_STATUS_HALF_FOLDED: + type = DisplayFeatureType.FOLD; + state = DisplayFeatureState.POSTURE_HALF_OPENED; + break; + case display.FoldStatus.FOLD_STATUS_UNKNOWN: + type = DisplayFeatureType.UNKNOWN; + state = DisplayFeatureState.UNKNOWN; + break; + default: + state = DisplayFeatureState.UNKNOWN; + type = DisplayFeatureType.FOLD; + break; } - foldCreaseRegion.creaseRects.forEach((rect: display.Rect) => { - displayFeatures.push(new DisplayFeature(rect, type, state)); - }); - } - - let cutoutInfo: display.CutoutInfo | undefined = await this.displayInfo?.getCutoutInfo(); - if (cutoutInfo != undefined) { - cutoutInfo.boundingRects.forEach((bounding: display.Rect) => { - displayFeatures.push(new DisplayFeature(bounding, DisplayFeatureType.CUTOUT)); - }); - - let waterFallRect: display.WaterfallDisplayAreaRects = cutoutInfo.waterfallDisplayAreaRects; - if (!this.isEmptyRect(waterFallRect.left)) { - displayFeatures.push(new DisplayFeature(waterFallRect.left, DisplayFeatureType.FOLD)); - } - if (!this.isEmptyRect(waterFallRect.right)) { - displayFeatures.push(new DisplayFeature(waterFallRect.right, DisplayFeatureType.FOLD)); - } - if (!this.isEmptyRect(waterFallRect.top)) { - displayFeatures.push(new DisplayFeature(waterFallRect.top, DisplayFeatureType.FOLD)); - } - if (!this.isEmptyRect(waterFallRect.bottom)) { - displayFeatures.push(new DisplayFeature(waterFallRect.bottom, DisplayFeatureType.FOLD)); + const displays = await display.getAllDisplays(); + for (let i = 0; i < displays.length; i++) { + let cutoutInfo = await displays[i].getCutoutInfo(); + bound = cutoutInfo.boundingRects; + displayFeatures.add(new DisplayFeature(bound, type, state)); } + Log.d(TAG, `FOLD device displayFeatures is : ${JSON.stringify(displayFeatures)}`) + } else { + type = DisplayFeatureType.CUTOUT; + state = DisplayFeatureState.UNKNOWN; + let infos = await displayInfos?.getCutoutInfo(); + bound = infos.boundingRects; + displayFeatures.add(new DisplayFeature(bound, type, state)); + Log.d(TAG, `UNFold device displayFeatures is : ${JSON.stringify(displayFeatures)}`) } - this.displayFeaturesCache.set(displayMode, displayFeatures); - return displayFeatures; - } - - private isEmptyRect(rect: display.Rect | null | undefined) { - if (rect == null || rect == undefined) { - return true - } - return rect.left == 0 && rect.top ==0 && rect.width == 0 && rect.height == 0; + this.viewportMetrics.displayFeatures = displayFeatures; + this.updateViewportMetrics(); } private windowSizeChangeCallback = (data: window.Size) => { @@ -396,14 +381,21 @@ export class FlutterView { Log.i(TAG, `unsubscribe accessibility state change, result: ${JSON.stringify(data)}`); }); this.mainWindow?.off('keyboardHeightChange', this.keyboardHeightChangeCallback); - + display.off('foldStatusChange', (status: display.FoldStatus) => { + Log.d(TAG, `foldStatusChange listner destory`); + }) } catch (e) { Log.e(TAG, "mainWindow off error: " + JSON.stringify(e)); } this.mainWindow = null; try { - display.off("change", this.displayChangeCallback); + display.off("change", ()=>{ + this.displayInfo = display.getDefaultDisplaySync(); + this.viewportMetrics.devicePixelRatio = this.displayInfo?.densityPixels; + Log.i(TAG, "Display Info: " + JSON.stringify(this.displayInfo)) + this.updateViewportMetrics() + }); } catch (e) { Log.e(TAG, "displayInfo off error" + JSON.stringify(e)); } @@ -565,20 +557,27 @@ export class FlutterView { private updateViewportMetrics(): boolean { if (this.isAttachedToFlutterEngine()) { Log.i(TAG, 'updateViewportMetrics devicePixelRatio:' + this.viewportMetrics.devicePixelRatio); - let disPlayFeature = this.viewportMetrics.displayFeatures; - let displayFeatureBound: number[] = new Array(disPlayFeature.length * 4); - let displayFeatureType: number[] = new Array(disPlayFeature.length); - let displayFeatureStatus: number[] = new Array(disPlayFeature.length); - - for (let i = 0; i < disPlayFeature.length; i++) { - displayFeatureBound[4 * i] = disPlayFeature[i].bounds.left; - displayFeatureBound[4 * i + 1] = disPlayFeature[i].bounds.top; - displayFeatureBound[4 * i + 2] = disPlayFeature[i].bounds.width; - displayFeatureBound[4 * i + 3] = disPlayFeature[i].bounds.height; - displayFeatureType[i] = disPlayFeature[i].type; - displayFeatureStatus[i] = disPlayFeature[i].state; + + const displayFeatures = this.viewportMetrics.displayFeatures; + let boundCount = 0; + for (let i = 0; i < displayFeatures.length; i++) { + boundCount = boundCount + displayFeatures[i].getBounds().length; + } + let displayFeatureBound: number[] = new Array(boundCount * 4); + let displayFeatureType: number[] = new Array(displayFeatures.length); + let displayFeatureStatus: number[] = new Array(displayFeatures.length); + for (let i = 0; i < displayFeatures.length; i++) { + let singleFeatureBound = displayFeatures[i].getBounds(); + for (let j = 0; j < singleFeatureBound.length; j++) { + displayFeatureBound[4*i + 4*j] = singleFeatureBound[j].left; + displayFeatureBound[4*i + 4*j + 1] = singleFeatureBound[j].top + displayFeatureBound[4*i + 4*j + 2] = singleFeatureBound[j].width; + displayFeatureBound[4*i + 4*j + 3] = singleFeatureBound[j].height; + } + displayFeatureType[i] = displayFeatures[i].getType(); + displayFeatureStatus[i] = displayFeatures[i].getState(); } - Log.d(TAG, 'update displayFeatureBound is:' + json.stringify(disPlayFeature)); + this?.flutterEngine?.getFlutterNapi()?.setViewportMetrics(this.viewportMetrics.devicePixelRatio, this.viewportMetrics.physicalWidth, this.viewportMetrics.physicalHeight, -- Gitee From b4649b42a18c6b4be297e43353c12d5724956ec6 Mon Sep 17 00:00:00 2001 From: shijie Date: Wed, 25 Dec 2024 15:50:38 +0800 Subject: [PATCH 6/6] =?UTF-8?q?fix:=20=E5=9B=9E=E9=80=80displayFeature?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=9A=84=E8=AE=BE=E5=A4=87=E4=BF=A1=E6=81=AF?= =?UTF-8?q?,=E4=BF=AE=E6=AD=A3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiaozn --- .../flutter_embedding/flutter/src/main/ets/view/FlutterView.ets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets index 9e8e971fdd..012c7608f9 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets @@ -89,7 +89,7 @@ export class ViewportMetrics { this.systemGestureInsetBottom === other.systemGestureInsetBottom && this.systemGestureInsetLeft === other.systemGestureInsetLeft && this.physicalTouchSlop === other.physicalTouchSlop && - this.displayFeatures == other.displayFeatures; + this.displayFeatures === other.displayFeatures; } } -- Gitee