Esempio n. 1
0
        private static void ShowSpotify()
        {
            if (Spotify.IsRunning())
            {
                var hWnd = Spotify.GetSpotify();

                // check Spotify's current window state
                var placement = new Win32.WINDOWPLACEMENT();
                Win32.GetWindowPlacement(hWnd, ref placement);

                int showCommand = Win32.Constants.SW_SHOW;

                // if Spotify is minimzed we need to send a restore so that the window
                // will come back exactly like it was before being minimized (i.e. maximized
                // or otherwise) otherwise if we call SW_RESTORE on a currently maximized window
                // then instead of staying maximized it will return to normal size.
                if (placement.showCmd == Win32.Constants.SW_SHOWMINIMIZED)
                {
                    showCommand = Win32.Constants.SW_RESTORE;
                }

                Win32.ShowWindow(hWnd, showCommand);

                Win32.SetForegroundWindow(hWnd);
                Win32.SetFocus(hWnd);
            }
        }
Esempio n. 2
0
        public static Song GetCurrentSong()
        {
            if (!Spotify.IsRunning())
            {
                return(null);
            }

            IntPtr        hWnd   = GetSpotify();
            int           length = Win32.GetWindowTextLength(hWnd);
            StringBuilder sb     = new StringBuilder(length + 1);

            Win32.GetWindowText(hWnd, sb, sb.Capacity);

            string title = sb.ToString();

            if (!string.IsNullOrWhiteSpace(title) && title != "Spotify")
            {
                // Unfortunately we don't have a great way to get the title from Spotify
                // so we need to do some gymnastics.
                // Music played from an artist's page is usually in the format "artist - song"
                // while music played from a playlist is often in the format "artist - song - album"
                // unfortunately this means that some songs that actually have a " - " in either the artist name
                // or in the song name will potentially display incorrectly
                var portions = title.Split(new string[] { " - " }, StringSplitOptions.None);

                var song   = (portions.Length > 1 ? portions[1] : null);
                var artist = portions[0];
                var album  = (portions.Length > 2 ? string.Join(" ", portions.Skip(2).ToArray()) : null); // take everything else that's left

                return(new Song(artist, song, album));
            }

            return(null);
        }
Esempio n. 3
0
        private static bool IsMinimized()
        {
            if (!Spotify.IsRunning())
            {
                return(false);
            }

            var hWnd = Spotify.GetSpotify();

            // check Spotify's current window state
            var placement = new Win32.WINDOWPLACEMENT();

            Win32.GetWindowPlacement(hWnd, ref placement);

            return(placement.showCmd == Win32.Constants.SW_SHOWMINIMIZED);
        }
Esempio n. 4
0
        public void DisplayAction(SpotifyAction action, Song trackBeforeAction)
        {
            //Anything that changes track doesn't need to be handled since
            //that will be handled in the timer event.

            const string VOLUME_UP_TEXT   = "Volume ++";
            const string VOLUME_DOWN_TEXT = "Volume --";
            const string MUTE_ON_OFF_TEXT = "Mute On/Off";
            const string NOTHINGS_PLAYING = "Nothing's playing";
            const string PAUSED_TEXT      = "Paused";
            const string STOPPED_TEXT     = "Stopped";
            const string SETTINGS_TEXT    = "Settings saved";

            if (!Spotify.IsRunning() && action != SpotifyAction.SettingsSaved)
            {
                toastIcon   = DEFAULT_ICON;
                Title1.Text = "Spotify not available!";
                Title2.Text = string.Empty;
                FadeIn();
                return;
            }

            Song currentTrack = trackBeforeAction;

            string prevTitle1 = Title1.Text;
            string prevTitle2 = Title2.Text;

            switch (action)
            {
            case SpotifyAction.PlayPause:
                if (trackBeforeAction != null)
                {
                    //We pressed pause
                    Title1.Text = "Paused";
                    Title2.Text = trackBeforeAction.ToString();
                    FadeIn();
                }
                currentSong = null;      //If we presses play this will force a toast to display in next timer event.
                break;

            case SpotifyAction.Stop:
                currentSong = null;
                Title1.Text = "Stopped";
                Title2.Text = trackBeforeAction.ToString();
                FadeIn();
                break;

            case SpotifyAction.SettingsSaved:
                Title1.Text = SETTINGS_TEXT;
                Title2.Text = "Here is a preview of your settings!";
                FadeIn();
                break;

            case SpotifyAction.NextTrack:          //No need to handle
                break;

            case SpotifyAction.PreviousTrack:      //No need to handle
                break;

            case SpotifyAction.VolumeUp:
                Title1.Text = VOLUME_UP_TEXT;
                Title2.Text = currentTrack.ToString();
                FadeIn();
                break;

            case SpotifyAction.VolumeDown:
                Title1.Text = VOLUME_DOWN_TEXT;
                Title2.Text = currentTrack.ToString();
                FadeIn();
                break;

            case SpotifyAction.Mute:
                Title1.Text = MUTE_ON_OFF_TEXT;
                Title2.Text = currentTrack.ToString();
                FadeIn();
                break;

            case SpotifyAction.ShowToast:
                if (currentTrack == null || !currentTrack.IsValid())
                {
                    toastIcon = DEFAULT_ICON;

                    Title1.Text = NOTHINGS_PLAYING;
                    Title2.Text = string.Empty;
                }
                else
                {
                    if (currentTrack != null && currentTrack.IsValid())
                    {
                        toastIcon = currentTrack.CoverArtUrl;

                        Title1.Text = currentTrack.Artist;
                        Title2.Text = currentTrack.Track;
                    }
                }

                FadeIn(force: true);
                break;

            case SpotifyAction.ShowSpotify:      //No need to handle
                break;

            case SpotifyAction.ThumbsUp:
                toastIcon = "Resources/thumbs_up.png";

                Title1.Text = "Thumbs Up!";
                Title2.Text = currentTrack.ToString();
                FadeIn();
                break;

            case SpotifyAction.ThumbsDown:
                toastIcon = "Resources/thumbs_down.png";

                Title1.Text = "Thumbs Down :(";
                Title2.Text = currentTrack.ToString();
                FadeIn();
                break;
            }
        }
Esempio n. 5
0
        public static void SendAction(SpotifyAction a)
        {
            if (!Spotify.IsRunning())
            {
                return;
            }

            // bah. Because control cannot fall through cases we need to special case volume
            if (SettingsXml.Current.ChangeSpotifyVolumeOnly)
            {
                if (a == SpotifyAction.VolumeUp)
                {
                    Telemetry.TrackEvent(TelemetryCategory.Action, Telemetry.TelemetryEvent.Action.VolumeUp);

                    VolumeHelper.IncrementVolume("Spotify");
                    return;
                }
                else if (a == SpotifyAction.VolumeDown)
                {
                    Telemetry.TrackEvent(TelemetryCategory.Action, Telemetry.TelemetryEvent.Action.VolumeDown);

                    VolumeHelper.DecrementVolume("Spotify");
                    return;
                }
                else if (a == SpotifyAction.Mute)
                {
                    Telemetry.TrackEvent(TelemetryCategory.Action, Telemetry.TelemetryEvent.Action.Mute);

                    VolumeHelper.ToggleApplicationMute("Spotify");
                    return;
                }
            }

            switch (a)
            {
            case SpotifyAction.CopyTrackInfo:
            case SpotifyAction.ShowToast:
                //Nothing
                break;

            case SpotifyAction.ShowSpotify:
                Telemetry.TrackEvent(TelemetryCategory.Action, Telemetry.TelemetryEvent.Action.ShowSpotify);


                if (Spotify.IsMinimized())
                {
                    ShowSpotify();
                }
                else
                {
                    Minimize();
                }

                break;

            case SpotifyAction.FastForward:

                Telemetry.TrackEvent(TelemetryCategory.Action, Telemetry.TelemetryEvent.Action.FastForward);

                SendComplexKeys("+{Right}");
                break;

            case SpotifyAction.Rewind:

                Telemetry.TrackEvent(TelemetryCategory.Action, Telemetry.TelemetryEvent.Action.Rewind);

                SendComplexKeys("+{Left}");
                break;

            default:

                Telemetry.TrackEvent(TelemetryCategory.Action, Telemetry.TelemetryEvent.Action.Default + a.ToString());

                Win32.SendMessage(GetSpotify(), Win32.Constants.WM_APPCOMMAND, IntPtr.Zero, new IntPtr((long)a));
                break;
            }
        }