more refactoring

This commit is contained in:
sirjonasxx 2022-02-13 04:14:44 +01:00
parent 33db81a8b5
commit 767fb1ab9b
15 changed files with 282 additions and 56 deletions

View File

@ -4,6 +4,8 @@ import gearth.misc.AdminValidator;
import gearth.misc.Cacher;
import gearth.misc.UpdateChecker;
import gearth.ui.GEarthController;
import gearth.ui.themes.Theme;
import gearth.ui.themes.ThemeFactory;
import gearth.ui.titlebar.TitleBarConfig;
import gearth.ui.titlebar.TitleBarController;
import javafx.application.Application;
@ -20,8 +22,7 @@ public class GEarth extends Application {
public static Application main;
public static String version = "1.5.1";
public static String gitApi = "https://api.github.com/repos/sirjonasxx/G-Earth/releases/latest";
public static String theme = "G-Earth_Dark";
public static String[] themes = new String[] {"G-Earth", "Tanji", "G-Earth_Dark"};
public static Theme theme;
private Stage stage;
private TitleBarController titleBar;
@ -29,7 +30,10 @@ public class GEarth extends Application {
static {
if (Cacher.getCacheContents().has("theme")) {
theme = Cacher.getCacheContents().getString("theme");
theme = ThemeFactory.themeForTitle(Cacher.getCacheContents().getString("theme"));
}
else {
theme = ThemeFactory.getDefaultTheme();
}
}
@ -62,14 +66,19 @@ public class GEarth extends Application {
}
@Override
public void onSetTheme(String theme) {
setTheme(theme);
public void setTheme(Theme theme) {
setGearthTheme(theme);
}
@Override
public Theme getCurrentTheme() {
return theme;
}
});
primaryStage.setResizable(false);
primaryStage.sizeToScene();
setTheme(theme);
setGearthTheme(theme);
primaryStage.show();
primaryStage.setOnCloseRequest( event -> closeGEarth());
@ -85,19 +94,23 @@ public class GEarth extends Application {
System.exit(0);
}
private void setTheme(String theme) {
private void setGearthTheme(Theme theme) {
GEarth.theme = theme;
Theme defaultTheme = ThemeFactory.getDefaultTheme();
stage.getScene().getStylesheets().clear();
stage.getScene().getStylesheets().add(GEarth.class.getResource(String.format("/gearth/ui/themes/%s/styling.css", theme)).toExternalForm());
stage.getScene().getStylesheets().add(GEarth.class.getResource(String.format("/gearth/ui/themes/%s/styling.css", theme.internalName())).toExternalForm());
stage.getIcons().clear();
stage.getIcons().add(new Image(GEarth.class.getResourceAsStream(String.format("/gearth/ui/themes/%s/logoSmall.png", theme))));
stage.getIcons().add(new Image(GEarth.class.getResourceAsStream(String.format("/gearth/ui/themes/%s/logoSmall.png", theme.overridesLogo() ? theme.internalName() : defaultTheme.internalName()))));
stage.setTitle((theme.overridesTitle() ? theme.title() : defaultTheme.title()) + " " + GEarth.version);
stage.setTitle(theme.split("_")[0] + " " + GEarth.version);
titleBar.setTitle(stage.getTitle());
controller.infoController.img_logo.setImage(new Image(GEarth.class.getResourceAsStream(String.format("/gearth/ui/themes/%s/logo.png", theme))));
controller.infoController.img_logo.setImage(new Image(GEarth.class.getResourceAsStream(
String.format(
"/gearth/ui/themes/%s/logo.png",
theme.overridesLogo() ? theme.internalName() : defaultTheme.internalName()
)
)));
controller.infoController.version.setText(stage.getTitle());
}

View File

@ -1,6 +1,8 @@
package gearth.ui.subforms.extensions.logger;
import gearth.GEarth;
import gearth.ui.titlebar.DefaultTitleBarConfig;
import gearth.ui.titlebar.TitleBarController;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
@ -35,26 +37,31 @@ public class ExtensionLogger {
stage = new Stage();
stage.setTitle(String.format("%s | Extension Console", GEarth.theme));
stage.setTitle("G-Earth | Extension Console");
stage.initModality(Modality.NONE);
stage.getIcons().add(new Image(getClass().getResourceAsStream(String.format("/gearth/ui/themes/%s/logoSmall.png", GEarth.theme))));
stage.setAlwaysOnTop(true);
stage.setMinHeight(235);
stage.setMinWidth(370);
Scene scene = new Scene(root);
scene.getStylesheets().add(String.format("/gearth/ui/themes/%s/styling.css", GEarth.theme));
scene.getStylesheets().add("/gearth/ui/extensions/logger/logger.css");
scene.getStylesheets().add("/gearth/ui/subforms/extensions/logger/logger.css");
ExtensionLoggerController controller = loader.getController();
controller.setStage(stage);
stage.setScene(scene);
// don't let the user close this window on their own
stage.setOnCloseRequest(windowEvent -> {
stage.hide();
isVisible = false;
TitleBarController titleBar = TitleBarController.create(stage, new DefaultTitleBarConfig(stage) {
@Override
public void onCloseClicked() {
stage.hide();
isVisible = false;
}
});
// // don't let the user close this window on their own
// stage.setOnCloseRequest(windowEvent -> {
// stage.hide();
// isVisible = false;
// });
} catch (IOException e) {
e.printStackTrace();
}

View File

@ -0,0 +1,7 @@
package gearth.ui.themes;
public class AnimeTheme {
// just kidding
}

View File

@ -0,0 +1,23 @@
package gearth.ui.themes;
public class DarkTheme implements Theme {
@Override
public String title() {
return "G-Earth Dark";
}
@Override
public String internalName() {
return "G-Earth_Dark";
}
@Override
public boolean overridesLogo() {
return false;
}
@Override
public boolean overridesTitle() {
return false;
}
}

View File

@ -0,0 +1,23 @@
package gearth.ui.themes;
public class LightTheme implements Theme {
@Override
public String title() {
return "G-Earth";
}
@Override
public String internalName() {
return "G-Earth";
}
@Override
public boolean overridesLogo() {
return true;
}
@Override
public boolean overridesTitle() {
return true;
}
}

View File

@ -0,0 +1,23 @@
package gearth.ui.themes;
public class TanjiTheme implements Theme {
@Override
public String title() {
return "Tanji";
}
@Override
public String internalName() {
return "Tanji";
}
@Override
public boolean overridesLogo() {
return false;
}
@Override
public boolean overridesTitle() {
return false;
}
}

View File

@ -1,7 +1,11 @@
package gearth.ui.themes;
public class Theme {
public interface Theme {
String title();
String internalName();
boolean overridesLogo();
boolean overridesTitle();
}

View File

@ -0,0 +1,27 @@
package gearth.ui.themes;
import java.util.Arrays;
import java.util.List;
public class ThemeFactory {
private static List<Theme> themes = Arrays.asList(
new LightTheme(),
new TanjiTheme(),
new DarkTheme()
);
public static Theme getDefaultTheme() {
return themes.get(0);
}
public static List<Theme> allThemes() {
return themes;
}
// returns default theme if not found
public static Theme themeForTitle(String title) {
return allThemes().stream().filter(theme -> theme.title().equals(title)).findFirst().orElse(getDefaultTheme());
}
}

View File

@ -0,0 +1,56 @@
package gearth.ui.titlebar;
import gearth.GEarth;
import gearth.ui.themes.Theme;
import gearth.ui.themes.ThemeFactory;
import javafx.scene.image.Image;
import javafx.stage.Stage;
// a typical config to be used in all windows that is not the g-earth main window
public class DefaultTitleBarConfig implements TitleBarConfig {
protected Stage stage;
private String currentStylesheet = null;
private Theme currentTheme;
public DefaultTitleBarConfig(Stage stage) {
this.stage = stage;
currentTheme = ThemeFactory.getDefaultTheme();
setTheme(currentTheme);
}
@Override
public boolean displayThemePicker() {
return false;
}
@Override
public void onCloseClicked() {
}
@Override
public void onMinimizeClicked() {
stage.setIconified(true);
}
@Override
public void setTheme(Theme theme) {
currentTheme = theme;
Theme defaultTheme = ThemeFactory.getDefaultTheme();
if (currentStylesheet != null) {
stage.getScene().getStylesheets().remove(currentStylesheet);
}
currentStylesheet = GEarth.class.getResource(String.format("/gearth/ui/themes/%s/styling.css", theme.internalName())).toExternalForm();
stage.getScene().getStylesheets().add(currentStylesheet);
stage.getIcons().clear();
stage.getIcons().add(new Image(GEarth.class.getResourceAsStream(String.format("/gearth/ui/themes/%s/logoSmall.png", theme.overridesLogo() ? theme.internalName() : defaultTheme.internalName()))));
}
@Override
public Theme getCurrentTheme() {
return currentTheme;
}
}

View File

@ -1,10 +1,13 @@
package gearth.ui.titlebar;
import gearth.ui.themes.Theme;
public interface TitleBarConfig {
boolean displayThemePicker();
void onCloseClicked();
void onMinimizeClicked();
void onSetTheme(String theme);
void setTheme(Theme theme);
Theme getCurrentTheme();
}

View File

@ -1,10 +1,15 @@
package gearth.ui.titlebar;
import gearth.GEarth;
import javafx.application.Platform;
import gearth.ui.themes.Theme;
import gearth.ui.themes.ThemeFactory;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
@ -13,12 +18,12 @@ import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.IOException;
import java.util.Arrays;
public class TitleBarController {
public Label titleLabel;
public Pane titleBar;
public ImageView titleIcon;
private Stage stage;
private TitleBarConfig config;
@ -38,9 +43,21 @@ public class TitleBarController {
stage.getScene().setRoot(newParent);
parent.getScene().setFill(Color.TRANSPARENT);
stage.titleProperty().addListener((i) -> controller.setTitle(stage.getTitle()));
controller.setTitle(stage.getTitle());
stage.getIcons().addListener((InvalidationListener) observable -> controller.updateIcon());
controller.updateIcon();
return controller;
}
public void updateIcon() {
titleIcon.setImage(stage.getIcons().size() > 0 ? stage.getIcons().get(0) :
new Image(GEarth.class.getResourceAsStream(
String.format("/gearth/ui/themes/%s/logoSmall.png", ThemeFactory.getDefaultTheme().internalName()))));
}
public void setTitle(String title) {
titleLabel.setText(title);
}
@ -51,7 +68,7 @@ public class TitleBarController {
}
public void handleMinimizeAction(MouseEvent event) {
stage.setIconified(true);
config.onMinimizeClicked();
}
private double xOffset, yOffset;
@ -67,8 +84,8 @@ public class TitleBarController {
}
public void toggleTheme(MouseEvent event) {
int themeIndex = Arrays.asList(GEarth.themes).indexOf(GEarth.theme);
config.onSetTheme(GEarth.themes[(themeIndex + 1) % GEarth.themes.length]);
int themeIndex = ThemeFactory.allThemes().indexOf(config.getCurrentTheme());
config.setTheme(ThemeFactory.allThemes().get((themeIndex + 1) % ThemeFactory.allThemes().size()));
}
}

View File

@ -949,7 +949,7 @@ VBox > .split-menu-button.last > .arrow-button {
}
#icon {
-fx-image: url("logoSmall.png");
/*-fx-image: url("logoSmall.png");*/
}
#theme-button {

View File

@ -992,7 +992,7 @@ VBox > .split-menu-button.last > .arrow-button {
}
#icon {
-fx-image: url("logoSmall.png");
/*-fx-image: url("logoSmall.png");*/
}
#theme-button {

View File

@ -949,7 +949,7 @@ VBox > .split-menu-button.last > .arrow-button {
}
#icon {
-fx-image: url("logoSmall.png");
/*-fx-image: url("logoSmall.png");*/
}
#theme-button {

View File

@ -1,35 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<Pane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="gearth.ui.titlebar.TitleBarController" id="title-bar" fx:id="titleBar" maxHeight="25.0" onMouseDragged="#handleMovementAction" onMousePressed="#handleClickAction" prefHeight="25.0" prefWidth="200.0">
<children>
<ImageView id="close-button" fitHeight="25.0" fitWidth="50.0" layoutX="601.0" onMouseClicked="#handleCloseAction" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@files/closeButton.png" />
</image>
</ImageView>
<ImageView id="minimize-button" fitHeight="25.0" fitWidth="50.0" layoutX="551.0" onMouseClicked="#handleMinimizeAction" pickOnBounds="true" preserveRatio="true">
<GridPane id="title-bar" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gearth.ui.titlebar.TitleBarController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="25.0" minHeight="25.0" prefHeight="25.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ImageView id="minimize-button" fitHeight="25.0" fitWidth="50.0" onMouseClicked="#handleMinimizeAction" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1">
<image>
<Image url="@files/minimizeButton.png" />
</image>
</ImageView>
<ImageView id="icon" fitHeight="16.0" fitWidth="16.0" layoutX="7.0" layoutY="5.0" pickOnBounds="true" preserveRatio="true">
<ImageView id="close-button" fitHeight="25.0" fitWidth="50.0" onMouseClicked="#handleCloseAction" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2">
<image>
<Image url="@../themes/G-Earth/logoSmall.png" />
<Image url="@files/closeButton.png" />
</image>
</ImageView>
<Label fx:id="titleLabel" layoutX="23.0" layoutY="5.0" text="G-Earth 1.5.1">
<padding>
<Insets left="2.0" />
</padding>
</Label>
<ImageView id="theme-button" fitHeight="20.0" fitWidth="38.0" layoutX="505.0" layoutY="3.0" onMouseClicked="#toggleTheme" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../themes/G-Earth/themeButton.png" />
</image></ImageView>
</children></Pane>
<GridPane maxHeight="25.0" maxWidth="1.7976931348623157E308" minHeight="25.0" minWidth="-Infinity" onMouseDragged="#handleMovementAction" onMousePressed="#handleClickAction" prefHeight="25.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="7.0" minWidth="7.0" prefWidth="7.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" />
<ColumnConstraints hgrow="ALWAYS" />
<ColumnConstraints maxWidth="-Infinity" />
<ColumnConstraints maxWidth="7.0" minWidth="7.0" prefWidth="7.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="25.0" minHeight="25.0" prefHeight="25.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ImageView id="icon" fx:id="titleIcon" fitHeight="16.0" fitWidth="16.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1">
<image>
<Image url="@../themes/G-Earth/logoSmall.png" />
</image>
</ImageView>
<Label fx:id="titleLabel" text="Placeholder" GridPane.columnIndex="2">
<padding>
<Insets left="2.0" />
</padding>
</Label>
<ImageView id="theme-button" fitHeight="20.0" fitWidth="38.0" onMouseClicked="#toggleTheme" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="3">
<image>
<Image url="@../themes/G-Earth/themeButton.png" />
</image>
</ImageView>
</children>
</GridPane>
</children>
</GridPane>