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

View File

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