fix: 修复自定义工具栏排序无效的问题

This commit is contained in:
hstyi
2025-01-09 14:45:18 +08:00
committed by hstyi
parent 70008978d8
commit 950ff517bb
2 changed files with 60 additions and 40 deletions

View File

@@ -147,15 +147,15 @@ class CustomizeToolBarDialog(
leftList.model.removeAllElements() leftList.model.removeAllElements()
rightList.model.removeAllElements() rightList.model.removeAllElements()
for (action in toolbar.getAllActions()) { for (action in toolbar.getAllActions()) {
actionManager.getAction(action)?.let { actionManager.getAction(action.id)?.let {
rightList.model.addElement(ActionHolder(action, it)) rightList.model.addElement(ActionHolder(action.id, it))
} }
} }
} }
// move first // move first
moveTopBtn.addActionListener { moveTopBtn.addActionListener {
val indices = rightList.selectedIndices val indices = rightList.selectedIndices.sortedDescending()
rightList.clearSelection() rightList.clearSelection()
for (index in indices.indices) { for (index in indices.indices) {
val ele = rightList.model.getElementAt(indices[index]) val ele = rightList.model.getElementAt(indices[index])
@@ -167,7 +167,7 @@ class CustomizeToolBarDialog(
// move up // move up
upBtn.addActionListener { upBtn.addActionListener {
val indices = rightList.selectedIndices val indices = rightList.selectedIndices.sortedDescending()
rightList.clearSelection() rightList.clearSelection()
for (index in indices) { for (index in indices) {
val ele = rightList.model.getElementAt(index) val ele = rightList.model.getElementAt(index)
@@ -179,7 +179,7 @@ class CustomizeToolBarDialog(
// move down // move down
downBtn.addActionListener { downBtn.addActionListener {
val indices = rightList.selectedIndices val indices = rightList.selectedIndices.sortedDescending()
rightList.clearSelection() rightList.clearSelection()
for (index in indices) { for (index in indices) {
val ele = rightList.model.getElementAt(index) val ele = rightList.model.getElementAt(index)
@@ -191,7 +191,7 @@ class CustomizeToolBarDialog(
// move last // move last
moveBottomBtn.addActionListener { moveBottomBtn.addActionListener {
val indices = rightList.selectedIndices val indices = rightList.selectedIndices.sortedDescending()
val size = rightList.model.size val size = rightList.model.size
rightList.clearSelection() rightList.clearSelection()
for (index in indices.indices) { for (index in indices.indices) {
@@ -219,7 +219,7 @@ class CustomizeToolBarDialog(
} }
leftBtn.addActionListener { leftBtn.addActionListener {
val indices = rightList.selectedIndices val indices = rightList.selectedIndices.sortedDescending()
for (index in indices) { for (index in indices) {
val ele = rightList.model.getElementAt(index) val ele = rightList.model.getElementAt(index)
rightList.model.removeElementAt(index) rightList.model.removeElementAt(index)
@@ -233,7 +233,7 @@ class CustomizeToolBarDialog(
} }
rightBtn.addActionListener { rightBtn.addActionListener {
val indices = leftList.selectedIndices val indices = leftList.selectedIndices.sortedDescending()
val rightSelectedIndex = if (rightList.selectedIndices.isEmpty()) rightList.model.size else val rightSelectedIndex = if (rightList.selectedIndices.isEmpty()) rightList.model.size else
rightList.selectionModel.maxSelectionIndex + 1 rightList.selectionModel.maxSelectionIndex + 1
@@ -259,14 +259,14 @@ class CustomizeToolBarDialog(
override fun windowOpened(e: WindowEvent) { override fun windowOpened(e: WindowEvent) {
removeWindowListener(this) removeWindowListener(this)
val allActions = toolbar.getAllActions().toMutableList()
val shownActions = toolbar.getShownActions().associate { Pair(it.id, it.visible) }
for (action in allActions) { for (action in toolbar.getActions()) {
if (shownActions[action] == false) { if (action.visible) {
actionManager.getAction(action)?.let { leftList.model.addElement(ActionHolder(action, it)) } actionManager.getAction(action.id)
?.let { rightList.model.addElement(ActionHolder(action.id, it)) }
} else { } else {
actionManager.getAction(action)?.let { rightList.model.addElement(ActionHolder(action, it)) } actionManager.getAction(action.id)
?.let { leftList.model.addElement(ActionHolder(action.id, it)) }
} }
} }
@@ -349,14 +349,13 @@ class CustomizeToolBarDialog(
override fun doOKAction() { override fun doOKAction() {
isOk = true isOk = true
val rightActions = mutableSetOf<String>() val actions = mutableListOf<ToolBarAction>()
for (i in 0 until rightList.model.size()) { for (i in 0 until rightList.model.size()) {
rightActions.add(rightList.model.getElementAt(i).id) actions.add(ToolBarAction(rightList.model.getElementAt(i).id, true))
} }
val actions = mutableListOf<ToolBarAction>() for (i in 0 until leftList.model.size()) {
for (action in toolbar.getAllActions()) { actions.add(ToolBarAction(leftList.model.getElementAt(i).id, false))
actions.add(ToolBarAction(action, rightActions.contains(action)))
} }
Database.instance.properties.putString("Termora.ToolBar.Actions", ohMyJson.encodeToString(actions)) Database.instance.properties.putString("Termora.ToolBar.Actions", ohMyJson.encodeToString(actions))

View File

@@ -35,33 +35,54 @@ class TermoraToolBar(
return toolbar return toolbar
} }
/**
* 获取到所有的 Action
*/
fun getAllActions(): List<ToolBarAction> {
return listOf(
ToolBarAction(Actions.SFTP, true),
ToolBarAction(Actions.TERMINAL_LOGGER, true),
ToolBarAction(Actions.MACRO, true),
ToolBarAction(Actions.KEYWORD_HIGHLIGHT, true),
ToolBarAction(Actions.KEY_MANAGER, true),
ToolBarAction(Actions.MULTIPLE, true),
ToolBarAction(Actions.FIND_EVERYWHERE, true),
ToolBarAction(Actions.SETTING, true),
)
}
fun getShownActions(): List<ToolBarAction> {
/**
* 获取到所有 Action会根据用户个性化排序/显示
*/
fun getActions(): List<ToolBarAction> {
val text = properties.getString( val text = properties.getString(
"Termora.ToolBar.Actions", "Termora.ToolBar.Actions",
StringUtils.EMPTY StringUtils.EMPTY
) )
val actions = getAllActions()
if (text.isBlank()) { if (text.isBlank()) {
return getAllActions().map { ToolBarAction(it, true) } return actions
} }
return ohMyJson.runCatching { // 存储的 action
val storageActions = (ohMyJson.runCatching {
ohMyJson.decodeFromString<List<ToolBarAction>>(text) ohMyJson.decodeFromString<List<ToolBarAction>>(text)
}.getOrNull() ?: getAllActions().map { ToolBarAction(it, true) } }.getOrNull() ?: return actions).toMutableList()
}
fun getAllActions(): List<String> { for (action in actions) {
return listOf( // 如果存储的 action 不包含这个,那么这个可能是新增的,新增的默认显示出来
Actions.SFTP, if (storageActions.none { it.id == action.id }) {
Actions.TERMINAL_LOGGER, storageActions.addFirst(ToolBarAction(action.id, true))
Actions.MACRO, }
Actions.KEYWORD_HIGHLIGHT, }
Actions.KEY_MANAGER,
Actions.MULTIPLE, // 如果存储的 Action 在所有 Action 里没有,那么移除
Actions.FIND_EVERYWHERE, storageActions.removeIf { e -> actions.none { e.id == it.id } }
Actions.SETTING,
) return storageActions
} }
fun rebuild() { fun rebuild() {
@@ -93,17 +114,17 @@ class TermoraToolBar(
updateBtn.addChangeListener { updateBtn.isVisible = updateBtn.isEnabled } updateBtn.addChangeListener { updateBtn.isVisible = updateBtn.isEnabled }
toolbar.add(updateBtn) toolbar.add(updateBtn)
// 获取显示的Action如果不是 false 那么就是显示出来 // 获取显示的Action如果不是 false 那么就是显示出来
val actions = getShownActions().associate { Pair(it.id, it.visible) } for (action in getActions()) {
for (action in getAllActions()) { if (action.visible) {
// actions[action] 有可能是 null那么极有可能表示这个 Action 是新增的 actionManager.getAction(action.id)?.let {
if (actions[action] != false) {
actionManager.getAction(action)?.let {
toolbar.add(actionContainerFactory.createButton(it)) toolbar.add(actionContainerFactory.createButton(it))
} }
} }
} }
if (toolbar is MyToolBar) { if (toolbar is MyToolBar) {
toolbar.adjust() toolbar.adjust()
} }