-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move layout manager for the app menu button to its own internal file
For #56
- Loading branch information
1 parent
292e1c9
commit e8c501c
Showing
2 changed files
with
126 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...ain/kotlin/org/pushingpixels/aurora/component/utils/appmenu/AppMenuButtonLayoutManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright 2020-2023 Aurora, Kirill Grouchnikov | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.pushingpixels.aurora.component.utils.appmenu | ||
|
||
import androidx.compose.ui.geometry.Rect | ||
import androidx.compose.ui.geometry.Size | ||
import androidx.compose.ui.text.Paragraph | ||
import androidx.compose.ui.text.TextStyle | ||
import androidx.compose.ui.text.font.FontFamily | ||
import androidx.compose.ui.unit.Constraints | ||
import androidx.compose.ui.unit.Density | ||
import androidx.compose.ui.unit.DpSize | ||
import androidx.compose.ui.unit.LayoutDirection | ||
import org.pushingpixels.aurora.component.layout.CommandButtonLayoutManager | ||
import org.pushingpixels.aurora.component.model.BaseCommand | ||
import org.pushingpixels.aurora.component.model.BaseCommandButtonPresentationModel | ||
import org.pushingpixels.aurora.component.model.CommandButtonKind | ||
|
||
internal class CommandButtonLayoutManagerRibbonApplicationMenuButton( | ||
override val layoutDirection: LayoutDirection, | ||
private val _density: Density, | ||
private val textStyle: TextStyle, | ||
private val fontFamilyResolver: FontFamily.Resolver | ||
) : CommandButtonLayoutManager { | ||
override val density = _density.density | ||
override val fontScale = _density.fontScale | ||
|
||
override fun getPreferredIconSize( | ||
command: BaseCommand, | ||
presentationModel: BaseCommandButtonPresentationModel | ||
): DpSize { | ||
return DpSize.Zero | ||
} | ||
|
||
override fun getPreferredSize( | ||
command: BaseCommand, | ||
presentationModel: BaseCommandButtonPresentationModel, | ||
preLayoutInfo: CommandButtonLayoutManager.CommandButtonPreLayoutInfo | ||
): Size { | ||
val paddingValues = presentationModel.contentPadding | ||
val by = presentationModel.verticalGapScaleFactor * paddingValues.verticalPaddings.toPx() | ||
val bx = presentationModel.horizontalGapScaleFactor * paddingValues.horizontalPaddings.toPx() | ||
|
||
val paragraph = Paragraph( | ||
text = command.text, style = textStyle, constraints = Constraints(maxWidth = Int.MAX_VALUE), | ||
density = _density, maxLines = 1, fontFamilyResolver = fontFamilyResolver | ||
) | ||
|
||
return Size( | ||
width = bx + paragraph.maxIntrinsicWidth, | ||
height = by + paragraph.height | ||
) | ||
} | ||
|
||
override fun getPreLayoutInfo( | ||
command: BaseCommand, | ||
presentationModel: BaseCommandButtonPresentationModel | ||
): CommandButtonLayoutManager.CommandButtonPreLayoutInfo { | ||
return CommandButtonLayoutManager.CommandButtonPreLayoutInfo( | ||
commandButtonKind = CommandButtonKind.PopupOnly, | ||
showIcon = false, | ||
texts = listOf(command.text), | ||
extraTexts = emptyList(), | ||
isTextInActionArea = true, | ||
separatorOrientation = null, | ||
showPopupIcon = false | ||
) | ||
} | ||
|
||
override fun getLayoutInfo( | ||
constraints: Constraints, | ||
command: BaseCommand, | ||
presentationModel: BaseCommandButtonPresentationModel, | ||
preLayoutInfo: CommandButtonLayoutManager.CommandButtonPreLayoutInfo | ||
): CommandButtonLayoutManager.CommandButtonLayoutInfo { | ||
val preferredSize = getPreferredSize(command, presentationModel, preLayoutInfo) | ||
|
||
val paddingValues = presentationModel.contentPadding | ||
val top = presentationModel.verticalGapScaleFactor * paddingValues.topPadding.toPx() | ||
val left = presentationModel.horizontalGapScaleFactor * | ||
paddingValues.calculateLeftPadding(layoutDirection).toPx() | ||
|
||
val paragraph = Paragraph( | ||
text = command.text, style = textStyle, constraints = Constraints(maxWidth = Int.MAX_VALUE), | ||
density = _density, maxLines = 1, fontFamilyResolver = fontFamilyResolver | ||
) | ||
|
||
return CommandButtonLayoutManager.CommandButtonLayoutInfo( | ||
fullSize = preferredSize, | ||
actionClickArea = Rect.Zero, | ||
popupClickArea = Rect( | ||
left = 0.0f, | ||
right = preferredSize.width, | ||
top = 0.0f, | ||
bottom = preferredSize.height | ||
), | ||
separatorArea = Rect.Zero, | ||
iconRect = Rect.Zero, | ||
textLayoutInfoList = listOf( | ||
CommandButtonLayoutManager.TextLayoutInfo( | ||
text = command.text, | ||
textRect = Rect( | ||
left = left, right = left + paragraph.maxIntrinsicWidth, | ||
top = top, bottom = top + paragraph.height | ||
) | ||
) | ||
), | ||
extraTextLayoutInfoList = emptyList(), | ||
popupActionRect = Rect.Zero | ||
) | ||
} | ||
} |