Skip to content

Commit

Permalink
Add button sides to command button presentation model
Browse files Browse the repository at this point in the history
More flexible app control over the presentation, also enables the target look of breadcrumb bar scroller buttons for #8
  • Loading branch information
kirill-grouchnikov committed Jan 3, 2022
1 parent e9e431f commit 65b8aca
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 30 deletions.
Expand Up @@ -218,6 +218,9 @@ fun Color.inverted(): Color {

/** Returns the version of this color based on the specified alpha. */
fun Color.withAlpha(alpha: Float): Color {
if (alpha == 1.0f) {
return this
}
return Color(this.red, this.green, this.blue, alpha, this.colorSpace)
}

Expand Down
Expand Up @@ -30,9 +30,11 @@ import androidx.compose.ui.platform.LocalFontLoader
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.text.resolveDefaults
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch
import org.pushingpixels.aurora.common.AuroraInternalApi
import org.pushingpixels.aurora.common.withAlpha
import org.pushingpixels.aurora.component.model.*
import org.pushingpixels.aurora.component.projection.CommandButtonProjection
import org.pushingpixels.aurora.component.utils.TransitionAwarePainter
Expand Down Expand Up @@ -92,10 +94,14 @@ fun AuroraBreadcrumbBar(commands: List<Command>, modifier: Modifier) {
modelStateInfoSnapshot = modelStateInfoSnapshot,
paintDelegate = { drawScope, iconSize, colorScheme ->
with(drawScope) {
val arrowWidth = ComboBoxSizingConstants.DefaultComboBoxArrowHeight.toPx()
val arrowHeight = ComboBoxSizingConstants.DefaultComboBoxArrowWidth.toPx()
val arrowWidth =
ComboBoxSizingConstants.DefaultComboBoxArrowHeight.toPx()
val arrowHeight =
ComboBoxSizingConstants.DefaultComboBoxArrowWidth.toPx()
val dx = (iconSize.toPx() - arrowWidth) / 2
val dy = (iconSize.toPx() - arrowHeight) / 2
val alpha = if (modelStateInfoSnapshot.currModelState.isDisabled)
colors.getAlpha(decorationAreaType, modelStateInfoSnapshot.currModelState) else 1.0f
translate(left = dx, top = dy) {
drawArrow(
drawScope = this,
Expand All @@ -104,7 +110,7 @@ fun AuroraBreadcrumbBar(commands: List<Command>, modifier: Modifier) {
strokeWidth = 2.0.dp.toPx(),
direction = PopupPlacementStrategy.Startward,
layoutDirection = layoutDirection,
color = colorScheme.markColor
color = colorScheme.markColor.withAlpha(alpha)
)
}
}
Expand All @@ -131,10 +137,14 @@ fun AuroraBreadcrumbBar(commands: List<Command>, modifier: Modifier) {
modelStateInfoSnapshot = modelStateInfoSnapshot,
paintDelegate = { drawScope, iconSize, colorScheme ->
with(drawScope) {
val arrowWidth = ComboBoxSizingConstants.DefaultComboBoxArrowHeight.toPx()
val arrowHeight = ComboBoxSizingConstants.DefaultComboBoxArrowWidth.toPx()
val arrowWidth =
ComboBoxSizingConstants.DefaultComboBoxArrowHeight.toPx()
val arrowHeight =
ComboBoxSizingConstants.DefaultComboBoxArrowWidth.toPx()
val dx = (iconSize.toPx() - arrowWidth) / 2
val dy = (iconSize.toPx() - arrowHeight) / 2
val alpha = if (modelStateInfoSnapshot.currModelState.isDisabled)
colors.getAlpha(decorationAreaType, modelStateInfoSnapshot.currModelState) else 1.0f
translate(left = dx, top = dy) {
drawArrow(
drawScope = this,
Expand All @@ -143,7 +153,7 @@ fun AuroraBreadcrumbBar(commands: List<Command>, modifier: Modifier) {
strokeWidth = 2.0.dp.toPx(),
direction = PopupPlacementStrategy.Endward,
layoutDirection = layoutDirection,
color = colorScheme.markColor
color = colorScheme.markColor.withAlpha(alpha)
)
}
}
Expand All @@ -168,7 +178,15 @@ fun AuroraBreadcrumbBar(commands: List<Command>, modifier: Modifier) {
// Leftwards scroller
CommandButtonProjection(
contentModel = leftScrollerCommand,
presentationModel = scrollerPresentationModel
presentationModel = scrollerPresentationModel.overlayWith(
overlay = CommandButtonPresentationModel.Overlay(
sides = Sides(
straightSides = hashSetOf(
if (layoutDirection == LayoutDirection.Ltr) Side.Right else Side.Left
)
)
)
)
).project()

Box(modifier = Modifier.horizontalScroll(stateHorizontal)) {
Expand All @@ -185,7 +203,15 @@ fun AuroraBreadcrumbBar(commands: List<Command>, modifier: Modifier) {
// Rightwards scroller
CommandButtonProjection(
contentModel = rightScrollerCommand,
presentationModel = scrollerPresentationModel
presentationModel = scrollerPresentationModel.overlayWith(
overlay = CommandButtonPresentationModel.Overlay(
sides = Sides(
straightSides = hashSetOf(
if (layoutDirection == LayoutDirection.Ltr) Side.Left else Side.Right
)
)
)
)
).project()

},
Expand Down
Expand Up @@ -337,8 +337,7 @@ internal fun AuroraCommandButton(
extraAction: (() -> Unit)? = null,
extraActionPreview: CommandActionPreview? = null,
presentationModel: CommandButtonPresentationModel,
overlays: Map<Command, CommandButtonPresentationModel.Overlay>,
buttonSides: Sides
overlays: Map<Command, CommandButtonPresentationModel.Overlay>
) {
val secondaryContentModel = rememberUpdatedState(command.secondaryContentModel)
val drawingCache = remember { CommandButtonDrawingCache() }
Expand Down Expand Up @@ -785,7 +784,7 @@ internal fun AuroraCommandButton(
height = height,
extraInsets = 0.5f,
isInner = false,
sides = buttonSides,
sides = presentationModel.sides,
drawScope = this
)

Expand Down Expand Up @@ -827,7 +826,7 @@ internal fun AuroraCommandButton(
height = height,
extraInsets = 1.0f,
isInner = true,
sides = buttonSides,
sides = presentationModel.sides,
drawScope = this
) else null

Expand Down Expand Up @@ -981,7 +980,7 @@ internal fun AuroraCommandButton(
height = height,
extraInsets = 0.5f,
isInner = false,
sides = buttonSides,
sides = presentationModel.sides,
drawScope = this
)

Expand Down Expand Up @@ -1023,7 +1022,7 @@ internal fun AuroraCommandButton(
height = height,
extraInsets = 1.0f,
isInner = true,
sides = buttonSides,
sides = presentationModel.sides,
drawScope = this
) else null

Expand Down
Expand Up @@ -224,8 +224,7 @@ internal fun AuroraCommandButtonPanel(
extraAction = extraAction,
extraActionPreview = commandPreviewListener,
presentationModel = commandPresentation,
overlays = overlays,
buttonSides = Sides()
overlays = overlays
)

val preLayoutInfo =
Expand Down
Expand Up @@ -56,18 +56,23 @@ private fun CommandButtonStripContent(
(index == 0) -> emptySet()
else -> setOf(leadingSide)
}
var currentPresentationModel = commandButtonPresentationModel.overlayWith(
overlay = CommandButtonPresentationModel.Overlay(
sides = Sides(openSides = openSides, straightSides = straightSides)
)
)
if (overlays.containsKey(command)) {
currentPresentationModel = currentPresentationModel.overlayWith(overlay = overlays[command]!!)
}
AuroraCommandButton(
modifier = Modifier,
actionInteractionSource = remember { MutableInteractionSource() },
popupInteractionSource = remember { MutableInteractionSource() },
command = command,
parentWindow = window,
extraAction = null,
presentationModel = if (overlays.containsKey(command))
commandButtonPresentationModel.overlayWith(overlay = overlays[command]!!)
else commandButtonPresentationModel,
overlays = overlays,
buttonSides = Sides(openSides = openSides, straightSides = straightSides)
presentationModel = currentPresentationModel,
overlays = overlays
)
}
}
Expand Down
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.ui.unit.dp
import org.pushingpixels.aurora.theming.BackgroundAppearanceStrategy
import org.pushingpixels.aurora.theming.IconFilterStrategy
import org.pushingpixels.aurora.theming.PopupPlacementStrategy
import org.pushingpixels.aurora.theming.Sides

object CommandButtonSizingConstants {
val WideButtonContentPadding = PaddingValues(start = 10.dp, top = 3.dp, end = 10.dp, bottom = 4.dp)
Expand Down Expand Up @@ -68,7 +69,8 @@ data class CommandButtonPresentationModel(
val horizontalGapScaleFactor: Float = 1.0f,
val verticalGapScaleFactor: Float = 1.0f,
val minWidth: Dp = 0.dp,
val isMenu: Boolean = false
val isMenu: Boolean = false,
val sides : Sides = Sides()
): PresentationModel {
data class Overlay(
val presentationState: CommandButtonPresentationState? = null,
Expand All @@ -92,6 +94,7 @@ data class CommandButtonPresentationModel(
val horizontalGapScaleFactor: Float? = null,
val verticalGapScaleFactor: Float? = null,
val isMenu: Boolean? = null,
val sides : Sides? = null
)

fun overlayWith(overlay: Overlay): CommandButtonPresentationModel {
Expand All @@ -117,7 +120,8 @@ data class CommandButtonPresentationModel(
contentPadding = overlay.contentPadding ?: this.contentPadding,
horizontalGapScaleFactor = overlay.horizontalGapScaleFactor ?: this.horizontalGapScaleFactor,
verticalGapScaleFactor = overlay.verticalGapScaleFactor ?: this.verticalGapScaleFactor,
isMenu = overlay.isMenu ?: this.isMenu
isMenu = overlay.isMenu ?: this.isMenu,
sides = overlay.sides ?: this.sides
)
}
}
Expand Up @@ -47,10 +47,7 @@ class CommandButtonProjection(
parentWindow = window,
extraAction = null,
presentationModel = this.presentationModel,
overlays = this.overlays ?: mapOf(),
buttonSides = Sides(
straightSides = if (presentationModel.isMenu) Side.values().toSet() else emptySet()
)
overlays = this.overlays ?: mapOf()
)
}
}
Expand Down
Expand Up @@ -511,7 +511,8 @@ private fun PopupGeneralContent(
backgroundAppearanceStrategy = BackgroundAppearanceStrategy.Flat,
horizontalAlignment = HorizontalAlignment.Leading,
contentPadding = menuPresentationModel.menuContentPadding,
isMenu = true
isMenu = true,
sides = Sides(straightSides = Side.values().toSet())
)

var runningCommandIndex = 0
Expand Down Expand Up @@ -559,8 +560,7 @@ private fun PopupGeneralContent(
}
},
presentationModel = currSecondaryPresentationModel,
overlays = overlays,
buttonSides = Sides(straightSides = Side.values().toSet())
overlays = overlays
)
runningCommandIndex++
}
Expand Down

0 comments on commit 65b8aca

Please sign in to comment.