DesktopProvider.kt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package eu.mjdev.desktop.provider
  2. //import dev.shreyaspatil.ai.client.generativeai.GenerativeModel
  3. //import dev.shreyaspatil.ai.client.generativeai.type.content
  4. import androidx.compose.runtime.MutableState
  5. import androidx.compose.runtime.compositionLocalOf
  6. import androidx.compose.runtime.mutableStateOf
  7. import androidx.compose.ui.unit.DpSize
  8. import androidx.compose.ui.unit.dp
  9. import eu.mjdev.desktop.components.controlcenter.ControlCenterPage
  10. import eu.mjdev.desktop.components.controlcenter.pages.*
  11. import eu.mjdev.desktop.helpers.ResourceStream
  12. import eu.mjdev.desktop.provider.data.User
  13. import eu.mjdev.desktop.provider.providers.AppsProvider
  14. import kotlinx.coroutines.CoroutineScope
  15. import java.awt.GraphicsDevice
  16. import java.awt.GraphicsEnvironment
  17. import java.awt.Toolkit
  18. import java.io.File
  19. import java.io.FileReader
  20. import javax.script.ScriptEngine
  21. import javax.script.ScriptEngineManager
  22. @Suppress("unused", "MemberVisibilityCanBePrivate", "PrivatePropertyName", "SameParameterValue")
  23. class DesktopProvider(
  24. private val scope: CoroutineScope? = null,
  25. ) {
  26. private val __currentUser: User by lazy { User.load() }
  27. private val _currentUser: MutableState<User> = mutableStateOf(__currentUser)
  28. val currentUser
  29. get() = _currentUser.value
  30. private val _controlCenterPages: MutableState<List<ControlCenterPage>> = mutableStateOf(CONTROL_CENTER_PAGES)
  31. private val scriptManager by lazy { ScriptEngineManager() }
  32. private val engine: ScriptEngine by lazy {
  33. scriptManager.getEngineByName("JavaScript").apply {
  34. // todo
  35. }
  36. }
  37. // val adbHandler = adbDevicesHandler(
  38. // coroutineScope = scope
  39. // ) { device ->
  40. // println("Device discovered : $device")
  41. // // when device connected
  42. // }
  43. val containerSize: DpSize by lazy {
  44. Toolkit.getDefaultToolkit().screenSize.let {
  45. DpSize(
  46. it.width.dp,
  47. it.height.dp
  48. )
  49. }
  50. }
  51. val controlCenterPages get() = _controlCenterPages.value
  52. val appsProvider by lazy { AppsProvider(this) }
  53. val graphicsEnvironment: GraphicsEnvironment
  54. get() = GraphicsEnvironment.getLocalGraphicsEnvironment()
  55. val graphicsDevice: GraphicsDevice
  56. get() = graphicsEnvironment.defaultScreenDevice
  57. val isTransparencySupported
  58. get() = graphicsDevice.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT)
  59. // val generativeModel: GenerativeModel by lazy {
  60. // GenerativeModel(
  61. // modelName = "gemini-1.5-pro-latest",
  62. // apiKey = loadKey("gemini")
  63. // )
  64. // }
  65. init {
  66. currentUser.theme.controlCenterExpandedWidth = containerSize.width.div(4)
  67. }
  68. private fun loadKey(key: String): String = runCatching {
  69. ResourceStream("keys/$key.key").string
  70. }.getOrNull().orEmpty()
  71. // fun askGemini(question: String): String = runBlocking {
  72. // generativeModel.generateContent(content {
  73. // text(question)
  74. // }).text
  75. // }.orEmpty()
  76. fun runScript(script: String): Any =
  77. engine.eval(script)
  78. fun runScript(file: File): Any =
  79. engine.eval(FileReader(file))
  80. fun executeCmd(cmd: String) {
  81. ProcessBuilder().apply {
  82. command(cmd)
  83. }.start()
  84. }
  85. fun executeCmd(cmd: String, directory: File) {
  86. ProcessBuilder().apply {
  87. directory(directory)
  88. command(cmd)
  89. }.start()
  90. }
  91. // fun runCommandForOutput(vararg cmd: String): String? = runCatching {
  92. // val sj = StringJoiner(System.lineSeparator())
  93. // var result = ""
  94. // try {
  95. // val p = ProcessBuilder(*cmd).start()
  96. // BufferedReader(InputStreamReader(p.inputStream))
  97. // .lines()
  98. // .iterator()
  99. // .forEachRemaining { newElement: String? ->
  100. // sj.add(newElement)
  101. // }
  102. // result = sj.toString()
  103. // p.waitFor()
  104. // p.destroy()
  105. // } catch (e: Exception) {
  106. // e.printStackTrace()
  107. // }
  108. // result
  109. // }.getOrNull()
  110. companion object {
  111. private val CONTROL_CENTER_PAGES = listOf(
  112. MainSettingsPage(),
  113. WifiSettingsPage(),
  114. BluetoothSettingsPage(),
  115. DisplaySettingsPage(),
  116. SoundSettingsPage()
  117. )
  118. val LocalDesktop = compositionLocalOf {
  119. DesktopProvider()
  120. }
  121. }
  122. }