Skip to content

Commit

Permalink
Improved setting padding
Browse files Browse the repository at this point in the history
  • Loading branch information
jahirfiquitiva committed Dec 28, 2018
1 parent cceb4ae commit 376c5eb
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 40 deletions.
9 changes: 5 additions & 4 deletions .idea/copyright/CC_BY_SA.xml
100755 → 100644

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 30 additions & 30 deletions .idea/copyright/profiles_settings.xml
100755 → 100644

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':library')
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.3.10'
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.3.11'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.cardview:cardview:1.0.0'
}
1 change: 0 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
android:layout_width="wrap_content"
android:layout_height="48dp"
android:minWidth="120dp"
android:background="@drawable/gradient"
app:chipIcon="@drawable/ic_palette"
app:chipText="FAB?"
app:chipTextColor="#fff"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.10'
ext.kotlin_version = '1.3.11'
repositories {
google()
jcenter()
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.3.10'
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.3.11'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.cardview:cardview:1.0.0'
}
Expand Down
31 changes: 31 additions & 0 deletions library/src/main/kotlin/com/jahirfiquitiva/chip/ChipView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ open class ChipView @JvmOverloads constructor(
val properWidth = if (measuredWidth < minWidth) minWidth else measuredWidth
setMeasuredDimension(properWidth, properHeight)
if (radius < 0F) radius = (properHeight / 2.0F)
val horPadding = (radius / 2.0F).roundToInt()
setPaddingHorizontal(horPadding)
setPaddingVertical((horPadding / 2.0F).roundToInt())
if (contentFillsChip) {
contentLayout?.minimumWidth = properWidth
contentLayout?.minimumHeight = properHeight
Expand Down Expand Up @@ -305,6 +308,34 @@ open class ChipView @JvmOverloads constructor(
contentLayout?.setPadding(left, top, right, bottom)
}

fun setPaddingLeft(left: Int) {
contentLayout?.setPaddingLeft(left)
}

fun setPaddingTop(padding: Int) {
contentLayout?.setPaddingTop(padding)
}

fun setPaddingRight(padding: Int) {
contentLayout?.setPaddingRight(padding)
}

fun setPaddingBottom(padding: Int) {
contentLayout?.setPaddingBottom(padding)
}

fun setPaddingHorizontal(padding: Int) {
contentLayout?.setPaddingHorizontal(padding)
}

fun setPaddingVertical(padding: Int) {
contentLayout?.setPaddingVertical(padding)
}

fun setPadding(padding: Int) {
contentLayout?.setPadding(padding)
}

private fun internalSetBackground() {
val fgResId: Int = try {
val attrs = intArrayOf(R.attr.selectableItemBackground)
Expand Down
34 changes: 33 additions & 1 deletion library/src/main/kotlin/com/jahirfiquitiva/chip/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package com.jahirfiquitiva.chip

import android.content.res.Resources
import android.graphics.Color
import android.view.View
import androidx.annotation.ColorInt
import androidx.annotation.FloatRange
import androidx.customview.widget.ViewDragHelper

/**
* Created by Allan Wang
Expand Down Expand Up @@ -53,4 +55,34 @@ internal fun Int.darken(
val (red, green, blue) = intArrayOf(Color.red(this), Color.green(this), Color.blue(this))
.map { (it * (1f - factor)).toInt() }
return Color.argb(Color.alpha(this), red, green, blue)
}
}

internal fun View.setPaddingLeft(padding: Int) = setPadding(padding, KAU_LEFT)

internal fun View.setPaddingTop(padding: Int) = setPadding(padding, KAU_TOP)

internal fun View.setPaddingRight(padding: Int) = setPadding(padding, KAU_RIGHT)

internal fun View.setPaddingBottom(padding: Int) = setPadding(padding, KAU_BOTTOM)

internal fun View.setPaddingHorizontal(padding: Int) = setPadding(padding, KAU_HORIZONTAL)

internal fun View.setPaddingVertical(padding: Int) = setPadding(padding, KAU_VERTICAL)

internal fun View.setPadding(padding: Int) = setPadding(padding, KAU_ALL)

private fun View.setPadding(padding: Int, flag: Int) {
setPadding(
if (flag and KAU_LEFT > 0) padding else paddingLeft,
if (flag and KAU_TOP > 0) padding else paddingTop,
if (flag and KAU_RIGHT > 0) padding else paddingRight,
if (flag and KAU_BOTTOM > 0) padding else paddingBottom)
}

private const val KAU_LEFT = ViewDragHelper.EDGE_LEFT
private const val KAU_RIGHT = ViewDragHelper.EDGE_RIGHT
private const val KAU_TOP = ViewDragHelper.EDGE_TOP
private const val KAU_BOTTOM = ViewDragHelper.EDGE_BOTTOM
private const val KAU_HORIZONTAL = KAU_LEFT or KAU_RIGHT
private const val KAU_VERTICAL = KAU_TOP or KAU_BOTTOM
private const val KAU_ALL = KAU_HORIZONTAL or KAU_VERTICAL
1 change: 0 additions & 1 deletion library/src/main/res/layout/chip.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center"
android:padding="4dp"
android:clipToPadding="false"
android:clipChildren="false"
android:animateLayoutChanges="true"
Expand Down

0 comments on commit 376c5eb

Please sign in to comment.