Make sure that notification boundaries don't exceed screen bounds

This commit is contained in:
Reinhard Pointner 2016-09-26 16:16:33 +08:00
parent 4b44495782
commit da885fd5f2
2 changed files with 11 additions and 32 deletions

View File

@ -4,31 +4,25 @@
package net.filebot.util.ui.notification; package net.filebot.util.ui.notification;
import static net.filebot.util.ui.SwingUI.*;
import java.awt.Window; import java.awt.Window;
import java.awt.event.ComponentAdapter; import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent; import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener; import java.awt.event.ComponentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import javax.swing.JWindow; import javax.swing.JWindow;
import javax.swing.Timer; import javax.swing.Timer;
import net.filebot.util.ui.SwingUI;
public class NotificationWindow extends JWindow { public class NotificationWindow extends JWindow {
private final int timeout; private final int timeout;
public NotificationWindow(Window owner, int timeout) { public NotificationWindow(Window owner, int timeout) {
this(owner, timeout, true); this(owner, timeout, true);
} }
public NotificationWindow(Window owner, int timeout, boolean closeOnClick) { public NotificationWindow(Window owner, int timeout, boolean closeOnClick) {
super(owner); super(owner);
this.timeout = timeout; this.timeout = timeout;
@ -36,21 +30,19 @@ public class NotificationWindow extends JWindow {
setAlwaysOnTop(true); setAlwaysOnTop(true);
if (closeOnClick) { if (closeOnClick) {
getGlassPane().addMouseListener(clickListener); getGlassPane().addMouseListener(mouseClicked(evt -> close()));
getGlassPane().setVisible(true); getGlassPane().setVisible(true);
} }
addComponentListener(closeOnTimeout); addComponentListener(closeOnTimeout);
} }
public NotificationWindow(Window owner) { public NotificationWindow(Window owner) {
this(owner, -1); this(owner, -1);
} }
public final void close() { public final void close() {
SwingUI.checkEventDispatchThread(); checkEventDispatchThread();
// window events are not fired automatically, required for layout updates // window events are not fired automatically, required for layout updates
processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
@ -67,21 +59,13 @@ public class NotificationWindow extends JWindow {
private Timer timer = null; private Timer timer = null;
@Override @Override
public void componentShown(ComponentEvent e) { public void componentShown(ComponentEvent e) {
if (timeout >= 0) { if (timeout >= 0) {
timer = SwingUI.invokeLater(timeout, new Runnable() { timer = invokeLater(timeout, () -> close());
@Override
public void run() {
close();
}
});
} }
} }
@Override @Override
public void componentHidden(ComponentEvent e) { public void componentHidden(ComponentEvent e) {
if (timer != null) { 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();
}
};
} }

View File

@ -84,21 +84,24 @@ public class QueueNotificationLayout implements NotificationLayout {
// avoid flickering by moving windows in reverse order // avoid flickering by moving windows in reverse order
Point anchor = getBaseAnchor(screen, insets); 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()) { if (!seq.hasNext()) {
return; return;
} }
NotificationWindow window = seq.next(); NotificationWindow window = seq.next();
Dimension size = window.getSize(); 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); 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 @Override