Skip to content

Commit

Permalink
Wire ribbon context menu on command button projections
Browse files Browse the repository at this point in the history
For #56
  • Loading branch information
kirill-grouchnikov committed Sep 1, 2023
1 parent 09efe99 commit cc1f7a2
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ interface OnShowContextualMenuListener {

fun getContextualMenuContentModel(
ribbon: Ribbon,
commandProjection: CommandButtonProjection
commandProjection: BaseCommandButtonProjection<*, *, *>
): CommandMenuContentModel

fun getContextualMenuContentModel(ribbon: Ribbon): CommandMenuContentModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ fun getComponentProjectionUnder(x: Float, y: Float) : Projection<*, *>? {
return null
}

@AuroraInternalApi
fun getCommandButtonProjectionUnder(x: Float, y: Float) : BaseCommandButtonProjection<*, *, *>? {
for (tracked in BoundsTracker.getBounds().entries) {
if (tracked.key is BaseCommandButtonProjection<*, *, *>) {
if (tracked.value.contains(x, y)) {
return tracked.key as BaseCommandButtonProjection<*, *, *>
}
}
}
return null
}

@AuroraInternalApi
@Composable
fun RibbonOverlay(modifier: Modifier, insets: Dp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,48 +382,43 @@ fun main() = auroraApplication {
)
)

val taskbarElements: MutableList<RibbonTaskbarElement> = mutableStateListOf()
taskbarElements.add(
RibbonTaskbarCommandProjection(
CommandButtonProjection(
contentModel = builder.pasteCommand,
presentationModel = CommandButtonPresentationModel()
)
)
)
taskbarElements.add(
RibbonTaskbarCommandProjection(
CommandButtonProjection(
contentModel = Command(
text = "",
icon = edit_clear(),
action = { println("Taskbar Clear activated") },
isActionEnabled = false
),
presentationModel = CommandButtonPresentationModel()
)
)
)
taskbarElements.add(
RibbonTaskbarComponentProjection(
ComboBoxProjection(
contentModel = fontFamilyComboBoxContentModel,
presentationModel = ComboBoxPresentationModel(displayConverter = { it.name }),
val taskbarElements: MutableList<RibbonTaskbarElement> = remember {
mutableStateListOf(
RibbonTaskbarCommandProjection(
CommandButtonProjection(
contentModel = builder.pasteCommand,
presentationModel = CommandButtonPresentationModel()
)
),
RibbonTaskbarCommandProjection(
CommandButtonProjection(
contentModel = Command(
text = "",
icon = edit_clear(),
action = { println("Taskbar Clear activated") },
isActionEnabled = false
),
presentationModel = CommandButtonPresentationModel()
)
),
RibbonTaskbarComponentProjection(
ComboBoxProjection(
contentModel = fontFamilyComboBoxContentModel,
presentationModel = ComboBoxPresentationModel(displayConverter = { it.name }),
)
),
// Add the same gallery we have in the first ribbon task to the taskbar, configuring
// its popup presentation with a 4x2 grid of slightly smaller buttons (instead of a 3x3
// grid of slightly larger ones in the in-task gallery popup).
// Content preview and selection is controlled by the same model and is kept in sync
// along all usages of the gallery content model in our ribbon.
RibbonTaskbarGalleryProjection(
galleryContentModel = styleGalleryContentModel,
galleryMetaPresentationModel = styleGalleryTaskbarMetaPresentationModel,
galleryInlineState = ribbonState.documentStyleGalleryInlineState
)
)
)
// Add the same gallery we have in the first ribbon task to the taskbar, configuring
// its popup presentation with a 4x2 grid of slightly smaller buttons (instead of a 3x3
// grid of slightly larger ones in the in-task gallery popup).
// Content preview and selection is controlled by the same model and is kept in sync
// along all usages of the gallery content model in our ribbon.
taskbarElements.add(
RibbonTaskbarGalleryProjection(
galleryContentModel = styleGalleryContentModel,
galleryMetaPresentationModel = styleGalleryTaskbarMetaPresentationModel,
galleryInlineState = ribbonState.documentStyleGalleryInlineState
)
)
}

var minimizedMode by remember { mutableStateOf(false) }
var contextualTaskGroup1Visible by remember { mutableStateOf(false) }
Expand Down Expand Up @@ -513,9 +508,29 @@ fun main() = auroraApplication {

override fun getContextualMenuContentModel(
ribbon: Ribbon,
commandProjection: CommandButtonProjection
commandProjection: BaseCommandButtonProjection<*, *, *>
): CommandMenuContentModel {
TODO("Not yet implemented")
val isInTaskbar = ribbon.taskbarElements.find {
(it is RibbonTaskbarCommandProjection) &&
(it.commandProjection.contentModel == commandProjection.contentModel)
} != null
val buttonCommand = if (isInTaskbar) {
Command(text = resourceBundle.getString("ContextMenu.removeFromTaskbar"),
action = {
taskbarElements.removeIf {
(it is RibbonTaskbarCommandProjection) &&
(it.commandProjection.contentModel == commandProjection.contentModel)
}
}
)
} else {
Command(text = resourceBundle.getString("ContextMenu.addToTaskbar"),
action = {
taskbarElements.add(RibbonTaskbarCommandProjection(commandProjection))
}
)
}
return build(ribbon, buttonCommand)
}

override fun getContextualMenuContentModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ import org.pushingpixels.aurora.component.projection.LabelProjection
import org.pushingpixels.aurora.component.ribbon.Ribbon
import org.pushingpixels.aurora.component.ribbon.RibbonTask
import org.pushingpixels.aurora.component.ribbon.RibbonTaskbarGalleryProjection
import org.pushingpixels.aurora.component.ribbon.impl.RibbonOverlay
import org.pushingpixels.aurora.component.ribbon.impl.RibbonTaskbar
import org.pushingpixels.aurora.component.ribbon.impl.getComponentProjectionUnder
import org.pushingpixels.aurora.component.ribbon.impl.getGalleryProjectionUnder
import org.pushingpixels.aurora.component.ribbon.impl.*
import org.pushingpixels.aurora.component.utils.TransitionAwarePainter
import org.pushingpixels.aurora.component.utils.TransitionAwarePainterDelegate
import org.pushingpixels.aurora.component.utils.popup.GeneralCommandMenuPopupHandler
Expand Down Expand Up @@ -611,6 +608,8 @@ private fun AuroraWindowScope.RibbonWindowInnerContent(
windowTitlePaneConfiguration
)

println("Ribbon taskbar with ${ribbon.taskbarElements.size} elements")

AuroraDecorationArea(decorationAreaType = DecorationAreaType.Header) {
Column(Modifier.fillMaxWidth().auroraBackground()) {
RibbonPrimaryBar(
Expand Down Expand Up @@ -989,10 +988,20 @@ private fun Modifier.ribbonContextMenu(ribbon: Ribbon): Modifier {
)
}
} else {
contentModel.apply {
value = ribbon.onShowContextualMenuListener!!.getContextualMenuContentModel(
ribbon = ribbon,
)
val ribbonCommandButton = getCommandButtonProjectionUnder(eventX, eventY)
if (ribbonCommandButton != null) {
contentModel.apply {
value = ribbon.onShowContextualMenuListener!!.getContextualMenuContentModel(
ribbon = ribbon,
commandProjection = ribbonCommandButton
)
}
} else {
contentModel.apply {
value = ribbon.onShowContextualMenuListener!!.getContextualMenuContentModel(
ribbon = ribbon,
)
}
}
}
}
Expand Down

0 comments on commit cc1f7a2

Please sign in to comment.