Browse Source

Stop USM firing events when there are no changes

pull/314/head
Chris Smith 9 years ago
parent
commit
3d9f272e93
1 changed files with 39 additions and 21 deletions
  1. 39
    21
      src/com/dmdirc/ui/messages/UnreadStatusManager.java

+ 39
- 21
src/com/dmdirc/ui/messages/UnreadStatusManager.java View File

@@ -46,9 +46,9 @@ public class UnreadStatusManager {
46 46
     private int unreadLines;
47 47
     private Optional<Colour> notificationColour = Optional.empty();
48 48
 
49
-    private Colour miscellaneousColour = Colour.GREEN;
50
-    private Colour messageColour = Colour.BLACK;
51
-    private Colour highlightColour = Colour.RED;
49
+    private Optional<Colour> miscellaneousColour = Optional.of(Colour.GREEN);
50
+    private Optional<Colour> messageColour = Optional.of(Colour.BLUE);
51
+    private Optional<Colour> highlightColour = Optional.of(Colour.RED);
52 52
 
53 53
     public UnreadStatusManager(final FrameContainer container) {
54 54
         this.container = container;
@@ -58,36 +58,35 @@ public class UnreadStatusManager {
58 58
     @Handler
59 59
     public void handleDisplayableEvent(final DisplayableEvent event) {
60 60
         if (event.getSource().equals(container)) {
61
-            unreadLines++;
62
-            raiseNotificationColour(miscellaneousColour);
61
+            updateStatus(miscellaneousColour, unreadLines + 1);
63 62
         }
64 63
     }
65 64
 
66 65
     @Handler
67 66
     public void handleChannelTextEvent(final BaseChannelTextEvent event) {
68 67
         if (event.getSource().equals(container)) {
69
-            raiseNotificationColour(messageColour);
68
+            updateStatus(messageColour);
70 69
         }
71 70
     }
72 71
 
73 72
     @Handler
74 73
     public void handleQueryTextEvent(final BaseQueryTextEvent event) {
75 74
         if (event.getSource().equals(container)) {
76
-            raiseNotificationColour(messageColour);
75
+            updateStatus(messageColour);
77 76
         }
78 77
     }
79 78
 
80 79
     @Handler
81 80
     public void handleChannelHighlightEvent(final ChannelHighlightEvent event) {
82 81
         if (event.getCause().getChannel().equals(container)) {
83
-            raiseNotificationColour(highlightColour);
82
+            updateStatus(highlightColour);
84 83
         }
85 84
     }
86 85
 
87 86
     @Handler
88 87
     public void handleQueryHighlightEvent(final QueryHighlightEvent event) {
89 88
         if (event.getCause().getQuery().equals(container)) {
90
-            raiseNotificationColour(highlightColour);
89
+            updateStatus(highlightColour);
91 90
         }
92 91
     }
93 92
 
@@ -100,22 +99,41 @@ public class UnreadStatusManager {
100 99
     }
101 100
 
102 101
     public void clearStatus() {
103
-        unreadLines = 0;
104
-        notificationColour = Optional.empty();
102
+        updateStatus(Optional.empty(), 0);
105 103
     }
106 104
 
107
-    private void raiseNotificationColour(final Colour colour) {
108
-        if (notificationColour.isPresent()) {
109
-            if (notificationColour.get().equals(miscellaneousColour)
110
-                    || colour.equals(highlightColour)) {
111
-                notificationColour = Optional.of(colour);
112
-            }
113
-        } else {
114
-            notificationColour = Optional.of(colour);
105
+    private void updateStatus(final Optional<Colour> desiredColour) {
106
+        updateStatus(desiredColour, unreadLines);
107
+    }
108
+
109
+    private void updateStatus(final Optional<Colour> desiredColour, final int newUnreadCount) {
110
+        final Optional<Colour> newColour = getBestColour(desiredColour, notificationColour);
111
+        final boolean updated = !newColour.equals(notificationColour)
112
+                || newUnreadCount != unreadLines;
113
+        notificationColour = newColour;
114
+        unreadLines = newUnreadCount;
115
+
116
+        if (updated) {
117
+            eventBus.publishAsync(new UnreadStatusChangedEvent(container, this, notificationColour,
118
+                    unreadLines));
115 119
         }
120
+    }
116 121
 
117
-        eventBus.publishAsync(new UnreadStatusChangedEvent(container, this, notificationColour,
118
-                unreadLines));
122
+    private Optional<Colour> getBestColour(
123
+            final Optional<Colour> desiredColour,
124
+            final Optional<Colour> existingColour) {
125
+        if (!desiredColour.isPresent()) {
126
+            // If we're trying to explicitly reset, go with the empty one.
127
+            return desiredColour;
128
+        }
129
+
130
+        if (desiredColour.equals(highlightColour)
131
+                || !existingColour.isPresent()
132
+                || existingColour.equals(miscellaneousColour)) {
133
+            return desiredColour;
134
+        } else {
135
+            return existingColour;
136
+        }
119 137
     }
120 138
 
121 139
 }

Loading…
Cancel
Save