From 355fa47187ce90f2fcd88d5be297bec406e2c70c Mon Sep 17 00:00:00 2001 From: Valeriy Vyrva Date: Wed, 18 Jan 2023 15:53:05 +0300 Subject: [PATCH] Make function detektWindow more idiomatic --- src/main/kotlin/Bot.kt | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/Bot.kt b/src/main/kotlin/Bot.kt index b3343c2..1d321f2 100644 --- a/src/main/kotlin/Bot.kt +++ b/src/main/kotlin/Bot.kt @@ -42,22 +42,17 @@ fun main() { private fun detektWindow(windowName: String): Rectangle { val user32 = MyUser32.instance - val rect = Rectangle(0, 0, 0, 0) - var windowTitle = "" - - val windows = WindowUtils.getAllWindows(true) - windows.forEach { - if (it.title.contains(windowName)) { - rect.setRect(it.locAndSize) - windowTitle = it.title + return WindowUtils.getAllWindows(true) + .filter { it.title.contains(windowName) } + .takeLast(1) + .onEach { + user32.FindWindow(null, it.title)?.also { hwnd -> + user32.ShowWindow(hwnd, User32.SW_SHOW) + user32.SetForegroundWindow(hwnd) + } } - } - - val tst: WinDef.HWND = user32.FindWindow(null, windowTitle) - user32.ShowWindow(tst, User32.SW_SHOW) - user32.SetForegroundWindow(tst) - - return rect + .map { it.locAndSize.bounds } + .firstOrNull() ?: Rectangle(0, 0, 0, 0) } private fun ifTargetSelectedAndHasHp(rectangle: Rectangle): Boolean {