-
Notifications
You must be signed in to change notification settings - Fork 6k
[Android R] Integrate DisplayCutouts into viewportMetrics #20921
Changes from 1 commit
7281731
036aa8c
8fb6c94
7e26c7d
56fb061
9504f64
b3cc87f
88e279f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -509,7 +509,8 @@ private int guessBottomKeyboardInset(WindowInsets insets) { | |
| public final WindowInsets onApplyWindowInsets(@NonNull WindowInsets insets) { | ||
| WindowInsets newInsets = super.onApplyWindowInsets(insets); | ||
|
|
||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | ||
| // getSystemGestureInsets() was introduced in API 29 and immediately deprecated in 30. | ||
| if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) { | ||
| Insets systemGestureInsets = insets.getSystemGestureInsets(); | ||
| viewportMetrics.systemGestureInsetTop = systemGestureInsets.top; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Super tangential, just realized we added this to mediaquery. We should move the iOS home bar to use this too :)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry disregard. After reading this from media query and android, I realized it doesn't mean the same thing |
||
| viewportMetrics.systemGestureInsetRight = systemGestureInsets.right; | ||
|
|
@@ -522,37 +523,54 @@ public final WindowInsets onApplyWindowInsets(@NonNull WindowInsets insets) { | |
| (SYSTEM_UI_FLAG_HIDE_NAVIGATION & getWindowSystemUiVisibility()) == 0; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These things are kinda big now. Feel free to split them into populatePreQ, populateQ, populateR if needed. |
||
|
|
||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
| int mask = android.view.WindowInsets.Type.ime(); | ||
| int mask = 0; | ||
| if (navigationBarVisible) { | ||
| mask = mask | android.view.WindowInsets.Type.navigationBars(); | ||
| } | ||
| if (statusBarVisible) { | ||
| mask = mask | android.view.WindowInsets.Type.statusBars(); | ||
| } | ||
| Insets uiInsets = insets.getInsets(mask); | ||
|
|
||
| viewportMetrics.paddingTop = uiInsets.top; | ||
| viewportMetrics.paddingRight = uiInsets.right; | ||
| viewportMetrics.paddingBottom = 0; | ||
| viewportMetrics.paddingBottom = uiInsets.bottom; | ||
| viewportMetrics.paddingLeft = uiInsets.left; | ||
|
|
||
| viewportMetrics.viewInsetTop = 0; | ||
| viewportMetrics.viewInsetRight = 0; | ||
| viewportMetrics.viewInsetBottom = uiInsets.bottom; | ||
| viewportMetrics.viewInsetLeft = 0; | ||
| Insets imeInsets = insets.getInsets(android.view.WindowInsets.Type.ime()); | ||
| viewportMetrics.viewInsetTop = imeInsets.top; | ||
| viewportMetrics.viewInsetRight = imeInsets.right; | ||
| viewportMetrics.viewInsetBottom = imeInsets.bottom; // Typically, only bottom is non-zero | ||
| viewportMetrics.viewInsetLeft = imeInsets.left; | ||
|
|
||
| Insets systemGestureInsets = insets.getInsets(android.view.WindowInsets.Type.systemGestures()); | ||
| viewportMetrics.systemGestureInsetTop = systemGestureInsets.top; | ||
| viewportMetrics.systemGestureInsetRight = systemGestureInsets.right; | ||
| viewportMetrics.systemGestureInsetBottom = systemGestureInsets.bottom; | ||
| viewportMetrics.systemGestureInsetLeft = systemGestureInsets.left; | ||
|
|
||
| // TODO(garyq): Expose the full rects of the display cutout. | ||
|
|
||
| // Take the max of the display cutout insets and existing insets to merge them | ||
| DisplayCutout cutout = insets.getCutout(); | ||
| // Take the max of the display cutout insets and existing padding to merge them | ||
| DisplayCutout cutout = insets.getDisplayCutout(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, neat that they have the insets apis separate from the rect apis. I suppose there's no regression here since on iOS, you already can't tell apart the notch rect from the insets to "utilize" the remaining area. Let's do the rect separately, sure.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. flutter/flutter#65088 Tracks the rect work |
||
| if (cutout != null) { | ||
| Insets waterfallInsets = cutout.getWaterfallInsets(); | ||
| viewportMetrics.systemGestureInsetTop = Math.max(Math.max(viewportMetrics.systemGestureInsetTop, waterfallInsets.top), cutout.getSafeInsetTop()); | ||
| viewportMetrics.systemGestureInsetRight = Math.max(Math.max(viewportMetrics.systemGestureInsetRight, waterfallInsets.right), cutout.getSafeInsetRight()); | ||
| viewportMetrics.systemGestureInsetBottom = Math.max(Math.max(viewportMetrics.systemGestureInsetBottom, waterfallInsets.bottom), cutout.getSafeInsetBottom()); | ||
| viewportMetrics.systemGestureInsetLeft = Math.max(Math.max(viewportMetrics.systemGestureInsetLeft, waterfallInsets.left), cutout.getSafeInsetLeft()); | ||
| viewportMetrics.paddingTop = | ||
| Math.max( | ||
| Math.max(viewportMetrics.paddingTop, waterfallInsets.top), | ||
| cutout.getSafeInsetTop()); | ||
| viewportMetrics.paddingRight = | ||
| Math.max( | ||
| Math.max(viewportMetrics.paddingRight, waterfallInsets.right), | ||
| cutout.getSafeInsetRight()); | ||
| viewportMetrics.paddingBottom = | ||
| Math.max( | ||
| Math.max(viewportMetrics.paddingBottom, waterfallInsets.bottom), | ||
| cutout.getSafeInsetBottom()); | ||
| viewportMetrics.paddingLeft = | ||
| Math.max( | ||
| Math.max(viewportMetrics.paddingLeft, waterfallInsets.left), | ||
| cutout.getSafeInsetLeft()); | ||
| } | ||
|
|
||
| } else { | ||
| // We zero the left and/or right sides to prevent the padding the | ||
| // navigation bar would have caused. | ||
|
|
@@ -584,7 +602,6 @@ public final WindowInsets onApplyWindowInsets(@NonNull WindowInsets insets) { | |
| viewportMetrics.viewInsetLeft = 0; | ||
| } | ||
|
|
||
|
|
||
| Log.v( | ||
| TAG, | ||
| "Updating window insets (onApplyWindowInsets()):\n" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have to do this now, but this got deprecated after 1 Android version 🤣. We could add an R check for the new API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh you're already doing getInsets below. If that works in Q, we should just merge it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, getInsets is API 30 only, so to support this for Q, we still need to do this. For R and above though, we can move to getInsets
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, SG