Add discord rich presence
This commit is contained in:
parent
c140844a71
commit
3cda67ac98
@ -6,6 +6,11 @@
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://repo.maven.apache.org/maven2" />
|
||||
</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>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
|
11
pom.xml
11
pom.xml
@ -52,6 +52,12 @@
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jitpack.io</id>
|
||||
<url>https://jitpack.io</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.formdev</groupId>
|
||||
@ -98,5 +104,10 @@
|
||||
<artifactId>jiconfont-google_material_design_icons</artifactId>
|
||||
<version>2.2.0.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.Vatuu</groupId>
|
||||
<artifactId>discord-rpc</artifactId>
|
||||
<version>1.6.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -16,6 +16,7 @@ public class Settings {
|
||||
private static final BuiltinPresets builtinPresets = new BuiltinPresets();
|
||||
|
||||
public Theme theme;
|
||||
public boolean discordRichPresence;
|
||||
public Vector<Preset> presets;
|
||||
|
||||
public Settings() {
|
||||
|
Loading…
Reference in New Issue
Block a user