Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.compose.snippets.adaptivelayouts

import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.captionBar
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.isCaptionBarVisible
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.windowInsetsTopHeight
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

// [START android_compose_desktop_window_insets_title]
@OptIn(ExperimentalLayoutApi::class)
@Composable
fun CaptionBar() {
if (WindowInsets.isCaptionBarVisible) {
Row(
modifier = Modifier
.windowInsetsTopHeight(WindowInsets.captionBar)
.fillMaxWidth()
.background(
if (isSystemInDarkTheme())
Color.White
else Color.Black
Comment on lines +48 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The background color logic for dark and light themes is inverted, which will cause the text to be unreadable due to poor contrast. In dark mode, this sets a white background while the text color from the theme is likely light, making it illegible. A similar issue occurs in light theme with a black background and dark text.

While swapping Color.White and Color.Black would fix the immediate issue, a better long-term solution is to use colors from your MaterialTheme. Since you're already using MaterialTheme.typography, using theme colors for the background will make your component more robust and adaptable.

Here's a suggestion that fixes the inversion. For a more robust solution, consider refactoring to use theme colors like MaterialTheme.colorScheme.surfaceVariant for the background and onSurfaceVariant for the text.

Suggested change
if (isSystemInDarkTheme())
Color.White
else Color.Black
if (isSystemInDarkTheme())
Color.Black
else Color.White


),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically
) {
Text(
"Caption Bar Title",
style = MaterialTheme.typography.titleMedium,
modifier = Modifier.padding(4.dp)
)
}
}
}
// [END android_compose_desktop_window_insets_title]
Loading