Skip to content

Commit

Permalink
feat: enahnce updater
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Dec 11, 2023
1 parent 5be2534 commit 5ddb08e
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 96 deletions.
89 changes: 40 additions & 49 deletions src/main/java/viewtify/Viewtify.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
import viewtify.ui.helper.UserActionHelper;
import viewtify.ui.view.AppearanceSetting;
import viewtify.update.Blueprint;
import viewtify.update.Update;

public final class Viewtify {

Expand Down Expand Up @@ -133,9 +132,6 @@ public Thread newThread(Runnable runnable) {
/** The directory of user's preference. */
public static final Variable<Directory> UserPreference = Variable.empty();

/** The current theme. */
public static final Variable<Theme> CurrentTheme = Variable.of(Theme.Light);

static {
JUL.replace();

Expand Down Expand Up @@ -206,15 +202,6 @@ public Thread newThread(Runnable runnable) {
/** The configurable setting. */
private StageStyle stageStyle = StageStyle.DECORATED;

/** The configurable setting. */
private Theme theme = Theme.Light;

/** The configurable setting. */
private ThemeType themeType = ThemeType.Flat;

/** The configurable setting. */
private Font font = Font.getDefault();

/** The configurable setting. */
private Class<? extends DesignScheme> scheme;

Expand All @@ -230,6 +217,12 @@ 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 @@ -382,7 +375,7 @@ public Viewtify use(StageStyle style) {
*/
public Viewtify use(Theme theme) {
if (theme != null) {
this.theme = theme;
Preferences.of(AppearanceSetting.class).theme.setDefault(theme);
}
return this;
}
Expand All @@ -395,7 +388,7 @@ public Viewtify use(Theme theme) {
*/
public Viewtify use(ThemeType themeType) {
if (themeType != null) {
this.themeType = themeType;
Preferences.of(AppearanceSetting.class).themeType.setDefault(themeType);
}
return this;
}
Expand All @@ -408,7 +401,9 @@ public Viewtify use(ThemeType themeType) {
*/
public Viewtify use(Font font) {
if (font != null) {
this.font = font;
AppearanceSetting appearance = Preferences.of(AppearanceSetting.class);
appearance.font.setDefault(font.getFamily());
appearance.fontSize.setDefault((int) font.getSize());
}
return this;
}
Expand All @@ -426,6 +421,15 @@ public Viewtify design(Class<? extends DesignScheme> scheme) {
return this;
}

/**
* Get applicaiton metadata.
*
* @return
*/
public String icon() {
return icon;
}

/**
* Configure application icon.
*
Expand All @@ -450,6 +454,19 @@ 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 @@ -531,12 +548,10 @@ public void activate(View application) {
* @param application The application {@link View} to activate.
*/
private void activate(View application, boolean isOperner) {
boolean canUpdate = I.env("UpdateOnStartup", updateArchive != null);
boolean isUpdate = canUpdate && Update.isValid(updateArchive);

View actual = isUpdate ? new Empty() : isOperner ? I.make(opener.v) : application;
View actual = isOperner ? I.make(opener.v) : application;
mainStage = new Stage(isOperner ? StageStyle.TRANSPARENT : stageStyle);
if (isUpdate) mainStage.setOpacity(0);
if (0 < x) mainStage.setX(x);
if (0 < y) mainStage.setY(y);

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

Expand Down Expand Up @@ -627,9 +642,10 @@ private synchronized void initializeOnlyOnce(Class applicationClass) {
I.load(applicationClass);

// collect stylesheets for application
AppearanceSetting appearance = Preferences.of(AppearanceSetting.class);
stylesheets.add(Theme.locate("ui"));
stylesheets.add(viewtify.theme.location);
stylesheets.add(viewtify.themeType.location);
stylesheets.add(appearance.theme.v.location);
stylesheets.add(appearance.themeType.v.location);
stylesheets.add(Locator.file(CSSProcessor.pretty().scheme(scheme).formatTo(prefs + "/application.css")).externalForm());
stylesheets.add(writeFontStylesheet(null));

Expand Down Expand Up @@ -701,7 +717,7 @@ private void checkActivationPolicy(String prefs) {
* @return
*/
private String writeFontStylesheet(Font font) {
font = Objects.requireNonNullElse(font, this.font);
font = Objects.requireNonNullElse(font, Font.getDefault());

String prefs = I.env("PreferenceDirectory");
File css = Locator.file(prefs + "/font.css");
Expand Down Expand Up @@ -903,8 +919,6 @@ public static <V> ViewtyDialog<?> dialog() {
* @param theme
*/
public static void manage(Theme theme) {
CurrentTheme.set(theme);

inUI(() -> {
for (Window window : Window.getWindows()) {
Scene scene = window.getScene();
Expand Down Expand Up @@ -1644,27 +1658,4 @@ public Location decode(String value) {
return locator;
}
}

private class Empty extends View {

/**
* {@inheritDoc}
*/
@Override
protected ViewDSL declareUI() {
return new ViewDSL() {
{
$(vbox);
}
};
}

/**
* {@inheritDoc}
*/
@Override
protected void initialize() {
Update.apply(updateArchive, false);
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/viewtify/WizardDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import stylist.StyleDSL;
import stylist.value.Numeric;
import viewtify.ViewtyDialog.DialogView;
import viewtify.preference.Preferences;
import viewtify.ui.UIButton;
import viewtify.ui.UIGridView;
import viewtify.ui.UIHBox;
Expand All @@ -32,6 +33,7 @@
import viewtify.ui.anime.Anime;
import viewtify.ui.anime.SwapAnime;
import viewtify.ui.helper.User;
import viewtify.ui.view.AppearanceSetting;

class WizardDialog<V> extends DialogView<V> {

Expand Down Expand Up @@ -239,7 +241,7 @@ protected void initialize() {
*/
private void update(int next) {
// current selected
Theme theme = Viewtify.CurrentTheme.exact();
Theme theme = Preferences.of(AppearanceSetting.class).theme.exact();

if (step == next) {
Anime.define()
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/viewtify/ui/helper/LoaderEffect.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
import javafx.scene.text.FontSmoothingType;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;

import kiss.Disposable;
import kiss.I;
import viewtify.Viewtify;
import viewtify.preference.Preferences;
import viewtify.ui.view.AppearanceSetting;

class LoaderEffect extends Blend {

Expand Down Expand Up @@ -183,7 +184,7 @@ private WritableImage drawStripe(int width, int height, int frame) {

int stripeWidth = stripeWidthColor + stripeWidthTransparent;
int offset = frame % stripeWidth;
Color color = Viewtify.CurrentTheme.v.accent();
Color color = Preferences.of(AppearanceSetting.class).theme.exact().accent();

for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
Expand All @@ -208,7 +209,7 @@ private static synchronized WritableImage drawText() {
if (textImage == null) {
Text text = new Text("Loading...");
text.setFont(Font.font("System", FontWeight.LIGHT, 14));
text.setFill(Viewtify.CurrentTheme.v.text());
text.setFill(Preferences.of(AppearanceSetting.class).theme.exact().text());
text.setFontSmoothingType(FontSmoothingType.GRAY);

SnapshotParameters params = new SnapshotParameters();
Expand Down
41 changes: 26 additions & 15 deletions src/main/java/viewtify/update/Blueprint.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package viewtify.update;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import psychopath.Directory;
Expand All @@ -30,18 +31,21 @@ public abstract class Blueprint implements Serializable {
/** The application classpath. */
public String classPath = System.getProperty("java.class.path");

/** The environment variables. */
private Map<String, String> env = new HashMap();

/**
* Boot this application.
*/
public final boolean boot() {
return boot(Map.of());
return boot(env);
}

/**
* Boot this application.
*/
public final boolean boot(MonitorableTask<Progress> task) {
return boot(Map.of(Updater.class.getName(), MonitorableTask.store(task)));
return env(Updater.class.getName(), MonitorableTask.store(task)).boot();
}

/**
Expand All @@ -53,25 +57,18 @@ public final boolean boot(MonitorableTask<Progress> task) {
* Deactivate the current application and boot this application.
*/
public final boolean reboot() {
return reboot(Map.of("UpdateOnStartup", "false"));
try {
return env("UpdateOnStartup", "false").boot();
} finally {
Viewtify.application().deactivate();
}
}

/**
* Deactivate the current application and boot this application.
*/
public final boolean reboot(MonitorableTask<Progress> task) {
return reboot(Map.of(Updater.class.getName(), MonitorableTask.store(task), "UpdateOnStartup", "false"));
}

/**
* Deactivate the current application and boot this application.
*/
public final boolean reboot(Map<String, String> params) {
try {
return boot(params);
} finally {
Viewtify.application().deactivate();
}
return env(Updater.class.getName(), MonitorableTask.store(task)).reboot();
}

/**
Expand All @@ -81,6 +78,20 @@ public final boolean reboot(Map<String, String> params) {
*/
public abstract Blueprint updater();

/**
* Assign environment variable.
*
* @param key A key.
* @param value A value.
* @return Chainable API
*/
public final Blueprint env(String key, String value) {
if (key != null && value != null) {
env.put(key, value);
}
return this;
}

/**
* Detect the current site.
*
Expand Down
51 changes: 33 additions & 18 deletions src/main/java/viewtify/update/Update.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@

import java.util.List;

import javafx.geometry.Rectangle2D;
import psychopath.Directory;
import psychopath.File;
import psychopath.Locator;
import viewtify.Viewtify;
import viewtify.preference.Preferences;
import viewtify.ui.view.AppearanceSetting;
import viewtify.util.ScreenSelector;

public class Update {

Expand Down Expand Up @@ -105,24 +109,35 @@ public static void apply(String archive, boolean forcibly) {
.disableCloseButton(forcibly)
.show(new Updater(forcibly))
.to(tasks -> {
origin.updater().reboot(monitor -> {
monitor.message("Installing the new version, please wait a minute.");

// ====================================
// copy resources
// ====================================
List<String> patterns = updateDir.children().map(c -> c.isFile() ? c.name() : c.name() + "/**").toList();
patterns.add("!.preferences for */**");
updateDir.trackCopyingTo(origin.root, o -> o.strip().glob(patterns).replaceDifferent().sync()).to(monitor);

// ====================================
// update version
// ====================================
origin.root.lastModifiedTime(file.lastModifiedTime());

monitor.message("Update is completed, reboot.", 100);
origin.reboot();
});
Rectangle2D bounds = ScreenSelector.Application.select();
AppearanceSetting appearance = Preferences.of(AppearanceSetting.class);

origin.updater()
.env("Icon", Viewtify.application().icon())
.env("Theme", appearance.theme.v.name())
.env("ThemeType", appearance.themeType.v.name())
.env("Font", appearance.font.v)
.env("FontSize", appearance.fontSize.v.toString())
.env("LocationX", String.valueOf(bounds.getMinX() + bounds.getWidth() / 2 - 190))
.env("LocationY", String.valueOf(bounds.getMinY() + bounds.getHeight() / 2 - 60))
.reboot(monitor -> {
monitor.message("Installing the new version, please wait a minute.");

// ====================================
// copy resources
// ====================================
List<String> patterns = updateDir.children().map(c -> c.isFile() ? c.name() : c.name() + "/**").toList();
patterns.add("!.preferences for */**");
updateDir.trackCopyingTo(origin.root, o -> o.strip().glob(patterns).replaceDifferent().sync()).to(monitor);

// ====================================
// update version
// ====================================
origin.root.lastModifiedTime(file.lastModifiedTime());

monitor.message("Update is completed, reboot.", 100);
origin.reboot();
});
});
}
}
Loading

0 comments on commit 5ddb08e

Please sign in to comment.