feat: press and hold Shift to close Tab (#131)

This commit is contained in:
hstyi
2025-01-25 16:24:36 +08:00
committed by GitHub
parent fa79473ece
commit 0da3c95759

View File

@@ -32,6 +32,25 @@ class MyTabbedPane : FlatTabbedPane() {
addMouseMotionListener(dragMouseAdaptor) addMouseMotionListener(dragMouseAdaptor)
} }
override fun processMouseEvent(e: MouseEvent) {
// Shift + Click ===> close tab
if (e.id == MouseEvent.MOUSE_CLICKED && SwingUtilities.isLeftMouseButton(e) && isShiftPressedOnly(e.modifiersEx)) {
val index = indexAtLocation(e.x, e.y)
if (index >= 0) {
tabCloseCallback?.accept(this, index)
return
}
}
super.processMouseEvent(e)
}
private fun isShiftPressedOnly(modifiersEx: Int): Boolean {
return (modifiersEx and InputEvent.ALT_DOWN_MASK) == 0
&& (modifiersEx and InputEvent.ALT_GRAPH_DOWN_MASK) == 0
&& (modifiersEx and InputEvent.CTRL_DOWN_MASK) == 0
&& (modifiersEx and InputEvent.SHIFT_DOWN_MASK) != 0
}
override fun setSelectedIndex(index: Int) { override fun setSelectedIndex(index: Int) {
val oldIndex = selectedIndex val oldIndex = selectedIndex
super.setSelectedIndex(index) super.setSelectedIndex(index)