|
| 1 | +package com.skyd.podaura.util |
| 2 | + |
| 3 | +import androidx.compose.runtime.Composable |
| 4 | +import androidx.compose.runtime.CompositionLocalProvider |
| 5 | +import androidx.compose.runtime.SideEffect |
| 6 | +import androidx.compose.runtime.remember |
| 7 | +import androidx.compose.ui.platform.LocalPlatformWindowInsets |
| 8 | +import androidx.compose.ui.platform.PlatformInsets |
| 9 | +import androidx.compose.ui.platform.PlatformWindowInsets |
| 10 | +import androidx.compose.ui.unit.Density |
| 11 | +import androidx.compose.ui.unit.dp |
| 12 | +import kotlinx.cinterop.CValue |
| 13 | +import kotlinx.cinterop.useContents |
| 14 | +import platform.AppKit.NSWindow |
| 15 | +import platform.AppKit.NSWindowStyleMaskFullSizeContentView |
| 16 | +import platform.AppKit.NSWindowTitleHidden |
| 17 | +import platform.Foundation.NSEdgeInsets |
| 18 | + |
| 19 | +private class AppKitWindowInsets(private val window: () -> NSWindow) : PlatformWindowInsets { |
| 20 | + override val systemBars: PlatformInsets |
| 21 | + get() { |
| 22 | + val window = window() |
| 23 | + val density = Density(window.backingScaleFactor.toFloat()) |
| 24 | + val safeAreaInsets = window.contentView?.safeAreaInsets?.toPlatformInsets(density) |
| 25 | + return safeAreaInsets ?: PlatformInsets(0, 0, 0, 0) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// Copied from https://github.com/JetBrains/compose-multiplatform-core/blob/jb-main/compose/ui/ui/src/iosMain/kotlin/androidx/compose/ui/unit/Conversions.ios.kt |
| 30 | +private fun CValue<NSEdgeInsets>.toPlatformInsets(density: Density) = useContents { |
| 31 | + density.PlatformInsets( |
| 32 | + left = left.dp, |
| 33 | + top = top.dp, |
| 34 | + right = right.dp, |
| 35 | + bottom = bottom.dp |
| 36 | + ) |
| 37 | +} |
| 38 | + |
| 39 | +@Composable |
| 40 | +fun ProvidePlatformWindowInsets( |
| 41 | + window: () -> NSWindow, |
| 42 | + content: @Composable () -> Unit |
| 43 | +) { |
| 44 | + val windowInsets = remember { AppKitWindowInsets(window) } |
| 45 | + |
| 46 | + SideEffect { |
| 47 | + val window = window() |
| 48 | + window.titlebarAppearsTransparent = true |
| 49 | + window.styleMask = window.styleMask or NSWindowStyleMaskFullSizeContentView |
| 50 | + window.titleVisibility = NSWindowTitleHidden |
| 51 | + window.contentView?.let { |
| 52 | + it.layer?.setBounds(it.bounds()) |
| 53 | + it.setNeedsDisplay(true) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + CompositionLocalProvider( |
| 58 | + LocalPlatformWindowInsets provides windowInsets, |
| 59 | + content = content |
| 60 | + ) |
| 61 | +} |
0 commit comments