forked from qupath/qupath-extension-training
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverlayHighlight.java
More file actions
272 lines (230 loc) · 9.48 KB
/
OverlayHighlight.java
File metadata and controls
272 lines (230 loc) · 9.48 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
package qupath.fx.controls.tour;
import javafx.animation.Transition;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;
import javafx.util.Duration;
import qupath.fx.utils.FXUtils;
import java.util.List;
/**
* Manage a window that can act as an overlay to highlight GUI elements.
* <p>
* Currently, this works by creating a transparent stage with a rectangle that can be moved and resized to highlight.
* In the future, this implementation might be changed (e.g. to apply CSS to the highlighted nodes directly).
*/
class OverlayHighlight implements TourHighlight {
private Stage stage;
private Rectangle rectangle;
private final BooleanProperty animateProperty = new SimpleBooleanProperty(true);
private final ChangeListener<Number> windowMoveListener = this::handleStageMoved;
private final ChangeListener<Number> windowResizeListener = this::handleStageResized;
/**
* Create a new highlighter.
*/
public OverlayHighlight() {}
private void handleStageMoved(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
if (stage != null && stage.isShowing())
hide();
}
private void handleStageResized(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
if (stage != null && stage.isShowing())
hide();
}
private void attachWindowListener(Window stage) {
stage.xProperty().addListener(windowMoveListener);
stage.yProperty().addListener(windowMoveListener);
stage.widthProperty().addListener(windowResizeListener);
stage.heightProperty().addListener(windowResizeListener);
}
private void detachWindowListener(Window stage) {
stage.xProperty().removeListener(windowMoveListener);
stage.yProperty().removeListener(windowMoveListener);
stage.widthProperty().removeListener(windowResizeListener);
stage.heightProperty().removeListener(windowResizeListener);
}
/**
* Hide the highlight window.
*/
public void hide() {
if (stage != null) {
stage.hide();
}
}
/**
* Show the highlight window, if available.
*/
public void show() {
if (stage != null) {
stage.show();
}
}
/**
* Get property to control whether highlights should animate when moving.
*/
public BooleanProperty animateProperty() {
return animateProperty;
}
private boolean initialize(Window owner) {
if (owner == null)
return false;
var stage = new Stage();
stage.initStyle(StageStyle.TRANSPARENT);
stage.initOwner(owner);
attachWindowListener(owner);
var rect = new Rectangle();
rect.getStyleClass().addAll("tour-highlight-rect");
var pane = new BorderPane(rect);
pane.getStyleClass().setAll("tour-highlight-pane");
// TODO: Consider Setting transparent because I'd like (I think) clicks to still pass through whatever is highlighted.
// I can only confirm this doesn't work on macOS though... possibly because of
// https://bugs.openjdk.org/browse/JDK-8088104
// rect.setMouseTransparent(true);
// pane.setMouseTransparent(true);
rect.setOnMouseClicked(this::handleMouseClick);
var scene = new Scene(pane, Color.TRANSPARENT);
// This was previously used to find highlight windows to close, but may no longer be needed
stage.getProperties().put("_INSTRUCTION_HIGHLIGHT", true);
stage.setScene(scene);
scene.getStylesheets().add(OverlayHighlight.class.getClassLoader().getResource("css/tour.css").toExternalForm());
this.rectangle = rect;
this.stage = stage;
return true;
}
private void handleMouseClick(MouseEvent event) {
if (stage != null)
stage.hide();
}
/**
* Ensure that we have a highlight stage that shares the same owner as the provided node,
* or default owner if no owner could be found.
*
* @param node
* @return true if the stage is initialized and has the correct owner, or false if no owner is found
*/
private boolean ensureInitializedForOwner(Node node) {
var owner = node == null ? null : FXUtils.getWindow(node);
if (stage != null) {
if (stage.getOwner() != owner) {
stage.hide();
if (stage.getOwner() != null)
detachWindowListener(stage.getOwner());
stage = null;
}
}
if (stage == null)
return initialize(owner);
else
return owner != null;
}
/**
* Highlight a collection of nodes.
* <ul>
* <li>If a single node is provided, then the highlight window is shown around it.</li>
* <li>If multiple nodes are provided, then the highlight is the bounding box of all nodes.</li>
* <li>If no nodes are provided, any existing highlight window is hidden.</li>
* </ul>
* Note that all non-visible nodes are ignored, but if a node is within a tab pane then this class will attempt
* to ensure that the parent tab is shown.
* @param nodes
*/
@Override
public void highlightNodes(List<? extends Node> nodes) {
highlightNodes(nodes, animateProperty.get());
}
private void highlightNodes(List<? extends Node> nodes, boolean doAnimate) {
var lastFocusedWindow = findFocusedWindow();
nodes = nodes.stream()
.filter(Node::isVisible)
.toList();
if (nodes.isEmpty()) {
hide();
return;
}
// Try to ensure any tab is visible
// (We assume that, if we have multiple nodes, all are in the same tab)
var firstNode = nodes.getFirst();
// This can occur whenever we're part of a toolbar overflow
if (!firstNode.isVisible() || (firstNode.getParent() != null && !firstNode.getParent().isVisible())) {
hide();
return;
}
// Ensure we have a stage with the required owner window,
// and return if there is no owner to be found
if (!ensureInitializedForOwner(firstNode)) {
hide();
return;
}
var bounds = TourUtils.computeScreenBounds(nodes);
double pad = 4;
// Target x,y for the stage - allow padding + 1 extra pixel for the stage itself
// (this seems to give better centering of the highlights, at least on macOS)
double targetX = bounds.getMinX() - pad - 1;
double targetY = bounds.getMinY() - pad - 1;
if (!doAnimate || !stage.isShowing() || rectangle.getWidth() == 0 || rectangle.getHeight() == 0) {
double newWidth = bounds.getWidth() + pad * 2;
double newHeight = bounds.getHeight() + pad * 2;
if (rectangle.getWidth() != newWidth || rectangle.getHeight() != newHeight) {
stage.hide();
rectangle.setWidth(bounds.getWidth() + pad * 2);
rectangle.setHeight(bounds.getHeight() + pad * 2);
}
stage.setX(targetX);
stage.setY(targetY);
} else {
// I wasn't able to get animation working for both stage x,y location and rectangle width,height -
// there seemed to be a bug whereby the simultaneous changing of the width,height resulted in the
// x,y coordinates being displaced.
rectangle.setWidth(bounds.getWidth() + 2 * pad);
rectangle.setHeight(bounds.getHeight() + 2 * pad);
stage.sizeToScene();
var animation = new HighlightTransition(stage, Duration.millis(300), targetX, targetY);
animation.playFromStart();
}
if (!stage.isShowing()) {
stage.show();
// We don't want to steal focus from the user
if (lastFocusedWindow != null)
lastFocusedWindow.requestFocus();
}
}
private static Window findFocusedWindow() {
return Window.getWindows()
.stream()
.filter(Window::isFocused)
.findFirst()
.orElse(null);
}
/**
* Transition to animate moving a stage to a target X and Y.
* Note that Stage.setX() and Stage.setY() ominously report that they may be ignored on some platforms.
*/
private static class HighlightTransition extends Transition {
private final Stage stage;
private final double startX, startY, targetX, targetY;
private HighlightTransition(Stage stage, Duration cycleDuration, double targetX, double targetY) {
this.stage = stage;
this.startX = stage.getX();
this.startY = stage.getY();
this.targetX = targetX;
this.targetY = targetY;
setCycleDuration(cycleDuration);
}
@Override
protected void interpolate(double frac) {
double newX = startX + frac * (targetX - startX);
double newY = startY + frac * (targetY - startY);
stage.setX(newX);
stage.setY(newY);
}
}
}