Skip to content

Commit

Permalink
Wire keytips to taskbar content
Browse files Browse the repository at this point in the history
Also support disabled look for keytips. For #56
  • Loading branch information
kirill-grouchnikov committed Sep 13, 2023
1 parent 82141b6 commit ad26290
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ fun AuroraRect.contains(x: Float, y: Float): Boolean {
(y < (this.y + this.height))
}

@AuroraInternalApi
val AuroraRect.isEmpty: Boolean
get() = (this.width == 0.0f) && (this.height == 0.0f)

@AuroraInternalApi
fun AuroraOffset.asOffset(density: Density): Offset {
return Offset(x / density.density, y / density.density)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private class CommandButtonDrawingCache(
isDark = false
),
val markPath: Path = Path()
): DrawingCache
) : DrawingCache

private fun Modifier.commandButtonActionHoverable(
interactionSource: MutableInteractionSource,
Expand Down Expand Up @@ -882,6 +882,8 @@ internal fun <M : BaseCommandMenuContentModel,
Layout(
modifier = modifier.commandButtonLocator(
originalProjection,
command,
presentationModel,
buttonTopLeftOffset,
buttonSize,
trackBounds,
Expand Down Expand Up @@ -1604,13 +1606,15 @@ internal fun <M : BaseCommandMenuContentModel,
KeyTipTracker.trackKeyTipOffset(
originalProjection,
presentationModel.actionKeyTip!!,
command.isActionEnabled,
layoutManager.getActionKeyTipAnchorCenterPoint(command, presentationModel, layoutInfo)
)
}
if ((presentationModel.popupKeyTip != null) && !layoutInfo.popupClickArea.isEmpty) {
KeyTipTracker.trackKeyTipOffset(
originalProjection,
presentationModel.popupKeyTip!!,
command.isSecondaryEnabled,
layoutManager.getPopupKeyTipAnchorCenterPoint(command, presentationModel, layoutInfo)
)
}
Expand Down Expand Up @@ -2040,6 +2044,7 @@ private fun CommandButtonKeyTip(
keyTipInfo = KeyTipTracker.KeyTipInfo(
projection = originalProjection,
keyTip = keyTip,
isEnabled = isEnabled,
screenRect = AuroraRect(0.0f, 0.0f, buttonSize.width.toFloat(), buttonSize.height.toFloat()),
anchor = Offset(size.width / 2.0f, size.height / 2.0f)
),
Expand All @@ -2059,7 +2064,9 @@ private fun CommandButtonKeyTip(

@OptIn(AuroraInternalApi::class)
private class CommandButtonLocator(
val projection: BaseCommandButtonProjection<*, *, *>,
val originalProjection: BaseCommandButtonProjection<*, *, *>,
val command: BaseCommand,
val presentationModel: BaseCommandButtonPresentationModel,
val topLeftOffset: AuroraOffset,
val size: MutableState<IntSize>,
val trackBounds: Boolean,
Expand All @@ -2082,21 +2089,23 @@ private class CommandButtonLocator(
height = coordinates.size.height.toFloat()
)
if (trackBounds) {
BoundsTracker.trackBounds(projection, bounds)
BoundsTracker.trackBounds(originalProjection, bounds)
}

if (trackKeyTips) {
if (projection.presentationModel.actionKeyTip != null) {
if (presentationModel.actionKeyTip != null) {
KeyTipTracker.trackKeyTipBase(
projection,
projection.presentationModel.actionKeyTip!!,
originalProjection,
presentationModel.actionKeyTip!!,
command.isActionEnabled,
bounds
)
}
if (projection.presentationModel.popupKeyTip != null) {
if (presentationModel.popupKeyTip != null) {
KeyTipTracker.trackKeyTipBase(
projection,
projection.presentationModel.popupKeyTip!!,
originalProjection,
presentationModel.popupKeyTip!!,
command.isSecondaryEnabled,
bounds
)
}
Expand All @@ -2107,9 +2116,21 @@ private class CommandButtonLocator(
@OptIn(AuroraInternalApi::class)
@Composable
private fun Modifier.commandButtonLocator(
projection: BaseCommandButtonProjection<*, *, *>,
originalProjection: BaseCommandButtonProjection<*, *, *>,
command: BaseCommand,
presentationModel: BaseCommandButtonPresentationModel,
topLeftOffset: AuroraOffset,
size: MutableState<IntSize>,
trackBounds: Boolean,
trackKeyTips: Boolean
) = this.then(CommandButtonLocator(projection, topLeftOffset, size, trackBounds, trackKeyTips))
) = this.then(
CommandButtonLocator(
originalProjection,
command,
presentationModel,
topLeftOffset,
size,
trackBounds,
trackKeyTips
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ internal fun <C : ContentModel, P : PresentationModel> RibbonMetaComponent(
KeyTipTracker.trackKeyTipOffset(
originalProjection,
originalProjection.ribbonComponentPresentationModel.keyTip,
originalProjection.enabled.invoke(),
Offset(captionMid.toFloat(), height / 2.0f)
)
} else {
Expand All @@ -334,6 +335,7 @@ internal fun <C : ContentModel, P : PresentationModel> RibbonMetaComponent(
KeyTipTracker.trackKeyTipOffset(
originalProjection,
originalProjection.ribbonComponentPresentationModel.keyTip,
originalProjection.enabled.invoke(),
Offset(componentMid, height / 2.0f)
)
}
Expand Down Expand Up @@ -394,6 +396,7 @@ private class MetaComponentLocator(
KeyTipTracker.trackKeyTipBase(
projection,
projection.presentationModel.ribbonComponentPresentationModel.keyTip!!,
projection.enabled.invoke(),
bounds
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import androidx.compose.ui.unit.*
import org.jetbrains.skia.*
import org.pushingpixels.aurora.common.AuroraInternalApi
import org.pushingpixels.aurora.common.AuroraRect
import org.pushingpixels.aurora.common.isEmpty
import org.pushingpixels.aurora.component.model.ContentModel
import org.pushingpixels.aurora.component.model.PresentationModel
import org.pushingpixels.aurora.component.projection.Projection
Expand All @@ -52,6 +53,7 @@ object KeyTipTracker {
data class KeyTipInfo(
var projection: Projection<ContentModel, PresentationModel>,
var keyTip: String,
var isEnabled: Boolean,
var screenRect: AuroraRect,
var anchor: Offset
)
Expand All @@ -61,6 +63,7 @@ object KeyTipTracker {
fun trackKeyTipBase(
projection: Projection<ContentModel, PresentationModel>,
keyTip: String,
isEnabled: Boolean,
screenRect: AuroraRect
) {
val existing = keyTips.find {
Expand All @@ -69,13 +72,14 @@ object KeyTipTracker {
if (existing != null) {
existing.screenRect = screenRect.copy()
} else {
keyTips.add(KeyTipInfo(projection, keyTip, screenRect, Offset.Zero))
keyTips.add(KeyTipInfo(projection, keyTip, isEnabled, screenRect, Offset.Zero))
}
}

fun trackKeyTipOffset(
projection: Projection<ContentModel, PresentationModel>,
keyTip: String,
isEnabled: Boolean,
anchor: Offset
) {
val existing = keyTips.find {
Expand All @@ -84,7 +88,12 @@ object KeyTipTracker {
if (existing != null) {
existing.anchor = anchor.copy()
} else {
keyTips.add(KeyTipInfo(projection, keyTip, AuroraRect(0.0f, 0.0f, 0.0f, 0.0f), anchor.copy()))
keyTips.add(
KeyTipInfo(
projection, keyTip, isEnabled,
AuroraRect(0.0f, 0.0f, 0.0f, 0.0f), anchor.copy()
)
)
}
}

Expand Down Expand Up @@ -123,18 +132,20 @@ fun RibbonKeyTipOverlay(modifier: Modifier, insets: Dp) {

Canvas(modifier = modifier) {
for (tracked in KeyTipTracker.getKeyTips()) {
drawKeyTip(
tracked,
textStyle,
density,
fontFamilyResolver,
layoutDirection,
insets,
drawingCache,
decorationAreaType,
skinColors,
painters
)
if (!tracked.screenRect.isEmpty) {
drawKeyTip(
tracked,
textStyle,
density,
fontFamilyResolver,
layoutDirection,
insets,
drawingCache,
decorationAreaType,
skinColors,
painters
)
}
}
}
}
Expand All @@ -145,7 +156,7 @@ internal fun getKeyTipSize(
density: Density,
fontFamilyResolver: FontFamily.Resolver,
layoutDirection: LayoutDirection
) : Pair<Size, Float> {
): Pair<Size, Float> {
val leftPadding = KeyTipPaddingValues.calculateLeftPadding(layoutDirection)
val rightPadding = KeyTipPaddingValues.calculateRightPadding(layoutDirection)
val topPadding = KeyTipPaddingValues.calculateTopPadding()
Expand All @@ -157,7 +168,8 @@ internal fun getKeyTipSize(
density = density, maxLines = 1, fontFamilyResolver = fontFamilyResolver
)

val tipWidth = leftPadding.value * density.density + paragraph.maxIntrinsicWidth + rightPadding.value * density.density
val tipWidth =
leftPadding.value * density.density + paragraph.maxIntrinsicWidth + rightPadding.value * density.density
val tipHeight = topPadding.value * density.density + paragraph.height + bottomPadding.value * density.density

return Pair(Size(tipWidth, tipHeight), paragraph.firstBaseline)
Expand All @@ -183,12 +195,10 @@ internal fun DrawScope.drawKeyTip(
val leftPadding = KeyTipPaddingValues.calculateLeftPadding(layoutDirection)
val topPadding = KeyTipPaddingValues.calculateTopPadding()

val fillScheme = skinColors.getColorScheme(decorationAreaType, ComponentState.Enabled)
val borderScheme = skinColors.getColorScheme(
decorationAreaType,
ColorSchemeAssociationKind.Border, ComponentState.Enabled
)
val alpha = skinColors.getAlpha(decorationAreaType, ComponentState.Enabled)
val state = if (keyTipInfo.isEnabled) ComponentState.Enabled else ComponentState.DisabledUnselected
val fillScheme = skinColors.getColorScheme(decorationAreaType, state)
val borderScheme = skinColors.getColorScheme(decorationAreaType, ColorSchemeAssociationKind.Border, state)
val alpha = skinColors.getAlpha(decorationAreaType, state)
val fillPainter = painters.fillPainter
val borderPainter = painters.borderPainter
val buttonShaper = ClassicButtonShaper.Instance
Expand Down
Loading

0 comments on commit ad26290

Please sign in to comment.