Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENHANCEMENT] Add dark mode #404 #466

Merged
merged 2 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/edu/rpi/legup/app/LegupPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 4,6 @@
import java.util.Map;
import java.util.prefs.Preferences;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LegupPreferences {

private static LegupPreferences instance;
Expand All @@ -19,6 16,7 @@ public class LegupPreferences {
public static final String WORK_DIRECTORY = "work-directory";
public static final String START_FULL_SCREEN = "start-full-screen";
public static final String AUTO_UPDATE = "auto-update";
public static final String DARK_MODE = "night-mode";
public static final String SHOW_MISTAKES = "show-mistakes";
public static final String SHOW_ANNOTATIONS = "show-annotations";
public static final String ALLOW_DEFAULT_RULES = "allow-default-rules";
Expand All @@ -29,6 27,7 @@ public class LegupPreferences {
defaultPreferencesMap.put(WORK_DIRECTORY, System.getProperty("user.home"));
defaultPreferencesMap.put(START_FULL_SCREEN, Boolean.toString(false));
defaultPreferencesMap.put(AUTO_UPDATE, Boolean.toString(true));
defaultPreferencesMap.put(DARK_MODE, Boolean.toString(false));
defaultPreferencesMap.put(SHOW_MISTAKES, Boolean.toString(true));
defaultPreferencesMap.put(SHOW_ANNOTATIONS, Boolean.toString(false));
defaultPreferencesMap.put(ALLOW_DEFAULT_RULES, Boolean.toString(false));
Expand All @@ -40,6 39,7 @@ public class LegupPreferences {
preferencesMap.put(WORK_DIRECTORY, preferences.get(WORK_DIRECTORY, defaultPreferencesMap.get(WORK_DIRECTORY)));
preferencesMap.put(START_FULL_SCREEN, preferences.get(START_FULL_SCREEN, defaultPreferencesMap.get(START_FULL_SCREEN)));
preferencesMap.put(AUTO_UPDATE, preferences.get(AUTO_UPDATE, defaultPreferencesMap.get(AUTO_UPDATE)));
preferencesMap.put(DARK_MODE, preferences.get(DARK_MODE, defaultPreferencesMap.get(DARK_MODE)));
preferencesMap.put(SHOW_MISTAKES, preferences.get(SHOW_MISTAKES, defaultPreferencesMap.get(SHOW_MISTAKES)));
preferencesMap.put(SHOW_ANNOTATIONS, preferences.get(SHOW_ANNOTATIONS, defaultPreferencesMap.get(SHOW_ANNOTATIONS)));
preferencesMap.put(ALLOW_DEFAULT_RULES, preferences.get(ALLOW_DEFAULT_RULES, defaultPreferencesMap.get(ALLOW_DEFAULT_RULES)));
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/edu/rpi/legup/ui/LegupUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 9,9 @@


import com.formdev.flatlaf.FlatLightLaf;
import com.formdev.flatlaf.FlatDarkLaf;
import edu.rpi.legup.app.GameBoardFacade;
import edu.rpi.legup.app.LegupPreferences;
import edu.rpi.legup.ui.lookandfeel.LegupLookAndFeel;
import edu.rpi.legup.ui.boardview.BoardView;
import edu.rpi.legup.ui.proofeditorui.treeview.TreePanel;

Expand Down Expand Up @@ -45,9 45,15 @@ public static String getOS() {
public LegupUI() {
setTitle("LEGUP");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LegupPreferences prefs = LegupPreferences.getInstance();

try {
UIManager.setLookAndFeel(new FlatLightLaf());
if(Boolean.valueOf(prefs.getUserPref(LegupPreferences.DARK_MODE))) {
UIManager.setLookAndFeel(new FlatDarkLaf());
}
else {
UIManager.setLookAndFeel(new FlatLightLaf());
}
}
catch (UnsupportedLookAndFeelException e) {
System.err.println("Not supported ui look and feel");
Expand Down
36 changes: 31 additions & 5 deletions src/main/java/edu/rpi/legup/ui/PreferencesDialog.java
Original file line number Diff line number Diff line change
@@ -1,7 1,5 @@
package edu.rpi.legup.ui;

import edu.rpi.legup.app.Config;
import edu.rpi.legup.app.GameBoardFacade;
import edu.rpi.legup.app.LegupPreferences;
import edu.rpi.legup.model.Puzzle;
import edu.rpi.legup.model.rules.Rule;
Expand All @@ -14,16 12,16 @@
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.formdev.flatlaf.FlatLightLaf;
import com.formdev.flatlaf.FlatDarkLaf;

public class PreferencesDialog extends JDialog {

private final static Logger LOGGER = Logger.getLogger(PreferencesDialog.class.getName());

private JCheckBox fullScreen, autoUpdate, showMistakes, showAnnotations, allowDefault, generateCases, immFeedback;
private JCheckBox fullScreen, autoUpdate, darkMode, showMistakes, showAnnotations, allowDefault, generateCases, immFeedback;
private JTextField workDirectory;

private static Image folderIcon;
Expand Down Expand Up @@ -81,6 79,21 @@ public PreferencesDialog(Frame frame) {
setVisible(true);
}

private void toggleDarkMode(LegupPreferences prefs) {
try {
if(Boolean.valueOf(prefs.getUserPref(LegupPreferences.DARK_MODE))) {
UIManager.setLookAndFeel(new FlatDarkLaf());
}
else {
UIManager.setLookAndFeel(new FlatLightLaf());
}
com.formdev.flatlaf.FlatLaf.updateUI();
}
catch (UnsupportedLookAndFeelException e) {
System.err.println("Not supported ui look and feel");
}
}

private JScrollPane createGeneralTab() {
LegupPreferences prefs = LegupPreferences.getInstance();
JScrollPane scrollPane = new JScrollPane();
Expand Down Expand Up @@ -131,6 144,15 @@ private JScrollPane createGeneralTab() {
contentPane.add(autoUpdateRow);
contentPane.add(Box.createRigidArea(new Dimension(0, 10)));

darkMode = new JCheckBox("Dark Mode", Boolean.valueOf(prefs.getUserPref(LegupPreferences.DARK_MODE)));
darkMode.setToolTipText("This turns dark mode on and off");
JPanel darkModeRow = new JPanel();
darkModeRow.setLayout(new BorderLayout());
darkModeRow.add(darkMode, BorderLayout.WEST);
darkModeRow.setMaximumSize(new Dimension(Integer.MAX_VALUE, darkModeRow.getPreferredSize().height));
contentPane.add(darkModeRow);
contentPane.add(Box.createRigidArea(new Dimension(0, 10)));

contentPane.add(createLeftLabel("Board View Preferences"));
contentPane.add(createLineSeparator());
showMistakes = new JCheckBox("Show Mistakes", Boolean.valueOf(prefs.getUserPref(LegupPreferences.SHOW_MISTAKES)));
Expand Down Expand Up @@ -306,10 328,14 @@ public void applyPreferences() {
prefs.setUserPref(LegupPreferences.WORK_DIRECTORY, workDirectory.getText());
prefs.setUserPref(LegupPreferences.START_FULL_SCREEN, Boolean.toString(fullScreen.isSelected()));
prefs.setUserPref(LegupPreferences.AUTO_UPDATE, Boolean.toString(autoUpdate.isSelected()));
prefs.setUserPref(LegupPreferences.DARK_MODE, Boolean.toString(darkMode.isSelected()));
prefs.setUserPref(LegupPreferences.SHOW_MISTAKES, Boolean.toString(showMistakes.isSelected()));
prefs.setUserPref(LegupPreferences.SHOW_ANNOTATIONS, Boolean.toString(showAnnotations.isSelected()));
prefs.setUserPref(LegupPreferences.ALLOW_DEFAULT_RULES, Boolean.toString(allowDefault.isSelected()));
prefs.setUserPref(LegupPreferences.AUTO_GENERATE_CASES, Boolean.toString(generateCases.isSelected()));
prefs.setUserPref(LegupPreferences.IMMEDIATE_FEEDBACK, Boolean.toString(immFeedback.isSelected()));

// toggle dark mode based on updated NIGHT_MODE variable
toggleDarkMode(prefs);
}
}