Skip to content

Commit 9ab8ea3

Browse files
committed
refactor(ui): improve session list rendering and interaction in ViewHistoryActionui): improve session history popup layout and UX
- Replace ColoredListCellRenderer with custom ListCellRenderer for better control - Add scroll pane with fixed dimensions and proper sizing - Implement text truncation with tooltips for long session names - Enhance delete button with hover effects and proper event handling - Set fixed cell heights and improved spacing for consistent appearance
1 parent 2f01323 commit 9ab8ea3

File tree

1 file changed

+22
-37
lines changed

1 file changed

+22
-37
lines changed

core/src/main/kotlin/cc/unitmesh/devti/gui/toolbar/ViewHistoryAction.kt

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@ import com.intellij.ui.components.JBList
2020
import com.intellij.ui.components.JBScrollPane
2121
import com.intellij.util.ui.JBUI
2222
import java.awt.*
23-
import java.awt.event.ComponentAdapter
24-
import java.awt.event.ComponentEvent
2523
import java.awt.event.MouseAdapter
2624
import java.awt.event.MouseEvent
2725
import javax.swing.*
28-
import javax.swing.border.EmptyBorder
2926

3027
class ViewHistoryAction : AnAction(
3128
AutoDevBundle.message("action.view.history.text"),
@@ -75,8 +72,8 @@ class ViewHistoryAction : AnAction(
7572
hasFocus: Boolean
7673
): Component {
7774
// 创建主面板,使用 BorderLayout
78-
val panel = JPanel(BorderLayout())
79-
panel.border = JBUI.Borders.empty(8, 12)
75+
val panel = JPanel(BorderLayout(10, 0))
76+
panel.border = JBUI.Borders.empty(4, 8)
8077

8178
// 设置背景颜色
8279
if (selected) {
@@ -88,46 +85,37 @@ class ViewHistoryAction : AnAction(
8885
}
8986
panel.isOpaque = true
9087

91-
// 左侧内容面板 - 包含会话名称和时间
92-
val contentPanel = JPanel()
93-
contentPanel.layout = BoxLayout(contentPanel, BoxLayout.Y_AXIS)
94-
contentPanel.isOpaque = false
95-
96-
// 会话名称标签 - 处理过长文本
9788
val sessionName = value.session.name
98-
val displayName = if (sessionName.length > 40) {
99-
sessionName.substring(0, 37) + "..."
89+
val maxLength = 30 // 根据UI宽度调整最大长度
90+
val displayName = if (sessionName.length > maxLength) {
91+
sessionName.substring(0, maxLength - 3) + "..."
10092
} else {
10193
sessionName
10294
}
10395

96+
val contentPanel = JPanel(FlowLayout(FlowLayout.LEFT, 0, 0))
97+
contentPanel.isOpaque = false
98+
99+
// 会话名称
104100
val titleLabel = JLabel(displayName)
105-
titleLabel.font = titleLabel.font.deriveFont(Font.BOLD, 13f)
101+
titleLabel.font = titleLabel.font.deriveFont(Font.BOLD, 12f)
106102
titleLabel.toolTipText = sessionName // 完整名称作为工具提示
107-
if (selected) {
108-
titleLabel.foreground = list.selectionForeground
109-
} else {
110-
titleLabel.foreground = list.foreground
111-
}
112-
103+
113104
// 时间标签
114-
val timeLabel = JLabel(value.relativeTime)
105+
val timeLabel = JLabel(" - ${value.relativeTime}")
115106
timeLabel.font = timeLabel.font.deriveFont(11f)
107+
116108
if (selected) {
109+
titleLabel.foreground = list.selectionForeground
117110
timeLabel.foreground = list.selectionForeground.darker()
118111
} else {
112+
titleLabel.foreground = list.foreground
119113
timeLabel.foreground = list.foreground.darker()
120114
}
121115

122-
// 设置标签对齐
123-
titleLabel.alignmentX = Component.LEFT_ALIGNMENT
124-
timeLabel.alignmentX = Component.LEFT_ALIGNMENT
125-
126116
contentPanel.add(titleLabel)
127-
contentPanel.add(Box.createVerticalStrut(2))
128117
contentPanel.add(timeLabel)
129118

130-
// 右侧删除按钮
131119
val deleteButton = JLabel(AllIcons.Actions.Close)
132120
deleteButton.cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)
133121
deleteButton.border = JBUI.Borders.emptyLeft(8)
@@ -146,13 +134,10 @@ class ViewHistoryAction : AnAction(
146134
}
147135
})
148136

149-
// 组装面板
150137
panel.add(contentPanel, BorderLayout.CENTER)
151138
panel.add(deleteButton, BorderLayout.EAST)
152139

153-
// 设置固定高度
154-
panel.preferredSize = Dimension(panel.preferredSize.width, 50)
155-
140+
panel.preferredSize = Dimension(panel.preferredSize.width, 35)
156141
return panel
157142
}
158143
}
@@ -170,8 +155,8 @@ class ViewHistoryAction : AnAction(
170155
val listItems = sessions.map { SessionListItem(it, formatRelativeTime(it.createdAt)) }
171156
val jbList = JBList(listItems)
172157
jbList.selectionMode = ListSelectionModel.SINGLE_SELECTION
173-
jbList.fixedCellHeight = 50 // 固定单元格高度
174-
158+
jbList.fixedCellHeight = 35
159+
175160
val onDeleteSession = { session: ChatSessionHistory ->
176161
val result = Messages.showYesNoDialog(
177162
project,
@@ -193,9 +178,9 @@ class ViewHistoryAction : AnAction(
193178
scrollPane.border = null
194179

195180
// 设置 Popup 的固定宽度和自适应高度
196-
val popupWidth = 450
181+
val popupWidth = 400
197182
val maxPopupHeight = 400
198-
val itemHeight = 50
183+
val itemHeight = 35 // 与fixedCellHeight一致
199184
val calculatedHeight = (sessions.size * itemHeight + 20).coerceAtMost(maxPopupHeight)
200185

201186
scrollPane.preferredSize = Dimension(popupWidth, calculatedHeight)
@@ -224,8 +209,8 @@ class ViewHistoryAction : AnAction(
224209

225210
val popup: JBPopup = popupBuilder.createPopup()
226211

227-
// 设置 Popup 的最小和最大尺寸
228-
popup.setMinimumSize(Dimension(popupWidth, 150))
212+
// 设置 Popup 的最小尺寸
213+
popup.setMinimumSize(Dimension(300, 150))
229214

230215
jbList.addListSelectionListener {
231216
if (!it.valueIsAdjusting && jbList.selectedIndex != -1) {

0 commit comments

Comments
 (0)