8000 Add new consumption options by chrisbanes · Pull Request #67 · chrisbanes/insetter · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Add new consumption options #67

Merged
merged 1 commit into from
Jun 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 85 additions & 24 deletions library/src/main/java/dev/chrisbanes/insetter/Insetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
Expand All @@ -28,6 +29,8 @@
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Locale;

/**
Expand All @@ -44,8 +47,8 @@
* .applyToView(view);
* </pre>
*
* Each inset type as on Android 10 (API level 29) is included, with variants for applying the inset
* as either padding or margin on the view.
* <p>Each inset type as on Android 10 (API level 29) is included, with variants for applying the
* inset as either padding or margin on the view.
*
* <p>You can also provide custom logic via the {@link
* Builder#setOnApplyInsetsListener(OnApplyInsetsListener)} function. The listener type is slightly
Expand All @@ -58,14 +61,33 @@
*/
public final class Insetter {

/** No consumption happens. This is the default value. */
public static final int CONSUME_NONE = 0;

/**
* All sides are consumed. This is similar to {@link
* WindowInsetsCompat#consumeSystemWindowInsets()}.
*/
public static final int CONSUME_ALL = 1;

/**
* Any specified sides are consumed. This selectively consumes any sides which are set via {@link
* Builder#applySystemWindowInsetsToPadding(int)} or other related functions.
*/
public static final int CONSUME_AUTO = 2;

@IntDef(value = {CONSUME_NONE, CONSUME_ALL, CONSUME_AUTO})
@Retention(RetentionPolicy.SOURCE)
public @interface ConsumeOptions {}

static final String TAG = "Insetter";

@Nullable private OnApplyInsetsListener onApplyInsetsListener;
private int paddingSystemWindowInsets;
private int marginSystemWindowInsets;
private int paddingSystemGestureInsets;
private int marginSystemGestureInsets;
private boolean consumeSystemWindowInsets;
private int consumeSystemWindowInsets;

private Insetter(@NonNull Builder builder) {
>
Expand All @@ -84,7 +106,7 @@ public static final class Builder {
private int marginSystemWindowInsets;
private int paddingSystemGestureInsets;
private int marginSystemGestureInsets;
private boolean consumeSystemWindowInsets;
private int consumeSystemWindowInsets = CONSUME_NONE;

private Builder() {
// private constructor.
Expand Down Expand Up @@ -153,7 +175,18 @@ public Builder applySystemGestureInsetsToMargin(@Sides int flags) {
*/
@NonNull
public Builder consumeSystemWindowInsets(boolean consumeSystemWindowInsets) {
this.consumeSystemWindowInsets = consumeSystemWindowInsets;
return consumeSystemWindowInsets(consumeSystemWindowInsets ? CONSUME_ALL : CONSUME_NONE);
}

/**
* @param consume how the system window insets should be consumed.
* @see Insetter#CONSUME_NONE
* @see Insetter#CONSUME_ALL
* @see Insetter#CONSUME_AUTO
*/
@NonNull
public Builder consumeSystemWindowInsets(@ConsumeOptions int consume) {
this.consumeSystemWindowInsets = consume;
return this;
}

Expand Down Expand Up @@ -192,20 +225,6 @@ public static Builder builder() {
*/
private void setOnApplyInsetsListener(@NonNull View view) {

final OnApplyInsetsListener listener =
onApplyInsetsListener != null
? onApplyInsetsListener
: new OnApplyInsetsListener() {
@Override
public void onApplyInsets(
@NonNull View view,
@NonNull WindowInsetsCompat insets,
@NonNull ViewState initialState) {

applyInsetsToView(view, insets, initialState);
}
};

final ViewState tagState = (ViewState) view.getTag(R.id.insetter_initial_state);

final ViewState initialState;
Expand All @@ -221,12 +240,54 @@ public void onApplyInsets(
new OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
listener.onApplyInsets(v, insets, initialState);

if (consumeSystemWindowInsets) {
return insets.consumeSystemWindowInsets();
final @Sides int sidesApplied;
if (onApplyInsetsListener != null) {
onApplyInsetsListener.onApplyInsets(v, insets, initialState);
// We don't have what sides all have been applied, so we assume all
sidesApplied = Side.ALL;
} else {
return insets;
applyInsetsToView(v, insets, initialState);
sidesApplied =
paddingSystemWindowInsets
| marginSystemWindowInsets
| paddingSystemGestureInsets
| marginSystemGestureInsets;
}

switch (consumeSystemWindowInsets) {
case CONSUME_ALL:
return insets.consumeSystemWindowInsets();
case CONSUME_AUTO:
if ((sidesApplied & Side.ALL) == Side.NONE) {
// If we did not apply any sides, just return the insets
return insets;
} else if ((sidesApplied & Side.ALL) == Side.ALL) {
// If all sides were applied, just return a consumed insets
return insets.consumeSystemWindowInsets();
} else {
// Otherwise we need to go through and consume each side
int left = insets.getSystemWindowInsetLeft();
int top = insets.getSystemWindowInsetTop();
int right = insets.getSystemWindowInsetRight();
int bottom = insets.getSystemWindowInsetBottom();

if (SideUtils.hasSide(sidesApplied, Side.LEFT)) {
left = 0;
}
if (SideUtils.hasSide(sidesApplied, Side.TOP)) {
top = 0;
}
if (SideUtils.hasSide(sidesApplied, Side.RIGHT)) {
right = 0;
}
if (SideUtils.hasSide(sidesApplied, Side.BOTTOM)) {
bottom = 0;
}
return insets.replaceSystemWindowInsets(left, top, right, bottom);
}
case CONSUME_NONE:
default:
return insets;
}
}
});
Expand Down
0