I'm building a pretty rudimentary app where every ...
# compose-desktop
c
I'm building a pretty rudimentary app where every 60 minutes it reminds me to "stand up from your desk!" So far I just have a
Copy code
scope.launch {
    while (isActive) {
        delay(60.minutes)
        isOpenFullScreen = true
    }
}
this doesn't work though, unless I switch over to my app in the macOS task switcher. then it will show my window. Am I missing something basic here?
a
which part 'doesn't work'?
a
We don’t recompose completely obscured windows.
Maybe we should.
Can you post a short reproducer?
c
@Alex Styl sorry for not being clear. but basically I would think that my code above would show my window. but it does not. i need to switch back to my application, and then the window will show. @Alexander Maryanovsky yeah i can do that. ive been out of the compose desktop game for a while now. where would i post that. i think github issues was shutdown right?
a
You can post here if it’s small enough, or open a ticket on YouTrack.
c
Okay. I will post here. One sec!
It is indeed pretty tiny
Copy code
fun main() = application {
    var isOpenFullScreen by remember { mutableStateOf(false) }


    if (isOpenFullScreen){
        Window(
            onCloseRequest = {
                isOpenFullScreen = false
            },
            title = "REMINDER"
        ) {
            Column {
                // content
                Text(text = "Stand up from your desk!")
            }
        }
    }

    val scope = rememberCoroutineScope()
    scope.launch {
        while (isActive) {
            delay(10.seconds)
            isOpenFullScreen = true
        }
    }
}
So basically... the above "works" only if my currently focussed app is the app. If I'm focussed in google chrome... and 10 seconds goes by then it will not show the window.
a
Thanks, I’ll look into it.
It looks like it should work.
c
Thanks! Much appreciated. it kinda puts a dent into my project here so I hope there is some workaround lol
a
As a workaround, you can probably show the window compose-wise, and then hide/show it via Swing (window.isVisible).
Ok, the issue doesn’t seem to reproduce for me. The window is shown after 10 seconds regardless of which app is active. What version of Compose are you using? Can you try the latest (v1.8.0+dev2053)? Are you sure the window isn’t shown but is just behind other windows? Also, you’re doing it slightly wrong. Don’t launch anything in the composition; use
LaunchedEffect
.
c
I used whatever version came with kmp.jetbrains.com as of ~48 hours ago 😅 Let me check!
I guess 1.7.0. Let me try the version you recommended
Copy code
Plugin [id: 'org.jetbrains.compose', version: '1.8.0+dev2053', apply: false] was not found in any of the following sources:
a
You need to add the JetBrains repository to use dev versions
c
Are you sure the window isn’t shown but is just behind other windows?
Ah. I think that's what's happening.
I want it to be shown in the foreground on top of other apps. I guess I can look to see if swing apps can bring themselves to foreground?
a
c
cool. will give that a try. downloading compose 1.8.0 dev dependency now...
Cool. I had to add another delay unfortunately. but it seems to work!
Copy code
LaunchedEffect(Unit){
    while (isActive) {
        delay(10.seconds)
        isOpenFullScreen = true
        delay(500)
        Desktop.getDesktop().requestForeground(true)
    }
}
looks like the additional delay is only needed the first time around. hm. anyway. this was super helpful. thank you @Alexander Maryanovsky!
a
You probably don’t need the delay. You just need to put that call inside the Window
c
oh good point!
Thank you!
a
No problem
c
i swear everyone on this slack needs a tip jar link lmao.
FWIW. moving it inside of the window did not help.
Copy code
if (isOpenFullScreen){
    Window(
        onCloseRequest = { isOpenFullScreen = false} ,
        state = rememberWindowState(size = DpSize(width, height)),
        undecorated = false,
        title = "REMINDER"
    ) {
        LaunchedEffect(Unit){
        Desktop.getDesktop().requestForeground(true)
        }
        Column {
            // content
            Text(text = "Stand up from your desk!")
        }
    }
}
this did not work. if I added the delay in there, then it indeed worked. this is fine for my app, but definitely let me know if you think im doing something "wrong". cheers
a
Maybe you need to wait for the window to be fully displayed (by registering a listener on it). But if the delay works, it sounds like an OK hack for such a small thing.
1
c
cool. this seemed to work when inside of the Window
Copy code
LaunchedEffect(Unit) {
                window.addWindowListener(object : WindowAdapter() {
                    override fun windowOpened(e: WindowEvent?) {
                        println("Window is fully displayed!")
                        Desktop.getDesktop().requestForeground(true)
                    }
                })
            }
a
Use
DisposableEffect
and unregister the listener properly too.
2