Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Dec 31, 2023
1 parent 95ea613 commit a073391
Showing 1 changed file with 66 additions and 88 deletions.
154 changes: 66 additions & 88 deletions src/main/java/viewtify/ViewtyDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.function.Supplier;

import javafx.beans.property.DoubleProperty;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
Expand Down Expand Up @@ -50,6 +49,7 @@
import kiss.Model;
import kiss.Variable;
import kiss.WiseConsumer;
import kiss.WiseRunnable;
import psychopath.Directory;
import psychopath.File;
import psychopath.Locator;
Expand All @@ -71,6 +71,8 @@
*/
public final class ViewtyDialog<T> {

private static final double animeation = 0.2;

/** The associated window. */
private Window baseWindow;

Expand Down Expand Up @@ -110,12 +112,6 @@ public final class ViewtyDialog<T> {
/** The dialog effect. */
private boolean fadable;

/** The dialog effect. */
private double fadeTime = 0.2;

/** The dialog effect. */
private double slideDistance = 10;

/** The dialog effect. */
private boolean blurable;

Expand Down Expand Up @@ -259,16 +255,7 @@ public ViewtyDialog<T> disableSystemButtonOrder() {
* @return
*/
public ViewtyDialog<T> fadable() {
return fadable(fadeTime);
}

/**
* Congifure the loading effect.
*
* @return
*/
public ViewtyDialog<T> fadable(double duration) {
return fadable(duration, sliding, slideDistance);
return fadable(sliding);
}

/**
Expand All @@ -277,37 +264,8 @@ public ViewtyDialog<T> fadable(double duration) {
* @return
*/
public ViewtyDialog<T> fadable(Side sliding) {
return fadable(fadeTime, sliding);
}

/**
* Congifure the loading effect.
*
* @return
*/
public ViewtyDialog<T> fadable(Side sliding, double distance) {
return fadable(fadeTime, sliding, distance);
}

/**
* Congifure the loading effect.
*
* @return
*/
public ViewtyDialog<T> fadable(double duration, Side sliding) {
return fadable(duration, sliding, slideDistance);
}

/**
* Congifure the loading effect.
*
* @return
*/
public ViewtyDialog<T> fadable(double duration, Side sliding, double distance) {
this.fadable = true;
this.fadeTime = duration;
this.sliding = sliding;
this.slideDistance = distance;
return this;
}

Expand Down Expand Up @@ -486,7 +444,7 @@ public <V, D extends DialogView<V>> Variable<V> show(D view, WiseConsumer<D> ini
Window window = dialogPane.getScene().getWindow();

if (fadable) {
double distance = sliding == null ? 0 : sliding == Side.TOP || sliding == Side.LEFT ? slideDistance : -slideDistance;
double distance = sliding == null ? 0 : sliding == Side.TOP || sliding == Side.LEFT ? 10 : -10;
DoubleProperty location = sliding == null || sliding.isVertical() ? Viewtify.property(window::getX, window::setX)
: Viewtify.property(window::getY, window::setY);

Expand All @@ -502,15 +460,15 @@ public <V, D extends DialogView<V>> Variable<V> show(D view, WiseConsumer<D> ini
window.setOpacity(0);
location.set(now - distance);

Anime.define().duration(fadeTime).effect(window.opacityProperty(), 1).effect(location, now).run();
Anime.define().duration(animeation).effect(window.opacityProperty(), 1).effect(location, now).run();
});

// hidden effect
EventHandler<Event> hidden = e -> {
if (window.getOpacity() != 0) {
e.consume();
Anime.define()
.duration(fadeTime)
.duration(animeation)
.effect(window.opacityProperty(), 0.5)
.effect(location, location.get() + distance)
.run(window::hide);
Expand Down Expand Up @@ -657,50 +615,74 @@ public Variable<String> showInput(String message, WiseConsumer<UIText<String>> s
});
}

private static long popupable;
private static Popup latestPopup;

/**
* @param builder
*/
public void showPopup(Supplier<UserInterfaceProvider<? extends Node>> builder) {
if (System.currentTimeMillis() < popupable) {
return;
WiseRunnable showPopup = () -> {
unblockable().disableButtons(true).style(StageStyle.TRANSPARENT).modal(Modality.NONE).show(new Popup(builder));
};

if (latestPopup == null) {
showPopup.run();
} else {
latestPopup.requestClosing(latestPopup.builder == builder ? null : showPopup);
}
}

unblockable().disableButtons(true).style(StageStyle.TRANSPARENT).modal(Modality.NONE).show(new DialogView<>() {
/**
* Special popup dialog.
*/
private static class Popup extends DialogView<Object> {

@Override
protected ViewDSL declareUI() {
return new ViewDSL() {
{
$(builder.get());
}
};
}
private final Supplier<UserInterfaceProvider<? extends Node>> builder;

@Override
protected void initialize() {
pane.getScene().setFill(null);
pane.getStyleClass().add("dialog-popup");
private WiseRunnable finisher;

Window window = pane.getScene().getWindow();
window.focusedProperty().addListener((object, old, focused) -> {
if (!focused) {
hidePopup(window, fadeTime);
}
});
window.addEventHandler(WindowEvent.WINDOW_HIDDEN, e -> {
});
}
});
}
/**
* @param builder
*/
private Popup(Supplier<UserInterfaceProvider<? extends Node>> builder) {
this.builder = builder;
}

/**
* Close the current popup window.
*/
private static void hidePopup(Window window, double fadeTime) {
if (window != null) {
popupable = System.currentTimeMillis() + (long) (fadeTime * 1000);
@Override
protected ViewDSL declareUI() {
return new ViewDSL() {
{
$(builder.get());
}
};
}

@Override
protected void initialize() {
latestPopup = this;

pane.getScene().setFill(null);
pane.getStyleClass().add("dialog-popup");

Window window = pane.getScene().getWindow();
window.focusedProperty().addListener((object, old, focused) -> {
if (!focused) {
requestClosing(null);
}
});
window.addEventHandler(WindowEvent.WINDOW_HIDDEN, e -> {
latestPopup = null;
if (finisher != null) finisher.run();
});
}

/**
* Request closing window.
*/
private void requestClosing(WiseRunnable finisher) {
if (finisher != null) this.finisher = finisher;

Window window = pane.getScene().getWindow();
window.fireEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSE_REQUEST));
}
}
Expand Down Expand Up @@ -828,12 +810,8 @@ private <V> Variable<V> showAndTell(Dialog<V> dialog, Supplier<V> defaultValue)
* Close all dialogs.
*/
public static void close() {
ObservableList<Window> windows = Window.getWindows();
for (int i = windows.size() - 1; 0 <= i; i--) {
Window window = windows.get(i);
if (window.getScene().getRoot() instanceof DialogPane) {
hidePopup(window, 0.2);
}
if (latestPopup != null) {
latestPopup.requestClosing(null);
}
}

Expand Down

0 comments on commit a073391

Please sign in to comment.