Skip to content

Commit

Permalink
Library: Fix nothing is shown in AS Layout Preview
Browse files Browse the repository at this point in the history
  • Loading branch information
fornewid committed May 20, 2020
1 parent 0dafbee commit 1037dcd
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class NeumorphButton @JvmOverloads constructor(
a.recycle()

shapeDrawable = NeumorphShapeDrawable(context, attrs, defStyleAttr, defStyleRes).apply {
setInEditMode(isInEditMode)
setShapeType(shapeType)
setShadowElevation(shadowElevation)
setShadowColorLight(shadowColorLight)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class NeumorphCardView @JvmOverloads constructor(
a.recycle()

shapeDrawable = NeumorphShapeDrawable(context, attrs, defStyleAttr, defStyleRes).apply {
setInEditMode(isInEditMode)
setShapeType(shapeType)
setShadowElevation(shadowElevation)
setShadowColorLight(shadowColorLight)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class NeumorphFloatingActionButton @JvmOverloads constructor(
a.recycle()

shapeDrawable = NeumorphShapeDrawable(context, attrs, defStyleAttr, defStyleRes).apply {
setInEditMode(isInEditMode)
setShapeType(shapeType)
setShadowElevation(shadowElevation)
setShadowColorLight(shadowColorLight)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class NeumorphImageButton @JvmOverloads constructor(
a.recycle()

shapeDrawable = NeumorphShapeDrawable(context, attrs, defStyleAttr, defStyleRes).apply {
setInEditMode(isInEditMode)
setShapeType(shapeType)
setShadowElevation(shadowElevation)
setShadowColorLight(shadowColorLight)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class NeumorphImageView @JvmOverloads constructor(
a.recycle()

shapeDrawable = NeumorphShapeDrawable(context, attrs, defStyleAttr, defStyleRes).apply {
setInEditMode(isInEditMode)
setShapeType(shapeType)
setShadowElevation(shadowElevation)
setShadowColorLight(shadowColorLight)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,15 @@ class NeumorphShapeDrawable : Drawable {
return invalidateSelf
}

fun setInEditMode(inEditMode: Boolean) {
drawableState.inEditMode = inEditMode
}

internal class NeumorphShapeDrawableState : ConstantState {

var shapeAppearanceModel: NeumorphShapeAppearanceModel
val blurProvider: BlurProvider
var inEditMode: Boolean = false

var padding: Rect? = null
var fillColor: ColorStateList? = null
Expand Down Expand Up @@ -400,6 +405,7 @@ class NeumorphShapeDrawable : Drawable {
constructor(orig: NeumorphShapeDrawableState) {
shapeAppearanceModel = orig.shapeAppearanceModel
blurProvider = orig.blurProvider
inEditMode = orig.inEditMode
alpha = orig.alpha
shapeType = orig.shapeType
shadowElevation = orig.shadowElevation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ class NeumorphTextView @JvmOverloads constructor(
tp.color = Color.BLACK
tp.textSize = textSize
tp.typeface = typeface
staticLayout(text, tp).draw(Canvas(this))
if (isInEditMode.not()) {
// layout preview is NOT supported in now.
staticLayout(text, tp).draw(Canvas(this))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ internal class FlatShape(

private fun Drawable.toBlurredBitmap(w: Int, h: Int): Bitmap? {
fun Bitmap.blurred(): Bitmap? {
if (drawableState.inEditMode) {
return this
}
return drawableState.blurProvider.blur(this)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ internal class PressedShape(

private fun generateShadowBitmap(w: Int, h: Int): Bitmap? {
fun Bitmap.blurred(): Bitmap? {
if (drawableState.inEditMode) {
return this
}
return drawableState.blurProvider.blur(this)
}

Expand Down

0 comments on commit 1037dcd

Please sign in to comment.