Skip to content

Glass ​

Use Glass for floating controls and buttons that sample the content behind them. Mark the background with Modifier.glassSource(source) and draw the foreground with Modifier.glass(source). Both use the same GlassSource. The Material and popup menu backdrops have their own source setup.

Captured from a SaltUI desktop window. The four buttons use the component’s default text style and sizing and share the same background edge.

Solid, Blur, Regular, and Transparent buttons over the same two-tone backdropSolid, Blur, Regular, and Transparent buttons over the same two-tone backdrop

These APIs live in com.moriafly.salt.ui.material and require @OptIn(UnstableSaltUiApi::class). Glass was introduced in 3.1.0-alpha01. The appearance shown here is from the 3.1.0-alpha02 source.

Get started ​

Run the example inside SaltTheme. The gradient is drawn first and registered as a source. The button appears in front as a sibling.

kotlin
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.moriafly.salt.ui.Button
import com.moriafly.salt.ui.ButtonAppearance
import com.moriafly.salt.ui.UnstableSaltUiApi
import com.moriafly.salt.ui.material.glass
import com.moriafly.salt.ui.material.glassSource
import com.moriafly.salt.ui.material.rememberGlassSource

@OptIn(UnstableSaltUiApi::class)
@Composable
fun GlassButtonExample(onClick: () -> Unit) {
    val source = rememberGlassSource()

    Box(
        modifier = Modifier
            .fillMaxWidth()
            .height(160.dp)
    ) {
        Box(
            modifier = Modifier
                .matchParentSize()
                .glassSource(source)
                .background(
                    Brush.linearGradient(
                        colors = listOf(Color(0xFF65B7FF), Color(0xFF263CA6))
                    )
                )
        )
        Button(
            onClick = onClick,
            text = "Glass button",
            modifier = Modifier
                .align(Alignment.Center)
                .glass(source = source),
            appearance = ButtonAppearance.Glass
        )
    }
}

Button handles clicks. ButtonAppearance.Glass gives the button a transparent container and lets the glass modifier provide visual feedback.

Effects and shapes ​

The effect defaults to SaltTheme.glassEffect, initially GlassEffect.Regular(). Override it on an individual surface when needed.

kotlin
Modifier.glass(
    source = source,
    shape = RoundedCornerShape(20.dp),
    effect = GlassEffect.Transparent
)

The default CircleShape draws a circle on a square control and a capsule on a wider control. Pass a RoundedCornerShape to customize the surface. The layout remains responsible for clipping its content.

EffectAppearance
GlassEffect.Solid()Draws the configured color without sampling.
GlassEffect.BlurUses theme-dependent Premium tint and 16 dp blur without refraction.
GlassEffect.Regular()Uses background fill, tint, refraction, and a light 2.5 dp blur.
GlassEffect.TransparentUses refraction without an extra background fill, tint, or color adjustment.

Use lightColor and darkColor on Solid or Regular to control background color and opacity. Regular also exposes lightTint and darkTint.

kotlin
Modifier.glass(
    source = source,
    effect = GlassEffect.Regular(
        lightColor = Color(0xB3FFFFFF),
        darkColor = Color(0xB3181818)
    )
)

All four effects respond to presses. The pointer- or touch-following highlight applies to Regular and Transparent.

Sources and drawing order ​

Draw each source behind the surface and give both a layout size. Register the wallpaper and list independently if both should appear through a glass control.

text
Box
├─ Wallpaper or background: glassSource(source)
├─ Scrolling list: glassSource(source)
└─ Floating button: glass(source)

A source can serve several glass controls. Keep the Glass surface outside the source node, typically as a sibling drawn later. Keep source mounting order stable when changing the content of a source, such as toggling a wallpaper. The sampled content comes from the app’s registered nodes.

Automatic cutouts ​

SaltUI keeps all pixels available for sampling. When displaying the original source on screen, it clips pixels under matching glass surfaces so sharp content does not show through the processed result. The cutout tracks a surface’s shape, movement, and scale. Source and surface need to share a Compose root.

Rounded edges ​

The cutout is inset slightly at the edge so partially transparent, antialiased pixels can overlap the original. The current implementation uses about 2.5 root-coordinate pixels and maps that distance into the source space. Rendering costs still depend on Haze and the graphics backend.

Limits ​

  • Solid and the pre-Android 12 fallback for Blur skip cutouts.
  • A fully transparent Glass effect on Android below 13 keeps the original pixels visible when fallback rendering cannot repaint the transparent source.
  • Register only the content that should be sampled. The cutout does not track every ancestor clip or opacity change. Check complex masks and animations on the target platform.

Theme defaults ​

Set the default effect for a theme subtree. An explicit effect on glass takes precedence.

kotlin
@OptIn(UnstableSaltUiApi::class)
@Composable
fun BlurTheme(content: @Composable () -> Unit) {
    SaltTheme(
        glassEffect = GlassEffect.Blur,
        content = content
    )
}

For a smaller scope, set LocalGlassEffect. Light and dark appearance follows SaltTheme.configs.isDarkTheme.

kotlin
CompositionLocalProvider(
    LocalGlassEffect provides GlassEffect.Transparent
) {
    // Glass uses the transparent effect by default in this scope.
}

Buttons and interaction ​

Glass observes pointer presses and drags. Give a custom surface an appropriate interactive component or click handler. Scrolling that takes over a gesture ends the glass press animation. Buttons retain their keyboard focus indication.

Regular and Transparent draw a highlight that follows a desktop pointer or a held touch and fades afterward. To build a custom BasicButton, use the same shape for the button and the Glass surface and set indication = null to avoid adding a second press indication.

kotlin
val shape = RoundedCornerShape(20.dp)

BasicButton(
    onClick = onClick,
    modifier = Modifier.glass(
        source = source,
        shape = shape
    ),
    shape = shape,
    colors = ButtonDefaults.colors(appearance = ButtonAppearance.Glass),
    indication = null
) {
    Text(text = "Action")
}

Share interaction with related content ​

Pass a rememberGlassInteractionState() to the surface if another part of the UI needs to follow its animation.

kotlin
val interactionState = rememberGlassInteractionState()

Modifier.glass(
    source = source,
    interactionState = interactionState
)

One state belongs to one surface. Other content can read scale, translation, and pivot. Translation and pivot use pixels, and the pivot is relative to the glass surface. Convert it to the target layout’s coordinates when sharing a transform. The button’s hit area stays at its layout position.

Platform support and cost ​

Platform or conditionBehavior
Android 13+Regular and Transparent can use the full refraction path.
Android 12Blur is supported. Regular and Transparent use their fallback rendering.
Android below 12Blur falls back to SaltTheme.colors.subBackground.
Desktop and iOSVisual output depends on the graphics backend and device.

Large surfaces and rapidly changing sources can increase rendering cost. Check the effect on your target devices. Use Solid for a translucent fill without sampling. Glass uses its own source and rendering path.

Troubleshooting ​

SymptomCheck
A wallpaper or list is missing from the glassConfirm that it is marked with the same source, has a size, and is drawn behind the effect.
The background is sharpCheck the effect, source modifier, and platform fallback.
The button shows two press effectsUse ButtonAppearance.Glass or remove the custom BasicButton indication.
The button draws no glassAdd Modifier.glass(...) and configure its source.
The look changes after switching a wallpaperKeep source nodes in a consistent drawing and mounting order.

Run the Glass sample to compare all four effects.

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