Skip to content

Desktop ​

SaltUI provides native windows, custom caption bars, and scaling for Windows, macOS, and Linux. The APIs are in the Desktop source set of salt-ui and typically require @OptIn(UnstableSaltUiApi::class).

SaltWindow ​

SaltWindow creates a native window while it is in composition. Respond to onCloseRequest by exiting the app or removing the child window from composition.

APIResponsibility
SaltWindow, SaltDialogWindowWindow lifetime and platform behavior
DesktopCaptionBarLayout and space for native window buttons
CaptionBarHitTestCaption hit region for the current SaltUI window
SaltWindowPropertiesAppearance, movement, and scaling
LocalSaltWindowInfoCurrent caption dimensions and button insets

Custom caption example ​

Apply caption padding to the main content. Draw the hit region above the content, and put interactive caption controls above the hit region. Clicking the Settings control then changes the content without starting a window drag.

kotlin
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.application
import com.moriafly.salt.ui.SaltTheme
import com.moriafly.salt.ui.Text
import com.moriafly.salt.ui.UnstableSaltUiApi
import com.moriafly.salt.ui.window.CaptionBarHitTest
import com.moriafly.salt.ui.window.DesktopCaptionBar
import com.moriafly.salt.ui.window.LocalSaltWindowInfo
import com.moriafly.salt.ui.window.SaltWindow

@OptIn(ExperimentalComposeUiApi::class, UnstableSaltUiApi::class)
fun main() = application {
    var showSettings by remember { mutableStateOf(false) }

    SaltTheme {
        SaltWindow(
            onCloseRequest = ::exitApplication,
            title = "Salt App"
        ) {
            val captionBarHeight = LocalSaltWindowInfo.current.captionBarHeight

            Box(Modifier.fillMaxSize()) {
                // 1. Leave space for the caption bar.
                Box(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(top = captionBarHeight)
                ) {
                    Text(if (showSettings) "Settings content" else "Main window content")
                }

                // 2. Register the hit region once per window.
                CaptionBarHitTest()

                // 3. Place interactive controls above the hit region.
                DesktopCaptionBar {
                    Row(
                        modifier = Modifier
                            .fillMaxSize()
                            .padding(horizontal = 12.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Text("Salt App", modifier = Modifier.weight(1f))
                        Text(
                            text = "Settings",
                            modifier = Modifier
                                .clickable { showSettings = !showSettings }
                                .padding(8.dp)
                        )
                    }
                }
            }
        }
    }
}

DesktopCaptionBar ​

DesktopCaptionBar uses captionBarHeight and reserves space on the side matching captionButtonsAlign and captionButtonsFullWidth. Use Modifier.align() in its BoxScope for custom content. Register the hit region separately at window level.

CaptionBarHitTest ​

Use CaptionBarHitTest inside a SaltWindow or SaltDialogWindow. It registers an invisible region with the platform handler. It defaults to the caption height and full width. Window movement follows SaltWindowProperties.moveable.

Register at most once per window ​

Put the registration at the window root outside page transitions. Overlapping branches, such as AnimatedContent or Crossfade, can register at the same time and cause IllegalStateException. Removing a registration clears it and permits replacement.

A child SaltWindow or SaltDialogWindow has its own scope. Compose Popup, Dialog, and ordinary windows inherit the parent registration scope and should leave it to the parent. This also applies when Compose uses a native popup layer. The automatic duplicate check is part of SaltUI #63.

SaltDialogWindow ​

A SaltDialogWindow is modal by default. Set modalityType = DialogModalityType.Modeless for a nonmodal child. In the example, showDialog controls the child’s lifetime, and each SaltUI window can register its own caption hit region.

kotlin
var showDialog by remember { mutableStateOf(false) }

SaltWindow(
    onCloseRequest = ::exitApplication,
    title = "Main window"
) {
    Box(Modifier.fillMaxSize()) {
        CaptionBarHitTest()
        DesktopCaptionBar {
            Text(
                text = "Open child window",
                modifier = Modifier
                    .clickable { showDialog = true }
                    .padding(8.dp)
            )
        }
    }

    if (showDialog) {
        SaltDialogWindow(
            onCloseRequest = { showDialog = false },
            title = "Child window"
        ) {
            Box(Modifier.fillMaxSize()) {
                CaptionBarHitTest()
                DesktopCaptionBar {
                    Text("Child window")
                }
            }
        }
    }
}

SaltWindowProperties ​

Create the window appearance with SaltWindowProperties.default(...) and pass it as properties.

kotlin
SaltWindow(
    onCloseRequest = ::exitApplication,
    properties = SaltWindowProperties.default(
        moveable = false,
        captionBarHeight = 48.dp,
        captionButtonHeight = 40.dp
    )
) {
    // Window content
}
PropertyUse
moveableAllow SaltUI's caption handler to move the window.
captionBarHeightCaption layout height and the default hit-region height.
captionButtonHeightButton height, at most the caption height.
captionButtonsVisibleShow native caption buttons.
backgroundTypeWindow backdrop. Check isSupported() before offering a type.
extraDisplayScale, extraFontScaleMultipliers on top of display and font scaling.

In 3.1, pass onVisibleChange and onResizeEdgeChange directly to SaltWindow or SaltDialogWindow. SaltWindowProperties and default() take no type argument.

LocalSaltWindowInfo ​

Read LocalSaltWindowInfo.current inside the SaltUI window for captionBarHeight, captionButtonsAlign, and captionButtonsFullWidth. Apply the caption height to the main content yourself. The caption bar reserves space only for its own contents.

SaltWindowExceptionHandler ​

Configure the callback before opening windows if your app has a central reporting entry point. The default throws the original exception.

kotlin
import com.moriafly.salt.ui.SaltUiFlags
import com.moriafly.salt.ui.window.internal.SaltWindowExceptionHandler

// Configure this at startup before creating windows.
SaltUiFlags.isDisableDefaultWindowExceptionHandler = true
SaltWindowExceptionHandler.onException = { throwable ->
    throwable.printStackTrace()
    // Report the exception here.
}

With isDisableDefaultWindowExceptionHandler = true (the default), SaltUI invokes its callback synchronously on the AWT event thread. A callback that returns consumes the exception. A callback that throws propagates the error. Setting the flag to false leaves the external Compose exception handler in control.

HWND ​

Use the hwnd extension on a ComposeWindow or ComposeDialog only on Windows.

kotlin
import com.moriafly.salt.core.os.OS
import com.moriafly.salt.ui.util.hwnd

if (OS.isWindows()) {
    val hwnd = window.hwnd
    // Use the native Windows handle.
}

SkiaLayer ​

Use findSkiaLayer() when integrating with the underlying renderer. It can return null.

kotlin
import com.moriafly.salt.ui.util.findSkiaLayer

window.findSkiaLayer()?.transparency = true

Google/Android 是 Google LLC 的注册商标,Salt Player 和糖醋音乐均是寻浔科技(上海)有限公司在中华人民共和国的注册商标