From 950ff517bb36ec71b7a8acf6917a1093d95589f3 Mon Sep 17 00:00:00 2001 From: hstyi Date: Thu, 9 Jan 2025 14:45:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=B7=A5=E5=85=B7=E6=A0=8F=E6=8E=92=E5=BA=8F=E6=97=A0?= =?UTF-8?q?=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/termora/CustomizeToolBarDialog.kt | 37 ++++++----- src/main/kotlin/app/termora/TermoraToolBar.kt | 63 ++++++++++++------- 2 files changed, 60 insertions(+), 40 deletions(-) diff --git a/src/main/kotlin/app/termora/CustomizeToolBarDialog.kt b/src/main/kotlin/app/termora/CustomizeToolBarDialog.kt index 2d65df1..a2b7d35 100644 --- a/src/main/kotlin/app/termora/CustomizeToolBarDialog.kt +++ b/src/main/kotlin/app/termora/CustomizeToolBarDialog.kt @@ -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() + val actions = mutableListOf() 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() - 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)) diff --git a/src/main/kotlin/app/termora/TermoraToolBar.kt b/src/main/kotlin/app/termora/TermoraToolBar.kt index e388756..4df526f 100644 --- a/src/main/kotlin/app/termora/TermoraToolBar.kt +++ b/src/main/kotlin/app/termora/TermoraToolBar.kt @@ -35,33 +35,54 @@ class TermoraToolBar( return toolbar } + /** + * 获取到所有的 Action + */ + fun getAllActions(): List { + 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 { + + /** + * 获取到所有 Action,会根据用户个性化排序/显示 + */ + fun getActions(): List { 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>(text) - }.getOrNull() ?: getAllActions().map { ToolBarAction(it, true) } - } + }.getOrNull() ?: return actions).toMutableList() - fun getAllActions(): List { - 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() }