Skip to content

Commit

Permalink
Fix attempt #1
Browse files Browse the repository at this point in the history
  • Loading branch information
sakithb committed Aug 29, 2021
1 parent e5cdde0 commit 0e6df05
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 88 deletions.
120 changes: 49 additions & 71 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ let onUpdateDelayChanged,

let mainLoop;
let settings;

// Tracking variables

let lastPlayer,
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -248,44 +239,36 @@ 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,
};
}
}
}

// // 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();
Expand Down Expand Up @@ -325,7 +308,6 @@ const updateMetadata = async () => {

const updateContent = () => {
if (lastStateChanged) {
// log("Updating state icon");
if (playerState === "Playing") {
buttonToggle.set_child(iconPause);
} else {
Expand All @@ -334,7 +316,6 @@ const updateContent = () => {
lastStateChanged = false;
}
if (lastPlayerChanged) {
// log("Updating player icon");
iconPlayer.set_icon_name(playerIcon);
lastPlayerChanged = false;
}
Expand All @@ -347,7 +328,6 @@ const startMainLoop = () => {
startTime = Date.now();
await updateMetadata();
updateContent();
// log(`Took ${Date.now() - startTime} milliseconds`);
})();
return true;
});
Expand Down Expand Up @@ -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", () => {
Expand All @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"uuid": "[email protected]",
"url": "https://github.com/cliffniff/media-controls",
"settings-schema": "org.gnome.shell.extensions.mediacontrols",
"version": 4,
"version": 5,
"shell-version": ["3.38", "40"]
}
47 changes: 31 additions & 16 deletions prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
"|...|",
"[...]",
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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;
}

0 comments on commit 0e6df05

Please sign in to comment.