Add discord rich presence

This commit is contained in:
Melon 2022-05-10 18:23:27 +01:00
parent c140844a71
commit 3cda67ac98
Signed by: melon
GPG Key ID: B0ADD5395BCDAAB6
5 changed files with 88 additions and 8 deletions

View File

@ -6,6 +6,11 @@
<option name="name" value="Central Repository" /> <option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" /> <option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository> </remote-repository>
<remote-repository>
<option name="id" value="jitpack.io" />
<option name="name" value="jitpack.io" />
<option name="url" value="https://jitpack.io" />
</remote-repository>
<remote-repository> <remote-repository>
<option name="id" value="central" /> <option name="id" value="central" />
<option name="name" value="Maven Central repository" /> <option name="name" value="Maven Central repository" />

11
pom.xml
View File

@ -52,6 +52,12 @@
</resource> </resource>
</resources> </resources>
</build> </build>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.formdev</groupId> <groupId>com.formdev</groupId>
@ -98,5 +104,10 @@
<artifactId>jiconfont-google_material_design_icons</artifactId> <artifactId>jiconfont-google_material_design_icons</artifactId>
<version>2.2.0.2</version> <version>2.2.0.2</version>
</dependency> </dependency>
<dependency>
<groupId>com.github.Vatuu</groupId>
<artifactId>discord-rpc</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,19 +1,22 @@
package xyz.mrmelon54.codesize; 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.components.InfoPanel;
import xyz.mrmelon54.codesize.enums.Theme; import xyz.mrmelon54.codesize.enums.Theme;
import xyz.mrmelon54.codesize.ex.CannotCreateConfigFolderException; import xyz.mrmelon54.codesize.ex.CannotCreateConfigFolderException;
import xyz.mrmelon54.codesize.process.FileFinder; import xyz.mrmelon54.codesize.process.FileFinder;
import xyz.mrmelon54.codesize.ui.PresetSelector; import xyz.mrmelon54.codesize.ui.PresetSelector;
import xyz.mrmelon54.codesize.utils.AboutDialog; import xyz.mrmelon54.codesize.utils.AboutDialog;
import xyz.mrmelon54.codesize.utils.ByteUtils;
import xyz.mrmelon54.codesize.utils.ComponentUtils; import xyz.mrmelon54.codesize.utils.ComponentUtils;
import xyz.mrmelon54.codesize.utils.Settings; import xyz.mrmelon54.codesize.utils.Settings;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import java.awt.*; import java.awt.*;
import java.awt.event.KeyEvent; import java.awt.event.*;
import java.awt.event.WindowEvent;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;
@ -21,6 +24,8 @@ import java.util.regex.Pattern;
public class CodeSize extends JFrame { public class CodeSize extends JFrame {
public final Settings settings; public final Settings settings;
private final Timer discordRpcTimer = new Timer(1000, e -> DiscordRPC.discordRunCallbacks());
private final long startTimestamp;
private JTextField pathField; private JTextField pathField;
private JTextField regexField; private JTextField regexField;
private InfoPanel infoPanel; private InfoPanel infoPanel;
@ -29,12 +34,15 @@ public class CodeSize extends JFrame {
public CodeSize(Settings settings) { public CodeSize(Settings settings) {
super("Code Size"); super("Code Size");
this.settings = settings; this.settings = settings;
this.startTimestamp = System.currentTimeMillis();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(800, 600)); setPreferredSize(new Dimension(800, 600));
setMinimumSize(new Dimension(800, 600)); setMinimumSize(new Dimension(800, 600));
setContentPane(createMainPanel()); setContentPane(createMainPanel());
setJMenuBar(createMenuBar()); setJMenuBar(createMenuBar());
Runtime.getRuntime().addShutdownHook(new Thread(this::shutdownRichPresence));
} }
private JPanel createMainPanel() { private JPanel createMainPanel() {
@ -85,6 +93,15 @@ public class CodeSize extends JFrame {
fileMenu.setMnemonic(KeyEvent.VK_F); fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu); 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 // Theme menu
JMenu themeMenu = new JMenu("Theme"); JMenu themeMenu = new JMenu("Theme");
ButtonGroup bg = new ButtonGroup(); ButtonGroup bg = new ButtonGroup();
@ -103,12 +120,21 @@ public class CodeSize extends JFrame {
} }
}); });
} }
fileMenu.add(themeMenu); toolsMenu.add(themeMenu);
// Quit item JCheckBoxMenuItem richPresenceItem = new JCheckBoxMenuItem("Discord Rich Presence");
JMenuItem quitItem = fileMenu.add(new JMenuItem("Quit")); richPresenceItem.setSelected(settings.discordRichPresence);
quitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_DOWN_MASK)); richPresenceItem.addItemListener(e -> {
quitItem.addActionListener(e -> CodeSize.this.dispatchEvent(new WindowEvent(CodeSize.this, WindowEvent.WINDOW_CLOSING))); 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 // Help menu
JMenu helpMenu = new JMenu("Help"); JMenu helpMenu = new JMenu("Help");
@ -123,6 +149,7 @@ public class CodeSize extends JFrame {
} }
public void run() { public void run() {
tickRichPresence();
setVisible(true); setVisible(true);
} }
@ -173,6 +200,38 @@ public class CodeSize extends JFrame {
FileFinder.search(mainFile, pattern, infoPanel); FileFinder.search(mainFile, pattern, infoPanel);
infoPanel.finishLoading(); infoPanel.finishLoading();
SwingUtilities.invokeLater(() -> searchButton.setEnabled(true)); SwingUtilities.invokeLater(() -> searchButton.setEnabled(true));
updateRichPresence();
}).start(); }).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);
}
} }

View File

@ -144,6 +144,10 @@ public class InfoPanel extends JPanel {
public void setValue(float v) { public void setValue(float v) {
value = v; value = v;
} }
public float getValue() {
return value;
}
} }
public record InfoPanelFile(File file, long lines, long size) { public record InfoPanelFile(File file, long lines, long size) {

View File

@ -16,6 +16,7 @@ public class Settings {
private static final BuiltinPresets builtinPresets = new BuiltinPresets(); private static final BuiltinPresets builtinPresets = new BuiltinPresets();
public Theme theme; public Theme theme;
public boolean discordRichPresence;
public Vector<Preset> presets; public Vector<Preset> presets;
public Settings() { public Settings() {