which content should be inside `desktopMain` and w...
# compose-desktop
p
which content should be inside
desktopMain
and which inside
commonMain
? I'm developing an app for Compose Desktop with Room, Retrofit, Koin, etc... and after a lot of research, tutorials and help here, I now have almost all the content inside
commonMain
, let's say 99%. The only kotlin code I have in my
desktopMain
module is more or less this:
Copy code
fun main() = application {
    [...]
    Window(
        onCloseRequest = ::exitApplication,
        title = stringResource(Res.string.app_name),
        state = windowState
    ) {
        MyApplicationTheme {
            App()
        }
    }
}
But then... what should I put inside
desktopMain
? For example... now I'm adding a
MenuBar
in the upper part of the window, and that seems to be a desktop only component... but I can't add it on the
desktopMain
module because the composables, navigation etc... and all UI stuff is on
commonMain
. So... what to do here? If I create a composable for the
MenuBar
on
desktopMain
, that composable should trigger navigation and stuff on viewmodels placed on the
commonMain
module, so I don't know how to link them.
j
If your only target is desktop, 100% of the code should be in commonMain
The only time you need to use desktopMain is if desktop is a separate target from something else (such as Android or web). Then commonMain becomes common to all your targets
But if you only have a single target, desktop (aka JVM), then commonMain and desktopMain are equivalent. The common code to a single target is just code for that target.
p
but the MenuBar is a desktop only composable, it should be created on desktopMain
isn't it?
if so... I have the navigation stuff on commonMain... how can MenuBar comunicate with navigation?
j
If your only target is desktop, then you can put 100% of your code in commonMain
p
well, I'll add Android in the future
j
Or put 100% in desktopMain. It doesn't matter. They're the same for a single target.
p
how can I deal with this problem?
a desktop only composable (menu bar) that needs to execute navigation, which is placed on commonMain
j
Expose an interface from common, delegate from desktop and android
p
can you explain what you mean?
the navhost is under the commonMain, not above it
desktopMain (main.kt) simply showing App.kt, and the MenuBar
commonMain (App.kt) ui, navigation...
desktopMain main.kt calls commonMain App.kt
and NavHost is in App.kt, logic for navigate is there
so how can I navigate in main.kt if the logic is in a class that is called from there?
j
I've never used view models or navigation. I don't find them to be designed to my taste, so not sure I can help specifically there.
m
@Pablo like Jake mentioned, can’t you just declare an interface in common (so accessed by NavHost) and provide implementation from desktop via dependency injection?
p
unfortunately not, I found another way to reuse the same navicontroller, but when sharing it between desktopMain and commonMain it produces an exception...
I simply created an AppState class with the navcontroller on commonMain, so no circular dependences, simply desktopMain imports it and declares it on my Window, using it to allow navigation from the MenuBar, then, I pass it to the commonMain module, passing it to the App.kt with the screens, and passing it to the navhost present there... But I can't make it work... when executing the app I got a runtime exception on the commonMain navigation part, when adding the first composable to navigation, with no literal results on google:
Could not find Navigator with class "class androidx.navigation.compose.ComposeNavigator". You must call NavController.addNavigator() for each navigation type.
I'm stuck... can't figure out how to share navigation between a MenuBar and screens, as MenuBar is desktop only component and screens are in commonMain module shared between targets
also Mark I can't understand exactly how you want to mount that interface and pass it, but as you can see, sharing them using other method, it gives runtime exception