Skip to content

Commit 2554828

Browse files
authored
feat(android): edge to edge cutout inset (#11069)
1 parent e01b52e commit 2554828

File tree

3 files changed

+123
-15
lines changed

3 files changed

+123
-15
lines changed
238 Bytes
Binary file not shown.

packages/core/ui/core/view/index.android.ts

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,15 @@ const INSET_TOP_CONSUMED = 20;
389389
const INSET_RIGHT_CONSUMED = 24;
390390
const INSET_BOTTOM_CONSUMED = 28;
391391
const INSET_BOTTOM_IME_CONSUMED = 36;
392+
const INSET_CUTOUT_LEFT = 40;
393+
const INSET_CUTOUT_TOP = 44;
394+
const INSET_CUTOUT_RIGHT = 48;
395+
const INSET_CUTOUT_BOTTOM = 52;
396+
397+
const INSET_CUTOUT_LEFT_CONSUMED = 56;
398+
const INSET_CUTOUT_TOP_CONSUMED = 60;
399+
const INSET_CUTOUT_RIGHT_CONSUMED = 64;
400+
const INSET_CUTOUT_BOTTOM_CONSUMED = 68;
392401

393402
const OverflowEdgeIgnore = -1;
394403
const OverflowEdgeNone: number = 0;
@@ -446,12 +455,12 @@ class Inset {
446455
this.view.setInt32(INSET_BOTTOM, value, true);
447456
}
448457

449-
public get ime(): number {
458+
public get imeBottom(): number {
450459
return this.view.getInt32(INSET_BOTTOM_IME, true);
451460
}
452461

453-
public set ime(value: number) {
454-
this.view.setInt32(INSET_BOTTOM, value, true);
462+
public set imeBottom(value: number) {
463+
this.view.setInt32(INSET_BOTTOM_IME, value, true);
455464
}
456465

457466
public get leftConsumed(): boolean {
@@ -494,8 +503,72 @@ class Inset {
494503
this.view.setInt32(INSET_BOTTOM_IME_CONSUMED, value ? 1 : 0, true);
495504
}
496505

506+
public get cutoutLeft(): number {
507+
return this.view.getInt32(INSET_CUTOUT_LEFT, true);
508+
}
509+
510+
public set cutoutLeft(value: number) {
511+
this.view.setInt32(INSET_CUTOUT_LEFT, value, true);
512+
}
513+
514+
public get cutoutTop(): number {
515+
return this.view.getInt32(INSET_CUTOUT_TOP, true);
516+
}
517+
518+
public set cutoutTop(value: number) {
519+
this.view.setInt32(INSET_CUTOUT_TOP, value, true);
520+
}
521+
522+
public get cutoutRight(): number {
523+
return this.view.getInt32(INSET_CUTOUT_RIGHT, true);
524+
}
525+
526+
public set cutoutRight(value: number) {
527+
this.view.setInt32(INSET_CUTOUT_RIGHT, value, true);
528+
}
529+
530+
public get cutoutBottom(): number {
531+
return this.view.getInt32(INSET_CUTOUT_BOTTOM, true);
532+
}
533+
534+
public set cutoutBottom(value: number) {
535+
this.view.setInt32(INSET_CUTOUT_BOTTOM, value, true);
536+
}
537+
538+
public get cutoutLeftConsumed(): boolean {
539+
return this.view.getInt32(INSET_CUTOUT_LEFT_CONSUMED, true) > 0;
540+
}
541+
542+
public set cutoutLeftConsumed(value: boolean) {
543+
this.view.setInt32(INSET_CUTOUT_LEFT_CONSUMED, value ? 1 : 0, true);
544+
}
545+
546+
public get cutoutTopConsumed(): boolean {
547+
return this.view.getInt32(INSET_CUTOUT_TOP_CONSUMED, true) > 0;
548+
}
549+
550+
public set cutoutTopConsumed(value: boolean) {
551+
this.view.setInt32(INSET_CUTOUT_TOP_CONSUMED, value ? 1 : 0, true);
552+
}
553+
554+
public get cutoutRightConsumed(): boolean {
555+
return this.view.getInt32(INSET_CUTOUT_RIGHT_CONSUMED, true) > 0;
556+
}
557+
558+
public set cutoutRightConsumed(value: boolean) {
559+
this.view.setInt32(INSET_CUTOUT_RIGHT_CONSUMED, value ? 1 : 0, true);
560+
}
561+
562+
public get cutoutBottomConsumed(): boolean {
563+
return this.view.getInt32(INSET_CUTOUT_BOTTOM_CONSUMED, true) > 0;
564+
}
565+
566+
public set cutoutBottomConsumed(value: boolean) {
567+
this.view.setInt32(INSET_CUTOUT_BOTTOM_CONSUMED, value ? 1 : 0, true);
568+
}
569+
497570
toString() {
498-
return `Inset: left=${this.left}, top=${this.top}, right=${this.right}, bottom=${this.bottom}, ` + `leftConsumed=${this.leftConsumed}, topConsumed=${this.topConsumed}, ` + `rightConsumed=${this.rightConsumed}, bottomConsumed=${this.bottomConsumed}`;
571+
return `Inset: left=${this.left}, top=${this.top}, right=${this.right}, bottom=${this.bottom}, ` + `leftConsumed=${this.leftConsumed}, topConsumed=${this.topConsumed}, ` + `rightConsumed=${this.rightConsumed}, bottomConsumed=${this.bottomConsumed}, ` + `cutoutLeft=${this.cutoutLeft}, cutoutTop=${this.cutoutTop}, cutoutRight=${this.cutoutRight}, cutoutBottom=${this.cutoutBottom}, ` + `cutoutLeftConsumed=${this.cutoutLeftConsumed}, cutoutTopConsumed=${this.cutoutTopConsumed}, ` + `cutoutRightConsumed=${this.cutoutRightConsumed}, cutoutBottomConsumed=${this.cutoutBottomConsumed}`;
499572
}
500573

501574
toJSON() {
@@ -508,6 +581,14 @@ class Inset {
508581
topConsumed: this.topConsumed,
509582
rightConsumed: this.rightConsumed,
510583
bottomConsumed: this.bottomConsumed,
584+
cutoutLeft: this.cutoutLeft,
585+
cutoutTop: this.cutoutTop,
586+
cutoutRight: this.cutoutRight,
587+
cutoutBottom: this.cutoutBottom,
588+
cutoutLeftConsumed: this.cutoutLeftConsumed,
589+
cutoutTopConsumed: this.cutoutTopConsumed,
590+
cutoutRightConsumed: this.cutoutRightConsumed,
591+
cutoutBottomConsumed: this.cutoutBottomConsumed,
511592
};
512593
}
513594
}

packages/ui-mobile-base/android/widgets/src/main/java/org/nativescript/widgets/LayoutBase.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,22 @@ public static final class BufferOffset {
5959
public static final int INSET_BOTTOM = 12;
6060
public static final int INSET_BOTTOM_IME = 32;
6161

62+
public static final int INSET_CUTOUT_LEFT = 40;
63+
public static final int INSET_CUTOUT_TOP = 44;
64+
public static final int INSET_CUTOUT_RIGHT = 48;
65+
public static final int INSET_CUTOUT_BOTTOM = 52;
66+
67+
6268
public static final int INSET_LEFT_CONSUMED = 16;
6369
public static final int INSET_TOP_CONSUMED = 20;
6470
public static final int INSET_RIGHT_CONSUMED = 24;
6571
public static final int INSET_BOTTOM_CONSUMED = 28;
6672
public static final int INSET_BOTTOM_IME_CONSUMED = 36;
73+
74+
public static final int INSET_CUTOUT_LEFT_CONSUMED = 56;
75+
public static final int INSET_CUTOUT_TOP_CONSUMED = 60;
76+
public static final int INSET_CUTOUT_RIGHT_CONSUMED = 64;
77+
public static final int INSET_CUTOUT_BOTTOM_CONSUMED = 68;
6778
}
6879

6980
int mPaddingLeft = 0;
@@ -72,13 +83,21 @@ public static final class BufferOffset {
7283
int mPaddingBottom = 0;
7384

7485
Insets edgeInsets = Insets.NONE;
75-
Insets appliedInsets = Insets.NONE;
7686
Insets imeInsets = Insets.NONE;
7787

78-
7988
int overflowEdge = OverflowEdgeIgnore;
8089

81-
private final ByteBuffer insetBuffer = ByteBuffer.allocateDirect(40);
90+
private static final byte[] EMPTY_INSETS = new byte[72];
91+
92+
private ByteBuffer mInsetBuffer;
93+
94+
private ByteBuffer getInsetBuffer() {
95+
if (mInsetBuffer == null) {
96+
mInsetBuffer = ByteBuffer.allocateDirect(72);
97+
mInsetBuffer.order(ByteOrder.nativeOrder());
98+
}
99+
return mInsetBuffer;
100+
}
82101

83102
private WindowInsetListener insetListener = null;
84103
private androidx.core.view.OnApplyWindowInsetsListener windowInsetsListener = null;
@@ -91,8 +110,6 @@ public interface WindowInsetListener {
91110
void onApplyWindowInsets(ByteBuffer inset);
92111
}
93112

94-
private static final byte[] EMPTY_INSETS = new byte[40];
95-
96113

97114
private boolean pendingInsetApply = false;
98115
private final OnAttachStateChangeListener onAttachStateChangeListener = new OnAttachStateChangeListener() {
@@ -112,7 +129,6 @@ public void onViewDetachedFromWindow(@NonNull View v) {
112129

113130
public LayoutBase(Context context, AttributeSet attrs, int defStyleAttr) {
114131
super(context, attrs, defStyleAttr);
115-
insetBuffer.order(ByteOrder.nativeOrder());
116132
}
117133

118134
public LayoutBase(Context context) {
@@ -222,16 +238,16 @@ public void setPadding(int left, int top, int right, int bottom) {
222238
}
223239

224240
private void putInset(int offset, int value) {
225-
insetBuffer.putInt(offset, value);
241+
getInsetBuffer().putInt(offset, value);
226242
}
227243

228244
private int getInset(int offset) {
229-
return insetBuffer.getInt(offset);
245+
return getInsetBuffer().getInt(offset);
230246
}
231247

232248
private void resetInset() {
233-
insetBuffer.position(0);
234-
insetBuffer.put(EMPTY_INSETS, 0, 40);
249+
getInsetBuffer().position(0);
250+
getInsetBuffer().put(EMPTY_INSETS, 0, EMPTY_INSETS.length);
235251
}
236252

237253
public void setOverflowEdge(int value) {
@@ -254,6 +270,7 @@ public WindowInsetsCompat onApplyWindowInsets(
254270

255271
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
256272
Insets ime = insets.getInsets(WindowInsetsCompat.Type.ime());
273+
Insets cutout = insets.getInsets(WindowInsetsCompat.Type.displayCutout());
257274

258275
if (systemBars == Insets.NONE && ime == Insets.NONE) {
259276
return WindowInsetsCompat.CONSUMED;
@@ -357,14 +374,24 @@ public WindowInsetsCompat onApplyWindowInsets(
357374
putInset(BufferOffset.INSET_BOTTOM, insetNavBottom);
358375
putInset(BufferOffset.INSET_BOTTOM_IME, insetImeBottom);
359376

377+
putInset(BufferOffset.INSET_CUTOUT_LEFT, cutout.left);
378+
putInset(BufferOffset.INSET_CUTOUT_TOP, cutout.top);
379+
putInset(BufferOffset.INSET_CUTOUT_RIGHT, cutout.right);
380+
putInset(BufferOffset.INSET_CUTOUT_BOTTOM, cutout.bottom);
381+
360382
putInset(BufferOffset.INSET_LEFT_CONSUMED, 0);
361383
putInset(BufferOffset.INSET_TOP_CONSUMED, 0);
362384
putInset(BufferOffset.INSET_RIGHT_CONSUMED, 0);
363385
putInset(BufferOffset.INSET_BOTTOM_CONSUMED, 0);
364386
putInset(BufferOffset.INSET_BOTTOM_IME_CONSUMED, 0);
365387

388+
putInset(BufferOffset.INSET_CUTOUT_LEFT_CONSUMED, 0);
389+
putInset(BufferOffset.INSET_CUTOUT_TOP_CONSUMED, 0);
390+
putInset(BufferOffset.INSET_CUTOUT_RIGHT_CONSUMED, 0);
391+
putInset(BufferOffset.INSET_CUTOUT_BOTTOM_CONSUMED, 0);
392+
366393
if (base.insetListener != null) {
367-
base.insetListener.onApplyWindowInsets(insetBuffer);
394+
base.insetListener.onApplyWindowInsets(getInsetBuffer());
368395
}
369396

370397
defaultConsume[0] = defaultConsume[1] = defaultConsume[2] = defaultConsume[3] = false;

0 commit comments

Comments
 (0)