diff --git a/composeApp/src/commonMain/kotlin/com/clipevery/clip/ClipboardService.kt b/composeApp/src/commonMain/kotlin/com/clipevery/clip/ClipboardService.kt index d733a7ed0..475927193 100644 --- a/composeApp/src/commonMain/kotlin/com/clipevery/clip/ClipboardService.kt +++ b/composeApp/src/commonMain/kotlin/com/clipevery/clip/ClipboardService.kt @@ -6,9 +6,7 @@ import com.clipevery.dao.clip.ClipDao import com.clipevery.dao.clip.ClipData import io.github.oshai.kotlinlogging.KLogger import kotlinx.coroutines.channels.Channel -import java.awt.datatransfer.Clipboard import java.awt.datatransfer.ClipboardOwner -import java.awt.datatransfer.Transferable interface ClipboardService : ClipboardMonitor, ClipboardOwner { @@ -16,64 +14,23 @@ interface ClipboardService : ClipboardMonitor, ClipboardOwner { var owner: Boolean - var ownerTransferable: Transferable? - - val systemClipboard: Clipboard - val appWindowManager: AppWindowManager val clipDao: ClipDao val configManager: ConfigManager - val clipConsumer: TransferableConsumer - - val clipProducer: TransferableProducer - val clipboardChannel: Channel Unit> - fun isValidContents(contents: Transferable?): Boolean { - return contents != null && contents.transferDataFlavors.isNotEmpty() - } - - fun getClipboardContentsBySafe(): Transferable? { - return try { - systemClipboard.getContents(null) - } catch (e: Exception) { - logger.error(e) { "getContentsBySafe error" } - null - } - } - suspend fun tryWriteClipboard( clipData: ClipData, localOnly: Boolean = false, filterFile: Boolean = false, - ) { - try { - clipProducer.produce(clipData, localOnly, filterFile)?.let { - ownerTransferable = it - owner = true - systemClipboard.setContents(ownerTransferable, this) - } - } catch (e: Exception) { - logger.error(e) { "tryWriteClipboard error" } - } - } + ) - suspend fun tryWriteRemoteClipboard(clipData: ClipData) { - clipDao.releaseRemoteClipData(clipData) { storeClipData, filterFile -> - clipboardChannel.trySend { tryWriteClipboard(storeClipData, localOnly = true, filterFile = filterFile) } - } - } + suspend fun tryWriteRemoteClipboard(clipData: ClipData) - suspend fun tryWriteRemoteClipboardWithFile(clipData: ClipData) { - clipDao.releaseRemoteClipDataWithFile(clipData.id) { storeClipData -> - clipboardChannel.trySend { tryWriteClipboard(storeClipData, localOnly = true, filterFile = false) } - } - } + suspend fun tryWriteRemoteClipboardWithFile(clipData: ClipData) - suspend fun clearRemoteClipboard(clipData: ClipData) { - clipDao.markDeleteClipData(clipData.id) - } + suspend fun clearRemoteClipboard(clipData: ClipData) } diff --git a/composeApp/src/commonMain/kotlin/com/clipevery/ui/settings/SettingsView.kt b/composeApp/src/commonMain/kotlin/com/clipevery/ui/settings/SettingsView.kt index 8dfd599ba..f584ad73e 100644 --- a/composeApp/src/commonMain/kotlin/com/clipevery/ui/settings/SettingsView.kt +++ b/composeApp/src/commonMain/kotlin/com/clipevery/ui/settings/SettingsView.kt @@ -326,8 +326,8 @@ fun SettingsView(currentPageViewContext: MutableState) { .height(20.dp), checked = enableClipboardListening, onCheckedChange = { - enableClipboardListening = it clipboardService.toggle() + enableClipboardListening = configManager.config.enableClipboardListening }, ) } diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/Clipevery.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/Clipevery.kt index a3c2f182a..763c095d8 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/Clipevery.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/Clipevery.kt @@ -319,7 +319,9 @@ class Clipevery { try { val pair = koinApplication.koin.get().acquireLock() if (pair.first) { - koinApplication.koin.get().start() + if (koinApplication.koin.get().config.enableClipboardListening) { + koinApplication.koin.get().start() + } koinApplication.koin.get() koinApplication.koin.get().start() koinApplication.koin.get() diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/clip/AbstractClipboardService.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/clip/AbstractClipboardService.kt new file mode 100644 index 000000000..5242ec557 --- /dev/null +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/clip/AbstractClipboardService.kt @@ -0,0 +1,72 @@ +package com.clipevery.clip + +import com.clipevery.dao.clip.ClipData +import java.awt.datatransfer.Clipboard +import java.awt.datatransfer.Transferable + +abstract class AbstractClipboardService : ClipboardService { + + abstract var ownerTransferable: Transferable? + + abstract val systemClipboard: Clipboard + + abstract val clipConsumer: TransferableConsumer + + abstract val clipProducer: TransferableProducer + + fun isValidContents(contents: Transferable?): Boolean { + return contents != null && contents.transferDataFlavors.isNotEmpty() + } + + fun getClipboardContentsBySafe(): Transferable? { + return try { + systemClipboard.getContents(null) + } catch (e: Exception) { + logger.error(e) { "getContentsBySafe error" } + null + } + } + + override suspend fun tryWriteClipboard( + clipData: ClipData, + localOnly: Boolean, + filterFile: Boolean, + ) { + try { + clipProducer.produce(clipData, localOnly, filterFile)?.let { + ownerTransferable = it + owner = true + systemClipboard.setContents(ownerTransferable, this) + } + } catch (e: Exception) { + logger.error(e) { "tryWriteClipboard error" } + } + } + + override suspend fun tryWriteRemoteClipboard(clipData: ClipData) { + clipDao.releaseRemoteClipData(clipData) { storeClipData, filterFile -> + clipboardChannel.trySend { tryWriteClipboard(storeClipData, localOnly = true, filterFile = filterFile) } + } + } + + override suspend fun tryWriteRemoteClipboardWithFile(clipData: ClipData) { + clipDao.releaseRemoteClipDataWithFile(clipData.id) { storeClipData -> + clipboardChannel.trySend { tryWriteClipboard(storeClipData, localOnly = true, filterFile = false) } + } + } + + override suspend fun clearRemoteClipboard(clipData: ClipData) { + clipDao.markDeleteClipData(clipData.id) + } + + @Synchronized + override fun toggle() { + val enableClipboardListening = configManager.config.enableClipboardListening + if (enableClipboardListening) { + stop() + } else { + start() + } + configManager.updateConfig { it.copy(enableClipboardListening = !enableClipboardListening) } + } +} diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/clip/DesktopClipboardService.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/clip/DesktopClipboardService.kt index b01cea7bc..046dab3ac 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/clip/DesktopClipboardService.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/clip/DesktopClipboardService.kt @@ -11,7 +11,7 @@ fun getDesktopClipboardService( configManager: ConfigManager, clipConsumer: TransferableConsumer, clipProducer: TransferableProducer, -): ClipboardService { +): AbstractClipboardService { val currentPlatform = currentPlatform() return if (currentPlatform.isMacos()) { MacosClipboardService(appWindowManager, clipDao, configManager, clipConsumer, clipProducer) diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/clip/LinuxClipboardService.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/clip/LinuxClipboardService.kt index 2e351e506..a21b04e27 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/clip/LinuxClipboardService.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/clip/LinuxClipboardService.kt @@ -34,7 +34,7 @@ class LinuxClipboardService( override val configManager: ConfigManager, override val clipConsumer: TransferableConsumer, override val clipProducer: TransferableProducer, -) : ClipboardService { +) : AbstractClipboardService() { companion object { const val XFIXES_SET_SELECTION_OWNER_NOTIFY_MASK = (1 shl 0).toLong() @@ -161,16 +161,6 @@ class LinuxClipboardService( configManager.updateConfig { it.copy(lastClipboardChangeCount = changeCount) } } - override fun toggle() { - val enableClipboardListening = configManager.config.enableClipboardListening - if (enableClipboardListening) { - stop() - } else { - start() - } - configManager.updateConfig { it.copy(enableClipboardListening = !enableClipboardListening) } - } - override fun lostOwnership( clipboard: Clipboard?, contents: Transferable?, diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/clip/MacosClipboardService.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/clip/MacosClipboardService.kt index 7fe0386ab..1beceea30 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/clip/MacosClipboardService.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/clip/MacosClipboardService.kt @@ -28,7 +28,7 @@ class MacosClipboardService( override val configManager: ConfigManager, override val clipConsumer: TransferableConsumer, override val clipProducer: TransferableProducer, -) : ClipboardService { +) : AbstractClipboardService() { override val logger: KLogger = KotlinLogging.logger {} private var changeCount = configManager.config.lastClipboardChangeCount @@ -110,10 +110,8 @@ class MacosClipboardService( @Synchronized override fun start() { - if (configManager.config.enableClipboardListening) { - if (job?.isActive != true) { - job = run() - } + if (job?.isActive != true) { + job = run() } } @@ -122,15 +120,4 @@ class MacosClipboardService( job?.cancel() configManager.updateConfig { it.copy(lastClipboardChangeCount = changeCount) } } - - @Synchronized - override fun toggle() { - val enableClipboardListening = configManager.config.enableClipboardListening - configManager.updateConfig { it.copy(enableClipboardListening = !enableClipboardListening) } - if (!enableClipboardListening) { - start() - } else { - stop() - } - } } diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/clip/WindowsClipboardService.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/clip/WindowsClipboardService.kt index 2ac1923d4..7ea99eb95 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/clip/WindowsClipboardService.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/clip/WindowsClipboardService.kt @@ -33,7 +33,7 @@ class WindowsClipboardService( override val configManager: ConfigManager, override val clipConsumer: TransferableConsumer, override val clipProducer: TransferableProducer, -) : ClipboardService, User32.WNDPROC { +) : AbstractClipboardService(), User32.WNDPROC { override val logger: KLogger = KotlinLogging.logger {} @Volatile @@ -131,13 +131,11 @@ class WindowsClipboardService( } override fun start() { - if (configManager.config.enableClipboardListening) { - if (job?.isActive != true) { - job = - serviceScope.launch(CoroutineName("WindowsClipboardService")) { - run() - } - } + if (job?.isActive != true) { + job = + serviceScope.launch(CoroutineName("WindowsClipboardService")) { + run() + } } } @@ -147,17 +145,6 @@ class WindowsClipboardService( configManager.updateConfig { it.copy(lastClipboardChangeCount = changeCount) } } - @Synchronized - override fun toggle() { - val enableClipboardListening = configManager.config.enableClipboardListening - if (enableClipboardListening) { - stop() - } else { - start() - } - configManager.updateConfig { it.copy(enableClipboardListening = !enableClipboardListening) } - } - private fun onChange() { try { val source =