Skip to content

Commit

Permalink
Close Notification onClick only not onDrag
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelrahmanBayoumi committed Sep 14, 2022
1 parent 2fd1ae8 commit c741f21
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.bayoumi.controllers.notification;

import com.bayoumi.util.gui.ClickHandlerAndIgnoreDrag;
import com.bayoumi.util.gui.Draggable;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;

Expand Down Expand Up @@ -39,6 +41,12 @@ public void initialize(URL location, ResourceBundle resources) {
closeButton.setVisible(false);
parent.setOnMouseEntered(event -> closeButton.setVisible(true));
parent.setOnMouseExited(event -> closeButton.setVisible(false));

parent.addEventHandler(MouseEvent.ANY, new ClickHandlerAndIgnoreDrag(e -> {
if (closeButton != null) {
closeButton.fire();
}
}));
}

public void setData(String msg, Image image) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.bayoumi.util.gui;

import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;

public class ClickHandlerAndIgnoreDrag implements EventHandler<MouseEvent> {

private final EventHandler<MouseEvent> onClickedEventHandler;

private boolean dragging = false;

public ClickHandlerAndIgnoreDrag(EventHandler<MouseEvent> onClickedEventHandler) {
this.onClickedEventHandler = onClickedEventHandler;
}

@Override
public void handle(MouseEvent event) {
if (event.getEventType() == MouseEvent.MOUSE_PRESSED) {
dragging = false;
} else if (event.getEventType() == MouseEvent.DRAG_DETECTED) {
dragging = true;
} else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
//maybe filter on dragging (== true)
} else if (event.getEventType() == MouseEvent.MOUSE_CLICKED) {
if (!dragging) {
onClickedEventHandler.handle(event);
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/
package com.bayoumi.util.gui.notfication;

import com.bayoumi.util.gui.ClickHandlerAndIgnoreDrag;
import impl.org.controlsfx.skin.NotificationBar;
import javafx.animation.*;
import javafx.collections.FXCollections;
Expand All @@ -38,6 +39,7 @@
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.*;
import javafx.util.Duration;
import org.controlsfx.control.action.Action;
Expand Down Expand Up @@ -540,7 +542,7 @@ public void relocateInParent(double x, double y) {
};
notificationBar.getStyleClass().addAll(notificationToShow.styleClass);

notificationBar.setOnMouseClicked(e -> {
notificationBar.addEventHandler(MouseEvent.ANY, new ClickHandlerAndIgnoreDrag(e -> {
if (notificationToShow.onAction != null) {
if (notificationToShow.closeCallback != null) {
notificationToShow.closeCallback.run();
Expand All @@ -551,8 +553,7 @@ public void relocateInParent(double x, double y) {
ActionEvent actionEvent = new ActionEvent(notificationBar, notificationBar);
notificationToShow.onAction.handle(actionEvent);
}
});

}));

popup.getContent().add(notificationBar);
popup.show(owner, 0, 0);
Expand Down

0 comments on commit c741f21

Please sign in to comment.