-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #716 from PBH-BTN/master
7.1.3
- Loading branch information
Showing
35 changed files
with
572 additions
and
593 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/com/ghostchu/peerbanhelper/gui/impl/swing/SwingTray.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.ghostchu.peerbanhelper.gui.impl.swing; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.awt.event.MouseAdapter; | ||
import java.awt.event.MouseEvent; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
public class SwingTray { | ||
|
||
private final JDialog jDialog; | ||
private final JPopupMenu jPopupMenu; | ||
private final TrayIcon trayIcon; | ||
|
||
public SwingTray(TrayIcon trayIcon, Consumer<MouseEvent> clickCallback, Consumer<MouseEvent> rightClickCallback) { | ||
this.trayIcon = trayIcon; | ||
this.jDialog = new JDialog(); | ||
jDialog.setUndecorated(true); | ||
jDialog.setSize(1, 1); | ||
this.jPopupMenu = new JPopupMenu() { | ||
@Override | ||
public void firePopupMenuWillBecomeInvisible() { | ||
jDialog.setVisible(false); | ||
} | ||
}; | ||
trayIcon.addMouseListener(new MouseAdapter() { | ||
@Override | ||
public void mouseReleased(MouseEvent e) { | ||
// 左键单击 | ||
if (e.getButton() == 1) { | ||
clickCallback.accept(e); | ||
return; | ||
} | ||
if (e.getButton() == 3 && e.isPopupTrigger()) { | ||
rightClickCallback.accept(e); | ||
// 获取屏幕相对位置 | ||
Point point = MouseInfo.getPointerInfo().getLocation(); | ||
// 将jDialog设置为鼠标位置 | ||
jDialog.setLocation(point.x, point.y); | ||
// 显示载体 | ||
jDialog.setVisible(true); | ||
var dimension = jPopupMenu.getPreferredSize(); | ||
jPopupMenu.setSize((int) dimension.getWidth() + 1, (int) dimension.getHeight() + 1); | ||
// 在载体的0,0处显示对话框 | ||
jPopupMenu.show(jDialog, 0, 0); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public void set(List<JMenuItem> items) { | ||
jPopupMenu.removeAll(); | ||
items.forEach(ele->{ | ||
if(ele == null) { | ||
jPopupMenu.addSeparator(); | ||
}else{ | ||
jPopupMenu.add(ele); | ||
} | ||
}); | ||
} | ||
|
||
public TrayIcon getTrayIcon() { | ||
return trayIcon; | ||
} | ||
|
||
public JDialog getjDialog() { | ||
return jDialog; | ||
} | ||
|
||
public JPopupMenu getjPopupMenu() { | ||
return jPopupMenu; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.