Compse for desktop拖拽文件夹获取文件夹路径

将目录拖拽进去,复制全路径,感觉挺方便的。
经常去顶部复制时路径只会复制父级目录,有时频繁的操作会很烦。使用compse配合awt写的一个小工具

import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.LastBaseline
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState
import java.awt.Toolkit

import java.awt.datatransfer.DataFlavor
import java.awt.datatransfer.StringSelection
import java.awt.dnd.DnDConstants
import java.awt.dnd.DropTarget
import java.awt.dnd.DropTargetDropEvent
import java.io.File


fun main() = application {

    val name = remember { mutableStateOf(System.getProperty("user.home")) }
    Window(
        onCloseRequest = ::exitApplication,
        title = "LogParse",
        state = rememberWindowState(width = 350.dp, height = 300.dp),
        icon = painterResource("app_icon.svg")
    ) {
        MaterialTheme {
            Column() {
                Row(
                    modifier = Modifier.fillMaxWidth()
                        .padding(8.dp),
                ) {
                    TextField(
                        value = name.value,
                        placeholder = {
                            Text("Base directory")
                        },
                        modifier = Modifier.alignBy(LastBaseline)
                            .weight(1.0f),
                        onValueChange = {
                            name.value = it
                        },
                    )
                }

                Row(
                    modifier = Modifier.fillMaxWidth()
                        .padding(8.dp),
                ) {
                    Button(
                        onClick = {
                            val selection = StringSelection(name.value)
                            val clipboard = Toolkit.getDefaultToolkit().systemClipboard
                            clipboard.setContents(selection, null)
                            println(name.value)
                        },
                        modifier = Modifier.alignByBaseline(),
                    ) {
                        Text("Copy")
                    }
                }
            }
            val target = object : DropTarget() {
                @Synchronized
                override fun drop(evt: DropTargetDropEvent) {
                    try {
                        evt.acceptDrop(DnDConstants.ACTION_REFERENCE)
                        val droppedFiles = evt
                            .transferable.getTransferData(
                                DataFlavor.javaFileListFlavor
                            ) as List<*>
                        droppedFiles.first()?.let {
                            name.value = TextFieldValue((it as File).absolutePath).text
                        }
                    } catch (ex: Exception) {
                        ex.printStackTrace()
                    }
                }
            }
            window.contentPane.dropTarget = target
        }
    }
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值