Skip to content

Commit

Permalink
🔨 Refactoring window management impl
Browse files Browse the repository at this point in the history
  • Loading branch information
guiyanakuang committed Jun 14, 2024
1 parent 8cca9d6 commit c5c1bc9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.clipevery.listen.ActiveGraphicsDevice
import com.clipevery.listener.ShortcutKeys
import com.clipevery.os.linux.api.X11Api
import com.sun.jna.platform.unix.X11.Window
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class LinuxAppWindowManager(
Expand Down Expand Up @@ -70,6 +71,9 @@ class LinuxAppWindowManager(
}

prevLinuxAppInfo = X11Api.bringToFront(SEARCH_WINDOW_TITLE)

delay(500)
searchFocusRequester.requestFocus()
}

override suspend fun unActiveSearchWindow(preparePaste: suspend () -> Boolean) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,38 @@
package com.clipevery.app

import com.clipevery.listen.ActiveGraphicsDevice
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.TestInstance
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TestAppWindowManager {

companion object {

var testAppWindowManager: AppWindowManager =
DesktopAppWindowManager(
lazy { throw NotImplementedError("not invoke") },
object : ActiveGraphicsDevice {
override fun getGraphicsDevice() = null
},
-1,
)

@BeforeEach
fun setUp() {
TestWindowManager.init()
}
}

@Test
fun testMockTestAppWindowManager() {
val mockOS = MockOS()
val testAppWindowManager = TestWindowManager(mockOS)
assertNull(testAppWindowManager.getPrevAppName())
runBlocking { testAppWindowManager.toPaste() }
assertEquals(1, TestWindowManager.pasterId)
assertEquals(1, testAppWindowManager.pasterId)
assertNull(testAppWindowManager.getCurrentActiveAppName())
testAppWindowManager.activeMainWindow()
assertEquals("Clipevery", testAppWindowManager.getCurrentActiveAppName())
testAppWindowManager.unActiveMainWindow()
assertNull(testAppWindowManager.getCurrentActiveAppName())
TestWindowManager.activeApp("Chrome")
mockOS.currentApp = "Chrome"
runBlocking { testAppWindowManager.activeSearchWindow() }
assertEquals("Clipevery", testAppWindowManager.getCurrentActiveAppName())
assertEquals("Chrome", testAppWindowManager.getPrevAppName())
runBlocking { testAppWindowManager.unActiveSearchWindow(preparePaste = { true }) }
assertEquals(2, TestWindowManager.pasterId)
assertEquals(2, testAppWindowManager.pasterId)
assertEquals("Chrome", testAppWindowManager.getCurrentActiveAppName())
testAppWindowManager.activeMainWindow()
runBlocking { testAppWindowManager.activeSearchWindow() }
assertEquals("Clipevery", testAppWindowManager.getCurrentActiveAppName())
assertEquals("Chrome", testAppWindowManager.getPrevAppName())
runBlocking { testAppWindowManager.unActiveSearchWindow(preparePaste = { false }) }
assertEquals(2, TestWindowManager.pasterId)
assertEquals(2, testAppWindowManager.pasterId)
runBlocking { testAppWindowManager.toPaste() }
assertEquals(3, TestWindowManager.pasterId)
assertEquals(3, testAppWindowManager.pasterId)
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,63 @@
package com.clipevery.app

object TestWindowManager : WindowManager {
import kotlinx.coroutines.runBlocking

var prevApp: String? = null
class TestWindowManager(
private val mockOS: MockOS,
) : AbstractAppWindowManager() {

var currentApp: String? = null
var prevApp: String? = null

var pasterId: Int = 0

var currentTitle: String? = null

override fun getPrevAppName(): String? {
return prevApp
}

override fun getCurrentActiveAppName(): String? {
return currentApp
return mockOS.currentApp
}

override fun activeMainWindow() {
showMainWindow = true
bringToFront(MAIN_WINDOW_TITLE)
}

override fun unActiveMainWindow() {
runBlocking {
bringToBack(MAIN_WINDOW_TITLE, false)
}
showMainWindow = false
}

override suspend fun activeSearchWindow() {
showSearchWindow = true

bringToFront(SEARCH_WINDOW_TITLE)
}

override suspend fun bringToFront(windowTitle: String) {
if (currentApp != "Clipevery") {
prevApp = currentApp
override suspend fun unActiveSearchWindow(preparePaste: suspend () -> Boolean) {
val toPaste = preparePaste()
bringToBack(SEARCH_WINDOW_TITLE, toPaste)
showSearchWindow = false
}

private fun bringToFront(windowTitle: String) {
currentTitle = windowTitle
if (mockOS.currentApp != "Clipevery") {
prevApp = mockOS.currentApp
}
currentApp = "Clipevery"
mockOS.currentApp = "Clipevery"
}

override suspend fun bringToBack(
private suspend fun bringToBack(
windowTitle: String,
toPaste: Boolean,
) {
currentApp = prevApp
currentTitle = windowTitle
mockOS.currentApp = prevApp
if (toPaste) {
toPaste()
}
Expand All @@ -36,14 +66,6 @@ object TestWindowManager : WindowManager {
override suspend fun toPaste() {
pasterId++
}

fun init() {
prevApp = null
currentApp = null
pasterId = 0
}

fun activeApp(appName: String) {
currentApp = appName
}
}

class MockOS(var currentApp: String? = null)

0 comments on commit c5c1bc9

Please sign in to comment.