milan jurkulák 2 лет назад
Родитель
Сommit
cde3c9fb0a

+ 16 - 1
src/main/kotlin/eu/mjdev/desktop/components/custom/Clock.kt

@@ -5,6 +5,7 @@ import androidx.compose.foundation.layout.Box
 import androidx.compose.foundation.layout.Column
 import androidx.compose.foundation.layout.fillMaxWidth
 import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
 import androidx.compose.ui.Alignment
 import androidx.compose.ui.Modifier
 import androidx.compose.ui.graphics.Color
@@ -15,6 +16,8 @@ import androidx.compose.ui.unit.sp
 import eu.mjdev.desktop.components.text.TextWithShadow
 import eu.mjdev.desktop.extensions.Custom.dateFlow
 import eu.mjdev.desktop.extensions.Custom.timeFlow
+import eu.mjdev.desktop.provider.DesktopProvider
+import eu.mjdev.desktop.provider.DesktopProvider.Companion.LocalDesktop
 
 @Preview
 @Composable
@@ -28,14 +31,17 @@ fun Clock(
     dateTextColor: Color = Color.White,
     showTime: Boolean = true,
     showDate: Boolean = true,
+    talkEveryHour: Boolean = true,
+    api: DesktopProvider = LocalDesktop.current,
 ) = Box(
     modifier = modifier,
     contentAlignment = Alignment.Center
 ) {
+    val time = timeFlow.value
     Column {
         if (showTime) TextWithShadow(
             modifier = Modifier.fillMaxWidth(),
-            text = timeFlow.value,
+            text = time,
             textAlign = TextAlign.Center,
             fontWeight = timeTextWeight,
             fontSize = timeTextSize,
@@ -50,4 +56,13 @@ fun Clock(
             color = dateTextColor
         )
     }
+    LaunchedEffect(time) {
+        if (time.isNotEmpty()) {
+            val parsed = time.split(":")
+            val isZeroMinutes = (parsed.getOrNull(1) ?: "-") == "00"
+            if (talkEveryHour && isZeroMinutes) {
+                api.aiProvider.talk("It is ${parsed.firstOrNull()} hour")
+            }
+        }
+    }
 }

+ 2 - 2
src/main/kotlin/eu/mjdev/desktop/extensions/Custom.kt

@@ -35,7 +35,7 @@ object Custom {
                     delay(5000L)
                 } while (true)
             }
-        }.collectAsState(initial = "1.1.1970")
+        }.collectAsState(initial = "")
 
     val timeFlow
         @Composable
@@ -52,7 +52,7 @@ object Custom {
                     delay(200L)
                 } while (true)
             }
-        }.collectAsState(initial = "00:00:00")
+        }.collectAsState(initial = "")
 
     // todo better solution
     fun <T> SnapshotStateList<T>.invalidate() = toList().also {

+ 44 - 41
src/main/kotlin/eu/mjdev/desktop/windows/WindowsTracker.kt

@@ -26,32 +26,31 @@ class WindowsTracker {
 
     @Suppress("DEPRECATED_IDENTITY_EQUALS")
     private fun recurse(root: Window, depth: Int): List<SystemWindow> {
-        return runCatching {
-            val windowList = mutableListOf<SystemWindow>()
-            val windowRef = WindowByReference()
-            val parentRef = WindowByReference()
-            val childrenRef = PointerByReference()
-            val childCountRef = IntByReference()
-            x11.XQueryTree(display, root, windowRef, parentRef, childrenRef, childCountRef)
-            if (childrenRef.value == null) return emptyList()
-            val ids: LongArray
-            if (Native.LONG_SIZE === java.lang.Long.BYTES) {
-                ids = childrenRef.value.getLongArray(0, childCountRef.value)
-            } else if (Native.LONG_SIZE === Integer.BYTES) {
-                val intIds: IntArray = childrenRef.value.getIntArray(0, childCountRef.value)
-                ids = LongArray(intIds.size)
-                for (i in intIds.indices) {
-                    ids[i] = intIds[i].toLong()
-                }
-            } else return emptyList()
+        val windowList = mutableListOf<SystemWindow>()
+        val windowRef = WindowByReference()
+        val parentRef = WindowByReference()
+        val childrenRef = PointerByReference()
+        val childCountRef = IntByReference()
+        x11.XQueryTree(display, root, windowRef, parentRef, childrenRef, childCountRef)
+        if (childrenRef.value == null) return emptyList()
+        val ids: LongArray
+        if (Native.LONG_SIZE === java.lang.Long.BYTES) {
+            ids = childrenRef.value.getLongArray(0, childCountRef.value)
+        } else if (Native.LONG_SIZE === Integer.BYTES) {
+            val intIds: IntArray = childrenRef.value.getIntArray(0, childCountRef.value)
+            ids = LongArray(intIds.size)
+            for (i in intIds.indices) {
+                ids[i] = intIds[i].toLong()
+            }
+        } else return emptyList()
 //            throw IllegalStateException("Unexpected size for Native.LONG_SIZE" + Native.LONG_SIZE)
-            for (id in ids) {
-                if (id == 0L) continue
-                val window = Window(id)
-                val name = XTextProperty()
-                val pids = mutableListOf<Long>()
-                // todo
-                x11.XGetWMName(display, window, name)
+        for (id in ids) {
+            if (id == 0L) continue
+            val window = Window(id)
+            val name = XTextProperty()
+            val pids = mutableListOf<Long>()
+            // todo
+            x11.XGetWMName(display, window, name)
 //            x11.XGetWindowAttributes()
 //            x11.XGetGeometry()
 //            x11.XGetWMHints()
@@ -60,19 +59,23 @@ class WindowsTracker {
 //            x11.XGetKeyboardMapping()
 //            x11.XGetModifierMapping()
 //            x11.XGetKeyboardControl()
-                windowList.add(
-                    SystemWindow(
-                        this,
-                        id,
-                        name.value ?: "",
-                        pids
-                    )
+            windowList.add(
+                SystemWindow(
+                    this,
+                    id,
+                    name.value ?: "",
+                    pids
                 )
+            )
+            // todo error free memory
+//            try {
 //                x11.XFree(name.getPointer())
-                recurse(window, depth + 1)
-            }
-            windowList
-        }.getOrNull() ?: emptyList()
+//            } catch (t: Throwable) {
+//                t.printStackTrace()
+//            }
+            recurse(window, depth + 1)
+        }
+        return windowList
     }
 
     class SystemWindow(
@@ -82,23 +85,23 @@ class WindowsTracker {
         val pids: List<Long>
     ) : IWindow {
         override fun toBack() {
-
+            // todo
         }
 
         override fun toFront() {
-
+            // todo
         }
 
         override fun minimize() {
-
+            // todo
         }
 
         override fun maximize() {
-
+            // todo
         }
 
         override fun close() {
-
+            // todo
         }
     }