From 0e6df05c2b412de15a700ec0ce357f6476ffb87c Mon Sep 17 00:00:00 2001 From: Sakith B Date: Sun, 29 Aug 2021 17:25:56 +0530 Subject: [PATCH] Fix attempt #1 --- extension.js | 120 +++++++++++++++++++++----------------------------- metadata.json | 2 +- prefs.js | 47 +++++++++++++------- 3 files changed, 81 insertions(+), 88 deletions(-) diff --git a/extension.js b/extension.js index 00fcf33..14097ce 100644 --- a/extension.js +++ b/extension.js @@ -41,6 +41,7 @@ let onUpdateDelayChanged, let mainLoop; let settings; + // Tracking variables let lastPlayer, @@ -51,23 +52,6 @@ let lastPlayer, mouseHovered, contentRemoved; -// Constants - -const playerIcons = { - default: "audio-x-generic-symbolic", - chromium: "chromium", - firefox: "firefox", - rhythmbox: "rhythmbox", - spotify: "spotify", - vlc: "vlc", -}; - -const positions = { - left: "_leftBox", - center: "_centerBox", - right: "_rightBox", -}; - // UI elements let buttonNext, @@ -82,46 +66,56 @@ let buttonNext, labelSeperatorStart, labelSeperatorEnd; -// Button trigger methods - -const _actionToggle = () => { - if (playerState === "Playing") { - buttonToggle.set_child(iconPause); - playerState = "Paused"; - } else { - buttonToggle.set_child(iconPlay); - playerState = "Playing"; - } - playbackAction("PlayPause", currentPlayer); -}; - -const _actionNext = () => { - playbackAction("Next", currentPlayer); -}; +// Constants -const _actionPrev = () => { - playbackAction("Previous", currentPlayer); +const playerIcons = { + default: "audio-x-generic-symbolic", + chromium: "chromium", + firefox: "firefox", + rhythmbox: "rhythmbox", + spotify: "spotify", + vlc: "vlc", }; -const _mouseAction = (event) => { - if (event.pseudo_class === "active") { - mouseActions[mouseActionsLeftClick](); - } else { - mouseActions[mouseActionsRightClick](); - } +const positions = { + left: "_leftBox", + center: "_centerBox", + right: "_rightBox", }; -const mouseActions = { +const playbackActions = { none: () => {}, - toggle_play: _actionToggle, + toggle_play: () => { + if (playerState === "Playing") { + buttonToggle.set_child(iconPause); + playerState = "Paused"; + } else { + buttonToggle.set_child(iconPlay); + playerState = "Playing"; + } + playbackAction("PlayPause", currentPlayer); + }, play: () => { playbackAction("Play", currentPlayer); }, pause: () => { playbackAction("Pause", currentPlayer); }, - next: _actionNext, - prev: _actionPrev, + next: () => { + playbackAction("Next", currentPlayer); + }, + prev: () => { + playbackAction("Previous", currentPlayer); + }, +}; + +// Performs corresponding mouse action +const _mouseAction = (event) => { + if (event.pseudo_class === "active") { + playbackActions[mouseActionsLeftClick](); + } else { + playbackActions[mouseActionsRightClick](); + } }; // Other utility methods @@ -202,7 +196,7 @@ const removeContent = () => { const updateData = (player, _playerState, _title, _artist) => { // log(mouseHovered, showAllOnHover); - let currentMetadata = `${_title || ""}${_artist ? " - " + _artist : ""}`; + let currentMetadata = `${_title}${_artist ? " - " + _artist : ""}`; // log(currentMetadata); let splittedPlayer = player.split("."); // log(splittedPlayer, player); @@ -225,9 +219,6 @@ const updateData = (player, _playerState, _title, _artist) => { lastState = _playerState; lastStateChanged = true; } - if (currentMetadata === "" && splittedPlayer.includes("vlc")) { - currentMetadata = "Playing video"; - } if (currentMetadata !== lastMetadata) { // log("Updating Metadata"); lastMetadata = currentMetadata; @@ -248,29 +239,22 @@ const updateData = (player, _playerState, _title, _artist) => { const updateMetadata = async () => { try { - // // log("Determining current player"); playersList = await getPlayers(); if (playersList.length > 0) { let playerStateMap = []; let playerDataMap = {}; - // // log("Starting for loop"); - // // log(`Player list - ${playersList}`); for (let i = 0; i <= playersList.length; i++) { player = playersList[i]; if (player) { _playerStatePromise = getPlaybackStatus(player); _metadataPromise = getMetadata(player); - // // log("Resolving promises"); - [_playerState, [_title, _artist, _id]] = await Promise.all([ - _playerStatePromise, - _metadataPromise, - ]); - // log(_id); - // // log("Promises resolved"); - if (_id) { + [_playerState, [_title, _artist, _url]] = await Promise.all( + [_playerStatePromise, _metadataPromise] + ); + if (_title || _url) { playerStateMap.push([player, _playerState]); playerDataMap[player] = { - _title, + _title: _title || _url, _artist, _playerState, }; @@ -278,14 +262,13 @@ const updateMetadata = async () => { } } - // // log(`${playerStateMap.length} eligible players found!`); let playingPlayers = playerStateMap.filter(([player, state]) => { if (state === "Playing") { return true; } return false; }); - // // log(`${playingPlayers.length} playing players found!`); + if (playingPlayers.length > 0) { if (contentRemoved) { addContent(); @@ -325,7 +308,6 @@ const updateMetadata = async () => { const updateContent = () => { if (lastStateChanged) { - // log("Updating state icon"); if (playerState === "Playing") { buttonToggle.set_child(iconPause); } else { @@ -334,7 +316,6 @@ const updateContent = () => { lastStateChanged = false; } if (lastPlayerChanged) { - // log("Updating player icon"); iconPlayer.set_icon_name(playerIcon); lastPlayerChanged = false; } @@ -347,7 +328,6 @@ const startMainLoop = () => { startTime = Date.now(); await updateMetadata(); updateContent(); - // log(`Took ${Date.now() - startTime} milliseconds`); })(); return true; }); @@ -516,13 +496,13 @@ const enable = () => { // Set childs and bind methods buttonNext.set_child(iconNext); - buttonNext.connect("button-release-event", _actionNext); + buttonNext.connect("button-release-event", playbackActions.next); buttonPrev.set_child(iconPrev); - buttonPrev.connect("button-release-event", _actionPrev); + buttonPrev.connect("button-release-event", playbackActions.prev); buttonToggle.set_child(iconPlay); - buttonToggle.connect("button-release-event", _actionToggle); + buttonToggle.connect("button-release-event", playbackActions.toggle_play); buttonLabel.connect("button-release-event", _mouseAction); buttonLabel.connect("enter-event", () => { @@ -535,8 +515,6 @@ const enable = () => { labelSeperatorStart.set_text(settings.get_string("seperator-char-start")); labelSeperatorEnd.set_text(settings.get_string("seperator-char-end")); - // buttonLabel.set_style(`width: ${maxDisplayLength}px;`); - // Initialize content updatePlayerIconEffects(); diff --git a/metadata.json b/metadata.json index 692c560..3d0960f 100644 --- a/metadata.json +++ b/metadata.json @@ -4,6 +4,6 @@ "uuid": "mediacontrols@cliffniff.github.com", "url": "https://github.com/cliffniff/media-controls", "settings-schema": "org.gnome.shell.extensions.mediacontrols", - "version": 4, + "version": 5, "shell-version": ["3.38", "40"] } diff --git a/prefs.js b/prefs.js index 2667f95..7a2f9cc 100644 --- a/prefs.js +++ b/prefs.js @@ -14,7 +14,14 @@ const [major] = Config.PACKAGE_VERSION.split("."); const shellVersion = Number.parseInt(major); const positions = ["left", "center", "right"]; -const mouseActions = ["none", "toggle_play", "play", "pause", "next", "prev"]; +const playbackActions = { + none: "None", + toggle_play: "Toggle play/pause", + play: "Play", + pause: "Pause", + next: "Next", + prev: "Previous", +}; const sepChars = [ "|...|", "[...]", @@ -240,9 +247,11 @@ function buildPrefsWidget() { placeholder_text: "Ex - '<...>'", }); - for (let i = 0; i < sepChars.length; i++) { - comboboxSepCharPresets.append(sepChars[i], sepChars[i]); - } + for (let i = 0; i < sepChars.length; i++) {} + + sepChars.forEach((sepChar) => { + comboboxSepCharPresets.append(sepChar, sepChar); + }); comboboxSepCharPresets.set_active( sepChars.indexOf( @@ -287,9 +296,9 @@ function buildPrefsWidget() { visible: true, }); - for (let i = 0; i < positions.length; i++) { - comboboxExtensionPosition.append(positions[i], positions[i]); - } + positions.forEach((position) => { + comboboxExtensionPosition.append(position, position); + }); comboboxExtensionPosition.set_active( positions.indexOf(settings.get_string("extension-position")) @@ -361,17 +370,19 @@ function buildPrefsWidget() { visible: true, }); - for (let i = 0; i < mouseActions.length; i++) { - comboboxMouseActionsLeftClick.append(mouseActions[i], mouseActions[i]); - comboboxMouseActionsRightClick.append(mouseActions[i], mouseActions[i]); - } + let playbackActionsKeys = Object.keys(playbackActions); + + playbackActionsKeys.forEach((key) => { + comboboxMouseActionsLeftClick.append(key, playbackActions[key]); + comboboxMouseActionsRightClick.append(key, playbackActions[key]); + }); comboboxMouseActionsLeftClick.set_active( - mouseActions.indexOf(settings.get_string("mouse-actions-left")) + playbackActionsKeys.indexOf(settings.get_string("mouse-actions-left")) ); comboboxMouseActionsRightClick.set_active( - mouseActions.indexOf(settings.get_string("mouse-actions-right")) + playbackActionsKeys.indexOf(settings.get_string("mouse-actions-right")) ); index++; @@ -469,16 +480,20 @@ function buildPrefsWidget() { comboboxMouseActionsLeftClick.connect("changed", (widget) => { settings.set_string( "mouse-actions-left", - mouseActions[widget.get_active()] + playbackActions[widget.get_active()] ); }); comboboxMouseActionsRightClick.connect("changed", (widget) => { settings.set_string( "mouse-actions-right", - mouseActions[widget.get_active()] + playbackActions[widget.get_active()] ); }); - scrolledWindow.set_child(widgetPrefs); + if (shellVersion < 40) { + scrolledWindow.add(widgetPrefs); + } else { + scrolledWindow.set_child(widgetPrefs); + } return scrolledWindow; }