Browse Source

Show window context menu when right-clicking anywhere on the title pane

decorations
Jannis Weis 3 years ago
parent
commit
3dba0f5a8c
No known key found for this signature in database
GPG Key ID: 7C9D8D4B558049AB
  1. 25
      windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/WindowsTitlePane.java

25
windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/WindowsTitlePane.java

@ -67,6 +67,17 @@ public class WindowsTitlePane extends CustomTitlePane {
private boolean oldResizable; private boolean oldResizable;
private PropertyChangeListener windowPropertyChangeListener; private PropertyChangeListener windowPropertyChangeListener;
private PropertyChangeListener rootPanePropertyChangeListener; private PropertyChangeListener rootPanePropertyChangeListener;
private final MouseListener contextMenuListener = new MouseAdapter() {
@Override
public void mouseReleased(final MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
Point location = MouseInfo.getPointerInfo().getLocation();
showWindowContextMenu(location);
}
super.mouseReleased(e);
}
};
private WindowListener windowListener; private WindowListener windowListener;
private TitlebarIcon closeIcon; private TitlebarIcon closeIcon;
private TitlebarIcon maximizeIcon; private TitlebarIcon maximizeIcon;
@ -166,6 +177,7 @@ public class WindowsTitlePane extends CustomTitlePane {
if (rootPane != null) { if (rootPane != null) {
rootPane.removePropertyChangeListener(rootPanePropertyChangeListener); rootPane.removePropertyChangeListener(rootPanePropertyChangeListener);
} }
removeMouseListener(contextMenuListener);
} }
protected void uninstallDecorations(final boolean removeDecorations) { protected void uninstallDecorations(final boolean removeDecorations) {
@ -240,6 +252,7 @@ public class WindowsTitlePane extends CustomTitlePane {
rootPane.addPropertyChangeListener(rootPanePropertyChangeListener); rootPane.addPropertyChangeListener(rootPanePropertyChangeListener);
window.addPropertyChangeListener(windowPropertyChangeListener); window.addPropertyChangeListener(windowPropertyChangeListener);
} }
addMouseListener(contextMenuListener);
} }
private WindowListener createWindowListener() { private WindowListener createWindowListener() {
@ -338,14 +351,22 @@ public class WindowsTitlePane extends CustomTitlePane {
JButton button = new JButton(); JButton button = new JButton();
button.putClientProperty("JButton.noShadowOverwrite", true); button.putClientProperty("JButton.noShadowOverwrite", true);
button.setComponentPopupMenu(createMenu()); button.setComponentPopupMenu(createMenu());
button.addActionListener(e -> button.getComponentPopupMenu().show(button, button.addActionListener(e -> {
button.getWidth() / 2, button.getHeight() / 2)); Point buttonLocation = new Point(0, windowIconButton.getHeight());
SwingUtilities.convertPointToScreen(buttonLocation, windowIconButton);
showWindowContextMenu(buttonLocation);
});
button.setFocusable(false); button.setFocusable(false);
button.setContentAreaFilled(false); button.setContentAreaFilled(false);
button.setBorderPainted(false); button.setBorderPainted(false);
return button; return button;
} }
private void showWindowContextMenu(final Point p) {
if (window == null) return;
windowIconButton.getComponentPopupMenu().show(window, p.x - window.getX(), p.y - window.getY());
}
private JPopupMenu createMenu() { private JPopupMenu createMenu() {
JPopupMenu menu = new JPopupMenu(); JPopupMenu menu = new JPopupMenu();
if (getDecorationStyle() == JRootPane.FRAME) { if (getDecorationStyle() == JRootPane.FRAME) {

Loading…
Cancel
Save