Last active
November 1, 2023 13:13
-
-
Save alexfacciorusso/1bd50d0424a83ed0b44ce48b3106a848 to your computer and use it in GitHub Desktop.
Useful Fluent composables for Compose Desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright © 2023 Alex Facciorusso | |
* | |
* Licensed under MIT The MIT License (MIT) | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | |
* documentation files (the “Software”), to deal in the Software without restriction, including without limitation the | |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | |
* permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | |
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS | |
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
import androidx.compose.foundation.BorderStroke | |
import androidx.compose.material3.MaterialTheme | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.ReadOnlyComposable | |
import androidx.compose.ui.unit.Dp | |
@Composable | |
@ReadOnlyComposable | |
fun fluentSurfaceBorderStroke() = BorderStroke( | |
Dp.Hairline, | |
MaterialTheme.colorScheme.onSurface.copy(alpha = .1f) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright © 2023 Alex Facciorusso | |
* | |
* Licensed under MIT The MIT License (MIT) | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | |
* documentation files (the “Software”), to deal in the Software without restriction, including without limitation the | |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | |
* permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | |
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS | |
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
import androidx.compose.material3.* | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.ReadOnlyComposable | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.graphics.luminance | |
import com.github.composenativelook.isSystemInDarkTheme | |
fun fluentLightColorScheme() = lightColorScheme( | |
surface = Color.White.copy(alpha = .60f), | |
surfaceTint = Color.White, | |
) | |
fun fluentDarkColorScheme() = darkColorScheme( | |
surface = Color.Transparent, | |
surfaceTint = Color.White, | |
) | |
@Composable | |
fun FluentMaterialTheme( | |
lightColorScheme: ColorScheme = fluentLightColorScheme(), | |
darkColorScheme: ColorScheme = fluentDarkColorScheme(), | |
overrideIsDark: Boolean? = null, | |
content: @Composable () -> Unit, | |
) { | |
val isDark = overrideIsDark ?: isSystemInDarkTheme() | |
val colorScheme = if (isDark) darkColorScheme else lightColorScheme | |
MaterialTheme( | |
colorScheme = colorScheme, | |
) { | |
content() | |
} | |
} | |
@Composable | |
@ReadOnlyComposable | |
fun MaterialTheme.isDark() = colorScheme.background.luminance() <= 0.179 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright © 2023 Alex Facciorusso | |
* | |
* Licensed under MIT The MIT License (MIT) | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | |
* documentation files (the “Software”), to deal in the Software without restriction, including without limitation the | |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | |
* permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | |
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS | |
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
import androidx.compose.foundation.BorderStroke | |
import androidx.compose.material3.MaterialTheme | |
import androidx.compose.material3.Surface | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.graphics.Shape | |
import androidx.compose.ui.unit.Dp | |
import androidx.compose.ui.unit.dp | |
enum class FluentSurfaceLevel(internal val elevationValue: Dp) { | |
Background(0.dp), | |
Level1(1.dp), | |
Level2(16.dp), | |
; | |
} | |
@Composable | |
fun fluentBorderForSection() = if (MaterialTheme.isDark()) null else fluentSurfaceBorderStroke() | |
@Composable | |
fun FluentSurface( | |
surfaceLevel: FluentSurfaceLevel = FluentSurfaceLevel.Level1, | |
border: BorderStroke? = null, | |
shape: Shape = MaterialTheme.shapes.small, | |
content: @Composable () -> Unit, | |
) { | |
Surface( | |
tonalElevation = surfaceLevel.elevationValue, | |
color = if (surfaceLevel == FluentSurfaceLevel.Background) Color.Transparent else MaterialTheme.colorScheme.surface, | |
border = border, | |
shape = shape, | |
content = content | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment