@@ -3,19 +3,23 @@ package processing.app.ui
33
44import androidx.compose.foundation.background
55import androidx.compose.foundation.clickable
6+ import androidx.compose.foundation.layout.Arrangement
67import androidx.compose.foundation.layout.Box
78import androidx.compose.foundation.layout.Row
89import androidx.compose.foundation.layout.fillMaxWidth
910import androidx.compose.foundation.layout.padding
11+ import androidx.compose.foundation.layout.heightIn
1012import androidx.compose.material.Text
1113import androidx.compose.runtime.Composable
1214import androidx.compose.runtime.mutableStateOf
1315import androidx.compose.runtime.remember
16+ import androidx.compose.ui.Alignment
1417import androidx.compose.ui.Modifier
1518import androidx.compose.ui.awt.ComposePanel
1619import androidx.compose.ui.graphics.Color
1720import androidx.compose.ui.layout.onGloballyPositioned
1821import androidx.compose.ui.layout.positionInRoot
22+ import androidx.compose.ui.text.style.TextAlign
1923import androidx.compose.ui.unit.IntOffset
2024import androidx.compose.ui.unit.dp
2125import processing.app.Base
@@ -34,64 +38,96 @@ fun themeColorOrFallback(key: String, fallback: AwtColor): Color {
3438 return awtToCompose(awt)
3539}
3640
41+ data class TopBarItemData (
42+ val label : String ,
43+ val onClick : (ComposePanel , Base , Int , Int ) -> Unit
44+ )
45+
3746@Composable
3847fun TopBar (panel : ComposePanel , base : Base ) {
3948 val blueBarColor = themeColorOrFallback(" toolbar.gradient.top" , AwtColor (107 , 160 , 204 ))
4049 val textColor = themeColorOrFallback(" toolbar.rollover.color" , AwtColor (0 , 0 , 0 ))
4150
42- val developAnchor = remember { mutableStateOf(IntOffset .Zero ) }
43- val developHeight = remember { mutableStateOf(0 ) }
51+ val items = listOf (
52+ TopBarItemData (" File" ) { p, b, x, y ->
53+ showMenuPopup(p, b, x, y)
54+ },
55+ TopBarItemData (" Edit" ) { p, b, x, y ->
56+ showMenuPopup(p, b, x, y)
57+ },
58+ TopBarItemData (" Sketch" ) { p, b, x, y ->
59+ showMenuPopup(p, b, x, y)
60+ },
61+ TopBarItemData (" Debug" ) { p, b, x, y ->
62+ showMenuPopup(p, b, x, y)
63+ },
64+ TopBarItemData (" Tools" ) { p, b, x, y ->
65+ showMenuPopup(p, b, x, y)
66+ },
67+ TopBarItemData (" Help" ) { p, b, x, y ->
68+ showMenuPopup(p, b, x, y)
69+ },
70+ TopBarItemData (" Develop" ) { p, b, x, y ->
71+ showDevelopPopup(p, b, x, y)
72+ }
73+ )
4474
4575 Row (
4676 modifier = Modifier
4777 .fillMaxWidth()
48- .background(blueBarColor)
49- .padding(start = 8 .dp)
78+ .background(blueBarColor),
79+ horizontalArrangement = Arrangement .SpaceEvenly ,
80+ verticalAlignment = Alignment .CenterVertically
5081 ) {
51- Box (
52- modifier = Modifier .onGloballyPositioned { coordinates ->
53- val position = coordinates.positionInRoot()
54- developAnchor.value = IntOffset (
55- position.x.toInt(),
56- position.y.toInt()
57- )
58- developHeight.value = coordinates.size.height
82+ items.forEach { item ->
83+ TopBarItem (
84+ label = item.label,
85+ textColor = textColor,
86+ modifier = Modifier .weight(1f )
87+ ) { x, y ->
88+ item.onClick(panel, base, x, y)
5989 }
60-
61- ) {
62- Text (
63- text = " Develop" ,
64- color = textColor,
65- modifier = Modifier
66- .padding(horizontal = 8 .dp, vertical = 4 .dp)
67- .clickable {
68- showDevelopPopup(
69- panel = panel,
70- base = base,
71- x = developAnchor.value.x,
72- y = developAnchor.value.y + developHeight.value
73- )
74- }
75- )
76- Text (
77- text = " New Button" ,
78- color = textColor,
79- modifier = Modifier
80-
81- .padding(horizontal = 40 .dp, vertical = 4 .dp)
82- .clickable {
83- showDevelopPopup(
84- panel = panel,
85- base = base,
86- x = developAnchor.value.x,
87- y = developAnchor.value.y + developHeight.value
88- )
89- }
90- )
9190 }
9291 }
9392}
9493
94+ @Composable
95+ private fun TopBarItem (
96+ label : String ,
97+ textColor : Color ,
98+ modifier : Modifier = Modifier ,
99+ onClick : (x: Int , y: Int ) -> Unit
100+ ) {
101+ val anchor = remember { mutableStateOf(IntOffset .Zero ) } // stores the location of the top bar item
102+ val height = remember { mutableStateOf(0 ) } // stores the height of the top bar item
103+
104+ Box (
105+ modifier = modifier
106+ .heightIn(min = 32 .dp)
107+ .onGloballyPositioned { coordinates ->
108+ val position = coordinates.positionInRoot() // top left corner of drop down
109+ anchor.value = IntOffset (position.x.toInt(), position.y.toInt()) // x and y positioning
110+ height.value = coordinates.size.height // height of the menu
111+ }
112+ .clickable {
113+ onClick(anchor.value.x, anchor.value.y + height.value)
114+ // anchor x lines up menu with top left edge of the clicked menu
115+ // anchor y + height value moves the pop up below the tool bar item
116+ }
117+ .padding(vertical = 4 .dp),
118+ contentAlignment = Alignment .Center
119+ ) {
120+ Text (
121+ text = label,
122+ color = textColor,
123+ textAlign = TextAlign .Center ,
124+ modifier = Modifier
125+ .fillMaxWidth()
126+ .padding(horizontal = 0 .dp, vertical = 4 .dp)
127+ )
128+ }
129+ }
130+
95131private fun showDevelopPopup (panel : ComposePanel , base : Base , x : Int , y : Int ) {
96132 val popup = JPopupMenu ()
97133
@@ -103,14 +139,40 @@ private fun showDevelopPopup(panel: ComposePanel, base: Base, x: Int, y: Int) {
103139 }
104140
105141 popup.add(updatesItem)
106- popup.show(panel, x, y) // this is ignoring the os differences
142+ popup.show(panel, x, y)
143+ }
144+
145+ private fun showMenuPopup (panel : ComposePanel , base : Base , x : Int , y : Int ) {
146+ val popup = JPopupMenu ()
147+
148+ val fileNew = JMenuItem (" New" )
149+ fileNew.addActionListener {
150+ base.handleNew()
151+ }
152+ popup.add(fileNew);
153+
154+ val fileOpen = JMenuItem (" Open" )
155+ fileOpen.addActionListener {
156+ base.handleOpenPrompt();
157+ }
158+
159+ popup.add(fileOpen);
160+
161+
162+
163+
164+ popup.show(panel, x, y)
107165}
108166
167+
168+
109169fun mountTopBar (panel : ComposePanel , base : Base ) {
110170 val awtBg = Theme .getColor(" toolbar.gradient.top" ) ? : AwtColor (107 , 160 , 204 )
111171 panel.background = awtBg
112172
113173 panel.setContent {
114174 TopBar (panel, base)
115175 }
116- }
176+ }
177+
178+
0 commit comments