Skip to content

Commit

Permalink
整理代码,修改部分错误,修改部分待实现功能
Browse files Browse the repository at this point in the history
  • Loading branch information
ZIDOUZI committed Feb 28, 2022
1 parent cd26658 commit 295d125
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 47 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 项目排除路径
/.gradle/
/build/
2 changes: 1 addition & 1 deletion .idea/compiler.xml

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

3 changes: 2 additions & 1 deletion .idea/gradle.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.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

14 changes: 11 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("SpellCheckingInspection")

import org.jetbrains.compose.compose
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
Expand All @@ -7,8 +9,9 @@ plugins {
id("org.jetbrains.compose") version "1.0.0"
}

group = "me.dou"
group = "zdz.dou"
version = "1.0"
val customSourceSet = sourceSets.create("customSourceSet")

repositories {
google()
Expand All @@ -26,11 +29,16 @@ tasks.withType<KotlinCompile> {

compose.desktop {
application {
mainClass = "MainKt"
mainClass = "zdz.groovyconvertkts.ui.main.MainWindowKt"
nativeDistributions {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
targetFormats(TargetFormat.Msi, TargetFormat.Exe)
packageName = "GroovyConvertKts"
packageVersion = "1.0.0"
appResourcesRootDir.set(project.file("\\src\\main\\resources\\"))
windows {
iconFile.set(project.file("\\src\\main\\resources\\kotlin.ico"))
}
from(customSourceSet)
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/main/kotlin/zdz/groovyconvertkts/ExtendFileDialog.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package zdz.groovyconvertkts

import androidx.compose.runtime.Composable
import androidx.compose.ui.window.AwtWindow
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.swing.Swing
import java.awt.FileDialog
import java.io.File
import javax.imageio.ImageIO
import javax.swing.JFileChooser
import javax.swing.JFrame
import javax.swing.LookAndFeel
import javax.swing.UIManager
import javax.swing.filechooser.FileFilter
import javax.swing.filechooser.FileNameExtensionFilter
import javax.swing.filechooser.FileSystemView.getFileSystemView
import androidx.compose.ui.window.AwtWindow
import java.awt.FileDialog

/**
* 根据JFileChooser改造而来的compose元件
Expand All @@ -35,6 +36,7 @@ import java.awt.FileDialog
@Composable
fun ExtendFileDialog(
title: String = "untitled",
iconPath: String? = null,//TODO: 实现图标修改
dir: String? = null,
mode: Mode = Mode.LOAD,
chooseMode: ChooseMode = ChooseMode.FILE,
Expand All @@ -51,11 +53,14 @@ fun ExtendFileDialog(
lookAndFeel?.let { UIManager.setLookAndFeel(it) }
?: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())

val f = JFrame(title)
f.iconImage = ImageIO.read(File(""))
val j = JFileChooser(dir ?: getFileSystemView().defaultDirectory.path)
j.dialogTitle = title
j.isMultiSelectionEnabled = chooseMode.value % 2 != 0
j.fileSelectionMode = chooseMode.value / 2
j.dragEnabled = enableDrag

if (chooseMode.value !in 2..3) {
j.fileFilter = fileFilter
}//TODO: 检测是否有异常
Expand Down
77 changes: 43 additions & 34 deletions src/main/kotlin/zdz/groovyconvertkts/core/Convert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,28 @@ import java.time.format.DateTimeFormatter
import java.util.*
import kotlin.text.RegexOption.DOT_MATCHES_ALL

fun processClipboard() {
fun getClipboardContents(): String {

print("[${currentTimeFormatted()}] - Trying to open clipboard.. ")
var clipboard = Toolkit.getDefaultToolkit().systemClipboard

val clipboard = Toolkit.getDefaultToolkit().systemClipboard
val contents = clipboard.getContents(null)
val hasTransferableText = contents != null && contents.isDataFlavorSupported(DataFlavor.stringFlavor)

val result =
if (hasTransferableText) contents?.getTransferData(DataFlavor.stringFlavor) as? String ?: "" else ""
println("Success!")
///////读写分界线
val selection = StringSelection(convert(result))
clipboard = Toolkit.getDefaultToolkit().systemClipboard
print("[${currentTimeFormatted()}] --- Saving to clipboard.. ")
clipboard.setContents(selection, selection)
}

fun processFile(file: File) {
print("[${currentTimeFormatted()}] - Trying to open file.. ")
if (!file.exists()) {
println("Didn't find a file in the path you specified. Exiting...")
}
println("Success!")
////////读写分界线
val fileIsAlreadyKts = file.path.takeLast(4) == ".kts"

if (fileIsAlreadyKts) {
println(
"\n### ### ### Warning! The script will overrite ${file.path}, since it ends with \".kts\"".red() +
"\n### ### ### Gradle might get crazy and all red, so you might want to \"gradle build\"\n".red()
)
}
return result
}

val newFilePath = if (fileIsAlreadyKts) file.path else "${file.path}.kts"
fun writeToClipboard(text: String) {
val selection = StringSelection(text)
val clipboard = Toolkit.getDefaultToolkit().getSystemClipboard()

println("[${currentTimeFormatted()}] --- Saving to: \"$newFilePath\".. ")
print("[${currentTimeFormatted()}] --- Saving to clipboard.. ")

val newFile = File(newFilePath)
newFile.createNewFile()
newFile.writeText(convert(file.readText()))
clipboard.setContents(selection, selection)
}

fun String.convertNestedTypes(buildTypes: String, named: String): String {
Expand Down Expand Up @@ -119,13 +103,38 @@ fun String.getExpressionBlock(

fun currentTimeFormatted(): String = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"))

fun processFile(file: File) {
print("[${currentTimeFormatted()}] - Trying to open file.. ")
if (!file.exists()) {
println("Didn't find a file in the path you specified. Exiting...")
}
println("Success!")
////////读写分界线
val fileIsAlreadyKts = file.path.takeLast(4) == ".kts"

if (fileIsAlreadyKts) {
println(
"\n### ### ### Warning! The script will overrite ${file.path}, since it ends with \".kts\"".red() +
"\n### ### ### Gradle might get crazy and all red, so you might want to \"gradle build\"\n".red()
)
}

val newFilePath = if (fileIsAlreadyKts) file.path else "${file.path}.kts"

println("[${currentTimeFormatted()}] --- Saving to: \"$newFilePath\".. ")

val newFile = File(newFilePath)
newFile.createNewFile()
newFile.writeText(convert(file.readText()))
}

// from https://github.com/importre/crayon
fun String.bold() = "\u001b[1m${this}\u001b[0m"
fun String.cyan() = "\u001b[36m${this}\u001b[0m"
fun String.green() = "\u001b[32m${this}\u001b[0m"
fun String.magenta() = "\u001b[35m${this}\u001b[0m"
fun String.red() = "\u001b[31m${this}\u001b[0m"
//fun String.bold() = "\u001b[1m${this}\u001b[0m"
//fun String.cyan() = "\u001b[36m${this}\u001b[0m"
//fun String.green() = "\u001b[32m${this}\u001b[0m"
//fun String.magenta() = "\u001b[35m${this}\u001b[0m"
//fun String.yellow() = "\u001b[33m${this}\u001b[0m"
fun String.yellow() = "\u001b[33m${this}\u001b[0m"

fun convert(text: String): String {

Expand Down Expand Up @@ -607,4 +616,4 @@ fun convert(text: String): String {
}

return convertedText
}
}
7 changes: 5 additions & 2 deletions src/main/kotlin/zdz/groovyconvertkts/ui/main/MainScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import zdz.groovyconvertkts.ExtendFileDialog
import zdz.groovyconvertkts.Title
import zdz.groovyconvertkts.core.convert
import zdz.groovyconvertkts.core.processFile
import zdz.groovyconvertkts.core.writeToClipboard
import java.io.File
import javax.swing.filechooser.FileNameExtensionFilter

Expand Down Expand Up @@ -66,17 +67,19 @@ fun MainScreen(ws: MainWindowState) {
}


Button(onClick = { convert(value) }) {
Button(onClick = { writeToClipboard(convert(value)) }) {
Text("转换输入框代码")
}
TextField(value = value, onValueChange = { value = it })

Text(text = "tip: 选择文件夹时请选择src目录哦", color = Color.Gray, fontSize = 12.sp)
Text(text = "tip1: 选择文件夹时请选择src目录哦", color = Color.Gray, fontSize = 12.sp)
Text(text = "tip2: 转换输入后会直接输出到剪贴板", color = Color.Gray, fontSize = 12.sp)
}

if (ws.isAwaiting) {
ExtendFileDialog(
title = if (ws.selectFolder) "选择文件夹" else "选择文件",
iconPath = "Kotlin.ico",
dir = ws.path,
dispose = { ws.isAwaiting = false },
chooseMode = if (ws.selectFolder) ChooseMode.FOLDER else ChooseMode.FILES,
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/zdz/groovyconvertkts/ui/main/MainWindow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ fun main() = application {
property.setProperty("notification", ws.turnoffNotification.toString())

property.store(out, "change the property")
print("保存配置成功")
print("\n保存配置成功")
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
exitApplication()
},
title = "Gradle 转换 Kts",
icon = painterResource("kotlin.ico")
icon = painterResource("Kotlin.ico")
) {
ws.rootComposeWindow = window
App(ws)
Expand Down

0 comments on commit 295d125

Please sign in to comment.