Skip to content

Commit a598e13

Browse files
committed
fix: ensure consistent font rendering across components for better display on macOS
1 parent 707da37 commit a598e13

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

src/main/java/com/laker/postman/common/component/tab/ClosableTabComponent.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ public ClosableTabComponent(String title, RequestItemProtocolEnum protocol) {
5151
setToolTipText(title); // 设置完整标题为tooltip
5252
this.tabbedPane = SingletonFactory.getInstance(RequestEditPanel.class).getTabbedPane();
5353

54+
// 先创建正确的字体,确保字符宽度计算和显示一致
55+
Font tabFont = FontsUtil.getDefaultFont(Font.PLAIN, 12);
5456
// 动态计算宽度,最大不超过MAX_TAB_WIDTH
55-
FontMetrics fm = getFontMetrics(getFont());
57+
FontMetrics fm = getFontMetrics(tabFont);
5658
// 计算关闭按钮占用的空间:按钮直径 + 间距 + 右边距 + 左侧图标空间
5759
int closeButtonSpace = CLOSE_DIAMETER + 4 + CLOSE_MARGIN; // 8px为文本与按钮之间的间距
5860
int iconSpace = 20; // 为协议图标预留空间
@@ -81,7 +83,7 @@ public boolean contains(int x, int y) { // 重写contains方法,避免遮挡
8183
return false;
8284
}
8385
};
84-
label.setFont(FontsUtil.getDefaultFont(Font.PLAIN, 12));
86+
label.setFont(tabFont);
8587
if (protocol != null) {
8688
label.setIcon(protocol.getIcon());
8789
} else {

src/main/java/com/laker/postman/common/component/tree/RequestTreeCellRenderer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public Component getTreeCellRendererComponent(JTree tree, Object value, boolean
3333
Object groupData = obj[1];
3434
String groupName = groupData instanceof RequestGroup ? ((RequestGroup) groupData).getName() : String.valueOf(groupData);
3535
setText(groupName);
36+
// 显式设置字体,确保在 DMG 中正确显示
37+
setFont(tree.getFont());
3638
} else if (RequestCollectionsLeftPanel.REQUEST.equals(obj[0])) {
3739
HttpRequestItem item = (HttpRequestItem) obj[1];
3840
applyRequestRendering(item);

src/main/java/com/laker/postman/panel/history/HistoryPanel.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,27 @@ protected void initUI() {
130130
* 优化的列表单元格渲染器 - 极简版,最大化性能
131131
*/
132132
private static class OptimizedHistoryListCellRenderer extends DefaultListCellRenderer {
133-
private final Font boldFont;
134-
private final Font plainFont;
133+
private Font boldFont;
134+
private Font plainFont;
135135
private final Color selectedBackground = new Color(180, 215, 255);
136136

137-
public OptimizedHistoryListCellRenderer() {
138-
Font baseFont = FontsUtil.getDefaultFont(Font.PLAIN, 12);
139-
boldFont = baseFont.deriveFont(Font.BOLD);
140-
plainFont = baseFont.deriveFont(Font.PLAIN);
141-
}
142-
143137
@Override
144138
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
145139
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
146140

141+
// 从 JList 获取字体,确保与组件设置的字体一致
142+
if (boldFont == null || plainFont == null) {
143+
Font listFont = list.getFont();
144+
if (listFont != null) {
145+
boldFont = listFont.deriveFont(Font.BOLD);
146+
plainFont = listFont.deriveFont(Font.PLAIN);
147+
} else {
148+
Font baseFont = FontsUtil.getDefaultFont(Font.PLAIN, 12);
149+
boldFont = baseFont.deriveFont(Font.BOLD);
150+
plainFont = baseFont.deriveFont(Font.PLAIN);
151+
}
152+
}
153+
147154
if (value instanceof String dateStr) {
148155
label.setText(dateStr);
149156
label.setFont(boldFont);

0 commit comments

Comments
 (0)