From 02688d3c755e06067bec0c05f9f62b3ba9d1c56a Mon Sep 17 00:00:00 2001 From: xey Date: Thu, 31 Oct 2024 11:43:33 -0300 Subject: [PATCH 1/3] DIscord RPC Integration with Discord, where it displays Subtitle Edit + the name of the subtitle on Discord. --- Directory.Packages.props | 1 + src/ui/Logic/DiscordRPC.cs | 36 ++++++++++++++++++++++++++ src/ui/Program.cs | 53 ++++++++++++++++++++++++++++++++------ src/ui/SubtitleEdit.csproj | 1 + 4 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 src/ui/Logic/DiscordRPC.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index 48e9452a86..82062064eb 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -3,6 +3,7 @@ true + diff --git a/src/ui/Logic/DiscordRPC.cs b/src/ui/Logic/DiscordRPC.cs new file mode 100644 index 0000000000..5cf2bd961c --- /dev/null +++ b/src/ui/Logic/DiscordRPC.cs @@ -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(); + } +} diff --git a/src/ui/Program.cs b/src/ui/Program.cs index b5ec6e58ec..c48cc0b3ba 100644 --- a/src/ui/Program.cs +++ b/src/ui/Program.cs @@ -3,33 +3,70 @@ using System.Diagnostics; using System.Threading; using System.Windows.Forms; +using DiscordRPC; +using System.Timers; namespace Nikse.SubtitleEdit { internal static class Program { - /// - /// The main entry point for the application. - /// + 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); + } + } + + + // 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) { diff --git a/src/ui/SubtitleEdit.csproj b/src/ui/SubtitleEdit.csproj index 5715fea048..ebca1f4eeb 100644 --- a/src/ui/SubtitleEdit.csproj +++ b/src/ui/SubtitleEdit.csproj @@ -269,6 +269,7 @@ + From d6b13baa4fd17c5e1aa089e09c5a7f005b5ea834 Mon Sep 17 00:00:00 2001 From: xey Date: Fri, 1 Nov 2024 13:34:40 -0300 Subject: [PATCH 2/3] forgot about that mb --- src/ui/Program.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ui/Program.cs b/src/ui/Program.cs index c48cc0b3ba..a58e34deb9 100644 --- a/src/ui/Program.cs +++ b/src/ui/Program.cs @@ -55,13 +55,13 @@ private static void CheckWindowTitle(object sender, EventArgs e) // 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) + catch { - Console.WriteLine("Error updating Discord RPC: " + ex.Message); + timer.Stop(); + timer.Dispose(); } } From 706b63548f3f21ba71e578b381adaef7fef0b3d8 Mon Sep 17 00:00:00 2001 From: xey Date: Fri, 1 Nov 2024 13:42:05 -0300 Subject: [PATCH 3/3] Removed --- src/ui/Program.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ui/Program.cs b/src/ui/Program.cs index a58e34deb9..c2e6962adc 100644 --- a/src/ui/Program.cs +++ b/src/ui/Program.cs @@ -3,8 +3,6 @@ using System.Diagnostics; using System.Threading; using System.Windows.Forms; -using DiscordRPC; -using System.Timers; namespace Nikse.SubtitleEdit {