VisibilityState.kt 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package eu.mjdev.desktop.components.sliding.base
  2. import androidx.compose.runtime.Composable
  3. import androidx.compose.runtime.MutableState
  4. import androidx.compose.runtime.mutableStateOf
  5. import androidx.compose.runtime.remember
  6. import androidx.compose.ui.layout.LayoutCoordinates
  7. import androidx.compose.ui.unit.DpSize
  8. import eu.mjdev.desktop.helpers.internal.DpBounds
  9. import eu.mjdev.desktop.helpers.internal.DpBounds.Companion.toDpBounds
  10. @Suppress("unused", "MemberVisibilityCanBePrivate")
  11. class VisibilityState(
  12. private val startState: Boolean = false,
  13. var enabled: Boolean = true
  14. ) {
  15. private val visibleState: MutableState<Boolean> = mutableStateOf(startState)
  16. private val boundsState: MutableState<DpBounds> = mutableStateOf(DpBounds.Zero)
  17. var bounds: DpBounds
  18. get() = boundsState.value
  19. set(value) {
  20. boundsState.value = value
  21. }
  22. var isVisible: Boolean
  23. get() = visibleState.value
  24. set(value) {
  25. if (enabled) {
  26. visibleState.value = value
  27. } else {
  28. visibleState.value = startState
  29. println("Visible state disabled, can not set value.")
  30. }
  31. }
  32. fun show() {
  33. if (!isVisible) {
  34. if (enabled) {
  35. visibleState.value = true
  36. }
  37. }
  38. }
  39. fun hide() {
  40. if (isVisible) {
  41. if (enabled) {
  42. visibleState.value = false
  43. }
  44. }
  45. }
  46. fun toggle() {
  47. visibleState.value = !visibleState.value
  48. }
  49. fun onPlaced(lc: LayoutCoordinates) {
  50. bounds = lc.toDpBounds()
  51. }
  52. fun updateSize(panelSize: DpSize) {
  53. bounds.height = panelSize.height
  54. bounds.width = panelSize.width
  55. }
  56. companion object {
  57. @Composable
  58. fun rememberVisibilityState(
  59. visible: Boolean = false,
  60. enabled: Boolean = true,
  61. ): VisibilityState {
  62. return remember(visible, enabled) {
  63. VisibilityState(visible, enabled)
  64. }
  65. }
  66. }
  67. }