Skip to content

Commit

Permalink
fix #1008 New Delayed Tooltip Shows Up Even If Mouse is No Longer Hov…
Browse files Browse the repository at this point in the history
…ering it
  • Loading branch information
vegegoku committed Feb 12, 2025
1 parent 706a06b commit acaad45
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.dominokit.domino.ui.popover;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static org.dominokit.domino.ui.collapsible.Collapsible.DUI_COLLAPSED;
import static org.dominokit.domino.ui.utils.Domino.*;

Expand Down Expand Up @@ -77,6 +78,7 @@ public abstract class BasePopover<T extends BasePopover<T>>
private boolean closeOnBlur;
private PopoverConfig ownConfig;
private int openDelay;
protected DelayedExecution delayedExecution;

/**
* Constructs a new BasePopover associated with the provided target element. BasePopover is the
Expand Down Expand Up @@ -219,7 +221,10 @@ public T open() {
*/
protected void doOpen() {
if (getOpenDelay() > 0) {
DelayedExecution.execute(this::openPopover, getOpenDelay());
if (nonNull(this.delayedExecution)) {
this.delayedExecution.cancel();
}
this.delayedExecution = DelayedExecution.execute(this::openPopover, getOpenDelay());
} else {
openPopover();
}
Expand Down Expand Up @@ -277,6 +282,9 @@ protected void doClose() {
body().removeEventListener(EventType.keydown.getName(), closeListener);
getConfig().getZindexManager().onPopupClose(this);
triggerCloseListeners((T) this);
if (nonNull(this.delayedExecution)) {
this.delayedExecution.cancel();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.dominokit.domino.ui.popover;

import static elemental2.dom.DomGlobal.document;
import static java.util.Objects.nonNull;
import static org.dominokit.domino.ui.dialogs.ModalBackDrop.DUI_REMOVE_TOOLTIPS;
import static org.dominokit.domino.ui.utils.Domino.*;
import static org.dominokit.domino.ui.utils.Domino.body;
Expand Down Expand Up @@ -154,6 +155,9 @@ protected EventListener getCloseListener() {
@Override
protected Tooltip closeOthers(String sourceId) {
ModalBackDrop.INSTANCE.closeTooltips(sourceId);
if (nonNull(this.delayedExecution)) {
this.delayedExecution.cancel();
}
return this;
}

Expand All @@ -171,5 +175,6 @@ protected void doOpen() {
public void detach() {
removeHandler.accept(this);
remove();
this.delayedExecution.cancel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,45 @@
*/
public class DelayedExecution {

private final Timer timer;
private final int delay;

private DelayedExecution(DelayedAction delayedAction, int delay) {
this.timer =
new Timer() {
@Override
public void run() {
delayedAction.doAction();
}
};
this.delay = delay;
this.timer.schedule(delay);
}

public DelayedExecution cancel() {
this.timer.cancel();
return this;
}

public DelayedExecution reschedule() {
this.timer.schedule(delay);
return this;
}

public DelayedExecution reschedule(int delay) {
this.timer.schedule(delay);
return this;
}

/**
* Executes the specified {@code delayedAction} after the specified delay defined in {@link
* DelayedActionConfig#getDelayedExecutionDefaultDelay()}.
*
* @param delayedAction The action to be executed after the delay.
*/
public static void execute(DelayedAction delayedAction) {
execute(delayedAction, DominoUIConfig.CONFIG.getUIConfig().getDelayedExecutionDefaultDelay());
public static DelayedExecution execute(DelayedAction delayedAction) {
return execute(
delayedAction, DominoUIConfig.CONFIG.getUIConfig().getDelayedExecutionDefaultDelay());
}

/**
Expand All @@ -50,13 +81,8 @@ public static void execute(DelayedAction delayedAction) {
* @param delayedAction The action to be executed after the delay.
* @param delay The delay in milliseconds before executing the action.
*/
public static void execute(DelayedAction delayedAction, int delay) {
new Timer() {
@Override
public void run() {
delayedAction.doAction();
}
}.schedule(delay);
public static DelayedExecution execute(DelayedAction delayedAction, int delay) {
return new DelayedExecution(delayedAction, delay);
}

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

0 comments on commit acaad45

Please sign in to comment.