perf: lazy loading OptionsPane and Fonts (#619)

This commit is contained in:
kanoshiou
2025-06-07 12:07:55 +08:00
committed by GitHub
parent f8d363836e
commit 1ae64fe0db
4 changed files with 42 additions and 27 deletions

View File

@@ -25,6 +25,7 @@ class HostDialog(owner: Window, host: Host? = null) : DialogWrapper(owner) {
isModal = true isModal = true
title = I18n.getString("termora.new-host.title") title = I18n.getString("termora.new-host.title")
setLocationRelativeTo(null) setLocationRelativeTo(null)
pane.setSelectedIndex(0)
init() init()
} }

View File

@@ -17,6 +17,7 @@ open class OptionsPane : JPanel(BorderLayout()) {
} }
private val cardLayout = CardLayout() private val cardLayout = CardLayout()
private val contentPanel = JPanel(cardLayout) private val contentPanel = JPanel(cardLayout)
private val loadedComponents = mutableMapOf<String, JComponent>()
init { init {
initView() initView()
@@ -103,16 +104,15 @@ open class OptionsPane : JPanel(BorderLayout()) {
throw UnsupportedOperationException("Title already exists") throw UnsupportedOperationException("Title already exists")
} }
} }
contentPanel.add(option.getJComponent(), option.getTitle())
tabListModel.addElement(option) tabListModel.addElement(option)
if (tabList.selectedIndex < 0) {
tabList.selectedIndex = 0
}
} }
fun removeOption(option: Option) { fun removeOption(option: Option) {
contentPanel.remove(option.getJComponent()) val title = option.getTitle()
loadedComponents[title]?.let {
contentPanel.remove(it)
loadedComponents.remove(title)
}
tabListModel.removeElement(option) tabListModel.removeElement(option)
} }
@@ -123,7 +123,17 @@ open class OptionsPane : JPanel(BorderLayout()) {
private fun initEvents() { private fun initEvents() {
tabList.addListSelectionListener { tabList.addListSelectionListener {
if (tabList.selectedIndex >= 0) { if (tabList.selectedIndex >= 0) {
cardLayout.show(contentPanel, tabListModel.get(tabList.selectedIndex).getTitle()) val option = tabListModel.get(tabList.selectedIndex)
val title = option.getTitle()
if (!loadedComponents.containsKey(title)) {
val component = option.getJComponent()
loadedComponents[title] = component
contentPanel.add(component, title)
SwingUtilities.updateComponentTreeUI(component)
}
cardLayout.show(contentPanel, title)
} }
} }
} }

View File

@@ -3,8 +3,6 @@ package app.termora
import java.awt.BorderLayout import java.awt.BorderLayout
import java.awt.Dimension import java.awt.Dimension
import java.awt.Window import java.awt.Window
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.BorderFactory import javax.swing.BorderFactory
import javax.swing.JComponent import javax.swing.JComponent
import javax.swing.JPanel import javax.swing.JPanel
@@ -20,8 +18,10 @@ class SettingsDialog(owner: Window) : DialogWrapper(owner) {
title = I18n.getString("termora.setting") title = I18n.getString("termora.setting")
setLocationRelativeTo(null) setLocationRelativeTo(null)
init() val index = properties.getString("Settings-SelectedOption")?.toIntOrNull() ?: 0
optionsPane.setSelectedIndex(index)
init()
initEvents() initEvents()
} }
@@ -31,14 +31,6 @@ class SettingsDialog(owner: Window) : DialogWrapper(owner) {
properties.putString("Settings-SelectedOption", optionsPane.getSelectedIndex().toString()) properties.putString("Settings-SelectedOption", optionsPane.getSelectedIndex().toString())
} }
}) })
addWindowListener(object : WindowAdapter() {
override fun windowActivated(e: WindowEvent) {
removeWindowListener(this)
val index = properties.getString("Settings-SelectedOption")?.toIntOrNull() ?: return
optionsPane.setSelectedIndex(index)
}
})
} }
override fun createCenterPanel(): JComponent { override fun createCenterPanel(): JComponent {

View File

@@ -604,16 +604,28 @@ class SettingsOptionsPane : OptionsPane() {
shellComboBox.selectedItem = terminalSetting.localShell shellComboBox.selectedItem = terminalSetting.localShell
val fonts = linkedSetOf("JetBrains Mono", "Source Code Pro", "Monospaced") fontComboBox.addItem(terminalSetting.font)
FontUtils.getAllFonts().forEach { var fontsLoaded = false
if (!fonts.contains(it.family)) {
fonts.addLast(it.family) fontComboBox.addPopupMenuListener(object : PopupMenuListener {
override fun popupMenuWillBecomeVisible(e: PopupMenuEvent) {
if (!fontsLoaded) {
val selectedItem = fontComboBox.selectedItem
fontComboBox.removeAllItems();
fontComboBox.addItem("JetBrains Mono")
fontComboBox.addItem("Source Code Pro")
fontComboBox.addItem("Monospaced")
FontUtils.getAvailableFontFamilyNames().forEach {
fontComboBox.addItem(it)
}
fontComboBox.selectedItem = selectedItem
fontsLoaded = true
} }
} }
for (font in fonts) { override fun popupMenuWillBecomeInvisible(e: PopupMenuEvent) {}
fontComboBox.addItem(font) override fun popupMenuCanceled(e: PopupMenuEvent) {}
} })
fontComboBox.selectedItem = terminalSetting.font fontComboBox.selectedItem = terminalSetting.font
debugComboBox.selectedItem = terminalSetting.debug debugComboBox.selectedItem = terminalSetting.debug