Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move offset to toolip #19

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.pseudoankit.coachmark.model

import androidx.compose.animation.core.AnimationSpec
import androidx.compose.runtime.Stable
import androidx.compose.ui.unit.Dp
import com.pseudoankit.coachmark.scope.CoachMarkScope
import com.pseudoankit.coachmark.util.CoachMarkKey

Expand All @@ -20,7 +21,8 @@ public data class TooltipConfig(
val toolTipPlacement: ToolTipPlacement,
val key: CoachMarkKey,
val highlightedViewShape: HighlightedViewConfig.Shape,
val animationState: AnimationState
val animationState: AnimationState,
val minOffsetFromScreen: Dp
) {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import com.pseudoankit.coachmark.util.highlightActualView
public class DimOverlayEffect(
private val color: Color = Color.Black.copy(alpha = .75f),
override val overlayAnimationSpec: AnimationSpec<Float> = CoachMarkDefaults.Overlay.animationSpec,
private val paddingForTooltip: Dp = CoachMarkDefaults.ToolTip.paddingForTooltip,
private val paddingForTooltip: Dp = CoachMarkDefaults.ToolTip.minOffsetFromScreen,
) : UnifyOverlayEffect {

@Composable
Expand All @@ -45,8 +45,7 @@ public class DimOverlayEffect(
highlightActualView(tooltip, density, previousTooltip.alpha)
}
},
content = content,
paddingForTooltip = paddingForTooltip,
content = content
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.layout.Placeable
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.Density
import com.pseudoankit.coachmark.model.ToolTipPlacement
import com.pseudoankit.coachmark.model.TooltipConfig
import com.pseudoankit.coachmark.util.CoachMarkDefaults

/** Containing composable must use these values for layoutId on current and previous tooltip. */
public object TooltipId {
Expand All @@ -34,30 +34,28 @@ public fun OverlayLayout(
configCurrent: TooltipConfig?,
configPrevious: TooltipConfig?,
modifier: Modifier = Modifier,
paddingForTooltip: Dp = CoachMarkDefaults.ToolTip.paddingForTooltip,
content: @Composable @UiComposable () -> Unit,
) {
val density = LocalDensity.current
Layout(content, modifier) { measurables, constraints ->

// child count < 2 occurs on first and last coach mark
require(measurables.size <= 2) { "OverlayLayout cannot have more than two children" }

val gapTooltipScreenPx = paddingForTooltip.roundToPx()

// measure children
val placeableCurrent = measure(
tooltipConfig = configCurrent,
layoutId = TooltipId.current,
measurables = measurables,
constraintsParent = constraints,
gapTooltipScreenPx = gapTooltipScreenPx
density = density
)
val placeablePrevious = measure(
tooltipConfig = configPrevious,
layoutId = TooltipId.previous,
measurables = measurables,
constraintsParent = constraints,
gapTooltipScreenPx = gapTooltipScreenPx
density = density,
)

// place children
Expand All @@ -78,10 +76,11 @@ private fun measure(
layoutId: Int,
measurables: List<Measurable>,
constraintsParent: Constraints,
gapTooltipScreenPx: Int,
density: Density,
): Placeable? {
if (tooltipConfig == null) return null

val gapTooltipScreenPx = with(density) { tooltipConfig.minOffsetFromScreen.roundToPx() }
// constrain max width to prevent tooltip running off screen
var maxWidth = when (tooltipConfig.toolTipPlacement) {
ToolTipPlacement.Start -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.pseudoankit.coachmark.scope

import androidx.compose.animation.core.AnimationSpec
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
import com.pseudoankit.coachmark.model.HighlightedViewConfig
import com.pseudoankit.coachmark.model.OverlayClickEvent
import com.pseudoankit.coachmark.model.ToolTipPlacement
Expand Down Expand Up @@ -35,7 +36,8 @@ public interface CoachMarkScope {
key: CoachMarkKey,
toolTipPlacement: ToolTipPlacement,
tooltipAnimationSpec: AnimationSpec<Float> = CoachMarkDefaults.ToolTip.animationSpec,
highlightedViewConfig: HighlightedViewConfig = HighlightedViewConfig()
highlightedViewConfig: HighlightedViewConfig = HighlightedViewConfig(),
minOffsetFromScreen: Dp = CoachMarkDefaults.ToolTip.minOffsetFromScreen
): Modifier

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInRoot
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.LayoutDirection
import com.pseudoankit.coachmark.model.HighlightedViewConfig
import com.pseudoankit.coachmark.model.OverlayClickEvent
Expand Down Expand Up @@ -54,6 +55,7 @@ internal class CoachMarkScopeImpl(
toolTipPlacement: ToolTipPlacement,
tooltipAnimationSpec: AnimationSpec<Float>,
highlightedViewConfig: HighlightedViewConfig,
minOffsetFromScreen: Dp,
): Modifier = onGloballyPositioned { layoutCoordinates ->
val startPadding =
highlightedViewConfig.padding.calculateStartPadding(layoutDirection).toPx(density)
Expand All @@ -74,7 +76,8 @@ internal class CoachMarkScopeImpl(
highlightedViewShape = highlightedViewConfig.shape,
animationState = TooltipConfig.AnimationState(
tooltipAnimationSpec = tooltipAnimationSpec
)
),
minOffsetFromScreen = minOffsetFromScreen
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public object CoachMarkDefaults {

public object ToolTip {
public val animationSpec: AnimationSpec<Float> = tween(ANIMATION_DURATION)
public val paddingForTooltip: Dp = 8.dp
public val minOffsetFromScreen: Dp = 0.dp
}

public object Overlay {
Expand Down
Loading