ChromeWindowScope.kt 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package eu.mjdev.desktop.windows
  2. import androidx.compose.animation.core.MutableTransitionState
  3. import androidx.compose.runtime.Composable
  4. import androidx.compose.runtime.MutableState
  5. import androidx.compose.runtime.State
  6. import androidx.compose.ui.unit.Dp
  7. import androidx.compose.ui.unit.DpOffset
  8. import androidx.compose.ui.unit.DpSize
  9. import androidx.compose.ui.unit.dp
  10. import androidx.compose.ui.window.FrameWindowScope
  11. import androidx.compose.ui.window.WindowPosition
  12. @Suppress("unused", "MemberVisibilityCanBePrivate")
  13. class ChromeWindowScope(
  14. val frameScope: FrameWindowScope,
  15. val windowState: ChromeWindowState,
  16. val animState: State<MutableTransitionState<Boolean>>,
  17. val windowVisibleState: MutableState<Boolean>
  18. ) {
  19. val window
  20. get() = frameScope.window
  21. val stateHelper: WindowStateHelper
  22. get() = windowState.stateHelper
  23. val focusHelper: WindowFocusHelper
  24. get() = windowState.focusHelper
  25. val x : Dp
  26. get() = frameScope.window.x.dp
  27. val y : Dp
  28. get() = frameScope.window.y.dp
  29. val width : Dp
  30. get() = frameScope.window.width.dp
  31. val height : Dp
  32. get() = frameScope.window.height.dp
  33. fun apply(
  34. block: ChromeWindowScope.() -> Unit
  35. ) = with(this) {
  36. block()
  37. }
  38. // fun requestFocus() =
  39. // stateHelper.requestFocus()
  40. fun setBounds(
  41. x: Dp = this.x,
  42. y: Dp = this.y,
  43. width: Dp = stateHelper.width,
  44. height: Dp = stateHelper.height
  45. ) = stateHelper.setBounds(x, y, width, height)
  46. fun setOffset(offset: DpOffset) =
  47. stateHelper.setBounds(x = offset.x, y = offset.y)
  48. fun setSize(size: DpSize) =
  49. stateHelper.setBounds(width = size.width, height = size.height)
  50. fun setPosition(position: WindowPosition) =
  51. stateHelper.setPosition(position)
  52. companion object {
  53. @Composable
  54. fun FrameWindowScope.withChromeWindowScope(
  55. windowState: ChromeWindowState,
  56. animState: State<MutableTransitionState<Boolean>>,
  57. windowVisibleState: MutableState<Boolean>,
  58. block: @Composable ChromeWindowScope.() -> Unit
  59. ) = with(ChromeWindowScope(this, windowState, animState, windowVisibleState)) {
  60. block()
  61. }
  62. }
  63. }