Skip to content

Commit

Permalink
GUI 的各种更改
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Jan 22, 2025
1 parent 8dee9ff commit 833825e
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/ghostchu/peerbanhelper/BuildMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void loadBuildMeta(YamlConfiguration configuration) {
this.commit = configuration.getString("git.commit.id.commit-id");
this.abbrev = configuration.getString("git.commit.id.abbrev");
this.os = System.getProperty("os.name");
this.compileTime = configuration.getString("git.build.time");
this.compileTime = configuration.getString("git.build.time", "Unknown");
}

public boolean isSnapshotOrBeta() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
<scrollpane id="f08b9" binding="loggerScrollPane">
<constraints border-constraint="Center"/>
<properties>
<font name="Consolas"/>
<enabled value="true"/>
<font/>
<verticalScrollBarPolicy value="22"/>
</properties>
<border type="none">
Expand All @@ -31,7 +32,9 @@
<children>
<component id="954ba" class="javax.swing.JList" binding="loggerTextList">
<constraints/>
<properties/>
<properties>
<font swing-font="TextArea.font"/>
</properties>
</component>
</children>
</scrollpane>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public MainWindow(SwingGuiImpl swingGUI) {
this.swingGUI = swingGUI;
if (SystemInfo.isMacFullWindowContentSupported)
getRootPane().putClientProperty("apple.awt.transparentTitleBar", true);
setTitle(tlUI(Lang.GUI_TITLE_LOADING, "Swing UI", Main.getMeta().getVersion(), Main.getMeta().getAbbrev()));
setTitle(tlUI(Lang.GUI_TITLE_LOADING, Main.getMeta().getVersion(), Main.getMeta().getAbbrev()));
// max dimension size or 720p
var maxAllowedWidth = Math.min(1280, Toolkit.getDefaultToolkit().getScreenSize().width);
var maxAllowedHeight = Math.min(720, Toolkit.getDefaultToolkit().getScreenSize().height);
Expand All @@ -91,6 +91,7 @@ public void windowClosing(WindowEvent e) {
this.webuiTab = new WebUITab(this);
}


public static void setTabTitle(JPanel tab, String title) {
JTabbedPane tabbedPane = (JTabbedPane) SwingUtilities.getAncestorOfClass(JTabbedPane.class, tab);
for (int tabIndex = 0; tabIndex < tabbedPane.getTabCount(); tabIndex++) {
Expand Down Expand Up @@ -151,11 +152,14 @@ public void dispose() {
tabbedPaneLogs.setLayout(new BorderLayout(0, 0));
tabbedPane.addTab("Logs", tabbedPaneLogs);
loggerScrollPane = new JScrollPane();
Font loggerScrollPaneFont = this.$$$getFont$$$("Consolas", -1, -1, loggerScrollPane.getFont());
loggerScrollPane.setEnabled(true);
Font loggerScrollPaneFont = this.$$$getFont$$$(null, -1, -1, loggerScrollPane.getFont());
if (loggerScrollPaneFont != null) loggerScrollPane.setFont(loggerScrollPaneFont);
loggerScrollPane.setVerticalScrollBarPolicy(22);
tabbedPaneLogs.add(loggerScrollPane, BorderLayout.CENTER);
loggerTextList = new JList();
Font loggerTextListFont = UIManager.getFont("TextArea.font");
if (loggerTextListFont != null) loggerTextList.setFont(loggerTextListFont);
loggerScrollPane.setViewportView(loggerTextList);
}

Expand Down Expand Up @@ -184,6 +188,26 @@ public void dispose() {
return mainPanel;
}

/** @noinspection ALL */
protected Font getFont(String fontName, int style, int size, Font currentFont) {
if (currentFont == null) return null;
String resultName;
if (fontName == null) {
resultName = currentFont.getName();
} else {
Font testFont = new Font(fontName, Font.PLAIN, 10);
if (testFont.canDisplay('a') && testFont.canDisplay('1') && testFont.canDisplay('中')) {
resultName = fontName;
} else {
resultName = currentFont.getName();
}
}
Font font = new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize());
boolean isMac = System.getProperty("os.name", "").toLowerCase(Locale.ENGLISH).startsWith("mac");
Font fontWithFallback = isMac ? new Font(font.getFamily(), font.getStyle(), font.getSize()) : new StyleContext().getFont(font.getFamily(), font.getStyle(), font.getSize());
return fontWithFallback instanceof FontUIResource ? fontWithFallback : new FontUIResource(fontWithFallback);
}


private void createUIComponents() {
// TODO: place custom component creation code here
Expand Down Expand Up @@ -317,11 +341,16 @@ private void deActiveBrowser(int tabPos) {
* @param cefBrowser 显示开发者工具的浏览器
*/
private void devToolsShow(CefBrowser cefBrowser) {
if (cefBrowser.getURL().startsWith("devtools://")) {
Thread.startVirtualThread(() -> parent.getSwingGUI().createDialog(Level.WARNING, "Cannot open devtools for a devtools page", "You cannot open PeerBanHelper devtools for a PeerBanHelper devtools page. (WHY YOU DO THAT? ARE YOU CRAZY?)"));
return;
}
if (devToolsDialog_ != null) {
devToolsDialog_.dispose();
}
// 因为是开发者工具,不能影响内容页面的显示,所以单独新建一个窗体显示
devToolsDialog_ = new JCEFSwingDevTools(new JFrame(), "PeerBanHelper WebUI DevTools", cefBrowser);
var title = "PeerBanHelper WebUI DevTools (" + app.getVersion().toString() + ")";
devToolsDialog_ = new JCEFSwingDevTools(new JFrame(), title, cefBrowser);
// devToolsDialog_ = new DevToolsDialog(owner_, "开发者工具", cefBrowser);
var maxAllowedWidth = Math.min(1280, Toolkit.getDefaultToolkit().getScreenSize().width);
var maxAllowedHeight = Math.min(720, Toolkit.getDefaultToolkit().getScreenSize().height);
Expand Down Expand Up @@ -530,7 +559,7 @@ public LogsTab(MainWindow parent) {
this.parent = parent;

parent.loggerTextList.setModel(new DefaultListModel<>());
parent.loggerTextList.setFont(new Font("微软雅黑", Font.PLAIN, 14));
parent.loggerTextList.setFont(parent.loggerTextList.getFont().deriveFont(14f));
parent.loggerTextList.setCellRenderer(new LogEntryRenderer());
parent.loggerTextList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
parent.loggerTextList.setLayoutOrientation(JList.VERTICAL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.text.StyleContext;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Locale;
import java.util.StringJoiner;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -61,9 +65,19 @@ public SwingGuiImpl(String[] args) {
}
}

private void setUIFont(String fontName) {
Enumeration<Object> keys = UIManager.getLookAndFeelDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof FontUIResource fontUIResource)
UIManager.put(key, getFont(fontName, -1, -1, fontUIResource));
}
}

private void updateGuiStuff() {
StringBuilder builder = new StringBuilder();
builder.append(tlUI(Lang.GUI_TITLE_LOADED, "Swing UI", Main.getMeta().getVersion(), Main.getMeta().getAbbrev()));
builder.append(tlUI(Lang.GUI_TITLE_LOADED, Main.getMeta().getVersion(), Main.getMeta().getAbbrev()));
StringJoiner joiner = new StringJoiner("", " [", "]");
joiner.setEmptyValue("");
ExchangeMap.GUI_DISPLAY_FLAGS.forEach(flag -> joiner.add(flag.getContent()));
Expand Down Expand Up @@ -166,6 +180,26 @@ public void applyLight() {
}
}

/** @noinspection ALL */
private Font getFont(String fontName, int style, int size, Font currentFont) {
if (currentFont == null) return null;
String resultName;
if (fontName == null) {
resultName = currentFont.getName();
} else {
Font testFont = new Font(fontName, Font.PLAIN, 10);
if (testFont.canDisplay('a') && testFont.canDisplay('1')) {
resultName = fontName;
} else {
resultName = currentFont.getName();
}
}
Font font = new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize());
boolean isMac = System.getProperty("os.name", "").toLowerCase(Locale.ENGLISH).startsWith("mac");
Font fontWithFallback = isMac ? new Font(font.getFamily(), font.getStyle(), font.getSize()) : new StyleContext().getFont(font.getFamily(), font.getStyle(), font.getSize());
return fontWithFallback instanceof FontUIResource ? fontWithFallback : new FontUIResource(fontWithFallback);
}

@Override
public void createMainWindow() {
mainWindow = new MainWindow(this);
Expand Down Expand Up @@ -281,7 +315,8 @@ public void createDialog(Level level, String title, String description) {
if (Taskbar.isTaskbarSupported() && Taskbar.getTaskbar().isSupported(Taskbar.Feature.USER_ATTENTION_WINDOW)) {
Taskbar.getTaskbar().requestWindowUserAttention(mainWindow);
}
JOptionPane.showMessageDialog(null, description, title, msgType);
var finalMsgType = msgType;
SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(null, description, title, finalMsgType));
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/lang/en-us/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ WEBAPI_INTERNAL_ERROR: "An internal server error occurred while processing the W
GITHUB_PAGE: "https://github.com/PBH-BTN/PeerBanHelper"
GUI_COPY_TO_CLIPBOARD_TITLE: "Copy to clipboard"
GUI_COPY_TO_CLIPBOARD_DESCRIPTION: "Successfully copied to system clipboard: \n{}"
GUI_TITLE_LOADING: "PeerBanHelper ({}) - Loading, please wait..."
GUI_TITLE_LOADED: "PeerBanHelper ({}) - {} ({})"
GUI_TITLE_LOADING: "PeerBanHelper - Loading, please wait..."
GUI_TITLE_LOADED: "PeerBanHelper - {} ({})"
WEBVIEW_DISABLED_WEBKIT_NOT_INCLUDED: "JavaFx Web module not found, you are using a lightweight build, WebUI tab not enabled"
WEBVIEW_ENABLED: "JavaFx Web found, WebUI tab enabled"
STATUS_TEXT_OK: "Currently working normally"
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/lang/messages_fallback.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ WEBAPI_INTERNAL_ERROR: "处理 WebAPI 请求时出现了一个内部服务器错
GITHUB_PAGE: "https://github.com/PBH-BTN/PeerBanHelper"
GUI_COPY_TO_CLIPBOARD_TITLE: "复制到剪贴板"
GUI_COPY_TO_CLIPBOARD_DESCRIPTION: "已成功复制到系统剪贴板: \n{}"
GUI_TITLE_LOADING: "PeerBanHelper ({}) - 正在加载,请稍候..."
GUI_TITLE_LOADED: "PeerBanHelper ({}) - {} ({})"
GUI_TITLE_LOADING: "PeerBanHelper - 正在加载,请稍候..."
GUI_TITLE_LOADED: "PeerBanHelper - {} ({})"
WEBVIEW_DISABLED_WEBKIT_NOT_INCLUDED: "未找到 JavaFx Web 模块,您正在使用精简构建,WebUI 选项卡未启用"
WEBVIEW_ENABLED: "已找到 JavaFx Web,WebUI 选项卡已启用"
STATUS_TEXT_OK: "当前工作正常"
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/lang/zh-cn/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ WEBAPI_INTERNAL_ERROR: "处理 WebAPI 请求时出现了一个内部服务器错
GITHUB_PAGE: "https://github.com/PBH-BTN/PeerBanHelper"
GUI_COPY_TO_CLIPBOARD_TITLE: "复制到剪贴板"
GUI_COPY_TO_CLIPBOARD_DESCRIPTION: "已成功复制到系统剪贴板: \n{}"
GUI_TITLE_LOADING: "PeerBanHelper ({}) - 正在加载,请稍候..."
GUI_TITLE_LOADED: "PeerBanHelper ({}) - {} ({})"
GUI_TITLE_LOADING: "PeerBanHelper - 正在加载,请稍候..."
GUI_TITLE_LOADED: "PeerBanHelper - {} ({})"
WEBVIEW_DISABLED_WEBKIT_NOT_INCLUDED: "未找到 JavaFx Web 模块,您正在使用精简构建,WebUI 选项卡未启用"
WEBVIEW_ENABLED: "已找到 JavaFx Web,WebUI 选项卡已启用"
STATUS_TEXT_OK: "当前工作正常"
Expand Down

0 comments on commit 833825e

Please sign in to comment.