Skip to content

Commit

Permalink
feat: support to open host by enter
Browse files Browse the repository at this point in the history
  • Loading branch information
hstyi committed Feb 24, 2025
1 parent f3c5009 commit 562c1f9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/app/termora/MyTabbedPane.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class MyTabbedPane : FlatTabbedPane() {
.getData(DataProviders.TermoraFrame) as TermoraFrame

init {
isFocusable = false
initEvents()
}

Expand Down
25 changes: 21 additions & 4 deletions src/main/kotlin/app/termora/NewHostTree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ import java.awt.Dimension
import java.awt.datatransfer.DataFlavor
import java.awt.datatransfer.Transferable
import java.awt.datatransfer.UnsupportedFlavorException
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import java.awt.event.*
import java.io.*
import java.util.*
import java.util.function.Function
Expand Down Expand Up @@ -216,6 +213,26 @@ class NewHostTree : JXTree() {
}
})

addKeyListener(object : KeyAdapter() {
override fun keyPressed(e: KeyEvent) {
if (e.keyCode == KeyEvent.VK_ENTER && doubleClickConnection) {
val nodes = getSelectionHostTreeNodes(false)
if (nodes.size == 1 && nodes.first().host.protocol == Protocol.Folder) {
val path = TreePath(model.getPathToRoot(nodes.first()))
if (isExpanded(path)) {
collapsePath(path)
} else {
expandPath(path)
}
} else {
for (node in getSelectionHostTreeNodes(true)) {
openHostAction?.actionPerformed(OpenHostActionEvent(e.source, node.host, e))
}
}
}
}
})

// rename
getCellEditor().addCellEditorListener(object : CellEditorListener {
override fun editingStopped(e: ChangeEvent) {
Expand Down
11 changes: 2 additions & 9 deletions src/main/kotlin/app/termora/TerminalTabbed.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,14 @@ class TerminalTabbed(
tabs[oldIndex].onLostFocus()
}

tabbedPane.getComponentAt(newIndex).requestFocusInWindow()

if (newIndex >= 0 && tabs.size > newIndex) {
tabs[newIndex].onGrabFocus()
}

SwingUtilities.invokeLater { tabbedPane.getComponentAt(newIndex).requestFocusInWindow() }

}

// 选择变动
tabbedPane.addChangeListener {
if (tabbedPane.selectedIndex >= 0) {
val c = tabbedPane.getComponentAt(tabbedPane.selectedIndex)
c.requestFocusInWindow()
}
}

// 右键菜单
tabbedPane.addMouseListener(object : MouseAdapter() {
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/app/termora/WelcomePanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import com.formdev.flatlaf.extras.components.FlatTextField
import org.apache.commons.lang3.StringUtils
import org.jdesktop.swingx.action.ActionManager
import java.awt.BorderLayout
import java.awt.Component
import java.awt.Dimension
import java.awt.KeyboardFocusManager
import java.awt.event.ActionEvent
import java.awt.event.ComponentAdapter
import java.awt.event.ComponentEvent
Expand All @@ -32,6 +34,7 @@ class WelcomePanel(private val windowScope: WindowScope) : JPanel(BorderLayout()
private var fullContent = properties.getString("WelcomeFullContent", "false").toBoolean()
private val dataProviderSupport = DataProviderSupport()
private val hostTreeModel = hostTree.model as NewHostTreeModel
private var lastFocused: Component? = null
private val filterableHostTreeModel = FilterableHostTreeModel(hostTree) {
searchTextField.text.isBlank()
}
Expand Down Expand Up @@ -258,6 +261,14 @@ class WelcomePanel(private val windowScope: WindowScope) : JPanel(BorderLayout()
return false
}

override fun onLostFocus() {
lastFocused = KeyboardFocusManager.getCurrentKeyboardFocusManager().focusOwner
}

override fun onGrabFocus() {
SwingUtilities.invokeLater { lastFocused?.requestFocusInWindow() }
}

override fun dispose() {
properties.putString("WelcomeFullContent", fullContent.toString())
properties.putString("Welcome.HostTree.state", TreeUtils.saveExpansionState(hostTree))
Expand Down

0 comments on commit 562c1f9

Please sign in to comment.