Skip to content

Commit

Permalink
feat: optimize the copy operation experience in settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
huoguangjin committed Jun 26, 2024
1 parent 2cfde1d commit 53b3256
Showing 1 changed file with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.github.huoguangjin.multihighlight.config.NamedTextAttr
import com.intellij.icons.AllIcons
import com.intellij.idea.ActionsBundle
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.IdeActions
import com.intellij.openapi.options.ConfigurableUi
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.ui.DialogPanel
Expand All @@ -23,7 +25,7 @@ class MultiHighlightConfigurableUi : ConfigurableUi<MultiHighlightConfig>, Dispo
private val namedTextAttrTable = TableView(model).apply {
isStriped = true
setShowColumns(false)
setSelectionMode(ListSelectionModel.SINGLE_SELECTION)
setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION)
}

private val chooserPanel: ChooserPanel = ColorChooserPanel().apply {
Expand All @@ -33,17 +35,22 @@ class MultiHighlightConfigurableUi : ConfigurableUi<MultiHighlightConfig>, Dispo
private val previewPanel: PreviewPanel = ColorPreviewPanel()

private val rootPanel: DialogPanel = panel {
val copyAction = DumbAwareAction.create(
ActionsBundle.message("action.EditorCopy.text"),
AllIcons.Actions.Copy
) {
doCopy()
}

val tableWithToolbar: JPanel = ToolbarDecorator.createDecorator(namedTextAttrTable)
.setAddAction { doAdd() }
.setEditAction { doEdit() }
.addExtraAction(DumbAwareAction.create(
ActionsBundle.message("action.EditorCopy.text"),
AllIcons.Actions.Copy
) {
doCopy()
})
.addExtraAction(copyAction)
.createPanel()

val ideCopyAction = ActionManager.getInstance().getAction(IdeActions.ACTION_COPY)
copyAction.registerCustomShortcutSet(ideCopyAction.shortcutSet, tableWithToolbar)

val chooserAndPreviewPanel: JPanel = JBSplitter(true, 0.3f).apply {
firstComponent = chooserPanel.panel
secondComponent = previewPanel.panel
Expand Down Expand Up @@ -139,6 +146,18 @@ class MultiHighlightConfigurableUi : ConfigurableUi<MultiHighlightConfig>, Dispo

private fun doCopy() {
val selected = namedTextAttrTable.selectedObject ?: return

val selectRows = namedTextAttrTable.selectedRows
if (selectRows.size > 1) {
selectRows.forEach {
val row = namedTextAttrTable.getRow(it)
model.insertRow(it + selectRows.size, NamedTextAttr("${row.name} copy", row))
}
val rowIndex = selectRows.last() + 1
namedTextAttrTable.setRowSelectionInterval(rowIndex, rowIndex + selectRows.size - 1)
return
}

val name = askForColorName("${selected.name} copy") ?: return

val newRow = namedTextAttrTable.selectedRow + 1
Expand Down

0 comments on commit 53b3256

Please sign in to comment.