Make sure that notification boundaries don't exceed screen bounds
This commit is contained in:
parent
4b44495782
commit
da885fd5f2
|
@ -4,31 +4,25 @@
|
|||
|
||||
package net.filebot.util.ui.notification;
|
||||
|
||||
import static net.filebot.util.ui.SwingUI.*;
|
||||
|
||||
import java.awt.Window;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
import java.awt.event.ComponentListener;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.WindowEvent;
|
||||
|
||||
import javax.swing.JWindow;
|
||||
import javax.swing.Timer;
|
||||
|
||||
import net.filebot.util.ui.SwingUI;
|
||||
|
||||
|
||||
public class NotificationWindow extends JWindow {
|
||||
|
||||
private final int timeout;
|
||||
|
||||
|
||||
public NotificationWindow(Window owner, int timeout) {
|
||||
this(owner, timeout, true);
|
||||
}
|
||||
|
||||
|
||||
public NotificationWindow(Window owner, int timeout, boolean closeOnClick) {
|
||||
super(owner);
|
||||
this.timeout = timeout;
|
||||
|
@ -36,21 +30,19 @@ public class NotificationWindow extends JWindow {
|
|||
setAlwaysOnTop(true);
|
||||
|
||||
if (closeOnClick) {
|
||||
getGlassPane().addMouseListener(clickListener);
|
||||
getGlassPane().addMouseListener(mouseClicked(evt -> close()));
|
||||
getGlassPane().setVisible(true);
|
||||
}
|
||||
|
||||
addComponentListener(closeOnTimeout);
|
||||
}
|
||||
|
||||
|
||||
public NotificationWindow(Window owner) {
|
||||
this(owner, -1);
|
||||
}
|
||||
|
||||
|
||||
public final void close() {
|
||||
SwingUI.checkEventDispatchThread();
|
||||
checkEventDispatchThread();
|
||||
|
||||
// window events are not fired automatically, required for layout updates
|
||||
processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
|
||||
|
@ -67,21 +59,13 @@ public class NotificationWindow extends JWindow {
|
|||
|
||||
private Timer timer = null;
|
||||
|
||||
|
||||
@Override
|
||||
public void componentShown(ComponentEvent e) {
|
||||
if (timeout >= 0) {
|
||||
timer = SwingUI.invokeLater(timeout, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
close();
|
||||
}
|
||||
});
|
||||
timer = invokeLater(timeout, () -> close());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void componentHidden(ComponentEvent e) {
|
||||
if (timer != null) {
|
||||
|
@ -91,12 +75,4 @@ public class NotificationWindow extends JWindow {
|
|||
|
||||
};
|
||||
|
||||
private final MouseAdapter clickListener = new MouseAdapter() {
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
close();
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -84,21 +84,24 @@ public class QueueNotificationLayout implements NotificationLayout {
|
|||
|
||||
// avoid flickering by moving windows in reverse order
|
||||
Point anchor = getBaseAnchor(screen, insets);
|
||||
align(anchor, notifications.iterator());
|
||||
align(anchor, screen, notifications.iterator());
|
||||
}
|
||||
|
||||
private void align(Point anchor, Iterator<NotificationWindow> seq) {
|
||||
private void align(Point anchor, Dimension screen, Iterator<NotificationWindow> seq) {
|
||||
if (!seq.hasNext()) {
|
||||
return;
|
||||
}
|
||||
|
||||
NotificationWindow window = seq.next();
|
||||
|
||||
Dimension size = window.getSize();
|
||||
size.width = Math.min(size.width, (int) (screen.width * 0.8));
|
||||
size.height = Math.min(size.height, (int) (screen.height * 0.2));
|
||||
|
||||
Point p = getLocation(anchor, size);
|
||||
align(getNextAnchor(anchor, size), seq);
|
||||
align(getNextAnchor(anchor, size), screen, seq);
|
||||
|
||||
window.setLocation(p);
|
||||
window.setBounds(p.x, p.y, size.width, size.height);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue