Nucleus
Lifecycle

Application entry point

Start a Nucleus app with nucleusApplication, and control how it quits, locks to one instance, and initializes FileKit.

View as Markdown

nucleusApplication is the main entry point of a Nucleus app. It starts the native event loop, runs your Compose content, and handles exit, system quit, single instance, and FileKit. It ships in nucleus-application.

nucleus-applicationmacOSWindowsLinuxGradle plugin: RecommendedWindow runtime: RequiredSingle-instance lock and app identity come from the packaged build.
import dev.nucleusframework.application.DecoratedWindow
import dev.nucleusframework.application.nucleusApplication

fun main(args: Array<String>) = nucleusApplication(args) {
    DecoratedWindow(onCloseRequest = ::exitApplication, title = "MyApp") {
        // window content
    }
}

Pass the main arguments so deep links and login launches reach the runtime. The app ends on exitApplication() or when its last window closes. Compose Desktop's AWT Window, DialogWindow, and Tray compile in this scope but are not supported. Use Window and DialogWindow from dev.nucleusframework.application, or DecoratedWindow. See Choose a window kind.

Choose what happens on exitNew

By default the JVM exits when the event loop stops: code 0 after a normal quit, 1 after a fatal error. Pass exitProcessOnExit = false to return to your main instead. A fatal error is then logged at SEVERE and rethrown, and you call exitProcess yourself, since the AWT thread can keep the JVM alive:

fun main(args: Array<String>) {
    nucleusApplication(args, exitProcessOnExit = false) { /* ... */ }
    flushLogs()
    exitProcess(0)
}

Handle a system quit

On macOS, Cmd+Q, Dock → Quit, logout, and shutdown call onCloseRequest on every open window, newest first, so your close-button logic covers a system quit. A window consents when its handler calls exitApplication() or closes the window. A window that stays open vetoes the quit. A window opened during the quit, such as a "Save changes?" dialog, delays the exit until it closes.

On Windows, logoff, restart, shutdown and a Restart Manager close (an installer replacing the app) take the same path. A window that stays open vetoes the session end, and Windows shows the app on its "This app is preventing shutdown" screen while a quit is still in flight. The app exits only once Windows confirms the session end, after its shutdown hooks run. A window that closes itself during its close request stays closed if Windows then cancels.

Changed in 3.0

In 2.x, a system quit on macOS terminated the app without calling onCloseRequest. A handler that never calls exitApplication() or closes its window now cancels the quit. See Migrate from 2.x.

A window that hides to the tray must let a real quit through. isQuitting is true during a system quit and resets if a window vetoes:

DecoratedWindow(
    onCloseRequest = { if (isQuitting) exitApplication() else visible = false },
    visible = visible,
) { /* ... */ }

Enforce a single instance

Packaged builds hold the single-instance lock. Dev runs skip it, so you can debug next to a packaged copy.

Changed in 3.0

enableSingleInstance now defaults to !ExecutableRuntime.isDev() instead of true. Pass true to test it from ./gradlew run. See Migrate from 2.x.

Initialize FileKit

When FileKit is on the classpath, nucleusApplication calls FileKit.init(NucleusApp.appId) before your content runs. Dev runs, packaged builds, and native images then share one data directory (%APPDATA%\<appId> on Windows, which the uninstaller deletes when deleteAppDataOnUninstall is set). Your own FileKit.init call, before nucleusApplication or inside content, wins. initializeFileKit = false skips it.

Set the locale and Dock policy

  • defaultLocale = Locale.FRENCH forces the locale before any UI is built, including Compose's text context menu and the default spellcheck language.
  • dockIconFollowsWindows = true (macOS) starts the app without a Dock icon and shows one only while a DecoratedWindow with hiddenFromDock = false is visible. Tray popups never count.

Watch for a stalled UI

On Windows, Nucleus logs SEVERE with a thread dump when the event loop stops responding, and calls your handlers on their own thread:

onUnresponsive { showWaitOrQuit() }
onResponsive { hideWaitOrQuit() }

Wrap a known long call in expectUnresponsive { } to skip the report. Detection rules and the -Dnucleus.tao.watchdog* flags are on Threading.

API reference

nucleusApplication

ParameterTypeDefaultDescription
argsArray<String>emptyArray()main arguments. Scanned for deep links and the login-launch marker.
enableSingleInstanceBoolean!ExecutableRuntime.isDev()Acquires the single-instance lock. A second instance forwards its deep link and exits.
defaultLocaleLocale?nullForces Locale.getDefault() and the user.* properties before the UI starts.
dockIconFollowsWindowsBooleanfalsemacOS only. Shows the Dock icon only while a visible window asks for it.
exitProcessOnExitBooleantrueTerminates the JVM when the app ends. false returns to the caller.
initializeFileKitBooleantrueCalls FileKit.init(NucleusApp.appId) when FileKit is present and not yet initialized.
content@Composable NucleusApplicationScope.() -> UnitrequiredThe application content.

NucleusApplicationScope

Extends Compose's ApplicationScope. LocalNucleusApplicationScope exposes it to composables deep in the tree.

MemberDescription
exitApplication()Asks the event loop to stop. During a system quit, counts as this window's consent.
isQuitting: Booleantrue while a system quit is asking the windows to close.
onDeepLink(block: (URI) -> Unit)Registers the deep-link callback. See Deep links.
onUnresponsive(block) / onResponsive(block)Windows event-loop stall callbacks.
expectUnresponsive(block): TRuns block without reporting the stall it causes.
aotMode, isAotTraining, isAotRuntimeThe AOT cache mode of the running JVM. See AOT cache.

What's next

  • Windows: DecoratedWindow and the window API.
  • Single instance: the lock and the second-launch hand-off.
  • Threading: the UI thread, native callbacks, and the watchdog.
  • Tray apps: hide to the tray and quit from a menu.
Was this page helpful?
Edit on GitHub

Last updated on