From 7c445bdadbd8520ca158c00c0ff487562c68866c Mon Sep 17 00:00:00 2001 From: hstyi Date: Thu, 9 Jan 2025 14:05:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=B7=A5=E5=85=B7=E6=A0=8F=E7=9A=84=E5=AD=98=E5=82=A8?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/termora/CustomizeToolBarDialog.kt | 30 ++++++++++-- src/main/kotlin/app/termora/TermoraToolBar.kt | 46 ++++++++++++------- 2 files changed, 56 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/app/termora/CustomizeToolBarDialog.kt b/src/main/kotlin/app/termora/CustomizeToolBarDialog.kt index e40159c..be691bb 100644 --- a/src/main/kotlin/app/termora/CustomizeToolBarDialog.kt +++ b/src/main/kotlin/app/termora/CustomizeToolBarDialog.kt @@ -30,6 +30,7 @@ class CustomizeToolBarDialog( private val leftBtn = JButton(Icons.left) private val rightBtn = JButton(Icons.right) + private val resetBtn = JButton(Icons.refresh) private val allToLeftBtn = JButton(Icons.applyNotConflictsRight) private val allToRightBtn = JButton(Icons.applyNotConflictsLeft) @@ -71,6 +72,8 @@ class CustomizeToolBarDialog( box.add(rightBtn) box.add(leftBtn) box.add(Box.createVerticalGlue()) + box.add(resetBtn) + box.add(Box.createVerticalGlue()) box.add(allToRightBtn) box.add(allToLeftBtn) box.add(Box.createVerticalStrut(leftList.fixedCellHeight)) @@ -140,6 +143,16 @@ class CustomizeToolBarDialog( } }) + resetBtn.addActionListener { + leftList.model.removeAllElements() + rightList.model.removeAllElements() + for (action in toolbar.getAllActions()) { + actionManager.getAction(action)?.let { + rightList.model.addElement(ActionHolder(action, it)) + } + } + } + // move first moveTopBtn.addActionListener { val indices = rightList.selectedIndices @@ -247,11 +260,14 @@ class CustomizeToolBarDialog( removeWindowListener(this) val allActions = toolbar.getAllActions().toMutableList() - val shownActions = toolbar.getShownActions() + val shownActions = toolbar.getShownActions().filter { it.visible } + .map { it.id } allActions.removeAll(shownActions) + for (action in allActions) { actionManager.getAction(action)?.let { leftList.model.addElement(ActionHolder(action, it)) } } + for (action in shownActions) { actionManager.getAction(action)?.let { rightList.model.addElement(ActionHolder(action, it)) } } @@ -333,11 +349,19 @@ class CustomizeToolBarDialog( override fun doOKAction() { isOk = true - val actions = mutableListOf() + + val rightActions = mutableSetOf() for (i in 0 until rightList.model.size()) { - actions.add(rightList.model.getElementAt(i).id) + rightActions.add(rightList.model.getElementAt(i).id) } + + val actions = mutableListOf() + for (action in toolbar.getAllActions()) { + actions.add(ToolBarAction(action, rightActions.contains(action))) + } + Database.instance.properties.putString("Termora.ToolBar.Actions", ohMyJson.encodeToString(actions)) + super.doOKAction() } diff --git a/src/main/kotlin/app/termora/TermoraToolBar.kt b/src/main/kotlin/app/termora/TermoraToolBar.kt index 06f6953..68ad5df 100644 --- a/src/main/kotlin/app/termora/TermoraToolBar.kt +++ b/src/main/kotlin/app/termora/TermoraToolBar.kt @@ -5,6 +5,7 @@ import app.termora.db.Database import com.formdev.flatlaf.extras.components.FlatTabbedPane import com.formdev.flatlaf.util.SystemInfo import com.jetbrains.WindowDecorations +import kotlinx.serialization.Serializable import org.apache.commons.lang3.StringUtils import org.jdesktop.swingx.action.ActionContainerFactory import org.jdesktop.swingx.action.ActionManager @@ -15,6 +16,13 @@ import java.awt.event.ComponentEvent import javax.swing.Box import javax.swing.JToolBar + +@Serializable +data class ToolBarAction( + val id: String, + val visible: Boolean, +) + class TermoraToolBar( private val titleBar: WindowDecorations.CustomTitleBar, private val tabbedPane: FlatTabbedPane @@ -22,15 +30,25 @@ class TermoraToolBar( private val properties by lazy { Database.instance.properties } private val toolbar by lazy { MyToolBar().apply { rebuild(this) } } - private val shownActions = mutableListOf() fun getJToolBar(): JToolBar { return toolbar } - fun getShownActions(): List { - return shownActions + fun getShownActions(): List { + val text = properties.getString( + "Termora.ToolBar.Actions", + StringUtils.EMPTY + ) + + if (text.isBlank()) { + return getAllActions().map { ToolBarAction(it, true) } + } + + return ohMyJson.runCatching { + ohMyJson.decodeFromString>(text) + }.getOrNull() ?: getAllActions().map { ToolBarAction(it, true) } } fun getAllActions(): List { @@ -53,7 +71,6 @@ class TermoraToolBar( val actionManager = ActionManager.getInstance() val actionContainerFactory = ActionContainerFactory(actionManager) - shownActions.clear() toolbar.removeAll() toolbar.add(actionContainerFactory.createButton(object : AnAction(StringUtils.EMPTY, Icons.add) { @@ -68,15 +85,6 @@ class TermoraToolBar( toolbar.add(Box.createHorizontalGlue()) - val actions = ohMyJson.runCatching { - ohMyJson.decodeFromString>( - properties.getString( - "Termora.ToolBar.Actions", - StringUtils.EMPTY - ) - ) - }.getOrNull() ?: getAllActions() - // update btn val updateBtn = actionContainerFactory.createButton(actionManager.getAction(Actions.APP_UPDATE)) @@ -84,10 +92,14 @@ class TermoraToolBar( updateBtn.addChangeListener { updateBtn.isVisible = updateBtn.isEnabled } toolbar.add(updateBtn) - for (action in actions) { - actionManager.getAction(action)?.let { - toolbar.add(actionContainerFactory.createButton(it)) - shownActions.add(action) + // 获取显示的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 { + toolbar.add(actionContainerFactory.createButton(it)) + } } }