Skip to content

Commit

Permalink
任务栏进度条行为(第一版)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Jan 21, 2025
1 parent 3bf6552 commit 8805961
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ public void windowClosing(WindowEvent e) {
setVisible(!swingGUI.isSilentStart());
this.logsTab = new LogsTab(this);
this.webuiTab = new WebUITab(this);
if (Taskbar.isTaskbarSupported() && Taskbar.getTaskbar().isSupported(Taskbar.Feature.PROGRESS_STATE_WINDOW)) {
Taskbar.getTaskbar().setWindowProgressState(this, Taskbar.State.INDETERMINATE);
}
Main.getEventBus().register(this);
}

@Subscribe
public void pbhStarted(PBHServerStartedEvent event) {
if (Taskbar.isTaskbarSupported() && Taskbar.getTaskbar().isSupported(Taskbar.Feature.PROGRESS_STATE_WINDOW)) {
Taskbar.getTaskbar().setWindowProgressState(this, Taskbar.State.OFF);
}
}

public static void setTabTitle(JPanel tab, String title) {
Expand All @@ -111,7 +122,6 @@ public static void copyText(String content) {


public void sync() {

}

private void openWebUI() {
Expand Down Expand Up @@ -227,7 +237,7 @@ private void initJCEFEngine() {
if (enumProgress == EnumProgress.DOWNLOADING) {
progressDialog.show();
progressDialog.setProgressDisplayIndeterminate(false);
progressDialog.updateProgress(v);
progressDialog.updateProgress(v / 100.0f);
}
if (enumProgress == EnumProgress.EXTRACTING) {
progressDialog.setDescription(tlUI(Lang.JCEF_DOWNLOAD_UNZIP_DESCRIPTION));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public SwingGuiImpl(String[] args) {
}
}

private void updateTitle() {
private void updateGuiStuff() {
StringBuilder builder = new StringBuilder();
builder.append(tlUI(Lang.GUI_TITLE_LOADED, "Swing UI", Main.getMeta().getVersion(), Main.getMeta().getAbbrev()));
StringJoiner joiner = new StringJoiner("", " [", "]");
Expand All @@ -68,6 +68,21 @@ private void updateTitle() {
mainWindow.setTitle(builder.append(joiner).toString());
}
});

// taskbar
if (Main.getServer().isGlobalPaused()) {
if (Taskbar.getTaskbar().isSupported(Taskbar.Feature.PROGRESS_STATE_WINDOW)) {
if (Taskbar.getTaskbar().isSupported(Taskbar.Feature.PROGRESS_VALUE_WINDOW)) {
Taskbar.getTaskbar().setWindowProgressValue(mainWindow, 100);
}
Taskbar.getTaskbar().setWindowProgressState(mainWindow, Taskbar.State.PAUSED);
} else {
if (Taskbar.getTaskbar().isSupported(Taskbar.Feature.PROGRESS_VALUE_WINDOW)) {
Taskbar.getTaskbar().setWindowProgressValue(mainWindow, 0);
}
Taskbar.getTaskbar().setWindowProgressState(mainWindow, Taskbar.State.OFF);
}
}
}


Expand Down Expand Up @@ -96,7 +111,7 @@ private void setupSwingDefaultFonts() {

@Override
public void onPBHFullyStarted(PeerBanHelperServer server) {
CommonUtil.getScheduler().scheduleWithFixedDelay(this::updateTitle, 0, 1, TimeUnit.SECONDS);
CommonUtil.getScheduler().scheduleWithFixedDelay(this::updateGuiStuff, 0, 1, TimeUnit.SECONDS);
}

@Subscribe
Expand Down Expand Up @@ -253,12 +268,14 @@ public void createDialog(Level level, String title, String description) {
if (level == Level.SEVERE) {
msgType = JOptionPane.ERROR_MESSAGE;
}
if (Taskbar.isTaskbarSupported() && Taskbar.getTaskbar().isSupported(Taskbar.Feature.USER_ATTENTION_WINDOW)) {
Taskbar.getTaskbar().requestWindowUserAttention(mainWindow);
}
JOptionPane.showMessageDialog(null, description, title, msgType);
}

@Override
public void createNotification(Level level, String title, String description) {

var swingTray = mainWindow.getTrayMenu().getSwingTrayDialog();
if (swingTray != null) {
var icon = swingTray.getTrayIcon();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,23 @@ public SwingProgressDialog(String title, String description, String buttonText,
frame.add(bottomPanel, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(false);
if (Taskbar.isTaskbarSupported()) {
Taskbar.getTaskbar().setWindowProgressState(frame, Taskbar.State.INDETERMINATE);
}
}

public void updateProgress(float progress) {
// 让进度条显示具体进度
SwingUtilities.invokeLater(() -> {
progressBar.setValue((int) (progress * 100)); // 更新进度条的值,0 到 100
if (Taskbar.isTaskbarSupported()) {
if (Taskbar.getTaskbar().isSupported(Taskbar.Feature.PROGRESS_VALUE_WINDOW)) {
Taskbar.getTaskbar().setWindowProgressValue(frame, (int) (progress * 100));
}
if (Taskbar.getTaskbar().isSupported(Taskbar.Feature.PROGRESS_STATE_WINDOW)) {
Taskbar.getTaskbar().setWindowProgressState(frame, Taskbar.State.NORMAL);
}
}
});
}

Expand Down Expand Up @@ -124,6 +135,9 @@ public void setProgressDisplayIndeterminate(boolean indeterminate) {
SwingUtilities.invokeLater(() -> {
progressBar.setIndeterminate(indeterminate);
progressBar.setStringPainted(!indeterminate);
if (Taskbar.isTaskbarSupported() && Taskbar.getTaskbar().isSupported(Taskbar.Feature.PROGRESS_STATE_WINDOW)) {
Taskbar.getTaskbar().setWindowProgressState(frame, indeterminate ? Taskbar.State.INDETERMINATE : Taskbar.State.NORMAL);
}
});
}

Expand Down

0 comments on commit 8805961

Please sign in to comment.