8000 New Kotlin DSL by chrisbanes · Pull Request #92 · 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.

New Kotlin DSL #92

Merged
merged 6 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
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
42 changes: 20 additions & 22 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,39 @@ blog post.

There are a number of libraries available:

### [insetter](library/)
### [Main library](library/)

The base library which provides an easy-to-use
The main library provides an easy-to-use
[Builder](/library/src/main/java/dev/chrisbanes/insetter/Insetter.kt) for
[OnApplyWindowInsetsListener](https://developer.android.com/reference/androidx/core/view/OnApplyWindowInsetsListener)
instances:

=== "Java"
=== "Kotlin"

``` java
Insetter.builder()
// This will apply the system window insets as padding to left, bottom and right of the view,
// maintaining the original padding (from the layout XML, style, etc)
.applySystemWindowInsetsToPadding(Side.LEFT | Side.BOTTOM | Side.RIGHT)
// This is a shortcut for view.setOnApplyWindowInsetsListener(builder.build())
.applyToView(view);
``` kotlin
view.applyInsetter {
// Apply the navigation bar insets...
type(navigationBars = true) {
// Add to padding on all sides
padding()
}
}
```

=== "Kotlin"
See [here](api/library/library/dev.chrisbanes.insetter/apply-insetter.html) for more information.

``` kotlin
=== "Java"

``` java
Insetter.builder()
// This will apply the system window insets as padding to left, bottom and right of the view,
// This will add the navigation bars insets as padding to all sides of the view,
// maintaining the original padding (from the layout XML, style, etc)
.applySystemWindowInsetsToPadding(Side.LEFT or Side.BOTTOM or Side.RIGHT)
.padding(WindowInsetsCompat.Type.navigationBars())
// This is a shortcut for view.setOnApplyWindowInsetsListener(builder.build())
.applyToView(view)
.applyToView(view);
```

It also provides some Kotlin-only extension functions allowing easy access to the library functions:

``` kotlin
bottomNav.applySystemWindowInsetsToPadding(bottom = true)
btnConfirm.applySystemWindowInsetsToMargin(bottom = true, right = true)
```
See [here](api/library/library/dev.chrisbanes.insetter/-insetter/) for more information.

### [insetter-dbx](dbx/)

Expand Down Expand Up @@ -105,7 +103,7 @@ at a later date.

## Download

=== "Java"
=== "Stable"

Latest version: ![GitHub release](https://img.shields.io/maven-central/v/dev.chrisbanes.insetter/insetter)

Expand Down
21 changes: 21 additions & 0 deletions library/api/library.api
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ public final class dev/chrisbanes/insetter/Insetter$Companion {
public abstract interface annotation class dev/chrisbanes/insetter/Insetter$ConsumeOptions : java/lang/annotation/Annotation {
}

public final class dev/chrisbanes/insetter/InsetterApplyTypeDsl {
public final fun margin ()V
public final fun margin (ZZZZZZ)V
public static synthetic fun margin$default (Ldev/chrisbanes/insetter/InsetterApplyTypeDsl;ZZZZZZILjava/lang/Object;)V
public final fun padding ()V
public final fun padding (ZZZZZZ)V
public static synthetic fun padding$default (Ldev/chrisbanes/insetter/InsetterApplyTypeDsl;ZZZZZZILjava/lang/Object;)V
}

public final class dev/chrisbanes/insetter/InsetterDsl {
public final fun consume (Z)V
public final fun type (ILkotlin/jvm/functions/Function1;)V
public final fun type (ZZZZZZZZLkotlin/jvm/functions/Function1;)V
public static synthetic fun type$default (Ldev/chrisbanes/insetter/InsetterDsl;ZZZZZZZZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
}

public final class dev/chrisbanes/insetter/InsetterDslKt {
public static final fun applyInsetter (Landroid/view/View;Lkotlin/jvm/functions/Function1;)Ldev/chrisbanes/insetter/Insetter;
}

public final class dev/chrisbanes/insetter/InsetterKtxKt {
public static final fun applySystemGestureInsetsToMargin (Landroid/view/View;ZZZZZ)Ldev/chrisbanes/insetter/Insetter;
public static synthetic fun applySystemGestureInsetsToMargin$default (Landroid/view/View;ZZZZZILjava/lang/Object;)Ldev/chrisbanes/insetter/Insetter;
Expand Down Expand Up @@ -72,6 +92,7 @@ public final class dev/chrisbanes/insetter/Side {
public static final field RIGHT I
public static final field TOP I
public static final fun create (ZZZZ)I
public static final fun create (ZZZZZZ)I
}

public abstract interface annotation class dev/chrisbanes/insetter/Sides : java/lang/annotation/Annotation {
Expand Down
219 changes: 219 additions & 0 deletions library/src/main/java/dev/chrisbanes/insetter/InsetterDsl.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.chrisbanes.insetter

import android.view.View
import androidx.core.view.WindowInsetsCompat

/**
* Allows easy building and applying of [WindowInsetsCompat] to this [View].
*
* As an example, to apply the [navigation bar][WindowInsetsCompat.Type.navigationBars] insets
* as padding to this view, you would call:
*
* ```
* view.applyInsetter {
* type(navigationBars = true) {
* padding()
* }
* }
* ```
*
* ### Applying different types
*
* If we want to apply different insets type, we can do that too:
*
* ```
* view.applyInsetter {
* // Apply the navigation bar insets as padding
* type(navigationBars = true) {
* padding()
* }
* // Apply the status bar insets as margin
* type(statusBars = true) {
* margin()
* }
* }
* ```
*
* Alternatively, if we want to apply multiple types as padding/margin, you can do the following:
*
* ```
* view.applyInsetter {
* // Apply the navigation bar and status bars insets as padding
* type(navigationBars = true, statusBars = true) {
* padding()
* }
* }
* ```
*
* ### Different dimensions
*
* Some times you need to apply sides differently, here we're applying left + right sides to margin,
* and then the bottom as padding:
*
* ```
* view.applyInsetter {
* // Apply the navigation bar insets...
* type(navigationBars = true) {
* // Add the left/right to the margin
* margin(left = true, right = true)
* // Add the bottom to padding
* padding(bottom = true)
* }
* }
* ```
*/
fun View.applyInsetter(build: InsetterDsl.() -> Unit): Insetter {
return InsetterDsl().apply(build).builder.applyToView(this)
}

@DslMarker
annotation class InsetterDslMarker

/**
* Class used in [View.applyInsetter].
*/
@InsetterDslMarker
class InsetterDsl internal constructor() {
internal var builder = Insetter.builder()

/**
* Set how the given [WindowInsetsCompat.Type]s are applied to the view.
*
* All of the common types are provided as boolean parameters. Setting multiple types
* to `true` will result in the combination of those insets to be applied.
*
* @throws IllegalArgumentException if all types are set to `false`.
*
* @param ime True to apply the [WindowInsetsCompat.Type.ime] insets.
* @param navigationBars True to apply the [WindowInsetsCompat.Type.navigationBars] insets.
* @param navigationBars True to apply the [WindowInsetsCompat.Type.navigationBars] insets.
* @param statusBars True to apply the [WindowInsetsCompat.Type.statusBars] insets.
* @param systemGestures True to apply the [WindowInsetsCompat.Type.systemGestures] insets.
* @param mandatorySystemGestures True to apply the [WindowInsetsCompat.Type.mandatorySystemGestures] insets.
* @param displayCutout True to apply the [WindowInsetsCompat.Type.displayCutout] insets.
* @param captionBar True to apply the [WindowInsetsCompat.Type.captionBar] insets.
* @param tappableElement True to apply the [WindowInsetsCompat.Type.tappableElement] insets.
*/
fun type(
ime: Boolean = false,
navigationBars: Boolean = false,
statusBars: Boolean = false,
systemGestures: Boolean = false,
mandatorySystemGestures: Boolean = false,
displayCutout: Boolean = false,
captionBar: Boolean = false,
tappableElement: Boolean = false,
f: InsetterApplyTypeDsl.() -> Unit,
) {
val type = windowInsetTypesOf(
ime,
navigationBars,
statusBars,
systemGestures,
mandatorySystemGestures,
displayCutout,
captionBar,
tappableElement
)
type(type, f)
}

/**
* Set how the given bitmask of [WindowInsetsCompat.Type]s in [type] are applied to the view.
*
* @throws IllegalArgumentException if [type] is empty, by passing in `0`.
*
* @param type Bit mask of [WindowInsetsCompat.Type]s apply.
*/
fun type(type: Int, f: InsetterApplyTypeDsl.() -> Unit,) {
require(type != 0) { "A type is required" }
builder = InsetterApplyTypeDsl(type, builder).apply(f).builder
}

fun consume(consume: Boolean) {
builder = builder.consume(if (consume) Insetter.CONSUME_ALL else Insetter.CONSUME_NONE)
}
}

/**
* Class used in [View.applyInsetter].
*/
@InsetterDslMarker
class InsetterApplyTypeDsl internal constructor(
private val type: Int,
internal var builder: Insetter.Builder,
) {
/**
* Add the [WindowInsetsCompat.Type] to all padding dimensions.
*/
fun padding() = padding(horizontal = true, vertical = true)

/**
* Add the [WindowInsetsCompat.Type] to the given padding dimensions.
*
* @param left Add the left value of the insets to the left padding.
* @param top Add the left value of the insets to the top padding.
* @param right Add the left value of the insets to the right padding.
* @param bottom Add the left value of the insets to the bottom padding.
* @param horizontal Add both the left and right values.
* @param vertical Add both the top and bottom values.
*/
fun padding(
left: Boolean = false,
top: Boolean = false,
right: Boolean = false,
bottom: Boolean = false,
horizontal: Boolean = false,
vertical: Boolean = false,
) {
builder = builder.padding(
insetType = type,
sides = Side.create(left, top, right, bottom, horizontal, vertical)
)
}

/**
* Add the [WindowInsetsCompat.Type] to all margin dimensions.
*/
fun margin() = margin(horizontal = true, vertical = true)

/**
* Add the [WindowInsetsCompat.Type] to the given margin dimensions.
*
* @param left Add the left value of the insets to the left padding.
* @param top Add the left value of the insets to the top padding.
* @param right Add the left value of the insets to the right padding.
* @param bottom Add the left value of the insets to the bottom padding.
* @param horizontal Add both the left and right values.
* @param vertical Add both the top and bottom values.
*/
fun margin(
left: Boolean = false,
top: Boolean = false,
right: Boolean = false,
bottom: Boolean = false,
horizontal: Boolean = false,
vertical: Boolean = false,
) {
builder = builder.margin(
insetType = type,
sides = Side.create(left, top, right, bottom, horizontal, vertical)
)
}
}
Loading
0