Skip to content

Commit 4664178

Browse files
cmagliefacchinm
authored andcommitted
Refactored notification popup
1 parent 8b53785 commit 4664178

File tree

3 files changed

+47
-28
lines changed

3 files changed

+47
-28
lines changed

app/src/cc/arduino/view/NotificationPopup.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
import java.awt.Image;
3838
import java.awt.Point;
3939
import java.awt.event.*;
40+
import java.awt.event.ComponentAdapter;
41+
import java.awt.event.ComponentEvent;
42+
import java.awt.event.MouseAdapter;
43+
import java.awt.event.MouseEvent;
44+
import java.awt.event.WindowAdapter;
45+
import java.awt.event.WindowEvent;
46+
import java.awt.event.WindowFocusListener;
4047
import java.util.Timer;
4148
import java.util.TimerTask;
4249

@@ -51,6 +58,8 @@
5158

5259
import cc.arduino.Constants;
5360
import processing.app.PreferencesData;
61+
import processing.app.Base;
62+
import processing.app.Editor;
5463
import processing.app.Theme;
5564

5665
import java.awt.event.KeyEvent;
@@ -66,18 +75,20 @@ public interface OptionalButtonCallbacks {
6675
void onOptionalButton1Callback();
6776
void onOptionalButton2Callback();
6877
}
78+
private Editor editor;
6979

70-
public NotificationPopup(Frame parent, HyperlinkListener hyperlinkListener,
80+
public NotificationPopup(Editor parent, HyperlinkListener hyperlinkListener,
7181
String message) {
7282
this(parent, hyperlinkListener, message, true, null, null, null);
83+
editor = parent;
7384
}
7485

75-
public NotificationPopup(Frame parent, HyperlinkListener hyperlinkListener,
86+
public NotificationPopup(Editor parent, HyperlinkListener hyperlinkListener,
7687
String message, boolean _autoClose) {
7788
this(parent, hyperlinkListener, message, _autoClose, null, null, null);
7889
}
7990

80-
public NotificationPopup(Frame parent, HyperlinkListener hyperlinkListener,
91+
public NotificationPopup(Editor parent, HyperlinkListener hyperlinkListener,
8192
String message, boolean _autoClose, OptionalButtonCallbacks listener, String button1Name, String button2Name) {
8293
super(parent, false);
8394

@@ -88,6 +99,7 @@ public NotificationPopup(Frame parent, HyperlinkListener hyperlinkListener,
8899
else {
89100
autoClose = false;
90101
}
102+
editor = parent;
91103
optionalButtonCallbacks = listener;
92104

93105
setLayout(new FlowLayout());
@@ -106,7 +118,9 @@ public NotificationPopup(Frame parent, HyperlinkListener hyperlinkListener,
106118
text.setEditable(false);
107119
text.setText("<html><body style=\"font-family:sans-serif; font-size: "
108120
+ scale(14) + ";\"> " + message + " </body></html>");
109-
text.addHyperlinkListener(hyperlinkListener);
121+
if (hyperlinkListener != null) {
122+
text.addHyperlinkListener(hyperlinkListener);
123+
}
110124
add(text);
111125

112126
if (button1Name != null) {
@@ -271,4 +285,28 @@ public void run() {
271285
setModal(true);
272286
}
273287
}
288+
289+
public void beginWhenFocused() {
290+
if (editor.isFocused()) {
291+
begin();
292+
return;
293+
}
294+
Base base = editor.getBase();
295+
296+
// If the IDE is not focused wait until it is focused again to
297+
// display the notification, this avoids the annoying side effect
298+
// to "steal" the focus from another application.
299+
WindowFocusListener wfl = new WindowFocusListener() {
300+
@Override
301+
public void windowLostFocus(WindowEvent evt) {
302+
}
303+
304+
@Override
305+
public void windowGainedFocus(WindowEvent evt) {
306+
begin();
307+
base.getEditors().forEach(e -> e.removeWindowFocusListener(this));
308+
}
309+
};
310+
base.getEditors().forEach(e -> e.addWindowFocusListener(wfl));
311+
}
274312
}

app/src/processing/app/Editor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ public void windowDeactivated(WindowEvent e) {
403403
EditorConsole.setCurrentEditorConsole(console);
404404
}
405405

406+
public Base getBase() {
407+
return base;
408+
}
409+
406410
/**
407411
* Handles files dragged & dropped from the desktop and into the editor
408412
* window. Dragging files into the editor window is the same as using

app/src/processing/app/NewBoardListener.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,34 +73,11 @@ public void checkForNewBoardAttached() {
7373
}
7474

7575
SwingUtilities.invokeLater(() -> {
76-
7776
ed = base.getActiveEditor();
7877
NotificationPopup notificationPopup = new NotificationPopup(ed,
7978
new UpdatableBoardsLibsFakeURLsHandler(base),
8079
newBoardManagerLink, false);
81-
if (ed.isFocused()) {
82-
notificationPopup.begin();
83-
return;
84-
}
85-
86-
// If the IDE is not focused wait until it is focused again to
87-
// display the notification, this avoids the annoying side effect
88-
// to "steal" the focus from another application.
89-
WindowFocusListener wfl = new WindowFocusListener() {
90-
@Override
91-
public void windowLostFocus(WindowEvent evt) {
92-
}
93-
94-
@Override
95-
public void windowGainedFocus(WindowEvent evt) {
96-
notificationPopup.begin();
97-
for (Editor e : base.getEditors())
98-
e.removeWindowFocusListener(this);
99-
}
100-
};
101-
102-
for (Editor e : base.getEditors())
103-
e.addWindowFocusListener(wfl);
80+
notificationPopup.beginWhenFocused();
10481
});
10582
}
10683
}

0 commit comments

Comments
 (0)