Skip to content

Commit acaad45

Browse files
committed
fix #1008 New Delayed Tooltip Shows Up Even If Mouse is No Longer Hovering it
1 parent 706a06b commit acaad45

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

domino-ui/src/main/java/org/dominokit/domino/ui/popover/BasePopover.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.dominokit.domino.ui.popover;
1717

1818
import static java.util.Objects.isNull;
19+
import static java.util.Objects.nonNull;
1920
import static org.dominokit.domino.ui.collapsible.Collapsible.DUI_COLLAPSED;
2021
import static org.dominokit.domino.ui.utils.Domino.*;
2122

@@ -77,6 +78,7 @@ public abstract class BasePopover<T extends BasePopover<T>>
7778
private boolean closeOnBlur;
7879
private PopoverConfig ownConfig;
7980
private int openDelay;
81+
protected DelayedExecution delayedExecution;
8082

8183
/**
8284
* Constructs a new BasePopover associated with the provided target element. BasePopover is the
@@ -219,7 +221,10 @@ public T open() {
219221
*/
220222
protected void doOpen() {
221223
if (getOpenDelay() > 0) {
222-
DelayedExecution.execute(this::openPopover, getOpenDelay());
224+
if (nonNull(this.delayedExecution)) {
225+
this.delayedExecution.cancel();
226+
}
227+
this.delayedExecution = DelayedExecution.execute(this::openPopover, getOpenDelay());
223228
} else {
224229
openPopover();
225230
}
@@ -277,6 +282,9 @@ protected void doClose() {
277282
body().removeEventListener(EventType.keydown.getName(), closeListener);
278283
getConfig().getZindexManager().onPopupClose(this);
279284
triggerCloseListeners((T) this);
285+
if (nonNull(this.delayedExecution)) {
286+
this.delayedExecution.cancel();
287+
}
280288
}
281289

282290
/**

domino-ui/src/main/java/org/dominokit/domino/ui/popover/Tooltip.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.dominokit.domino.ui.popover;
1717

1818
import static elemental2.dom.DomGlobal.document;
19+
import static java.util.Objects.nonNull;
1920
import static org.dominokit.domino.ui.dialogs.ModalBackDrop.DUI_REMOVE_TOOLTIPS;
2021
import static org.dominokit.domino.ui.utils.Domino.*;
2122
import static org.dominokit.domino.ui.utils.Domino.body;
@@ -154,6 +155,9 @@ protected EventListener getCloseListener() {
154155
@Override
155156
protected Tooltip closeOthers(String sourceId) {
156157
ModalBackDrop.INSTANCE.closeTooltips(sourceId);
158+
if (nonNull(this.delayedExecution)) {
159+
this.delayedExecution.cancel();
160+
}
157161
return this;
158162
}
159163

@@ -171,5 +175,6 @@ protected void doOpen() {
171175
public void detach() {
172176
removeHandler.accept(this);
173177
remove();
178+
this.delayedExecution.cancel();
174179
}
175180
}

domino-ui/src/main/java/org/dominokit/domino/ui/utils/DelayedExecution.java

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,45 @@
3434
*/
3535
public class DelayedExecution {
3636

37+
private final Timer timer;
38+
private final int delay;
39+
40+
private DelayedExecution(DelayedAction delayedAction, int delay) {
41+
this.timer =
42+
new Timer() {
43+
@Override
44+
public void run() {
45+
delayedAction.doAction();
46+
}
47+
};
48+
this.delay = delay;
49+
this.timer.schedule(delay);
50+
}
51+
52+
public DelayedExecution cancel() {
53+
this.timer.cancel();
54+
return this;
55+
}
56+
57+
public DelayedExecution reschedule() {
58+
this.timer.schedule(delay);
59+
return this;
60+
}
61+
62+
public DelayedExecution reschedule(int delay) {
63+
this.timer.schedule(delay);
64+
return this;
65+
}
66+
3767
/**
3868
* Executes the specified {@code delayedAction} after the specified delay defined in {@link
3969
* DelayedActionConfig#getDelayedExecutionDefaultDelay()}.
4070
*
4171
* @param delayedAction The action to be executed after the delay.
4272
*/
43-
public static void execute(DelayedAction delayedAction) {
44-
execute(delayedAction, DominoUIConfig.CONFIG.getUIConfig().getDelayedExecutionDefaultDelay());
73+
public static DelayedExecution execute(DelayedAction delayedAction) {
74+
return execute(
75+
delayedAction, DominoUIConfig.CONFIG.getUIConfig().getDelayedExecutionDefaultDelay());
4576
}
4677

4778
/**
@@ -50,13 +81,8 @@ public static void execute(DelayedAction delayedAction) {
5081
* @param delayedAction The action to be executed after the delay.
5182
* @param delay The delay in milliseconds before executing the action.
5283
*/
53-
public static void execute(DelayedAction delayedAction, int delay) {
54-
new Timer() {
55-
@Override
56-
public void run() {
57-
delayedAction.doAction();
58-
}
59-
}.schedule(delay);
84+
public static DelayedExecution execute(DelayedAction delayedAction, int delay) {
85+
return new DelayedExecution(delayedAction, delay);
6086
}
6187

6288
/** A functional interface representing an action to be executed after a delay. */

0 commit comments

Comments
 (0)