Avoid notification flickering by moving windows in reverse order

This commit is contained in:
Reinhard Pointner 2016-03-11 08:16:54 +00:00
parent d53dd1ffec
commit 0a039d943b
5 changed files with 46 additions and 47 deletions

View File

@ -23,7 +23,7 @@ public class NotificationHandler extends Handler {
private final NotificationManager manager;
public NotificationHandler(String title) {
this(title, 2500, new NotificationManager(new QueueNotificationLayout(NORTH, SOUTH)));
this(title, 2500, new NotificationManager(new QueueNotificationLayout(NORTH, SOUTH), 5));
}
public NotificationHandler(String title, int timeout, NotificationManager manager) {

View File

@ -4,11 +4,12 @@
package net.filebot.util.ui.notification;
public interface NotificationLayout {
public void add(NotificationWindow notification);
public void remove(NotificationWindow notification);
public int size();
}

View File

@ -4,38 +4,35 @@
package net.filebot.util.ui.notification;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import net.filebot.util.ui.SwingUI;
public class NotificationManager {
private final NotificationLayout layout;
private final int limit;
public NotificationManager() {
this(new QueueNotificationLayout());
this(new QueueNotificationLayout(), 5);
}
public NotificationManager(NotificationLayout layout) {
public NotificationManager(NotificationLayout layout, int limit) {
this.layout = layout;
this.limit = limit;
}
public void show(NotificationWindow notification) {
SwingUI.checkEventDispatchThread();
notification.addWindowListener(new RemoveListener());
layout.add(notification);
notification.setVisible(true);
if (layout.size() < limit) {
layout.add(notification);
notification.addWindowListener(new RemoveListener());
notification.setVisible(true);
}
}
private class RemoveListener extends WindowAdapter {
@Override

View File

@ -4,7 +4,6 @@
package net.filebot.util.ui.notification;
import static net.filebot.util.ui.notification.Direction.*;
import java.awt.Dimension;
@ -13,9 +12,9 @@ import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class QueueNotificationLayout implements NotificationLayout {
private final List<NotificationWindow> notifications = new ArrayList<NotificationWindow>();
@ -24,26 +23,22 @@ public class QueueNotificationLayout implements NotificationLayout {
private final Direction direction;
private final Direction growAnchor;
public QueueNotificationLayout() {
this(SOUTH_EAST, WEST);
}
public QueueNotificationLayout(Direction alignment, Direction direction) {
this.alignment = alignment;
this.growAnchor = alignment;
this.direction = direction;
}
public QueueNotificationLayout(Direction orientation, Direction direction, Direction growAnchor) {
this.alignment = orientation;
this.direction = direction;
this.growAnchor = growAnchor;
}
private Point getBaseAnchor(Dimension screen, Insets insets) {
Point p = new Point();
@ -59,7 +54,6 @@ public class QueueNotificationLayout implements NotificationLayout {
return p;
}
private Point getLocation(Point anchor, Dimension size) {
Point p = new Point();
@ -69,7 +63,6 @@ public class QueueNotificationLayout implements NotificationLayout {
return p;
}
private Point getNextAnchor(Point anchor, Dimension size) {
Point p = new Point();
@ -79,30 +72,34 @@ public class QueueNotificationLayout implements NotificationLayout {
return p;
}
@Override
public void add(NotificationWindow notification) {
notifications.add(notification);
align(notification.getGraphicsConfiguration());
}
private void align(GraphicsConfiguration gc) {
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
// avoid flickering by moving windows in reverse order
Point anchor = getBaseAnchor(screen, insets);
for (NotificationWindow window : notifications) {
Dimension size = window.getSize();
Point p = getLocation(anchor, size);
window.setLocation(p);
anchor = getNextAnchor(anchor, size);
}
align(anchor, notifications.iterator());
}
private void align(Point anchor, Iterator<NotificationWindow> seq) {
if (!seq.hasNext()) {
return;
}
NotificationWindow window = seq.next();
Dimension size = window.getSize();
Point p = getLocation(anchor, size);
align(getNextAnchor(anchor, size), seq);
window.setLocation(p);
}
@Override
public void remove(NotificationWindow notification) {
@ -111,4 +108,9 @@ public class QueueNotificationLayout implements NotificationLayout {
}
}
@Override
public int size() {
return notifications.size();
}
}

View File

@ -4,7 +4,6 @@
package net.filebot.util.ui.notification;
import static net.filebot.util.ui.notification.Direction.*;
import java.awt.Dimension;
@ -12,23 +11,19 @@ import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
public class SimpleNotificationLayout implements NotificationLayout {
private NotificationWindow currentNotification;
private NotificationWindow current;
private Direction alignment;
public SimpleNotificationLayout() {
this(NORTH);
}
public SimpleNotificationLayout(Direction alignment) {
this.alignment = alignment;
}
private Point getBaseAnchor(Dimension screen, Insets insets) {
Point p = new Point();
@ -44,7 +39,6 @@ public class SimpleNotificationLayout implements NotificationLayout {
return p;
}
private Point getLocation(Point anchor, Dimension size) {
Point p = new Point();
@ -54,7 +48,6 @@ public class SimpleNotificationLayout implements NotificationLayout {
return p;
}
@Override
public void add(NotificationWindow notification) {
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
@ -64,17 +57,23 @@ public class SimpleNotificationLayout implements NotificationLayout {
Point anchor = getBaseAnchor(screen, insets);
notification.setLocation(getLocation(anchor, size));
if (currentNotification != null) {
currentNotification.close();
if (current != null) {
current.close();
}
currentNotification = notification;
current = notification;
}
@Override
public void remove(NotificationWindow notification) {
if (current != null && current == notification) {
current.close();
}
current = null;
}
@Override
public int size() {
return current == null ? 0 : 1;
}
}