diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
index 712ab9d..8fe40c8 100644
--- a/.idea/jarRepositories.xml
+++ b/.idea/jarRepositories.xml
@@ -6,6 +6,11 @@
+
+
+
+
+
@@ -17,4 +22,4 @@
-
\ No newline at end of file
+
diff --git a/pom.xml b/pom.xml
index 7b5d0cf..75e4315 100644
--- a/pom.xml
+++ b/pom.xml
@@ -52,6 +52,12 @@
+
+
+ jitpack.io
+ https://jitpack.io
+
+
com.formdev
@@ -98,5 +104,10 @@
jiconfont-google_material_design_icons
2.2.0.2
+
+ com.github.Vatuu
+ discord-rpc
+ 1.6.2
+
\ No newline at end of file
diff --git a/src/main/java/xyz/mrmelon54/codesize/CodeSize.java b/src/main/java/xyz/mrmelon54/codesize/CodeSize.java
index ff38013..530bfb7 100644
--- a/src/main/java/xyz/mrmelon54/codesize/CodeSize.java
+++ b/src/main/java/xyz/mrmelon54/codesize/CodeSize.java
@@ -1,19 +1,22 @@
package xyz.mrmelon54.codesize;
+import net.arikia.dev.drpc.DiscordEventHandlers;
+import net.arikia.dev.drpc.DiscordRPC;
+import net.arikia.dev.drpc.DiscordRichPresence;
import xyz.mrmelon54.codesize.components.InfoPanel;
import xyz.mrmelon54.codesize.enums.Theme;
import xyz.mrmelon54.codesize.ex.CannotCreateConfigFolderException;
import xyz.mrmelon54.codesize.process.FileFinder;
import xyz.mrmelon54.codesize.ui.PresetSelector;
import xyz.mrmelon54.codesize.utils.AboutDialog;
+import xyz.mrmelon54.codesize.utils.ByteUtils;
import xyz.mrmelon54.codesize.utils.ComponentUtils;
import xyz.mrmelon54.codesize.utils.Settings;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
-import java.awt.event.KeyEvent;
-import java.awt.event.WindowEvent;
+import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
@@ -21,6 +24,8 @@ import java.util.regex.Pattern;
public class CodeSize extends JFrame {
public final Settings settings;
+ private final Timer discordRpcTimer = new Timer(1000, e -> DiscordRPC.discordRunCallbacks());
+ private final long startTimestamp;
private JTextField pathField;
private JTextField regexField;
private InfoPanel infoPanel;
@@ -29,12 +34,15 @@ public class CodeSize extends JFrame {
public CodeSize(Settings settings) {
super("Code Size");
this.settings = settings;
+ this.startTimestamp = System.currentTimeMillis();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(800, 600));
setMinimumSize(new Dimension(800, 600));
setContentPane(createMainPanel());
setJMenuBar(createMenuBar());
+
+ Runtime.getRuntime().addShutdownHook(new Thread(this::shutdownRichPresence));
}
private JPanel createMainPanel() {
@@ -85,6 +93,15 @@ public class CodeSize extends JFrame {
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
+ // Quit item
+ JMenuItem quitItem = fileMenu.add(new JMenuItem("Quit"));
+ quitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_DOWN_MASK));
+ quitItem.addActionListener(e -> CodeSize.this.dispatchEvent(new WindowEvent(CodeSize.this, WindowEvent.WINDOW_CLOSING)));
+
+ // Tools menu
+ JMenu toolsMenu = menuBar.add(new JMenu("Tools"));
+ fileMenu.setMnemonic(KeyEvent.VK_T);
+
// Theme menu
JMenu themeMenu = new JMenu("Theme");
ButtonGroup bg = new ButtonGroup();
@@ -103,12 +120,21 @@ public class CodeSize extends JFrame {
}
});
}
- fileMenu.add(themeMenu);
+ toolsMenu.add(themeMenu);
- // Quit item
- JMenuItem quitItem = fileMenu.add(new JMenuItem("Quit"));
- quitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_DOWN_MASK));
- quitItem.addActionListener(e -> CodeSize.this.dispatchEvent(new WindowEvent(CodeSize.this, WindowEvent.WINDOW_CLOSING)));
+ JCheckBoxMenuItem richPresenceItem = new JCheckBoxMenuItem("Discord Rich Presence");
+ richPresenceItem.setSelected(settings.discordRichPresence);
+ richPresenceItem.addItemListener(e -> {
+ settings.discordRichPresence = richPresenceItem.isSelected();
+ tickRichPresence();
+ try {
+ settings.save();
+ } catch (IOException | CannotCreateConfigFolderException ex) {
+ System.out.println("Failed to save settings");
+ ex.printStackTrace();
+ }
+ });
+ toolsMenu.add(richPresenceItem);
// Help menu
JMenu helpMenu = new JMenu("Help");
@@ -123,6 +149,7 @@ public class CodeSize extends JFrame {
}
public void run() {
+ tickRichPresence();
setVisible(true);
}
@@ -173,6 +200,38 @@ public class CodeSize extends JFrame {
FileFinder.search(mainFile, pattern, infoPanel);
infoPanel.finishLoading();
SwingUtilities.invokeLater(() -> searchButton.setEnabled(true));
+ updateRichPresence();
}).start();
}
+
+ private void tickRichPresence() {
+ if (settings.discordRichPresence) {
+ if (!discordRpcTimer.isRunning()) {
+ DiscordEventHandlers handlers = new DiscordEventHandlers.Builder().setReadyEventHandler(discordUser -> System.out.println("Welcome " + discordUser.username + "#" + discordUser.discriminator)).build();
+ DiscordRPC.discordInitialize("973597639339831356", handlers, true);
+ discordRpcTimer.start();
+ updateRichPresence();
+ }
+ } else shutdownRichPresence();
+ }
+
+ private void shutdownRichPresence() {
+ if (discordRpcTimer.isRunning()) {
+ discordRpcTimer.stop();
+ DiscordRPC.discordShutdown();
+ }
+ }
+
+ private void updateRichPresence() {
+ DiscordRichPresence discordRichPresence = new DiscordRichPresence();
+ long totalSize = (long) infoPanel.totalSize.getValue();
+ long totalLines = (long) infoPanel.totalLines.getValue();
+ if (totalSize == 0 && totalLines == 0) discordRichPresence.details = "No project data loaded..";
+ else {
+ discordRichPresence.details = "Total size: " + ByteUtils.ToBytesCount(totalSize);
+ discordRichPresence.state = "Total lines: " + totalLines;
+ }
+ discordRichPresence.startTimestamp = startTimestamp;
+ DiscordRPC.discordUpdatePresence(discordRichPresence);
+ }
}
diff --git a/src/main/java/xyz/mrmelon54/codesize/components/InfoPanel.java b/src/main/java/xyz/mrmelon54/codesize/components/InfoPanel.java
index 0d191da..168eeb7 100644
--- a/src/main/java/xyz/mrmelon54/codesize/components/InfoPanel.java
+++ b/src/main/java/xyz/mrmelon54/codesize/components/InfoPanel.java
@@ -144,6 +144,10 @@ public class InfoPanel extends JPanel {
public void setValue(float v) {
value = v;
}
+
+ public float getValue() {
+ return value;
+ }
}
public record InfoPanelFile(File file, long lines, long size) {
diff --git a/src/main/java/xyz/mrmelon54/codesize/utils/Settings.java b/src/main/java/xyz/mrmelon54/codesize/utils/Settings.java
index 2068ce8..8020a23 100644
--- a/src/main/java/xyz/mrmelon54/codesize/utils/Settings.java
+++ b/src/main/java/xyz/mrmelon54/codesize/utils/Settings.java
@@ -16,6 +16,7 @@ public class Settings {
private static final BuiltinPresets builtinPresets = new BuiltinPresets();
public Theme theme;
+ public boolean discordRichPresence;
public Vector presets;
public Settings() {