Skip to content

Commit

Permalink
Merge branch 'defaultconfig' into 'master'
Browse files Browse the repository at this point in the history
Default Config

Closes #150, #233, and #267
GitHub #89 and #20

See merge request roan/KeysPerSecond!42
  • Loading branch information
RoanH committed Jan 16, 2024
2 parents 642d9ed + fb02005 commit c50ea20
Show file tree
Hide file tree
Showing 7 changed files with 369 additions and 186 deletions.
129 changes: 17 additions & 112 deletions KeysPerSecond/src/dev/roanh/kps/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,13 @@
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.DirectoryStream.Filter;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
Expand All @@ -54,7 +45,7 @@

import com.github.kwhat.jnativehook.NativeHookException;

import dev.roanh.kps.config.ConfigParser;
import dev.roanh.kps.config.ConfigLoader;
import dev.roanh.kps.config.Configuration;
import dev.roanh.kps.config.ThemeColor;
import dev.roanh.kps.config.UpdateRate;
Expand Down Expand Up @@ -204,31 +195,33 @@ public class Main{
* @param args - configuration file path
*/
public static void main(String[] args){
//Work around for a JDK bug
//work around for a JDK bug
ExclamationMarkPath.check(args);

//Basic setup and info
//check for a passed config
String config = null;
if(args.length >= 1 && !args[0].equalsIgnoreCase("-relaunch")){
config = args[0];
System.out.println("Attempting to load config: " + config);
}

//info print
System.out.println("Control keys:");
System.out.println("Ctrl + P: Causes the program to reset and print the average and maximum value");
System.out.println("Ctrl + U: Terminates the program");
System.out.println("Ctrl + I: Causes the program to reset and print the key press statistics");
System.out.println("Ctrl + Y: Hides/shows the GUI");
System.out.println("Ctrl + T: Pauses/resumes the counter");
System.out.println("Ctrl + R: Reloads the configuration");
Util.installUI();

//set UI components
Util.installUI();
content = new GridPanel();
frame = new JFrame("KeysPerSecond");
layout = new Layout(content);
content.setLayout(layout);

//Set dialog defaults
//set dialog defaults
Dialog.setDialogIcon(iconSmall);
Dialog.setParentFrame(frame);
Dialog.setDialogTitle("KeysPerSecond");
Expand All @@ -248,40 +241,14 @@ public static void main(String[] args){
eventManager.registerKeyPressListener(listener);
eventManager.registerKeyReleaseListener(listener);

//Set configuration for the keys
if(config != null){
try{
Configuration toLoad = parseConfiguration(config);
if(toLoad != null){
Main.config = toLoad;
System.out.println("Loaded config file: " + toLoad.getPath().toString());
}else{
System.out.println("The provided config file does not exist.");
}
}catch(IOException e){
e.printStackTrace();
Dialog.showErrorDialog("Failed to load the given configuration file\nCause: " + e.getMessage());
}
}else{
try{
MainDialog.configure(Main.config);
}catch(Exception e){
e.printStackTrace();
try{
Dialog.showErrorDialog("Failed to load the configuration menu, however you can use the live menu instead");
}catch(Throwable t){
t.printStackTrace();
}
System.err.println("Failed to load the configuration menu, however you can use the live menu instead");
}
//load any given configurations
if(!ConfigLoader.quickLoadConfiguration(config)){
//if no (valid) ones were found let the user create a new config
MainDialog.configure(Main.config);
}

//Build GUI
try{
buildGUI();
}catch(IOException e){
e.printStackTrace();
}
//build GUI
buildGUI();

//register default event handlers
eventManager.registerButtonPressListener(Main::pressEventButton);
Expand All @@ -290,55 +257,9 @@ public static void main(String[] args){
eventManager.registerKeyReleaseListener(Main::releaseEventKey);
eventManager.registerKeyPressListener(Main::triggerCommandKeys);

//Enter the main loop
//enter the main loop
mainLoop();
}

/**
* Parses the given command line argument configuration file by
* loading the file from disk while treating unknown characters
* as wildcards to deal with Windows argument encoding issues.
* @param config The configuration file path.
* @return The loaded configuration file or <code>null</code>
* if the file was not found.
* @throws IOException When an IOException occurs.
*/
private static final Configuration parseConfiguration(String config) throws IOException{
try{
Path path = Paths.get(config);
return Files.exists(path) ? ConfigParser.read(path) : null;
}catch(InvalidPathException e){
int index = config.lastIndexOf(File.separatorChar);
try{
Path dir = Paths.get(config.substring(0, index));
final String name = config.substring(index + 1);
Filter<Path> filter = p->{
String other = Objects.toString(p.getFileName());
for(int i = 0; i < name.length(); i++){
char ch = name.charAt(i);
if(ch == '?'){
continue;
}
if(i >= other.length() || ch != other.charAt(i)){
return false;
}
}
return true;
};

try(DirectoryStream<Path> files = Files.newDirectoryStream(dir, filter)){
Iterator<Path> iter = files.iterator();
if(iter.hasNext()){
return ConfigParser.read(iter.next());
}
}

return null;
}catch(InvalidPathException e2){
return null;
}
}
}

/**
* Main loop of the program
Expand Down Expand Up @@ -501,7 +422,7 @@ private static void triggerCommandKeys(int code){
suspended = !suspended;
Menu.pause.setSelected(suspended);
}else if(commands.getCommandReload().matches(code)){
reloadConfig();
ConfigLoader.reloadConfig();
Menu.resetData();
}
}
Expand Down Expand Up @@ -543,11 +464,9 @@ protected static final void changeUpdateRate(UpdateRate newRate){
}

/**
* Builds the main GUI of the program
* @throws IOException When an IO Exception occurs, this can be thrown
* when the program fails to load its resources
* Builds the main GUI of the program.
*/
protected static final void buildGUI() throws IOException{
private static final void buildGUI(){
Menu.createMenu();
frame.setResizable(false);
frame.setIconImage(icon);
Expand Down Expand Up @@ -683,20 +602,6 @@ public static void removeKey(int keycode){
keys.remove(keycode);
}

/**
* Reloads the current configuration from file.
*/
public static void reloadConfig(){
if(config.getPath() != null){
try{
config = ConfigParser.read(config.getPath());
}catch(IOException e){
e.printStackTrace();
Dialog.showErrorDialog("Failed to reload the configuration, cause: " + e.getMessage());
}
}
}

static{
Image img;
try{
Expand Down
11 changes: 9 additions & 2 deletions KeysPerSecond/src/dev/roanh/kps/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@
import javax.swing.plaf.basic.BasicMenuItemUI;
import javax.swing.plaf.basic.BasicMenuUI;

import dev.roanh.kps.config.ConfigParser;
import dev.roanh.kps.config.ConfigLoader;
import dev.roanh.kps.config.UpdateRate;
import dev.roanh.kps.config.group.KeyPanelSettings;
import dev.roanh.kps.panels.GraphPanel;
import dev.roanh.kps.ui.dialog.AboutDialog;
import dev.roanh.kps.ui.dialog.ColorDialog;
import dev.roanh.kps.ui.dialog.CommandKeysDialog;
import dev.roanh.kps.ui.dialog.DefaultConfigDialog;
import dev.roanh.kps.ui.dialog.KeysDialog;
import dev.roanh.kps.ui.dialog.LayoutDialog;
import dev.roanh.kps.ui.dialog.StatsSavingDialog;
Expand Down Expand Up @@ -141,6 +142,7 @@ protected static final void createMenu(){
JCheckBoxMenuItem windowed = new JCheckBoxMenuItem("Windowed mode");
JMenuItem save = new JMenuItem("Save config");
JMenuItem load = new JMenuItem("Load config");
JMenuItem defConf = new JMenuItem("Default config");
JMenuItem saveStats = new JMenuItem("Save stats");
JMenuItem loadStats = new JMenuItem("Load stats");
components.add(saveStats);
Expand All @@ -151,6 +153,7 @@ protected static final void createMenu(){
components.add(about);
components.add(minimizeButton);
components.add(save);
components.add(defConf);
components.add(snap);
components.add(exit);
components.add(pause);
Expand Down Expand Up @@ -275,8 +278,11 @@ protected static final void createMenu(){
save.addActionListener((e)->{
Main.config.saveConfig(true);
});
defConf.addActionListener(e->{
DefaultConfigDialog.showDefaultConfigDialog();
});
load.addActionListener((e)->{
if(ConfigParser.loadConfiguration()){
if(ConfigLoader.loadConfiguration()){
resetData();
}
});
Expand Down Expand Up @@ -313,6 +319,7 @@ protected static final void createMenu(){

saveLoad.add(load);
saveLoad.add(save);
saveLoad.add(defConf);
saveLoad.add(loadStats);
saveLoad.add(saveStats);

Expand Down
Loading

0 comments on commit c50ea20

Please sign in to comment.