Skip to content

Commit

Permalink
Start wiring ribbon contextual menu
Browse files Browse the repository at this point in the history
Display contextual menu for the ribbon and wire it to some of the ribbon's contextual menu listener logic for galleries. For #56
  • Loading branch information
kirill-grouchnikov committed Sep 1, 2023
1 parent 9c0ce95 commit 6497c00
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class CommandButtonProjection(
) : BaseCommandButtonProjection<Command, CommandButtonPresentationModel, CommandButtonProjection>(
contentModel, presentationModel, secondaryOverlays
) {
@OptIn(AuroraInternalApi::class)
@Composable
fun project(
modifier: Modifier = Modifier,
Expand All @@ -151,6 +152,7 @@ class CommandButtonProjection(
)
}

@OptIn(AuroraInternalApi::class)
@Composable
override fun reproject(modifier: Modifier) {
project(
Expand All @@ -162,6 +164,7 @@ class CommandButtonProjection(
)
}

@OptIn(AuroraInternalApi::class)
@Composable
override fun reproject(
modifier: Modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,54 @@ import org.pushingpixels.aurora.common.AuroraInternalApi
import org.pushingpixels.aurora.component.model.ContentModel
import org.pushingpixels.aurora.component.model.PresentationModel
import org.pushingpixels.aurora.component.projection.Projection
import org.pushingpixels.aurora.component.ribbon.RibbonGalleryProjection
import org.pushingpixels.aurora.component.utils.AuroraRect
import org.pushingpixels.aurora.component.utils.contains

internal object BoundsTracker {
private val bounds: MutableMap<Projection<ContentModel, PresentationModel>, AuroraRect> = hashMapOf()

fun trackBounds(projection: Projection<ContentModel, PresentationModel>, rect: AuroraRect) {
bounds[projection] = rect
println("Added tracking, total ${bounds.size}")
// if (projection is RibbonGalleryProjection) {
// println("Gallery at $rect")
// }
//println("Added tracking, total ${bounds.size}")
}

fun untrackBounds(projection: Projection<ContentModel, PresentationModel>) {
bounds.remove(projection)
println("Removed tracking, total ${bounds.size}")
//println("Removed tracking, total ${bounds.size}")
}

internal fun getBounds(): MutableMap<Projection<ContentModel, PresentationModel>, AuroraRect> = bounds
}

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

@AuroraInternalApi
@Composable
fun RibbonOverlay(modifier: Modifier, insets: Dp) {
Canvas(modifier = modifier) {
for (tracked in BoundsTracker.getBounds().values) {
for (tracked in BoundsTracker.getBounds().entries) {
val color = when (tracked.key) {
is RibbonGalleryProjection -> Color.Blue
else -> Color.Red
}
drawRect(
color = Color.Red,
topLeft = Offset(tracked.x - insets.toPx(), tracked.y - insets.toPx()),
size = Size(tracked.width, tracked.height),
color = color,
topLeft = Offset(tracked.value.x - insets.toPx(), tracked.value.y - insets.toPx()),
size = Size(tracked.value.width, tracked.value.height),
style = Stroke(width=1.0f, cap= StrokeCap.Butt, join= StrokeJoin.Round)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import org.pushingpixels.aurora.common.AuroraInternalApi
import org.pushingpixels.aurora.component.AuroraVerticalScrollbar
import org.pushingpixels.aurora.component.ScrollBarSizingConstants
import org.pushingpixels.aurora.component.getPreferredCommandPopupMenuPanelSize
Expand All @@ -51,7 +52,8 @@ import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt

internal data class GeneralPopupContentLayoutInfo(
@AuroraInternalApi
data class GeneralPopupContentLayoutInfo(
override val popupSize: Size,
val fullSize: Size,
val buttonPanelSize: Size,
Expand All @@ -62,7 +64,8 @@ internal data class GeneralPopupContentLayoutInfo(
val gutterWidth: Float
) : BaseCascadingCommandMenuPopupLayoutInfo

internal object GeneralCommandMenuPopupHandler : CascadingCommandMenuHandler<
@AuroraInternalApi
object GeneralCommandMenuPopupHandler : CascadingCommandMenuHandler<
CommandMenuContentModel, CommandPopupMenuPresentationModel, GeneralPopupContentLayoutInfo> {
override fun getPopupContentLayoutInfo(
menuContentModel: CommandMenuContentModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import java.awt.GraphicsEnvironment
import java.text.MessageFormat
import java.util.*
import javax.swing.JColorChooser
import javax.swing.JOptionPane
import kotlin.system.exitProcess

fun main() = auroraApplication {
Expand Down Expand Up @@ -433,14 +434,80 @@ fun main() = auroraApplication {

val applicationMenuCommandButtonProjection = builder.getApplicationMenuCommandButtonProjection()

val onShowContextualMenuListener = object : OnShowContextualMenuListener {
private fun build(ribbon: Ribbon, vararg commands: Command): CommandMenuContentModel {
val allCommands: MutableList<Command> = arrayListOf()
if (commands.isNotEmpty()) {
allCommands.addAll(commands)
}

if (minimizedMode) {
allCommands.add(Command(
text = resourceBundle.getString("ContextMenu.showRibbon"),
action = { minimizedMode = false }
))
} else {
allCommands.add(Command(
text = resourceBundle.getString("ContextMenu.hideRibbon"),
action = { minimizedMode = true }
))
}
allCommands.add(Command(
text = resourceBundle.getString("ContextMenu.configureRibbon"),
action = { println("Configure ribbon option selected") }
))

return CommandMenuContentModel(groups = listOf(CommandGroup(commands = allCommands.toList())))
}

override fun getContextualMenuContentModel(ribbon: Ribbon): CommandMenuContentModel {
return build(ribbon)
}

override fun <C : ContentModel, P : PresentationModel> getContextualMenuContentModel(
ribbon: Ribbon,
componentProjection: Projection<C, P>
): CommandMenuContentModel {
TODO("Not yet implemented")
}

override fun getContextualMenuContentModel(
ribbon: Ribbon,
commandProjection: CommandButtonProjection
): CommandMenuContentModel {
TODO("Not yet implemented")
}

override fun getContextualMenuContentModel(
ribbon: Ribbon,
galleryProjection: RibbonGalleryProjection
): CommandMenuContentModel {
val isInTaskbar = ribbon.taskbarElements.find {
(it is RibbonTaskbarGalleryProjection) &&
(it.galleryContentModel == galleryProjection.contentModel)
} != null
val galleryCommand = if (isInTaskbar) {
Command(text = resourceBundle.getString("ContextMenu.removeFromTaskbar"),
action = { println("Dummy") }
)
} else {
Command(text = resourceBundle.getString("ContextMenu.addToTaskbar"),
action = { println("Dummy") }
)
}
return build(ribbon, galleryCommand)
}
}

val ribbon = Ribbon(
tasks = listOf(pageLayoutTask, writeTask, animationsTask, wrappedTask),
contextualTaskGroups = contextualTaskGroups,
taskbarElements = taskbarElements,
taskbarKeyTipPolicy = DefaultRibbonTaskbarKeyTipPolicy(),
anchoredCommands = builder.getAnchoredCommands(),
applicationMenuCommandButtonProjection = applicationMenuCommandButtonProjection,
isMinimized = minimizedMode
isMinimized = minimizedMode,
onShowContextualMenuListener = onShowContextualMenuListener
)

val vmf = MessageFormat(resourceBundle.getString("GroupVisibility.text"))
Expand Down
Loading

0 comments on commit 6497c00

Please sign in to comment.