-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔨 Refactoring window management impl
- Loading branch information
1 parent
74c74bd
commit 8cca9d6
Showing
17 changed files
with
367 additions
and
426 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
composeApp/src/commonMain/kotlin/com/clipevery/app/AppTokenService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.clipevery.app | ||
|
||
interface AppTokenService { | ||
|
||
var showToken: Boolean | ||
|
||
var token: CharArray | ||
|
||
fun startRefreshToken() | ||
|
||
fun stopRefreshToken() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 0 additions & 17 deletions
17
composeApp/src/commonMain/kotlin/com/clipevery/app/WindowManager.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
composeApp/src/desktopMain/kotlin/com/clipevery/app/AbstractAppWindowManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.clipevery.app | ||
|
||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.focus.FocusRequester | ||
import androidx.compose.ui.unit.DpSize | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.window.WindowPlacement | ||
import androidx.compose.ui.window.WindowPosition | ||
import androidx.compose.ui.window.WindowState | ||
import com.clipevery.path.DesktopPathProvider | ||
import com.clipevery.path.PathProvider | ||
import com.clipevery.utils.Memoize | ||
import com.clipevery.utils.ioDispatcher | ||
import io.github.oshai.kotlinlogging.KLogger | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.SupervisorJob | ||
import java.awt.Rectangle | ||
|
||
abstract class AbstractAppWindowManager : AppWindowManager { | ||
|
||
companion object { | ||
const val MAIN_WINDOW_TITLE: String = "Clipevery" | ||
|
||
const val SEARCH_WINDOW_TITLE: String = "Clipevery Search" | ||
|
||
// only use in Windows | ||
const val MENU_WINDOW_TITLE: String = "Clipevery Menu" | ||
} | ||
|
||
protected val logger: KLogger = KotlinLogging.logger {} | ||
|
||
protected val ioScope = CoroutineScope(ioDispatcher + SupervisorJob()) | ||
|
||
protected val pathProvider: PathProvider = DesktopPathProvider | ||
|
||
override var showMainWindow by mutableStateOf(false) | ||
|
||
override var mainWindowState: WindowState by mutableStateOf( | ||
WindowState( | ||
placement = WindowPlacement.Floating, | ||
position = WindowPosition.PlatformDefault, | ||
size = DpSize(width = 460.dp, height = 710.dp), | ||
), | ||
) | ||
|
||
override var showMainDialog by mutableStateOf(false) | ||
|
||
override var showSearchWindow by mutableStateOf(false) | ||
|
||
override var searchWindowState: WindowState by mutableStateOf( | ||
WindowState( | ||
placement = WindowPlacement.Floating, | ||
position = WindowPosition.Aligned(Alignment.Center), | ||
size = DpSize(width = 800.dp, height = 520.dp), | ||
), | ||
) | ||
|
||
override var searchFocusRequester = FocusRequester() | ||
|
||
override val searchWindowDetailViewDpSize = DpSize(width = 500.dp, height = 240.dp) | ||
|
||
protected val calPosition: (Rectangle) -> WindowPosition = | ||
Memoize.memoize { bounds -> | ||
val windowSize = searchWindowState.size | ||
WindowPosition( | ||
x = (bounds.x.dp + ((bounds.width.dp - windowSize.width) / 2)), | ||
y = (bounds.y.dp + ((bounds.height.dp - windowSize.height) / 2)), | ||
) | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
composeApp/src/desktopMain/kotlin/com/clipevery/app/DesktopAppTokenService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.clipevery.app | ||
|
||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import com.clipevery.utils.mainDispatcher | ||
import kotlinx.coroutines.CoroutineName | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.isActive | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import kotlin.random.Random | ||
|
||
class DesktopAppTokenService : AppTokenService { | ||
|
||
private var startRefreshNumber: Int = 0 | ||
|
||
private var refreshTokenJob: Job? = null | ||
|
||
private val scope = CoroutineScope(Dispatchers.IO) | ||
|
||
override var showToken by mutableStateOf(false) | ||
|
||
override var token by mutableStateOf(charArrayOf('0', '0', '0', '0', '0', '0')) | ||
|
||
private suspend fun refreshToken() { | ||
withContext(mainDispatcher) { | ||
token = CharArray(6) { (Random.nextInt(10) + '0'.code).toChar() } | ||
} | ||
} | ||
|
||
@Synchronized | ||
override fun startRefreshToken() { | ||
if (startRefreshNumber++ == 0) { | ||
refreshTokenJob = | ||
scope.launch(CoroutineName("RefreshToken")) { | ||
while (isActive) { | ||
refreshToken() | ||
delay(30000) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Synchronized | ||
override fun stopRefreshToken() { | ||
startRefreshNumber -= 1 | ||
if (startRefreshNumber == 0) { | ||
refreshTokenJob?.cancel() | ||
} | ||
} | ||
} |
Oops, something went wrong.