/// <inheritdoc /> /// <summary> /// Initialization of the package; this method is called right after the package is sited, so this is the place /// where you can put all the initialization code that rely on services provided by VisualStudio. /// </summary> protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress <ServiceProgressData> progress) { try { // Switches to the UI thread in order to consume some services used in command initialization await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); // Query service asynchronously from the UI thread _dte = await GetServiceAsync(typeof(SDTE)) as DTE; Assumes.Present(_dte); _dteEvents = _dte.Events; _dteEvents.WindowEvents.WindowActivated += OnWindowSwitch; if (Settings.IsPresenceEnabled) { DiscordController.Initialize(); DiscordRPC.UpdatePresence(ref DiscordController.Presence); } PresenceCommand.Initialize(this); await base.InitializeAsync(cancellationToken, progress); } catch (Exception) { //ignored } }
/// <summary> /// Initialization of the package; this method is called right after the package is sited, so this is the place /// where you can put all the initialization code that rely on services provided by VisualStudio. /// </summary> protected override void Initialize() { // Try to deserialize the config file, it should throw an error if it doesn't exist. // in that case, we'll want to create a new instance and save it. Config = Configuration.Deserialize(); _dte = (DTE)GetService(typeof(SDTE)); _dteEvents = _dte.Events; _dteEvents.WindowEvents.WindowActivated += OnWindowSwitch; if (Config.PresenceEnabled) { DiscordController.Initialize(); DiscordRPC.UpdatePresence(ref DiscordController.presence); } base.Initialize(); ToggleImagePosition.Initialize(this); TogglePresence.Initialize(this); ToggleFileNameDisplay.Initialize(this); ToggleProjectNameDisplay.Initialize(this); ToggleTimestampDisplay.Initialize(this); ToggleTimestampReset.Initialize(this); TogglePresence.Instance.SetPresence(DiscordController.presence); }
/// <summary> /// Initializes a new instance of the <see cref="TogglePresence" /> class. /// Adds our command handlers for menu (commands must exist in the command table file) /// </summary> /// <param name="package">Owner package, not null.</param> private PresenceCommand(Package package) { this.package = package ?? throw new ArgumentNullException(nameof(package)); if (!(ServiceProvider.GetService(typeof(IMenuCommandService)) is OleMenuCommandService commandService)) { return; } OleMenuCommand AddCommand(int id, EventHandler toggled) { toggled += (s, e) => Settings.Save(); var menuCommand = new OleMenuCommand(toggled, new CommandID(CommandSet, id)); menuCommand.BeforeQueryStatus += (s, e) => { if (s is OleMenuCommand command) { command.Checked = GetSettingValue(command.CommandID.ID); } }; commandService.AddCommand(menuCommand); return(menuCommand); } OleMenuCommand presenceCmd = AddCommand(TogglePresenceId, (s, e) => { Settings.IsPresenceEnabled ^= true; DisableIfNeeded(); }); presenceCmd.BeforeQueryStatus += (s, e) => { if (Settings.IsPresenceEnabled) { return; } DiscordRPCVSPackage.DiscordController.Initialize(); DiscordRPC.UpdatePresence(ref DiscordRPCVSPackage.DiscordController.Presence); }; AddCommand(DisplayFileNameId, (s, e) => Settings.IsFileNameShown ^= true); AddCommand(DisplaySolutionNameId, (s, e) => Settings.IsSolutionNameShown ^= true); AddCommand(DisplayTimestampId, (s, e) => Settings.IsTimestampShown ^= true); OleMenuCommand resetTimestampCmd = AddCommand(ResetTimestampId, (s, e) => Settings.IsTimestampResetEnabled ^= true); resetTimestampCmd.BeforeQueryStatus += (s, e) => ((OleMenuCommand)s).Visible = Settings.IsTimestampShown; AddCommand(ToggleImagePositionId, (s, e) => Settings.IsLanguageImageLarge ^= true); }
/// <summary> /// When switching between windows /// </summary> /// <param name="windowActivated"></param> /// <param name="lastWindow"></param> private void OnWindowSwitch(Window windowActivated, Window lastWindow) { // Get Extension var ext = Path.GetExtension(windowActivated.Document.FullName); // Update the RichPresence DiscordController.presence = new DiscordRPC.RichPresence() { largeImageKey = "visualstudio", largeImageText = "Visual Studio", smallImageKey = (Languages.ContainsKey(ext)) ? Languages[ext] : "smallvs", smallImageText = (Languages.ContainsKey(ext)) ? Languages[ext] : "", }; // Add things to the presence based on config. if (Config.DisplayFileName) { DiscordController.presence.details = Path.GetFileName(GetExactPathName(windowActivated.Document.FullName)); } if (Config.DisplayProject) { DiscordController.presence.state = "Developing " + Path.GetFileNameWithoutExtension(_dte.Solution.FileName); } // Initialize timestamp if (Config.DisplayTimestamp && !InitializedTimestamp) { DiscordController.presence.startTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; InitialTimestamp = DiscordController.presence.startTimestamp; InitializedTimestamp = true; } // Reset it if (Config.ResetTimestamp && InitializedTimestamp && Config.DisplayTimestamp) { DiscordController.presence.startTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; } // Set it equal to the initial timestamp (To not reset) else if (Config.DisplayTimestamp && !Config.ResetTimestamp) { DiscordController.presence.startTimestamp = InitialTimestamp; } if (Config.PresenceEnabled) { DiscordRPC.UpdatePresence(ref DiscordController.presence); } }
/// <summary> /// Initialization of the package; this method is called right after the package is sited, so this is the place /// where you can put all the initialization code that rely on services provided by VisualStudio. /// </summary> protected override void Initialize() { // Try to deserialize the config file, it should throw an error if it doesn't exist. // in that case, we'll want to create a new instance and save it. _dte = (DTE)GetService(typeof(SDTE)); _dteEvents = _dte.Events; _dteEvents.WindowEvents.WindowActivated += OnWindowSwitch; if (Settings.IsPresenceEnabled) { DiscordController.Initialize(); DiscordRPC.UpdatePresence(ref DiscordController.presence); } base.Initialize(); PresenceCommand.Initialize(this); }
/// <summary> /// When switching between windows /// </summary> /// <param name="windowActivated"></param> /// <param name="lastWindow"></param> private void OnWindowSwitch(Window windowActivated, Window lastWindow) { // Get Extension var ext = Path.GetExtension(windowActivated.Document.FullName); // Update the RichPresence DiscordController.presence = new DiscordRPC.RichPresence() { details = Path.GetFileName(GetExactPathName(windowActivated.Document.FullName)), state = "Developing " + Path.GetFileNameWithoutExtension(_dte.Solution.FileName), largeImageKey = "visualstudio", largeImageText = "Visual Studio", smallImageKey = (Languages.ContainsKey(ext)) ? Languages[ext] : "smallvs", smallImageText = (Languages.ContainsKey(ext)) ? Languages[ext] : "", startTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds }; DiscordRPC.UpdatePresence(ref DiscordController.presence); }
/// <summary> /// Initialization of the package; this method is called right after the package is sited, so this is the place /// where you can put all the initialization code that rely on services provided by VisualStudio. /// </summary> protected override void Initialize() { _dte = (DTE)GetService(typeof(SDTE)); _dteEvents = _dte.Events; _dteEvents.WindowEvents.WindowActivated += OnWindowSwitch; DiscordController.Initialize(); DiscordController.presence = new DiscordRPC.RichPresence() { details = "Idle", state = "Looking for a project", largeImageKey = "visualstudio", largeImageText = "Visual Studio", smallImageKey = "smallvs", startTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds }; DiscordRPC.UpdatePresence(ref DiscordController.presence); base.Initialize(); }
#pragma warning disable VSTHRD100 // Avoid async void methods /// <summary> /// When switching between windows /// </summary> /// <param name="windowActivated"></param> /// <param name="lastWindow"></param> private async void OnWindowSwitch(Window windowActivated, Window lastWindow) #pragma warning restore VSTHRD100 // Avoid async void methods { try { await JoinableTaskFactory.SwitchToMainThreadAsync(DisposalToken); // Get Extension string ext = ""; if (windowActivated.Document != null) { ext = Path.GetExtension(windowActivated.Document.FullName); } // Update the RichPresence Images based on config. DiscordController.Presence = Settings.IsLanguageImageLarge ? new DiscordRPC.RichPresence { largeImageKey = _languages.ContainsKey(ext) ? _languages[ext] : "visualstudio", largeImageText = _languages.ContainsKey(ext) ? _languages[ext] : "", smallImageKey = "visualstudio", smallImageText = "Visual Studio 2019" } : new DiscordRPC.RichPresence { largeImageKey = "visualstudio", largeImageText = "Visual Studio 2019", smallImageKey = _languages.ContainsKey(ext) ? _languages[ext] : "visualstudio", smallImageText = _languages.ContainsKey(ext) ? _languages[ext] : "" }; // Add things to the presence based on config. if (Settings.IsFileNameShown && windowActivated.Document != null) { DiscordController.Presence.details = Path.GetFileName(GetExactPathName(windowActivated.Document.FullName)); } if (Settings.IsSolutionNameShown && _dte.Solution != null) { DiscordController.Presence.state = "Developing " + Path.GetFileNameWithoutExtension(_dte.Solution.FileName); } // Initialize timestamp if (Settings.IsTimestampShown && !InitializedTimestamp) { DiscordController.Presence.startTimestamp = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; InitialTimestamp = DiscordController.Presence.startTimestamp; InitializedTimestamp = true; } // Reset it if (Settings.IsTimestampResetEnabled && InitializedTimestamp && Settings.IsTimestampShown) { DiscordController.Presence.startTimestamp = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; } // Set it equal to the initial timestamp (To not reset) else if (Settings.IsTimestampShown && !Settings.IsTimestampResetEnabled) { DiscordController.Presence.startTimestamp = InitialTimestamp; } if (Settings.IsPresenceEnabled) { DiscordController.Initialize(); DiscordRPC.UpdatePresence(ref DiscordController.Presence); } else { DiscordRPC.Shutdown(); } } catch (Exception) { // ignored } }
/// <summary> /// Enables Presence /// </summary> private void EnableRPC() { new DiscordController().Initialize(); DiscordRPC.UpdatePresence(ref presence); }