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

View File

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

View File

@ -1,10 +1,15 @@
package gearth.ui.titlebar; package gearth.ui.titlebar;
import gearth.GEarth; 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.fxml.FXMLLoader;
import javafx.scene.Parent; import javafx.scene.Parent;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent; import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
@ -13,12 +18,12 @@ import javafx.stage.Stage;
import javafx.stage.StageStyle; import javafx.stage.StageStyle;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
public class TitleBarController { public class TitleBarController {
public Label titleLabel; public Label titleLabel;
public Pane titleBar; public Pane titleBar;
public ImageView titleIcon;
private Stage stage; private Stage stage;
private TitleBarConfig config; private TitleBarConfig config;
@ -38,9 +43,21 @@ public class TitleBarController {
stage.getScene().setRoot(newParent); stage.getScene().setRoot(newParent);
parent.getScene().setFill(Color.TRANSPARENT); 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; 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) { public void setTitle(String title) {
titleLabel.setText(title); titleLabel.setText(title);
} }
@ -51,7 +68,7 @@ public class TitleBarController {
} }
public void handleMinimizeAction(MouseEvent event) { public void handleMinimizeAction(MouseEvent event) {
stage.setIconified(true); config.onMinimizeClicked();
} }
private double xOffset, yOffset; private double xOffset, yOffset;
@ -67,8 +84,8 @@ public class TitleBarController {
} }
public void toggleTheme(MouseEvent event) { public void toggleTheme(MouseEvent event) {
int themeIndex = Arrays.asList(GEarth.themes).indexOf(GEarth.theme); int themeIndex = ThemeFactory.allThemes().indexOf(config.getCurrentTheme());
config.onSetTheme(GEarth.themes[(themeIndex + 1) % GEarth.themes.length]); config.setTheme(ThemeFactory.allThemes().get((themeIndex + 1) % ThemeFactory.allThemes().size()));
} }
} }

View File

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

View File

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

View File

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

View File

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