Skip to content

Commit

Permalink
Overlay, Sound
Browse files Browse the repository at this point in the history
  • Loading branch information
jombidev committed Apr 22, 2023
1 parent b2cd33e commit d14a47c
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 28 deletions.
1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
implementation("com.github.kwhat", "jnativehook", "2.2.2")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
implementation("com.fasterxml.jackson.core:jackson-databind:2.14.2")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.14.2")
}

val fatJar = task("fatJar", type = Jar::class) {
Expand Down
13 changes: 0 additions & 13 deletions src/main/kotlin/dev/jombi/copytotrans/Config.kt

This file was deleted.

40 changes: 31 additions & 9 deletions src/main/kotlin/dev/jombi/copytotrans/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ package dev.jombi.copytotrans
import com.github.kwhat.jnativehook.GlobalScreen
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener
import dev.jombi.copytotrans.config.makeDefaultConfig
import dev.jombi.copytotrans.config.shouldPlaySound
import dev.jombi.copytotrans.overlay.OverlayManager
import dev.jombi.copytotrans.translator.impl.newg.FailedToTranslateException
import java.awt.Toolkit
import java.awt.datatransfer.DataFlavor
import java.awt.datatransfer.StringSelection
import javax.sound.sampled.AudioSystem
import javax.sound.sampled.Clip
import javax.sound.sampled.DataLine
import javax.sound.sampled.LineEvent
import kotlin.io.path.Path
import kotlin.io.path.inputStream
import kotlin.system.exitProcess

fun main() {
makeDefaultConfig()
val clipboard = Toolkit.getDefaultToolkit().systemClipboard
val google = Keybinding(listOf(NativeKeyEvent.VC_CONTROL, NativeKeyEvent.VC_SHIFT, NativeKeyEvent.VC_Q)) {
val target = clipboard.getData(DataFlavor.stringFlavor) as String
Expand All @@ -19,34 +29,30 @@ fun main() {
println("Translated: $translated, using 'Google' translator")
val sel = StringSelection(translated)
clipboard.setContents(sel, sel)
OverlayManager.show(translated)
playFinishedSound()
} catch (e: FailedToTranslateException) {
println("Failed to translate: ${e.message}")
val sel = StringSelection(e.message)
clipboard.setContents(sel, sel)

}
}
val googleOld = Keybinding(listOf(NativeKeyEvent.VC_CONTROL, NativeKeyEvent.VC_SHIFT, NativeKeyEvent.VC_ALT, NativeKeyEvent.VC_Q)) {
val target = clipboard.getData(DataFlavor.stringFlavor) as String
println("Received: $target")
val translated = Translators.GoogleOld.translate(target)
println("Translated: $translated, using 'Google Old' translator")
val sel = StringSelection(translated)
clipboard.setContents(sel, sel)
}
val papago = Keybinding(listOf(NativeKeyEvent.VC_CONTROL, NativeKeyEvent.VC_SHIFT, NativeKeyEvent.VC_W)) {
val target = clipboard.getData(DataFlavor.stringFlavor) as String
println("Received: $target")
val translated = Translators.Papago.translate(target)
println("Translated: $translated, using 'Papago' translator")
val sel = StringSelection(translated)
clipboard.setContents(sel, sel)
OverlayManager.show(translated)
playFinishedSound()
}
val exitKey = Keybinding(listOf(NativeKeyEvent.VC_CONTROL, NativeKeyEvent.VC_ESCAPE)) {
GlobalScreen.unregisterNativeHook()
exitProcess(0)
}
val keybindings = arrayOf(google, googleOld, papago, exitKey)
val keybindings = arrayOf(google/*, googleOld*/, papago, exitKey)
GlobalScreen.registerNativeHook()
GlobalScreen.addNativeKeyListener(object : NativeKeyListener {
override fun nativeKeyPressed(e: NativeKeyEvent) {
Expand All @@ -57,4 +63,20 @@ fun main() {
for (k in keybindings) k.unpress(e.keyCode)
}
})
OverlayManager.init()
}

fun playFinishedSound() {
if (!shouldPlaySound()) return
try {
val stream = AudioSystem.getAudioInputStream(Path("res/sharexfinish.wav").inputStream())
val format = stream.format
val info = DataLine.Info(Clip::class.java, format)
val clip = AudioSystem.getLine(info) as Clip
clip.open(stream)
clip.start()
clip.addLineListener { if (it.type == LineEvent.Type.STOP) clip.close() }
} catch (e: Exception) {
e.printStackTrace()
}
}
66 changes: 66 additions & 0 deletions src/main/kotlin/dev/jombi/copytotrans/TranslateStatus.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package dev.jombi.copytotrans

import java.awt.AWTException
import java.awt.BorderLayout
import java.awt.Color
import java.awt.Toolkit
import javax.swing.JDialog
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.SwingUtilities
import kotlin.system.exitProcess

class TranslateStatus : JDialog() {
private val lab: JLabel
var hideMs = 0L
val transResult: JLabel
init {
isUndecorated = true
isAlwaysOnTop = true
isAutoRequestFocus = false
isFocusable = false
focusableWindowState = false
defaultCloseOperation = DISPOSE_ON_CLOSE

opacity = 0.75f
val panel = JPanel()
panel.layout = BorderLayout()
panel.background = Color(1f, 1f, 1f)
lab = JLabel("Translation finished")
panel.add(lab, BorderLayout.NORTH)
transResult = JLabel("text")
panel.add(transResult, BorderLayout.SOUTH)
add(panel)
}

fun delayedHideLoopback() {
Thread {
while (true) {
if (hideMs - System.currentTimeMillis() < 0L) hideOverlay()
Thread.sleep(10L)
}
}.start()
}

fun draw() {
SwingUtilities.invokeLater {
try {
val screenSize = Toolkit.getDefaultToolkit().screenSize
setLocation(screenSize.width / 2 - lab.width, screenSize.height / 2 + 32)
// showOverlay()
pack()
} catch (e: AWTException) {
// error("Error while showing overlay", e)
exitProcess(-1)
}
}
}

fun showOverlay() {
if (!isVisible) isVisible = true
}
fun hideOverlay() {
if (isVisible) isVisible = false
}

}
28 changes: 28 additions & 0 deletions src/main/kotlin/dev/jombi/copytotrans/config/Config.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.jombi.copytotrans.config

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import kotlin.io.path.*

val mapper = jacksonObjectMapper()

fun getConfig(): Property = mapper.readValue(Path("config.json").inputStream())
private fun getRawConfig(): JsonNode = mapper.readTree(Path("config.json").apply { if (!exists() || !isRegularFile()) { createFile();writeText("{}") } }.inputStream())

fun makeDefaultConfig() {
val cfg = getRawConfig()
val path = Path("config.json")
val p = Property()
if (cfg.hasNonNull("papagoKey")) p.papagoKey = cfg["papagoKey"].asText()
if (cfg.hasNonNull("papagoSecret")) p.papagoSecret = cfg["papagoSecret"].asText()
if (cfg.hasNonNull("sound")) p.sound = cfg["sound"].asBoolean()
if (cfg.hasNonNull("overlay")) p.overlay = cfg["overlay"].asBoolean()
mapper.writerWithDefaultPrettyPrinter().writeValue(path.outputStream(), p)
}

fun getPapagoApiKey(): String = getConfig().papagoKey
fun getPapagoApiSecret(): String = getConfig().papagoSecret

fun shouldShowOverlay(): Boolean = getConfig().overlay
fun shouldPlaySound(): Boolean = getConfig().sound
8 changes: 8 additions & 0 deletions src/main/kotlin/dev/jombi/copytotrans/config/Property.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.jombi.copytotrans.config

data class Property(
var papagoKey: String = "",
var papagoSecret: String = "",
var overlay: Boolean = true,
var sound: Boolean = true
)
19 changes: 19 additions & 0 deletions src/main/kotlin/dev/jombi/copytotrans/overlay/OverlayManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.jombi.copytotrans.overlay

import dev.jombi.copytotrans.TranslateStatus
import dev.jombi.copytotrans.config.shouldShowOverlay

object OverlayManager {
private val ts = TranslateStatus()
fun show(text: String) {
if (shouldShowOverlay()) {
ts.transResult.text = text
ts.hideMs = System.currentTimeMillis() + 5000L
ts.showOverlay()
}
}
fun init() {
ts.delayedHideLoopback()
ts.draw()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.jombi.copytotrans.translator.impl

import dev.jombi.copytotrans.buildUrlEncoded
import dev.jombi.copytotrans.mapper
import dev.jombi.copytotrans.config.mapper
import dev.jombi.copytotrans.translator.Translator
import java.net.HttpURLConnection
import java.net.URL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package dev.jombi.copytotrans.translator.impl

import dev.jombi.copytotrans.buildUrlEncoded
import dev.jombi.copytotrans.getPapagoApiKey
import dev.jombi.copytotrans.getPapagoApiSecret
import dev.jombi.copytotrans.mapper
import dev.jombi.copytotrans.config.getPapagoApiKey
import dev.jombi.copytotrans.config.getPapagoApiSecret
import dev.jombi.copytotrans.config.mapper
import dev.jombi.copytotrans.translator.Translator
import java.net.HttpURLConnection
import java.net.URL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.jombi.copytotrans.translator.impl.newg

import dev.jombi.copytotrans.buildUrlEncoded
import dev.jombi.copytotrans.mapper
import dev.jombi.copytotrans.config.mapper
import dev.jombi.copytotrans.translator.Translator
import java.net.HttpURLConnection
import java.net.URL
Expand Down Expand Up @@ -71,6 +71,7 @@ class GoogleRPCTranslate : Translator {
val dataThing = mapper.readTree(resp)
val parsed = mapper.readTree(dataThing[0][2].asText())
val l = arrayListOf<String>()
println(dataThing)
for (node in parsed[1][0][0][5]) {
if (node.size() < 2) continue
l.add(node[0].asText())
Expand Down

0 comments on commit d14a47c

Please sign in to comment.