Skip to content

Adaptive windows ​

Use the available window space to select a layout. Read device form factor separately for device-specific behavior. salt-ui already depends on salt-adaptive-window. To use it on its own, add the matching version and opt in with @OptIn(UnstableSaltAdaptiveWindowApi::class).

kotlin
implementation("io.github.moriafly:salt-adaptive-window:<TAG>")

DeviceFormFactor ​

The available values are Phone, Tablet, Foldable, Desktop, Car, and Unknown.

WindowSizeClass ​

ClassWindow width
CompactBelow 600 dp
Medium600 dp up to 840 dp
Expanded840 dp and above

Read the current window ​

kotlin
import com.moriafly.salt.adaptive.window.LocalSaltAdaptiveWindow
import com.moriafly.salt.adaptive.window.UnstableSaltAdaptiveWindowApi
import com.moriafly.salt.adaptive.window.WindowSizeClass

@OptIn(UnstableSaltAdaptiveWindowApi::class)
@Composable
fun Example() {
    val window = LocalSaltAdaptiveWindow.current
    if (window.sizeClass == WindowSizeClass.Compact) {
        CompactContent()
    } else {
        WideContent()
    }
}

SaltAdaptiveWindow ​

windowSize reports the containing window in dp. sizeClass classifies its width and formFactor describes the device. Resizing, rotating, and entering split screen update the values. Desktop windows have independent LocalWindowInfo values. iOS uses the window size.

Design for available space ​

A tablet can have a Compact split-screen window. On Android, the phone or tablet estimate uses the maximum window bounds for the current display where available, with a display-size fallback before Android 11. Foldables and cars also use platform features. Layout containers still handle system bars, cutouts, and the keyboard.

Theme and Sheet ​

The current window provides adaptive information automatically. SheetDisplayMode.Auto uses Floating at 600 dp wide and 480 dp tall or larger and Bottom otherwise. It reads the containing window size before insets, then handles insets for actual placement. Set SheetDisplayMode.Bottom or Floating to control the presentation explicitly. See Sheet.

Override window information ​

Use ProvideSaltAdaptiveWindow to set controlled values in a preview or test fixture.

kotlin
ProvideSaltAdaptiveWindow(
    adaptiveWindow = SaltAdaptiveWindow(
        formFactor = DeviceFormFactor.Phone,
        sizeClass = WindowSizeClass.Compact,
        windowSize = DpSize(390.dp, 844.dp)
    )
) {
    PreviewContent()
}

LocalSaltAdaptiveWindow.current reads the nearest override if present.

currentSaltAdaptiveWindow ​

currentSaltAdaptiveWindow() reads the platform window directly, ignoring any explicit override.

Compatibility ​

The legacy two-argument constructor, copy entry point, and Local entry point remain binary compatible. A two-argument value uses DpSize.Unspecified. Sheet uses the host window dimensions in that case.

Platform behavior ​

Read each native window’s values in its own composition. Check narrow or split-screen layouts separately from full-screen layouts, and handle system bar and IME insets at the relevant container.

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