-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToast.java
390 lines (338 loc) · 12 KB
/
Toast.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import com.sun.javafx.Utils;
import javafx.animation.FadeTransition;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.*;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Popup;
import javafx.stage.Window;
import javafx.util.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
/**
* User: Alexander Vos
* Date: 19.02.13
* Time: 23:35
*/
public class Toast extends Popup {
private static final Logger log = LoggerFactory.getLogger(Toast.class);
private static double defaultContentOpacity = 0.9;
private static Duration defaultFadeInDuration = new Duration(600);
private static Duration defaultFadeOutDuration = new Duration(300);
public static double getDefaultContentOpacity() {
return defaultContentOpacity;
}
public static void setDefaultContentOpacity(double opacity) {
Toast.defaultContentOpacity = opacity;
}
public static Duration getDefaultFadeInDuration() {
return defaultFadeInDuration;
}
public static void setDefaultFadeInDuration(Duration duration) {
if (duration == null) {
throw new NullPointerException();
}
Toast.defaultFadeInDuration = duration;
}
public static Duration getDefaultFadeOutDuration() {
return defaultFadeOutDuration;
}
public static void setDefaultFadeOutDuration(Duration duration) {
if (duration == null) {
throw new NullPointerException();
}
Toast.defaultFadeOutDuration = duration;
}
private static volatile boolean alreadyShowing = false;
private static final Queue<Toast> toastQueue = new LinkedBlockingQueue<>(50);
public static int clearWaitingToasts() {
int count = toastQueue.size();
toastQueue.clear();
return count;
}
private Node content;
private Duration duration;
private boolean autoCenter = true;
private Window window;
private Node anchor;
private double screenX;
private double screenY;
private double contentOpacity = defaultContentOpacity;
private Duration fadeInDuration = defaultFadeInDuration;
private Duration fadeOutDuration = defaultFadeOutDuration;
private Timeline hideTimer;
public Toast() {
setAutoHide(true);
}
public void setContent(Node content) {
if (content == null) {
throw new NullPointerException();
}
this.content = content;
content.getStyleClass().add("toast");
super.getContent().setAll(content);
}
public Duration getDuration() {
return duration;
}
public void setDuration(Duration duration) {
if (duration == null) {
throw new NullPointerException();
}
this.duration = duration;
}
public boolean isAutoCenter() {
return autoCenter;
}
public void setAutoCenter(boolean autoCenter) {
this.autoCenter = autoCenter;
}
public double getContentOpacity() {
return contentOpacity;
}
public void setContentOpacity(double opacity) {
this.contentOpacity = opacity;
}
public Duration getFadeInDuration() {
return fadeInDuration;
}
public void setFadeInDuration(Duration duration) {
if (duration == null) {
throw new NullPointerException();
}
this.fadeInDuration = duration;
}
public Duration getFadeOutDuration() {
return fadeOutDuration;
}
public void setFadeOutDuration(Duration duration) {
if (duration == null) {
throw new NullPointerException();
}
this.fadeOutDuration = duration;
}
@Override
public void show(Window window) {
this.show(window, Double.NaN, Double.NaN);
}
@Override
public void show(Window window, double screenX, double screenY) {
if (window == null) {
return;
}
this.window = window;
this.anchor = null;
this.screenX = screenX;
this.screenY = screenY;
tryShow();
}
public void show(Node anchor) {
autoCenter = true;
this.show(anchor, Double.NaN, Double.NaN);
}
@Override
public void show(Node anchor, double screenX, double screenY) {
if (anchor == null) {
return;
}
this.window = null;
this.anchor = anchor;
this.screenX = screenX;
this.screenY = screenY;
tryShow();
}
public void show(Node anchor, Side side, double offsetX, double offsetY) {
if (anchor == null) {
return;
}
autoCenter = false;
HPos hpos = side == Side.LEFT ? HPos.LEFT : side == Side.RIGHT ? HPos.RIGHT : HPos.CENTER;
VPos vpos = side == Side.TOP ? VPos.TOP : side == Side.BOTTOM ? VPos.BOTTOM : VPos.CENTER;
// translate from anchor/hpos/vpos/offsetX/offsetY into screenX/screenY
Point2D point = Utils.pointRelativeTo(anchor, content.prefWidth(-1), content.prefHeight(-1), hpos, vpos, offsetX, offsetY, true);
this.show(anchor, point.getX(), point.getY());
}
public void show(Node anchor, Side side) {
this.show(anchor, side, 0, 0);
}
private void tryShow() {
if (window == null && anchor == null) {
throw new IllegalStateException("window and anchor node are both null");
}
if (alreadyShowing) {
// if another toast is already showing add this one to the waiting queue
if (toastQueue.offer(this)) {
log.debug("toast enqueued: {}", this);
} else {
log.error("toast queue exceeded it's capacity");
}
} else {
alreadyShowing = true;
doShow();
}
}
private void doShow() {
log.trace("show toast: {}", this);
if (window != null) {
if (autoCenter) {
connectAutoCenterHandler();
}
if (Double.isNaN(screenX) || Double.isNaN(screenY)) {
super.show(window);
} else {
super.show(window, screenX, screenY);
}
} else { // anchor
if (autoCenter) {
Scene scene = anchor.getScene();
if (scene != null) {
window = scene.getWindow();
}
if (window == null) {
throw new IllegalStateException("anchor node is not attached to a window");
}
connectAutoCenterHandler();
}
super.show(anchor, Double.isNaN(screenX) ? 0.0 : screenX, Double.isNaN(screenY) ? 0.0 : screenY);
}
if (isAutoHide() && !duration.isIndefinite()) {
hideTimer = new Timeline(new KeyFrame(duration));
hideTimer.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
hideTimer = null;
Toast.this.hide();
}
});
hideTimer.playFromStart();
}
FadeTransition transition = new FadeTransition(fadeInDuration, content);
transition.setFromValue(0.0);
transition.setToValue(contentOpacity);
transition.play();
}
private void connectAutoCenterHandler() {
XListener xListener = new XListener();
super.widthProperty().addListener(xListener);
YListener yListener = new YListener();
super.heightProperty().addListener(yListener);
}
@Override
public void hide() {
log.trace("hide toast: {}", this);
if (hideTimer != null) {
// cancel the timer if the toast is hidden before the timer fires
hideTimer.stop();
hideTimer = null;
}
if (!isShowing()) {
return;
}
FadeTransition transition = new FadeTransition(fadeOutDuration, content);
transition.setToValue(0.0);
transition.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
doHide();
}
});
transition.play();
}
private void doHide() {
super.hide();
if (toastQueue.isEmpty()) {
alreadyShowing = false;
} else {
// get the next toast and show it (may flicker if not enqueued in the FX thread!?)
final Toast toast = toastQueue.poll();
log.debug("toast unqueued: {}", toast);
Platform.runLater(new Runnable() {
@Override
public void run() {
toast.doShow();
}
});
}
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("Toast");
sb.append("{content=").append(content);
sb.append('}');
return sb.toString();
}
// helper methods
public static final Duration DURATION_SHORT = Duration.seconds(2);
public static final Duration DURATION_LONG = Duration.seconds(4);
private static final Color DEFAULT_BACKGROUND_COLOR = Color.gray(0.2);
private static final double DEFAULT_BACKGROUND_ARC = 8.0;
private static final Insets DEFAULT_CONTENT_MARGIN = new Insets(8, 16, 8, 16);
public static Toast makeToast(Node content, Duration duration) {
if (content == null) {
throw new NullPointerException("content");
}
if (duration == null) {
throw new NullPointerException("duration");
}
Rectangle rect = new Rectangle();
rect.getStyleClass().add("background");
rect.setFill(DEFAULT_BACKGROUND_COLOR);
rect.setArcWidth(DEFAULT_BACKGROUND_ARC);
rect.setArcHeight(DEFAULT_BACKGROUND_ARC);
StackPane pane = new StackPane();
rect.widthProperty().bind(pane.widthProperty());
rect.heightProperty().bind(pane.heightProperty());
StackPane.setMargin(content, DEFAULT_CONTENT_MARGIN);
pane.getChildren().addAll(rect, content);
Toast toast = new Toast();
toast.setContent(pane);
toast.setDuration(duration);
return toast;
}
private static final Color DEFAULT_FILL_COLOR = Color.WHITE;
public static Toast makeText(String text, boolean wrapText, double maxWidth, double maxHeight, Duration duration) {
Label label = new Label(text);
label.getStyleClass().add("text");
label.setWrapText(wrapText);
label.setMaxWidth(maxWidth);
label.setMaxHeight(maxHeight);
label.setTextFill(DEFAULT_FILL_COLOR);
return makeToast(label, duration);
}
public static Toast makeText(String text, Duration duration) {
return makeText(text, true, 500, 250, duration);
}
private class XListener implements ChangeListener<Number> {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
double x = window.getX() + window.getWidth() / 2 - Toast.super.getWidth() / 2;
if (!Double.isNaN(screenX)) {
x += screenX; // use as offset
}
Toast.super.setX(x);
}
}
private class YListener implements ChangeListener<Number> {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
double y = window.getY() + window.getHeight() / 2 - Toast.super.getHeight() / 2;
if (!Double.isNaN(screenY)) {
y += screenY; // use as offset
}
Toast.super.setY(y);
}
}
}