private async void WindowActivated(Window windowActivated, Window lastWindow) { await this.JoinableTaskFactory.SwitchToMainThreadAsync(); if (!this.Discord.IsInitialized && !this.Discord.IsDisposed) { if (!this.Discord.Initialize()) { ActivityLog.LogError("DiscordRPforVS", $"{Translates.LogError(Settings.Default.translates)}"); } } if (windowActivated.Document != null) { await this.UpdatePresenceAsync(windowActivated.Document).ConfigureAwait(true); } }
protected override async System.Threading.Tasks.Task InitializeAsync(CancellationToken cancellationToken, IProgress <ServiceProgressData> progress) { try { await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); Settings.Upgrade(); // Writes the language used to the global variable Settings.Default.translates = System.Globalization.CultureInfo.CurrentCulture.ToString().Replace("-", ""); Settings.Default.Save(); ide = GetGlobalService(typeof(SDTE)) as DTE; ide.Events.WindowEvents.WindowActivated += this.WindowActivated; ide.Events.SolutionEvents.BeforeClosing += this.SolutionBeforeClosing; String ideVersion = ide.Version.Split(new Char[1] { '.' })[0]; this.versionString = $"Visual Studio {Constants.IdeVersions[Int32.Parse(ideVersion, CultureInfo.InvariantCulture)]}"; this.versionImageKey = $"dev{ideVersion}"; await SettingsCommand.InitializeAsync(this).ConfigureAwait(true); if (!this.Discord.IsInitialized && !this.Discord.IsDisposed) { if (!this.Discord.Initialize()) { ActivityLog.LogError("DiscordRPforVS", $"{Translates.LogError(Settings.Default.translates)}"); } } if (Settings.loadOnStartup) { await this.UpdatePresenceAsync(ide.ActiveDocument).ConfigureAwait(true); } await base.InitializeAsync(cancellationToken, progress).ConfigureAwait(true); } catch (OperationCanceledException exc) { ActivityLog.LogError(exc.Source, exc.Message); } }
/// <summary> /// Updates the Presence with the document /// </summary> /// <param name="document"></param> /// <param name="overrideTimestampReset"></param> /// <returns></returns> public async System.Threading.Tasks.Task UpdatePresenceAsync(Document document, Boolean overrideTimestampReset = false) { try { await this.JoinableTaskFactory.SwitchToMainThreadAsync(); if (!Settings.enabled) { if (!this.Discord.IsInitialized && !this.Discord.IsDisposed) { if (!this.Discord.Initialize()) { ActivityLog.LogError("DiscordRPforVS", $"{Translates.LogError(Settings.Default.translates)}"); } } this.Discord.ClearPresence(); return; } this.Presence.Details = this.Presence.State = this.Assets.LargeImageKey = this.Assets.LargeImageText = this.Assets.SmallImageKey = this.Assets.SmallImageText = String.Empty; if (Settings.secretMode) { this.Presence.Details = Translates.Presence_Details(Settings.Default.translates); this.Presence.State = Translates.Presence_State(Settings.Default.translates); goto finish; } this.Presence.Details = this.Presence.State = ""; String[] language = Array.Empty <String>(); if (document != null) { String filename = Path.GetFileName(path: document.FullName).ToUpperInvariant(), ext = Path.GetExtension(filename); List <KeyValuePair <String[], String[]> > list = Constants.Languages.Where(lang => Array.IndexOf(lang.Key, filename) > -1 || Array.IndexOf(lang.Key, ext) > -1).ToList(); language = list.Count > 0 ? list[0].Value : Array.Empty <String>(); } Boolean supported = language.Length > 0; this.Assets.LargeImageKey = Settings.largeLanguage ? supported ? language[0] : $"{Translates.Text(Settings.Default.translates)}" : this.versionImageKey; this.Assets.LargeImageText = Settings.largeLanguage ? supported ? language[1] + $" {Translates.File(Settings.Default.translates)}" : Translates.UnrecognizedExtension(Settings.Default.translates) : this.versionString; this.Assets.SmallImageKey = Settings.largeLanguage ? this.versionImageKey : supported?language[0] : $"{Translates.Text(Settings.Default.translates)}"; this.Assets.SmallImageText = Settings.largeLanguage ? this.versionString : supported? language[1] + $" {Translates.File(Settings.Default.translates)}" : Translates.UnrecognizedExtension(Settings.Default.translates); if (Settings.showFileName) { this.Presence.Details = !(document is null) ? Path.GetFileName(document.FullName) : Translates.NoFile(Settings.Default.translates); } if (Settings.showSolutionName) { Boolean idling = ide.Solution is null || String.IsNullOrEmpty(ide.Solution.FullName); this.Presence.State = idling ? Translates.Idling(Settings.Default.translates) : $"{Translates.Developing(Settings.Default.translates)} {Path.GetFileNameWithoutExtension(ide.Solution.FileName)}"; if (idling) { this.Assets.LargeImageKey = this.versionImageKey; this.Assets.LargeImageText = this.versionString; this.Assets.SmallImageKey = this.Assets.SmallImageText = ""; } } if (Settings.showTimestamp && document != null) { if (!this.InitializedTimestamp) { this.InitialTimestamps = this.Presence.Timestamps = new Timestamps() { Start = DateTime.UtcNow }; this.InitializedTimestamp = true; } if (Settings.resetTimestamp && !overrideTimestampReset) { this.Presence.Timestamps = new Timestamps() { Start = DateTime.UtcNow } } ; else if (Settings.resetTimestamp && overrideTimestampReset) { this.Presence.Timestamps = this.CurrentTimestamps; } else if (!Settings.resetTimestamp && !overrideTimestampReset) { this.Presence.Timestamps = this.InitialTimestamps; } this.CurrentTimestamps = this.Presence.Timestamps; } finish :; this.Presence.Assets = this.Assets; if (!this.Discord.IsInitialized && !this.Discord.IsDisposed) { if (!this.Discord.Initialize()) { ActivityLog.LogError("DiscordRPforVS", Translates.LogError(Settings.Default.translates)); } } this.Discord.SetPresence(this.Presence); } catch (ArgumentException e) { ActivityLog.LogError(e.Source, e.Message); } }