Repository navigation
Expand file tree
/
Copy pathTooltipService.java
More file actions
530 lines (448 loc) · 19.7 KB
/
Copy pathTooltipService.java
File metadata and controls
530 lines (448 loc) · 19.7 KB
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
package com.simulator;
import javafx.animation.*;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.stage.Popup;
import javafx.util.Duration;
import org.kordamp.ikonli.javafx.FontIcon;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class TooltipService {
private static TooltipService instance;
private Popup currentTooltip;
private Timeline showAnimation;
private Timeline hideAnimation;
private Node currentTargetNode;
// ENTERPRISE: Professional timer management
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(r -> {
Thread t = new Thread(r, "TooltipService-Timer");
t.setDaemon(true);
return t;
});
private ScheduledFuture<?> showTask;
private ScheduledFuture<?> hideTask;
// PERFORMANCE: Debouncing configuration
private static final long HOVER_DELAY_MS = 800;
private static final long HIDE_DELAY_MS = 300;
// STATE: Simple state management
private volatile boolean isShowing = false;
private volatile boolean isScheduledToShow = false;
private volatile boolean isScheduledToHide = false;
private TooltipService() {}
public static TooltipService getInstance() {
if (instance == null) {
synchronized (TooltipService.class) {
if (instance == null) {
instance = new TooltipService();
}
}
}
return instance;
}
/**
* ✅ NEW: Cancel tooltip immediately on user click/interaction
*/
public synchronized void cancelTooltipOnInteraction() {
// Cancel any pending operations
cancelShowTask();
cancelHideTask();
// Immediately hide current tooltip without delay
if (isShowing && currentTooltip != null) {
try {
// Stop any running animations
if (showAnimation != null && showAnimation.getStatus() == Animation.Status.RUNNING) {
showAnimation.stop();
}
if (hideAnimation != null && hideAnimation.getStatus() == Animation.Status.RUNNING) {
hideAnimation.stop();
}
// Immediate fast hide animation
VBox content = (VBox) currentTooltip.getContent().get(0);
Timeline quickHide = new Timeline();
quickHide.getKeyFrames().addAll(
new KeyFrame(Duration.ZERO,
new KeyValue(content.opacityProperty(), content.getOpacity()),
new KeyValue(content.scaleXProperty(), content.getScaleX()),
new KeyValue(content.scaleYProperty(), content.getScaleY())
),
new KeyFrame(Duration.millis(100),
new KeyValue(content.opacityProperty(), 0.0, Interpolator.EASE_IN),
new KeyValue(content.scaleXProperty(), 0.7, Interpolator.EASE_IN),
new KeyValue(content.scaleYProperty(), 0.7, Interpolator.EASE_IN)
)
);
quickHide.setOnFinished(e -> {
currentTooltip.hide();
currentTooltip = null;
isShowing = false;
currentTargetNode = null;
});
quickHide.play();
} catch (Exception e) {
forceHideTooltip();
}
}
// Reset state
currentTargetNode = null;
isScheduledToShow = false;
isScheduledToHide = false;
System.out.println("⚡ Tooltip cancelled on user interaction");
}
/**
* ENTERPRISE: Professional hover handling with debouncing
*/
public synchronized void showAlgorithmTooltip(Node targetNode, AlgorithmInfo algorithmInfo) {
// Cancel any pending hide operations
cancelHideTask();
// If already showing for same target, do nothing
if (isShowing && currentTargetNode == targetNode) {
return;
}
// If showing for different target, hide current first
if (isShowing && currentTargetNode != targetNode) {
hideCurrentTooltipImmediate();
}
// Cancel any pending show operations
cancelShowTask();
// Set new target
currentTargetNode = targetNode;
isScheduledToShow = true;
// DEBOUNCING: Schedule show with delay
showTask = scheduler.schedule(() -> {
Platform.runLater(() -> {
if (currentTargetNode == targetNode && isScheduledToShow) {
createAndShowTooltip(targetNode, algorithmInfo);
}
});
}, HOVER_DELAY_MS, TimeUnit.MILLISECONDS);
System.out.println("🕐 Tooltip scheduled to show for: " + algorithmInfo.getTitle());
}
/**
* ENTERPRISE: Professional hide handling with delay
*/
public synchronized void hideCurrentTooltip() {
// Cancel any pending show operations
cancelShowTask();
if (!isShowing) {
return;
}
// Cancel any existing hide task
cancelHideTask();
isScheduledToHide = true;
currentTargetNode = null;
// DEBOUNCING: Schedule hide with small delay
hideTask = scheduler.schedule(() -> {
Platform.runLater(() -> {
if (isScheduledToHide) {
hideCurrentTooltipImmediate();
}
});
}, HIDE_DELAY_MS, TimeUnit.MILLISECONDS);
System.out.println("🕐 Tooltip scheduled to hide");
}
private synchronized void hideCurrentTooltipImmediate() {
isScheduledToShow = false;
isScheduledToHide = false;
if (currentTooltip != null && isShowing) {
try {
hideWithAnimation(currentTooltip);
} catch (Exception e) {
System.err.println("❌ Error hiding tooltip: " + e.getMessage());
forceHideTooltip();
}
}
}
private void forceHideTooltip() {
try {
if (currentTooltip != null) {
currentTooltip.hide();
}
} catch (Exception e) {
System.err.println("❌ Force hide failed: " + e.getMessage());
} finally {
currentTooltip = null;
isShowing = false;
currentTargetNode = null;
}
}
private void cancelShowTask() {
if (showTask != null && !showTask.isDone()) {
showTask.cancel(false);
isScheduledToShow = false;
}
}
private void cancelHideTask() {
if (hideTask != null && !hideTask.isDone()) {
hideTask.cancel(false);
isScheduledToHide = false;
}
}
public boolean isTooltipShowing() {
return isShowing;
}
private void createAndShowTooltip(Node targetNode, AlgorithmInfo algorithmInfo) {
try {
// Create new tooltip
currentTooltip = createTooltipPopup(algorithmInfo);
// Position tooltip
positionTooltip(currentTooltip, targetNode);
// Show with animation
showWithAnimation(currentTooltip, targetNode);
System.out.println("✅ Tooltip shown for: " + algorithmInfo.getTitle());
} catch (Exception e) {
System.err.println("❌ Error creating tooltip: " + e.getMessage());
e.printStackTrace();
}
}
private Popup createTooltipPopup(AlgorithmInfo info) {
Popup popup = new Popup();
popup.setAutoHide(false);
popup.setConsumeAutoHidingEvents(false);
popup.setHideOnEscape(true);
// Create tooltip content
VBox content = new VBox();
content.setSpacing(14);
content.setPadding(new Insets(18));
content.setAlignment(Pos.TOP_LEFT);
content.setMaxWidth(380);
content.setPrefWidth(360);
// ✅ CRITICAL: Add mouse event handlers to prevent mouse dance
content.setOnMouseEntered(e -> {
cancelHideTask(); // Keep tooltip visible when mouse is on it
System.out.println("🖱️ Mouse entered tooltip - keeping visible");
});
content.setOnMouseExited(e -> {
hideCurrentTooltip(); // Hide when mouse leaves tooltip
System.out.println("🖱️ Mouse exited tooltip - scheduling hide");
});
// Apply theme-aware styling
updateTooltipTheme(content);
// Header with icon and title
HBox header = new HBox(12);
header.setAlignment(Pos.CENTER_LEFT);
FontIcon icon = new FontIcon();
icon.setIconLiteral(info.getIconLiteral());
icon.setIconSize(26);
icon.getStyleClass().add("tooltip-icon");
Label titleLabel = new Label(info.getTitle());
titleLabel.getStyleClass().add("tooltip-title");
header.getChildren().addAll(icon, titleLabel);
// Description
Label descriptionLabel = new Label(info.getDescription());
descriptionLabel.getStyleClass().add("tooltip-description");
descriptionLabel.setWrapText(true);
// Use Cases Section
Label useCasesTitle = new Label("Common Use Cases:");
useCasesTitle.getStyleClass().add("tooltip-section-title");
Label useCasesLabel = new Label(info.getUseCases());
useCasesLabel.getStyleClass().add("tooltip-text");
useCasesLabel.setWrapText(true);
// Time Complexity
HBox complexityBox = new HBox(10);
complexityBox.setAlignment(Pos.CENTER_LEFT);
Label complexityTitle = new Label("Time Complexity:");
complexityTitle.getStyleClass().add("tooltip-section-title");
Label complexityLabel = new Label(info.getTimeComplexity());
complexityLabel.getStyleClass().add("tooltip-complexity");
complexityBox.getChildren().addAll(complexityTitle, complexityLabel);
// Space Complexity
HBox spaceComplexityBox = new HBox(10);
spaceComplexityBox.setAlignment(Pos.CENTER_LEFT);
Label spaceTitle = new Label("Space Complexity:");
spaceTitle.getStyleClass().add("tooltip-section-title");
Label spaceLabel = new Label(info.getSpaceComplexity());
spaceLabel.getStyleClass().add("tooltip-complexity");
spaceComplexityBox.getChildren().addAll(spaceTitle, spaceLabel);
content.getChildren().addAll(
header,
descriptionLabel,
useCasesTitle,
useCasesLabel,
complexityBox,
spaceComplexityBox
);
popup.getContent().add(content);
return popup;
}
private void updateTooltipTheme(VBox content) {
boolean isDarkMode = ThemeManager.getInstance().isDarkMode();
if (isDarkMode) {
content.setStyle(
"-fx-background-color: #1a202c; " +
"-fx-border-color: #4a5568; " +
"-fx-border-width: 1px; " +
"-fx-background-radius: 12px; " +
"-fx-border-radius: 12px; " +
"-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.8), 20, 0, 0, 8);" +
"-fx-background-insets: 0;" +
"-fx-border-insets: 0;"
);
content.getStyleClass().clear();
content.getStyleClass().addAll("tooltip-container", "dark-theme");
} else {
content.setStyle(
"-fx-background-color: #ffffff; " +
"-fx-border-color: #cbd5e0; " +
"-fx-border-width: 1px; " +
"-fx-background-radius: 12px; " +
"-fx-border-radius: 12px; " +
"-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.15), 20, 0, 0, 8);" +
"-fx-background-insets: 0;" +
"-fx-border-insets: 0;"
);
content.getStyleClass().clear();
content.getStyleClass().addAll("tooltip-container", "light-theme");
}
}
private void positionTooltip(Popup popup, Node targetNode) {
try {
double nodeX = targetNode.localToScreen(targetNode.getBoundsInLocal()).getMinX();
double nodeY = targetNode.localToScreen(targetNode.getBoundsInLocal()).getMinY();
double nodeWidth = targetNode.getBoundsInLocal().getWidth();
double nodeHeight = targetNode.getBoundsInLocal().getHeight();
// ✅ SIMPLE & RELIABLE: Basic positioning with larger offsets
double tooltipX = nodeX + nodeWidth + 35; // Increased offset
double tooltipY = nodeY - 20; // Slightly higher
// Screen bounds checking
double screenWidth = javafx.stage.Screen.getPrimary().getVisualBounds().getWidth();
double screenHeight = javafx.stage.Screen.getPrimary().getVisualBounds().getHeight();
// Intelligent positioning
if (tooltipX + 400 > screenWidth) {
tooltipX = nodeX - 435; // Show on left with larger gap
}
if (tooltipY + 280 > screenHeight) {
tooltipY = screenHeight - 300;
}
tooltipX = Math.max(20, tooltipX);
tooltipY = Math.max(20, tooltipY);
popup.setX(tooltipX);
popup.setY(tooltipY);
} catch (Exception e) {
System.err.println("❌ Error positioning tooltip: " + e.getMessage());
}
}
private void showWithAnimation(Popup popup, Node targetNode) {
try {
popup.show(targetNode.getScene().getWindow());
// ✅ ENHANCED: Set transparency after show
Platform.runLater(() -> {
try {
if (popup.getScene() != null) {
popup.getScene().setFill(null);
popup.getScene().getRoot().setStyle("-fx-background-color: transparent;");
}
} catch (Exception e) {
// Ignore transparency errors
}
});
isShowing = true;
VBox content = (VBox) popup.getContent().get(0);
content.setOpacity(0.0);
content.setScaleX(0.85);
content.setScaleY(0.85);
if (showAnimation != null && showAnimation.getStatus() == Animation.Status.RUNNING) {
showAnimation.stop();
}
showAnimation = new Timeline();
showAnimation.getKeyFrames().addAll(
new KeyFrame(Duration.ZERO,
new KeyValue(content.opacityProperty(), 0.0),
new KeyValue(content.scaleXProperty(), 0.85),
new KeyValue(content.scaleYProperty(), 0.85)
),
new KeyFrame(Duration.millis(300),
new KeyValue(content.opacityProperty(), 1.0, Interpolator.EASE_OUT),
new KeyValue(content.scaleXProperty(), 1.0, Interpolator.EASE_OUT),
new KeyValue(content.scaleYProperty(), 1.0, Interpolator.EASE_OUT)
)
);
showAnimation.play();
} catch (Exception e) {
System.err.println("❌ Error showing tooltip animation: " + e.getMessage());
isShowing = false;
}
}
private void hideWithAnimation(Popup popup) {
try {
if (showAnimation != null && showAnimation.getStatus() == Animation.Status.RUNNING) {
showAnimation.stop();
}
VBox content = (VBox) popup.getContent().get(0);
if (hideAnimation != null && hideAnimation.getStatus() == Animation.Status.RUNNING) {
hideAnimation.stop();
}
hideAnimation = new Timeline();
hideAnimation.getKeyFrames().addAll(
new KeyFrame(Duration.ZERO,
new KeyValue(content.opacityProperty(), content.getOpacity()),
new KeyValue(content.scaleXProperty(), content.getScaleX()),
new KeyValue(content.scaleYProperty(), content.getScaleY())
),
new KeyFrame(Duration.millis(200),
new KeyValue(content.opacityProperty(), 0.0, Interpolator.EASE_IN),
new KeyValue(content.scaleXProperty(), 0.85, Interpolator.EASE_IN),
new KeyValue(content.scaleYProperty(), 0.85, Interpolator.EASE_IN)
)
);
hideAnimation.setOnFinished(e -> {
try {
popup.hide();
currentTooltip = null;
isShowing = false;
currentTargetNode = null;
} catch (Exception ex) {
System.err.println("❌ Error in hide animation finish: " + ex.getMessage());
forceHideTooltip();
}
});
hideAnimation.play();
} catch (Exception e) {
System.err.println("❌ Error hiding tooltip animation: " + e.getMessage());
forceHideTooltip();
}
}
public void shutdown() {
try {
cancelShowTask();
cancelHideTask();
hideCurrentTooltipImmediate();
scheduler.shutdown();
if (!scheduler.awaitTermination(1, TimeUnit.SECONDS)) {
scheduler.shutdownNow();
}
} catch (InterruptedException e) {
scheduler.shutdownNow();
Thread.currentThread().interrupt();
}
}
public static class AlgorithmInfo {
private final String title;
private final String description;
private final String useCases;
private final String timeComplexity;
private final String spaceComplexity;
private final String iconLiteral;
public AlgorithmInfo(String title, String description, String useCases,
String timeComplexity, String spaceComplexity, String iconLiteral) {
this.title = title;
this.description = description;
this.useCases = useCases;
this.timeComplexity = timeComplexity;
this.spaceComplexity = spaceComplexity;
this.iconLiteral = iconLiteral;
}
public String getTitle() { return title; }
public String getDescription() { return description; }
public String getUseCases() { return useCases; }
public String getTimeComplexity() { return timeComplexity; }
public String getSpaceComplexity() { return spaceComplexity; }
public String getIconLiteral() { return iconLiteral; }
}
}