Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add file drag and drop to external app #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.abdownloadmanager.desktop.pages.home

import ir.amirab.downloader.monitor.IDownloadItemState
import java.awt.datatransfer.DataFlavor
import java.awt.datatransfer.Transferable
import java.awt.datatransfer.UnsupportedFlavorException
import java.io.File

val DownloadItemListDataFlavor = DataFlavor(
IDownloadItemState::class.java,
"Download Item"
)

class DownloadItemTransferable(
val items: List<IDownloadItemState>,
) : Transferable {
override fun getTransferDataFlavors(): Array<DataFlavor> {
return arrayOf(
DataFlavor.javaFileListFlavor,
DataFlavor.stringFlavor,
DownloadItemListDataFlavor,
)
}

override fun isDataFlavorSupported(flavor: DataFlavor?): Boolean {
return (flavor in arrayOf(
DataFlavor.javaFileListFlavor,
DataFlavor.stringFlavor,
DownloadItemListDataFlavor,
))
}

override fun getTransferData(flavor: DataFlavor?): Any {
return when (flavor) {
DataFlavor.javaFileListFlavor -> items.map {
File(it.folder, it.name)
}

DataFlavor.stringFlavor -> items.map {
it.downloadLink
}.joinToString("\n")

DownloadItemListDataFlavor -> items
else -> throw UnsupportedFlavorException(flavor)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ fun HomePage(component: HomeComponent) {
.fillMaxSize()
.dragAndDropTarget(
shouldStartDragAndDrop = {
it.awtTransferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor) ||
if (it.awtTransferable.isDataFlavorSupported(DownloadItemListDataFlavor)) {
// this item is ours we don't want to use our download item for import list usage
return@dragAndDropTarget false
} else it.awtTransferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor) ||
it.awtTransferable.isDataFlavorSupported(DataFlavor.stringFlavor)
},
target = remember {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@ import com.abdownloadmanager.desktop.ui.widget.menu.MenuDisabledItemBehavior
import com.abdownloadmanager.desktop.ui.widget.menu.ShowOptionsInDropDown
import ir.amirab.util.compose.action.MenuItem
import androidx.compose.foundation.*
import androidx.compose.foundation.draganddrop.dragAndDropSource
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draganddrop.DragAndDropTransferAction
import androidx.compose.ui.draganddrop.DragAndDropTransferData
import androidx.compose.ui.draganddrop.DragAndDropTransferable
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.key.*
import androidx.compose.ui.input.pointer.*
import androidx.compose.ui.unit.dp
import com.abdownloadmanager.desktop.pages.home.DownloadItemTransferable
import com.abdownloadmanager.utils.FileIconProvider
import com.abdownloadmanager.utils.category.CategoryManager
import com.abdownloadmanager.utils.category.rememberCategoryOf
Expand Down Expand Up @@ -82,8 +88,12 @@ fun DownloadList(
it in selectionList
}
}

}

val listToBeDragged by rememberUpdatedState(
downloadList.filter { it.id in selectionList }
)

val tableInteractionSource = remember { MutableInteractionSource() }

fun newSelection(ids: List<Long>, isSelected: Boolean) {
Expand All @@ -110,6 +120,31 @@ fun DownloadList(
key = { it.id },
list = downloadList,
modifier = modifier
.dragAndDropSource(
drawDragDecoration = {
},
block = {
detectDragGestures(
onDrag = { _ ->
val selectedDownloads = listToBeDragged
if (selectedDownloads.isEmpty()) {
return@detectDragGestures
}
startTransfer(
DragAndDropTransferData(
transferable = DragAndDropTransferable(
DownloadItemTransferable(selectedDownloads)
),
supportedActions = listOf(
// TODO it doesn't work!
DragAndDropTransferAction.Copy,
),
)
)
}
)
}
)
.onKeyEvent {
val ctrlPressed = it.isCtrlPressed
val shiftPressed = it.isShiftPressed
Expand Down Expand Up @@ -366,5 +401,4 @@ fun ShowDownloadOptions(
}
}

}

}