Add themes

This commit is contained in:
Oystein Kristoffer Tveit 2021-02-24 14:22:14 +01:00
parent 4661495683
commit 8ddcb9743e
9 changed files with 71 additions and 24 deletions

View File

@ -20,10 +20,10 @@ Probably tastes better than any Apple editor and NetBeans combined.
- [X] Shortcuts
- [X] Change languages
- [X] Toggle line comment
- [ ] Soft wrap
- [ ] Darkmode/Lightmode or color themes
- [X] Soft wrap
- [X] Darkmode/Lightmode or color themes
- [ ] Add icons for files/folders in filetree
- [ ] Add [code coverage check](https://docs.gitlab.com/ee/user/project/merge_requests/test_coverage_visualization.html) to verify quality of unit tests
- [X] Add [code coverage check](https://docs.gitlab.com/ee/user/project/merge_requests/test_coverage_visualization.html) to verify quality of unit tests
## Maybe TODO?
- [ ] List Chooser

View File

@ -10,6 +10,7 @@ import javafx.scene.image.Image;
import javafx.stage.Stage;
import app.events.FileSaveStateChangedEvent;
import app.events.LanguageChangedEvent;
import app.events.ThemeChangedEvent;
import app.model.Model;
public class Main extends Application {
@ -46,17 +47,15 @@ public class Main extends Application {
Model.setScene(scene);
}
private void applyCSS() {
scene.getStylesheets().add(
getClass().getResource("/styling/themes/monokai.css").toExternalForm());
}
/**
* Set up default values and settings for the editor.
*/
private void setupDefaults() {
scene.getStylesheets().setAll("", "");
MainController mainController = fxmlLoader.getController();
mainController.getEventBus().post(new LanguageChangedEvent("Java"));
mainController.getEventBus().post(new ThemeChangedEvent("Monokai"));
mainController.getEventBus().post(new FileSaveStateChangedEvent(true));
mainController.setHostServices(getHostServices());
}
@ -71,7 +70,6 @@ public class Main extends Application {
setupWindow(window);
loadFXML();
createScene();
applyCSS();
setupDefaults();
window.setScene(scene);

View File

@ -11,6 +11,7 @@ import app.controllers.*;
import app.events.ExitApplicationEvent;
import app.events.LanguageChangedEvent;
import app.events.OpenLinkInBrowserEvent;
import app.events.ThemeChangedEvent;
import app.model.Model;
import javafx.application.HostServices;
import javafx.application.Platform;
@ -65,26 +66,28 @@ public class MainController implements Initializable {
this.hostServices = hostServices;
}
private void setCSSAt(int position, String cssPath) {
String nextStyleSheet =
getClass()
.getResource(cssPath)
.toExternalForm();
Model.getScene().getStylesheets().set(position, nextStyleSheet);
}
/**
* Change the CSS according to which language is being used.
* @param event
*/
@Subscribe
private void handle(LanguageChangedEvent event) {
boolean containsSeveralStylesheets = Model.getScene().getStylesheets().size() != 1;
if (containsSeveralStylesheets)
Model.getScene().getStylesheets().remove(1);
String nextStyleSheet =
getClass()
.getResource("/styling/language/" + event.getLanguage().toLowerCase() + ".css")
.toExternalForm();
Model.getScene().getStylesheets().add(nextStyleSheet);
this.setCSSAt(1, "/styling/languages/" + event.getLanguage().toLowerCase() + ".css");
}
@Subscribe
private void handle(ThemeChangedEvent event) {
this.setCSSAt(0, "/styling/themes/" + event.getTheme().toLowerCase().replace(" ", "-") + ".css");
}
@Subscribe
private void handle(OpenLinkInBrowserEvent event) {

View File

@ -10,6 +10,7 @@ import com.google.common.eventbus.Subscribe;
import app.events.ExitApplicationEvent;
import app.events.LanguageChangedEvent;
import app.events.OpenLinkInBrowserEvent;
import app.events.ThemeChangedEvent;
import app.events.ToggleCommentEvent;
import app.events.ToggleWrapTextEvent;
import javafx.event.ActionEvent;
@ -107,6 +108,13 @@ public class MenubarController implements Initializable, Controller {
this.eventBus.post(new ToggleWrapTextEvent(isSelected));
}
@FXML
private void handleThemeChange(ActionEvent event) {
this.eventBus.post(
new ThemeChangedEvent(
((RadioMenuItem) event.getSource()).getText()));
}
/**
* Handles the event where there was an exit request from the menu.
* @param event

View File

@ -0,0 +1,15 @@
package app.events;
public class ThemeChangedEvent {
private String theme;
public ThemeChangedEvent(String theme) {
this.theme = theme;
}
public String getTheme() {
return theme;
}
}

View File

@ -7,6 +7,7 @@
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.control.RadioMenuItem?>
<?import javafx.scene.control.CheckMenuItem?>
<MenuBar
fx:id="menubar"
@ -18,13 +19,13 @@
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="New File" accelerator="Shortcut+n"/>
<MenuItem mnemonicParsing="false" text="New Folder" accelerator="Shortcut+N"/>
<MenuItem mnemonicParsing="false" text="New Folder" accelerator="Shortcut+Shift+N"/>
<SeparatorMenuItem/>
<MenuItem mnemonicParsing="false" text="Open File" accelerator="Shortcut+o" onAction="#handleOpenFile"/>
<MenuItem mnemonicParsing="false" text="Open Project" accelerator="Shortcut+O" onAction="#handleOpenProject"/>
<MenuItem mnemonicParsing="false" text="Open Project" accelerator="Shortcut+Shift+O" onAction="#handleOpenProject"/>
<SeparatorMenuItem/>
<MenuItem mnemonicParsing="false" text="Save" accelerator="Shortcut+s"/>
<MenuItem mnemonicParsing="false" text="Save as" accelerator="Shortcut+S"/>
<MenuItem mnemonicParsing="false" text="Save as" accelerator="Shortcut+Shift+S"/>
<SeparatorMenuItem/>
<fx:define>
@ -44,6 +45,28 @@
toggleGroup="$languageToggleGroup"/>
</items>
</Menu>
<SeparatorMenuItem/>
<CheckMenuItem mnemonicParsing="false" text="Wrap text" accelerator="Alt+z" onAction="#handleToggleWraptext"/>
<SeparatorMenuItem/>
<fx:define>
<ToggleGroup fx:id="themeToggleGroup"/>
</fx:define>
<Menu mnemonicParsing="false" text="Change color theme">
<items>
<!-- TODO: Generate buttons based on classes -->
<RadioMenuItem text="Monokai"
selected="true"
onAction="#handleThemeChange"
toggleGroup="$themeToggleGroup"/>
<RadioMenuItem text="Solarized Light"
onAction="#handleThemeChange"
toggleGroup="$themeToggleGroup"/>
</items>
</Menu>
<SeparatorMenuItem/>
<MenuItem mnemonicParsing="false" text="Exit" accelerator="Shortcut+w" onAction="#handleExitApplication"/>
</items>