Skip to content

Commit

Permalink
Merge pull request #565 from skydoves/improve/update-methods
Browse files Browse the repository at this point in the history
Implement update relevant methods
  • Loading branch information
skydoves committed Dec 27, 2023
2 parents 9cde427 + a1f95f0 commit 7d27797
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class ComposeActivity : ComponentActivity() {
) {
Button(
modifier = Modifier.size(160.dp, 60.dp),
onClick = { balloonWindow2?.showAlignTop() },
onClick = { balloonWindow2?.showAlignBottom() },
) {
Text(text = "wrap balloon")
}
Expand Down
10 changes: 10 additions & 0 deletions balloon-compose/api/balloon-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public abstract interface class com/skydoves/balloon/compose/BalloonWindow {
public abstract fun showAsDropDown (II)V
public abstract fun showAtCenter (IILcom/skydoves/balloon/BalloonCenterAlign;)V
public abstract fun update (II)V
public abstract fun updateAlign (Lcom/skydoves/balloon/BalloonAlign;II)V
public abstract fun updateAlignBottom (II)V
public abstract fun updateAlignEnd (II)V
public abstract fun updateAlignStart (II)V
public abstract fun updateAlignTop (II)V
public abstract fun updateSizeOfBalloonCard (II)V
}

Expand Down Expand Up @@ -116,6 +121,11 @@ public final class com/skydoves/balloon/compose/BalloonWindow$DefaultImpls {
public static synthetic fun showAsDropDown$default (Lcom/skydoves/balloon/compose/BalloonWindow;IIILjava/lang/Object;)V
public static synthetic fun showAtCenter$default (Lcom/skydoves/balloon/compose/BalloonWindow;IILcom/skydoves/balloon/BalloonCenterAlign;ILjava/lang/Object;)V
public static synthetic fun update$default (Lcom/skydoves/balloon/compose/BalloonWindow;IIILjava/lang/Object;)V
public static synthetic fun updateAlign$default (Lcom/skydoves/balloon/compose/BalloonWindow;Lcom/skydoves/balloon/BalloonAlign;IIILjava/lang/Object;)V
public static synthetic fun updateAlignBottom$default (Lcom/skydoves/balloon/compose/BalloonWindow;IIILjava/lang/Object;)V
public static synthetic fun updateAlignEnd$default (Lcom/skydoves/balloon/compose/BalloonWindow;IIILjava/lang/Object;)V
public static synthetic fun updateAlignStart$default (Lcom/skydoves/balloon/compose/BalloonWindow;IIILjava/lang/Object;)V
public static synthetic fun updateAlignTop$default (Lcom/skydoves/balloon/compose/BalloonWindow;IIILjava/lang/Object;)V
}

public final class com/skydoves/balloon/compose/ComposableSingletons$BalloonComposeViewKt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,30 @@ internal class BalloonComposeView(
yOff: Int,
): Balloon = balloon.relayShowAlign(align, balloon, anchorView, xOff, yOff)

override fun updateAlignTop(xOff: Int, yOff: Int) {
balloon.updateAlignTop(anchorView, xOff, yOff)
}

override fun updateAlignBottom(xOff: Int, yOff: Int) {
balloon.updateAlignBottom(anchorView, xOff, yOff)
}

override fun updateAlignEnd(xOff: Int, yOff: Int) {
balloon.updateAlignEnd(anchorView, xOff, yOff)
}

override fun updateAlignStart(xOff: Int, yOff: Int) {
balloon.updateAlignStart(anchorView, xOff, yOff)
}

override fun updateAlign(align: BalloonAlign, xOff: Int, yOff: Int) {
balloon.updateAlign(align, anchorView, xOff, yOff)
}

@Deprecated(
"Use updateAlign instead.",
replaceWith = ReplaceWith("updateAlign(BalloonAlign.Top, xOff, yOff)"),
)
override fun update(xOff: Int, yOff: Int): Unit = balloon.update(anchorView, xOff, yOff)

override fun showAlign(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,58 @@ public interface BalloonWindow {
yOff: Int = 0,
): Balloon

/**
* Update the balloon on an [anchorView] as the top alignment with x-off and y-off.
*
* @param xOff A horizontal offset from the anchor in pixels.
* @param yOff A vertical offset from the anchor in pixels.
*/
public fun updateAlignTop(xOff: Int = 0, yOff: Int = 0)

/**
* Update the balloon on an [anchorView] as the bottom alignment with x-off and y-off.
*
* @param xOff A horizontal offset from the anchor in pixels.
* @param yOff A vertical offset from the anchor in pixels.
*/
public fun updateAlignBottom(xOff: Int = 0, yOff: Int = 0)

/**
* Update the balloon on an [anchorView] as the end alignment with x-off and y-off.
*
* @param xOff A horizontal offset from the anchor in pixels.
* @param yOff A vertical offset from the anchor in pixels.
*/
public fun updateAlignEnd(xOff: Int = 0, yOff: Int = 0)

/**
* Update the balloon on an [anchorView] as the start alignment with x-off and y-off.
*
* @param xOff A horizontal offset from the anchor in pixels.
* @param yOff A vertical offset from the anchor in pixels.
*/
public fun updateAlignStart(xOff: Int = 0, yOff: Int = 0)

/**
* Update the balloon on an [anchorView] as the bottom alignment with x-off and y-off.
*
* @param align Decides where the balloon should be placed.
* @param xOff A horizontal offset from the anchor in pixels.
* @param yOff A vertical offset from the anchor in pixels.
*/
public fun updateAlign(align: BalloonAlign, xOff: Int = 0, yOff: Int = 0)

/**
* updates popup and arrow position of the popup based on
* a new target anchor view with additional x-off and y-off.
*
* @param xOff A horizontal offset from the anchor in pixels.
* @param yOff A vertical offset from the anchor in pixels.
*/
@Deprecated(
"Use updateAlign instead.",
replaceWith = ReplaceWith("updateAlign(BalloonAlign.Top, xOff, yOff)"),
)
public fun update(xOff: Int = 0, yOff: Int = 0)

/** updates the size of the balloon card. */
Expand Down
20 changes: 20 additions & 0 deletions balloon/api/balloon.api
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,26 @@ public final class com/skydoves/balloon/Balloon : androidx/lifecycle/DefaultLife
public final fun update (Landroid/view/View;I)V
public final fun update (Landroid/view/View;II)V
public static synthetic fun update$default (Lcom/skydoves/balloon/Balloon;Landroid/view/View;IIILjava/lang/Object;)V
public final fun updateAlign (Lcom/skydoves/balloon/BalloonAlign;Landroid/view/View;)V
public final fun updateAlign (Lcom/skydoves/balloon/BalloonAlign;Landroid/view/View;I)V
public final fun updateAlign (Lcom/skydoves/balloon/BalloonAlign;Landroid/view/View;II)V
public static synthetic fun updateAlign$default (Lcom/skydoves/balloon/Balloon;Lcom/skydoves/balloon/BalloonAlign;Landroid/view/View;IIILjava/lang/Object;)V
public final fun updateAlignBottom (Landroid/view/View;)V
public final fun updateAlignBottom (Landroid/view/View;I)V
public final fun updateAlignBottom (Landroid/view/View;II)V
public static synthetic fun updateAlignBottom$default (Lcom/skydoves/balloon/Balloon;Landroid/view/View;IIILjava/lang/Object;)V
public final fun updateAlignEnd (Landroid/view/View;)V
public final fun updateAlignEnd (Landroid/view/View;I)V
public final fun updateAlignEnd (Landroid/view/View;II)V
public static synthetic fun updateAlignEnd$default (Lcom/skydoves/balloon/Balloon;Landroid/view/View;IIILjava/lang/Object;)V
public final fun updateAlignStart (Landroid/view/View;)V
public final fun updateAlignStart (Landroid/view/View;I)V
public final fun updateAlignStart (Landroid/view/View;II)V
public static synthetic fun updateAlignStart$default (Lcom/skydoves/balloon/Balloon;Landroid/view/View;IIILjava/lang/Object;)V
public final fun updateAlignTop (Landroid/view/View;)V
public final fun updateAlignTop (Landroid/view/View;I)V
public final fun updateAlignTop (Landroid/view/View;II)V
public static synthetic fun updateAlignTop$default (Lcom/skydoves/balloon/Balloon;Landroid/view/View;IIILjava/lang/Object;)V
public final fun updateSizeOfBalloonCard (II)V
}

Expand Down
95 changes: 83 additions & 12 deletions balloon/src/main/kotlin/com/skydoves/balloon/Balloon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1467,30 +1467,101 @@ public class Balloon private constructor(
}

/**
* updates popup and arrow position of the popup based on
* a new target anchor view with additional x-off and y-off.
* Update the balloon on a given new [anchor] as the top alignment with x-off and y-off.
*
* @param anchor A target view which popup will be shown to.
* @param xOff A horizontal offset from the anchor in pixels.
* @param yOff A vertical offset from the anchor in pixels.
*/
@JvmOverloads
public fun update(anchor: View, xOff: Int = 0, yOff: Int = 0) {
update(anchor = anchor) {
this.bodyWindow.update(anchor, xOff, yOff, getMeasuredWidth(), getMeasuredHeight())
public fun updateAlignTop(anchor: View, xOff: Int = 0, yOff: Int = 0) {
update(BalloonPlacement(anchor = anchor, align = BalloonAlign.TOP, xOff = xOff, yOff = yOff))
}

/**
* Update the balloon on a given new [anchor] as the bottom alignment with x-off and y-off.
*
* @param anchor A target view which popup will be shown to.
* @param xOff A horizontal offset from the anchor in pixels.
* @param yOff A vertical offset from the anchor in pixels.
*/
@JvmOverloads
public fun updateAlignBottom(anchor: View, xOff: Int = 0, yOff: Int = 0) {
update(BalloonPlacement(anchor = anchor, align = BalloonAlign.BOTTOM, xOff = xOff, yOff = yOff))
}

/**
* Update the balloon on a given new [anchor] as the end alignment with x-off and y-off.
*
* @param anchor A target view which popup will be shown to.
* @param xOff A horizontal offset from the anchor in pixels.
* @param yOff A vertical offset from the anchor in pixels.
*/
@JvmOverloads
public fun updateAlignEnd(anchor: View, xOff: Int = 0, yOff: Int = 0) {
update(BalloonPlacement(anchor = anchor, align = BalloonAlign.END, xOff = xOff, yOff = yOff))
}

/**
* Update the balloon on a given new [anchor] as the start alignment with x-off and y-off.
*
* @param anchor A target view which popup will be shown to.
* @param xOff A horizontal offset from the anchor in pixels.
* @param yOff A vertical offset from the anchor in pixels.
*/
@JvmOverloads
public fun updateAlignStart(anchor: View, xOff: Int = 0, yOff: Int = 0) {
update(BalloonPlacement(anchor = anchor, align = BalloonAlign.START, xOff = xOff, yOff = yOff))
}

/**
* Update the balloon on a given new [anchor] as the bottom alignment with x-off and y-off.
*
* @param align Decides where the balloon should be placed.
* @param anchor A target view which popup will be shown to.
* @param xOff A horizontal offset from the anchor in pixels.
* @param yOff A vertical offset from the anchor in pixels.
*/
@JvmOverloads
public fun updateAlign(align: BalloonAlign, anchor: View, xOff: Int = 0, yOff: Int = 0) {
update(BalloonPlacement(anchor = anchor, align = align, xOff = xOff, yOff = yOff))
}

/** updates popup and arrow position of the popup based on the given [placement]. */
@MainThread
private fun update(placement: BalloonPlacement) {
if (isShowing) {
initializeArrow(placement.anchor)

val (xOff, yOff) = calculateOffset(placement)
this.bodyWindow.update(
placement.anchor,
xOff,
yOff,
getMeasuredWidth(),
getMeasuredHeight(),
)
if (builder.isVisibleOverlay) {
overlayBinding.balloonOverlayView.forceInvalidate()
}
}
}

/** updates popup and arrow position of the popup based on a new target anchor view. */
@MainThread
private inline fun update(anchor: View, crossinline block: () -> Unit) {
if (isShowing) {
initializeArrow(anchor)
block()
}
/**
* updates popup and arrow position of the popup based on
* a new target anchor view with additional x-off and y-off.
*
* @param anchor A target view which popup will be shown to.
* @param xOff A horizontal offset from the anchor in pixels.
* @param yOff A vertical offset from the anchor in pixels.
*/
@JvmOverloads
@Deprecated(
"Use updateAlign instead.",
replaceWith = ReplaceWith("updateAlign(BalloonAlign.Top, anchor, xOff, yOff)"),
)
public fun update(anchor: View, xOff: Int = 0, yOff: Int = 0) {
update(placement = BalloonPlacement(anchor = anchor, xOff = xOff, yOff = yOff))
}

/** dismiss the popup menu. */
Expand Down

0 comments on commit 7d27797

Please sign in to comment.