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

DIscord RPC #8961

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="DiscordRichPresence" Version="1.2.1.24" />
<PackageVersion Include="MSTest" Version="3.6.1" />
<PackageVersion Include="System.Drawing.Common" Version="8.0.10" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
Expand Down
36 changes: 36 additions & 0 deletions src/ui/Logic/DiscordRPC.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using DiscordRPC;

public class DiscordRPCMain
{
private DiscordRpcClient _client;

public void Initialize()
{
// Aplication ID
_client = new DiscordRpcClient("1301527670818996244");
_client.Initialize();
}

public void UpdatePresence(string fileName = "No file opened")
{
if (_client != null)
{
_client.SetPresence(new RichPresence
{
State = fileName,
Timestamps = Timestamps.Now,
Assets = new Assets
{
LargeImageKey = "logo", // Image
LargeImageText = "Subtitle Edit"
}
});
}
}


public void Shutdown()
{
_client?.Dispose();
}
}
53 changes: 45 additions & 8 deletions src/ui/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,70 @@
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using DiscordRPC;
using System.Timers;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this been used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was testing other things and committing with that... it's already been removed 👍 thanks for noticing


namespace Nikse.SubtitleEdit
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
private static DiscordRPCMain discordRpcMain;
private static System.Windows.Forms.Timer timer;
private static string lastFileName = "";

[STAThread]
private static void Main()
{
#if !DEBUG
// Add the event handler for handling UI thread exceptions to the event.
Application.ThreadException += Application_ThreadException;

// Set the unhandled exception mode to force all Windows Forms errors to go through our handler.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

// Add the event handler for handling non-UI thread exceptions to the event.
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
#endif

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

// Initialize Discord RPC
discordRpcMain = new DiscordRPCMain();
discordRpcMain.Initialize();

// Set up the timer to monitor the window title
timer = new System.Windows.Forms.Timer();
timer.Interval = 2000; // Check every 2 seconds
timer.Tick += CheckWindowTitle;
timer.Start();

Application.Run(new Main());

// Shutdown Discord RPC
discordRpcMain.Shutdown();
}

private static void CheckWindowTitle(object sender, EventArgs e)
{
try
{
var process = Process.GetCurrentProcess();
var windowTitle = process.MainWindowTitle;

if (!string.IsNullOrEmpty(windowTitle) && windowTitle != lastFileName)
{
lastFileName = windowTitle;

// Extract name from the window
var fileName = windowTitle.Contains(" - ") ? windowTitle.Split(new string[] { " - " }, StringSplitOptions.None)[0] : "No file opened";

discordRpcMain.UpdatePresence($"Editing: {fileName}");
}
}
catch (Exception ex)
{
Console.WriteLine("Error updating Discord RPC: " + ex.Message);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe on recursive fail just disable & dispose the timer?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

god i completely forgot about that sorry

}
}



// Handle the UI exceptions by showing a dialog box, and asking the user whether or not they wish to abort execution.
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
Expand Down
1 change: 1 addition & 0 deletions src/ui/SubtitleEdit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@
</HunspellAssemblies>
</ItemGroup>
<ItemGroup>
<PackageReference Include="DiscordRichPresence" />
<PackageReference Include="Microsoft.Net.Http" />
<PackageReference Include="Microsoft.Win32.Registry" />
<PackageReference Include="NAudio.Core" />
Expand Down