feat: 优化自定义工具栏的存储结构

This commit is contained in:
hstyi
2025-01-09 14:05:10 +08:00
committed by hstyi
parent f24151f6d8
commit 7c445bdadb
2 changed files with 56 additions and 20 deletions

View File

@@ -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<String>()
val rightActions = mutableSetOf<String>()
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<ToolBarAction>()
for (action in toolbar.getAllActions()) {
actions.add(ToolBarAction(action, rightActions.contains(action)))
}
Database.instance.properties.putString("Termora.ToolBar.Actions", ohMyJson.encodeToString(actions))
super.doOKAction()
}

View File

@@ -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<String>()
fun getJToolBar(): JToolBar {
return toolbar
}
fun getShownActions(): List<String> {
return shownActions
fun getShownActions(): List<ToolBarAction> {
val text = properties.getString(
"Termora.ToolBar.Actions",
StringUtils.EMPTY
)
if (text.isBlank()) {
return getAllActions().map { ToolBarAction(it, true) }
}
return ohMyJson.runCatching {
ohMyJson.decodeFromString<List<ToolBarAction>>(text)
}.getOrNull() ?: getAllActions().map { ToolBarAction(it, true) }
}
fun getAllActions(): List<String> {
@@ -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<List<String>>(
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))
}
}
}