コード例 #1
0
        /// <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);
            }

            if (Config.DisplayTimestamp)
            {
                DiscordController.presence.startTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            }

            DiscordRPC.UpdatePresence(ref DiscordController.presence);
        }
コード例 #2
0
        /// <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();
            TogglePresence.Initialize(this);
            ToggleFileNameDisplay.Initialize(this);
            ToggleProjectNameDisplay.Initialize(this);
            ToggleTimestampDisplay.Initialize(this);
            ToggleTimestampReset.Initialize(this);

            TogglePresence.Instance.SetPresence(DiscordController.presence);
        }
コード例 #3
0
 /// <summary>
 ///     Disables the presence if needed.
 /// </summary>
 private static void DisableIfNeeded()
 {
     if (!Settings.IsPresenceEnabled)
     {
         DiscordRPC.Shutdown();
     }
 }
コード例 #4
0
        /// <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;

            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();
            ToggleFileNameDisplay.Initialize(this);
            ToggleProjectNameDisplay.Initialize(this);
            ToggleTimestampDisplay.Initialize(this);
        }
コード例 #5
0
        /// <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
            }
        }
コード例 #6
0
 /// <summary>
 /// Checks if presebce should be disabled or not
 /// </summary>
 private void CheckIfShouldDisable()
 {
     if (!DiscordRPCVSPackage.Config.PresenceEnabled)
     {
         DiscordRPC.Shutdown();
     }
 }
コード例 #7
0
 /// <summary>
 /// Disables the presence if needed.
 /// </summary>
 private static void DisableIfNeeded()
 {
     if (!Config.PresenceEnabled)
     {
         DiscordRPC.Shutdown();
     }
 }
コード例 #8
0
 /// <summary>
 ///     Initializes Discord RPC
 /// </summary>
 public void Initialize()
 {
     handlers = new DiscordRPC.EventHandlers();
     handlers.readyCallback         = ReadyCallback;
     handlers.disconnectedCallback += DisconnectedCallback;
     handlers.errorCallback        += ErrorCallback;
     DiscordRPC.Initialize(applicationId, ref handlers, true, optionalSteamId);
 }
コード例 #9
0
 /// <summary>
 ///     Initializes Discord RPC
 /// </summary>
 public void Initialize()
 {
     _handlers = new DiscordRPC.EventHandlers {
         readyCallback = ReadyCallback
     };
     _handlers.disconnectedCallback += DisconnectedCallback;
     _handlers.errorCallback        += ErrorCallback;
     DiscordRPC.Initialize(ApplicationId, ref _handlers, true, OptionalSteamId);
 }
コード例 #10
0
        /// <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);
        }
コード例 #11
0
        /// <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);
        }
コード例 #12
0
        /// <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();
        }
コード例 #13
0
 /// <summary>
 /// Called to ask the package if the shell can be closed. By default this method returns canClose as true and S_OK.
 /// </summary>
 /// <param name="canClose">Returns true if the shell can be closed, otherwise false.</param>
 /// <returns>S_OK(0) if the method succeeded, otherwise an error code.</returns>
 protected override int QueryClose(out bool canClose)
 {
     DiscordRPC.Shutdown();
     return(base.QueryClose(out canClose));
 }
コード例 #14
0
#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
            }
        }
コード例 #15
0
        /// <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 = "";

            if (windowActivated.Document != null)
            {
                ext = Path.GetExtension(windowActivated.Document.FullName);
            }

            // Update the RichPresence Images based on config.
            if (Settings.IsLanguageImageLarge)
            {
                DiscordController.presence = new DiscordRPC.RichPresence()
                {
                    largeImageKey  = (Languages.ContainsKey(ext)) ? Languages[ext] : "smallvs",
                    largeImageText = (Languages.ContainsKey(ext)) ? Languages[ext] : "",
                    smallImageKey  = "visualstudio",
                    smallImageText = "Visual Studio",
                };
            }
            else if (!Settings.IsLanguageImageLarge)
            {
                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 (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 = (Int32)(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 = (Int32)(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)
            {
                DiscordRPC.UpdatePresence(ref DiscordController.presence);
            }
        }
コード例 #16
0
        /// <summary>
        /// Enables Presence
        /// </summary>
        private void EnableRPC()
        {
            new DiscordController().Initialize();

            DiscordRPC.UpdatePresence(ref presence);
        }