Skip to content

Commit

Permalink
feat: provide update notification
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Dec 11, 2023
1 parent 699a6c0 commit 06579d7
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 45 deletions.
61 changes: 37 additions & 24 deletions src/main/java/viewtify/Viewtify.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
import java.util.function.Supplier;
import java.util.stream.Collectors;

import com.sun.javafx.application.PlatformImpl;

import javafx.application.Platform;
import javafx.beans.InvalidationListener;
import javafx.beans.binding.DoubleExpression;
Expand Down Expand Up @@ -73,6 +71,9 @@
import javafx.stage.StageStyle;
import javafx.stage.Window;
import javafx.stage.WindowEvent;

import com.sun.javafx.application.PlatformImpl;

import kiss.Decoder;
import kiss.Disposable;
import kiss.Encoder;
Expand All @@ -95,8 +96,11 @@
import viewtify.ui.anime.Anime;
import viewtify.ui.helper.User;
import viewtify.ui.helper.UserActionHelper;
import viewtify.ui.toast.Toast;
import viewtify.ui.view.AppearanceSetting;
import viewtify.update.Blueprint;
import viewtify.update.Update;
import viewtify.update.UpdateSetting;

public final class Viewtify {

Expand Down Expand Up @@ -205,6 +209,9 @@ public Thread newThread(Runnable runnable) {
/** The configurable setting. */
private Class<? extends DesignScheme> scheme;

/** The configurable setting. */
private BiConsumer<Stage, Scene> initializer;

/** The configurable setting. */
private Variable<Class<? extends View>> opener = Variable.empty();

Expand All @@ -217,12 +224,6 @@ public Thread newThread(Runnable runnable) {
/** The configurable setting. */
private String title;

/** The configurable setting. */
private double x;

/** The configurable setting. */
private double y;

/** The configurable setting. */
private String version = "1.0.0";

Expand Down Expand Up @@ -297,7 +298,18 @@ public static synchronized Viewtify application() {
}

/**
* Configure the closing request.
* Configure the initialize phase.
*
* @param initializer
* @return
*/
public Viewtify onInitialize(BiConsumer<Stage, Scene> initializer) {
this.initializer = initializer;
return this;
}

/**
* Configure the opening request.
*
* @param opener
* @return
Expand Down Expand Up @@ -454,19 +466,6 @@ public Viewtify title(String title) {
return this;
}

/**
* Configure application location.
*
* @param x
* @param y
* @return
*/
public Viewtify location(double x, double y) {
this.x = x;
this.y = y;
return this;
}

/**
* Configure application update strategy.
*
Expand Down Expand Up @@ -550,8 +549,6 @@ public void activate(View application) {
private void activate(View application, boolean isOperner) {
View actual = isOperner ? I.make(opener.v) : application;
mainStage = new Stage(isOperner ? StageStyle.TRANSPARENT : stageStyle);
if (0 < x) mainStage.setX(x);
if (0 < y) mainStage.setY(y);

Scene scene = new Scene((Parent) actual.ui());

Expand All @@ -566,6 +563,10 @@ private void activate(View application, boolean isOperner) {
}
});

if (initializer != null) {
initializer.accept(mainStage, scene);
}

if (isOperner) {
scene.setFill(null);
mainStage.setOnHidden(e -> {
Expand Down Expand Up @@ -606,6 +607,18 @@ private void activate(View application, boolean isOperner) {
if (screen != null) screen.close();
});
}

// check update
UpdateSetting updater = Preferences.of(UpdateSetting.class);
if (updater.checkOnStartup.is(true) && Update.isAvailable(updateArchive)) {
I.schedule(1, TimeUnit.SECONDS).to(() -> {
Toast.show(I
.translate(Terminator, "A newer version is available. Would you like to update? [Update](0) [Don't now](1)"), hide -> {
hide.run();
Update.apply();
}, Runnable::run);
});
}
}

/**
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/viewtify/ui/toast/Toast.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import kiss.Disposable;
import kiss.I;
import kiss.Variable;
import kiss.WiseConsumer;
import stylist.Style;
import stylist.StyleDSL;
Expand Down Expand Up @@ -59,7 +60,22 @@ public static void show(String message, WiseConsumer<Runnable>... actions) {
if (setting.enable.is(true)) {
Notification notification = new Notification();
Runnable hide = () -> remove(notification);
notification.builder = () -> TextNotation.parse(message, I.signal(actions).map(x -> x.bindLast(hide)).toList());
notification.builder = () -> TextNotation
.parse(message, setting.width.v - styles.pad * 2, I.signal(actions).map(x -> x.bindLast(hide)).toList());

add(notification);
}
}

/**
* Show the specified node.
*/
public static void show(Variable<String> message, WiseConsumer<Runnable>... actions) {
if (setting.enable.is(true)) {
Notification notification = new Notification();
Runnable hide = () -> remove(notification);
notification.builder = () -> TextNotation
.parse(message, setting.width.v - styles.pad * 2, I.signal(actions).map(x -> x.bindLast(hide)).toList());

add(notification);
}
Expand Down Expand Up @@ -209,7 +225,7 @@ private synchronized Popup ui() {
if (ui == null) {
ui = new Popup();
VBox box = new VBox(builder.get());
StyleHelper.of(box).style(Styles.popup);
StyleHelper.of(box).style(styles.popup);
box.setMaxWidth(setting.width.v);
box.setMinWidth(setting.width.v);
box.setOpacity(setting.opacity.v / 100d);
Expand All @@ -231,10 +247,12 @@ private synchronized Popup ui() {
/**
* Notification style.
*/
private static interface Styles extends StyleDSL {
private static interface styles extends StyleDSL {

int pad = 15;

Style popup = () -> {
padding.vertical(15, px).horizontal(15, px);
padding.size(pad, px);
background.color("derive(-fx-control-inner-background, 10%)");
border.radius(5, px).color("-fx-light-text-color");
};
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/viewtify/update/Update.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;

import javafx.geometry.Rectangle2D;

import psychopath.Directory;
import psychopath.File;
import psychopath.Locator;
Expand Down Expand Up @@ -58,7 +59,7 @@ private static String validate(String archive) {
// ====================================
Blueprint origin = Blueprint.detect();
if (!file.isAfter(origin.root)) {
return "The latest version is used, no need to update.";
// return "The latest version is used, no need to update.";
}

// We can update
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/viewtify/update/UpdateSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,4 @@ public class UpdateSetting extends Preferences {

/** The enable status. */
public final Preference<Boolean> checkOnStartup = initialize(true);

/** The enable status. */
public final Preference<Boolean> applyAuto = initialize(false);
}
4 changes: 0 additions & 4 deletions src/main/java/viewtify/update/UpdateSettingView.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public class UpdateSettingView extends View {

private UICheckSwitch checkOnStartup;

private UICheckSwitch applyAuto;

private UILabel versionApp;

private UIButton confirm;
Expand All @@ -45,7 +43,6 @@ protected ViewDSL declareUI() {
{
$(vbox, () -> {
form(en("Confirm update on startup"), FormStyles.InputMin, checkOnStartup);
form(en("Apply update automatically"), FormStyles.InputMin, applyAuto);
form(en("Confirm update"), confirm);
form(en("Application"), versionApp);
form(en("Operating System"), versionOS);
Expand All @@ -72,7 +69,6 @@ protected void initialize() {
UpdateSetting setting = Preferences.of(UpdateSetting.class);

checkOnStartup.sync(setting.checkOnStartup);
applyAuto.sync(setting.applyAuto);
confirm.action(Update::apply);

I.schedule(0, 6, TimeUnit.HOURS, false).to(() -> {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/viewtify/update/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.text.Font;

import kiss.I;
import kiss.Variable;
import psychopath.Progress;
Expand Down Expand Up @@ -154,7 +155,10 @@ public static void main(String[] args) {
.use(I.env("Theme", Theme.Light))
.use(I.env("ThemeType", ThemeType.Flat))
.use(Font.font(I.env("Font"), I.env("FontSize", 12)))
.location(I.env("LocationX", 0d), I.env("LocationY", 0d))
.onInitialize((stage, scene) -> {
stage.setX(I.env("LocationX", 0d));
stage.setY(I.env("LocationY", 0d));
})
.activate(Updater.class);
}
}
61 changes: 53 additions & 8 deletions src/main/java/viewtify/util/TextNotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,29 @@ public static TextFlow parse(String message, WiseRunnable... actions) {
* @return
*/
public static TextFlow parse(String message, List<WiseRunnable> actions) {
return parse(message, -1, actions);
}

/**
* Parse as {@link TextFlow}.
*
* @param message A wiki-like notation text.
* @return
*/
public static TextFlow parse(String message, int width, WiseRunnable... actions) {
return parse(message, width, List.of(actions));
}

/**
* Parse as {@link TextFlow}.
*
* @param message A wiki-like notation text.
* @return
*/
public static TextFlow parse(String message, int width, List<WiseRunnable> actions) {
TextFlow flow = new TextFlow();
flow.setLineSpacing(2);
parse(flow.getChildren(), message, actions);
parse(flow.getChildren(), width, message, actions);
return flow;
}

Expand All @@ -111,18 +131,38 @@ public static TextFlow parse(Variable message, WiseRunnable... actions) {
* @return
*/
public static TextFlow parse(Variable message, List<WiseRunnable> actions) {
return parse(message, -1, actions);
}

/**
* Parse as {@link TextFlow}.
*
* @param message A wiki-like notation text.
* @return
*/
public static TextFlow parse(Variable message, int width, WiseRunnable... actions) {
return parse(message, width, List.of(actions));
}

/**
* Parse as {@link TextFlow}.
*
* @param message A wiki-like notation text.
* @return
*/
public static TextFlow parse(Variable message, int width, List<WiseRunnable> actions) {
TextFlow flow = new TextFlow();
flow.setLineSpacing(2);
ObservableList<Node> children = flow.getChildren();

message.observing().on(Viewtify.UIThread).to(text -> {
children.clear();
parse(children, I.transform(text, String.class), actions);
parse(children, width, I.transform(text, String.class), actions);
});
return flow;
}

private static void parse(ObservableList<Node> children, String message, List<WiseRunnable> actions) {
private static void parse(ObservableList<Node> children, int width, String message, List<WiseRunnable> actions) {
if (message == null) {
return;
}
Expand All @@ -138,7 +178,11 @@ private static void parse(ObservableList<Node> children, String message, List<Wi
case '[':
inLink = true;
if (builder.length() != 0) {
children.add(new Label(builder.toString()));
Label label = new Label(builder.toString());
label.setWrapText(true);
label.setMaxWidth(width);
System.out.println(builder + " " + width);
children.add(label);
builder = new StringBuilder();
}
break;
Expand Down Expand Up @@ -186,10 +230,11 @@ private static void parse(ObservableList<Node> children, String message, List<Wi
}
}

if (children.isEmpty()) {
children.add(new Label(builder.toString()));
} else if (builder.length() != 0) {
children.add(new Label(builder.toString()));
if (children.isEmpty() || builder.length() != 0) {
Label label = new Label(builder.toString());
label.setWrapText(true);
label.setMaxWidth(width);
children.add(label);
}
}
}

0 comments on commit 06579d7

Please sign in to comment.