Search code examples
android-jetpack-composekotlin-multiplatform

Jetpack Compose Desktop is there a way to add elevation to window? How can I set minimun size of window?


I'm having trouble going through tutorials of compose desktop.

I have two question regarding the basics of compose desktop.

First, is there a way to add elevation to the entire windows?

Second, is there a way to set the mininum size of the window? When I click on the edge of the windows, even without any arrows showing, the windows can be resized, but I can't find a way to add constraints on it's minimum size. It will shink until it disappears.

My current preview looks like this.

enter image description here

My code looks like this.

fun main() = application {

val windowState = rememberWindowState()
windowState.size = WindowSize(392.dp, 642.dp)

var isVisible by remember { mutableStateOf(true) }

val trayState = rememberTrayState()

Tray(
    state = trayState,
    icon = TrayIcon,
    hint = "PickMessengerV2",
    onAction = {
        isVisible = true
    },
    menu = {
        Item(
            "Exit",
            onClick = {
                exitApplication()
            }
        )
    }
)

Window(
    onCloseRequest = {
        isVisible = false
    },
    state = windowState,
    undecorated = true,
    resizable = true,
    visible = isVisible
) {


    AppTheme(darkTheme = false) {
        Row(modifier = Modifier.fillMaxSize()) {
            var menuIdx by remember {
                mutableStateOf(value = 0)
            }
            WindowDraggableArea {
                Column(
                    modifier = Modifier.width(67.dp).fillMaxHeight().background(Color(0xFFf3f5f6)),
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {

                    val onClick: (Int) -> Unit = { myIndex ->
                        menuIdx = myIndex
                    }

                    mainMenu(
                        menuIdx,
                        0,
                        listOf("LeftMenu_friend_ON.png", "LeftMenu_friend_OFF.png"),
                        marginTop = 36,
                        onClick = onClick
                    )
                    mainMenu(
                        menuIdx,
                        1,
                        listOf("LeftMenu_ Orga_ON.png", "LeftMenu_ Orga_OFF.png"),
                        onClick = onClick
                    )
                    mainMenu(
                        menuIdx,
                        2,
                        listOf("LeftMenu_chat_ON.png", "LeftMenu_chat_OFF.png"),
                        onClick = onClick
                    )
                    mainMenu(
                        menuIdx,
                        3,
                        listOf("LeftMenu_letter_OFF_2.png", "LeftMenu_letter_OFF.png"),
                        onClick = onClick
                    )
                    mainMenu(
                        menuIdx,
                        4,
                        listOf("LeftMenu_file_ON.png", "LeftMenu_file_OFF.png"),
                        onClick = onClick
                    )
                    mainMenu(
                        menuIdx,
                        5,
                        listOf("LeftMenu_ Coop_ON.png", "LeftMenu_ Coop_OFF.png"),
                        onClick = onClick
                    )
                    mainMenu(
                        menuIdx,
                        6,
                        listOf("LeftMenu_bnad_ON.png", "LeftMenu_band_OFF.png"),
                        onClick = onClick
                    )
                }
            }

            Column(modifier = Modifier.fillMaxSize().background(MaterialTheme.colors.background)) {

                WindowDraggableArea{
                    Row(
                        modifier = Modifier.fillMaxWidth().height(31.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Spacer(modifier = Modifier.weight(1f))
                        Box(modifier = Modifier.fillMaxHeight().clickable {
                            windowState.isMinimized = true
                        }.padding(start = 6.dp, end = 6.dp), contentAlignment = Alignment.Center) {
                            NsImage("win_1.png")
                        }
                        Box(modifier = Modifier.fillMaxHeight().clickable {
                            windowState.placement = if(windowState.placement == WindowPlacement.Fullscreen){
                                WindowPlacement.Floating
                            }else {
                                WindowPlacement.Fullscreen
                            }
                        }.padding(start = 6.dp, end = 6.dp), contentAlignment = Alignment.Center) {
                            NsImage("win_2.png")
                        }
                        Box(modifier = Modifier.fillMaxHeight().clickable {
                            isVisible = false
                        }.padding(start = 6.dp, end = 6.dp), contentAlignment = Alignment.Center) {
                            NsImage("win_3.png")
                        }
                        Spacer(modifier = Modifier.width(6.dp))
                    }
                }
            }
        }
    }
}

}


Solution

  • Here's the solution for minimum window size: You need to access the AWT API through the FrameWindowScope

        fun FrameWindowScope.setMinSize() {
            window.minimumSize = Dimension(yourMinWidth, yourMinHeight)
        }
    

    And then you just call the setMinSize() in your Window function.