Skip to content

Commit

Permalink
🔖 V13
Browse files Browse the repository at this point in the history
  • Loading branch information
koolskateguy89 committed Aug 20, 2022
1 parent dff17e1 commit 2d78914
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 112 deletions.
9 changes: 9 additions & 0 deletions spotify-controller@koolskateguy89/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ class Toggle extends St.Icon {

const ControlBar = GObject.registerClass(
class ControlBar extends PanelMenu.Button {
previous;
next;
toggle;
buttons;
bar;

_init(spotify) {
super._init();

Expand Down Expand Up @@ -214,6 +220,9 @@ class ControlBar extends PanelMenu.Button {
});

class Extension {
spotify;
controlBar;

constructor() {
}

Expand Down
227 changes: 116 additions & 111 deletions spotify-controller@koolskateguy89/mediaPlayer.js
Original file line number Diff line number Diff line change
@@ -1,111 +1,116 @@
const { Gio } = imports.gi;

// thanks esenliyim - https://github.com/esenliyim/sp-tray/blob/master/panelButton.js
// https://wiki.gnome.org/Gjs/Examples/DBusClient
// https://www.andyholmes.ca/articles/dbus-in-gjs.html#high-level-proxies
const path = '/org/mpris/MediaPlayer2';
const playerInterface = `
<node>
<interface name="org.mpris.MediaPlayer2.Player">
<property name="PlaybackStatus" type="s" access="read" />
<method name="Next" />
<method name="Previous" />
<method name="PlayPause" />
</interface>
</node>
`;

// Declare the proxy class based on the interface
const PlayerProxy = Gio.DBusProxy.makeProxyWrapper(playerInterface);

/**
* A Gio.DBuxProxy of the `org.mpris.MediaPlayer2.Player` interface.
* @typedef {Object} MediaPlayerProxy
* @property {string} PlaybackStatus
*/

/**
* @callback playerConsumer
* @param {MediaPlayer} player
*/

/**
* A non-player specific wrapper of a proxy for a media player.
* @todo Write the documentation.
*/
class MediaPlayer {
/**
* @param {playerConsumer=} onAppeared
* @param {playerConsumer=} onVanished
* @param {playerConsumer=} onPropertiesChanged
*/
constructor(onAppeared, onVanished, onPropertiesChanged) {
this.onAppeared = onAppeared;
this.onVanished = onVanished;
this.onPropertiesChanged = onPropertiesChanged;

this.proxy = null;
this.proxySignals = [];
}

/**
* @param {string} dest - The well-known name
*/
setupProxy(dest = 'org.mpris.MediaPlayer2.spotify') {
if (this.proxy)
return;

try {
// Get the MediaPlayer instance from the bus
this.proxy = new PlayerProxy(Gio.DBus.session, dest, path);
} catch (e) {
logError(e);
return;
}

this.proxySignals.push(this.proxy.connect(
"g-properties-changed",
(proxy, changed, invalidated) => {
this.onPropertiesChanged?.(this);
}
));

// https://www.andyholmes.ca/articles/dbus-in-gjs.html#low-level-proxies
this.proxySignals.push(this.proxy.connect(
"notify::g-name-owner",
(proxy, pspec) => {
if (proxy.g_name_owner === null) {
log(`${proxy.g_name} has vanished`);
this.onVanished?.(this);
} else {
log(`${proxy.g_name} has appeared`);
this.onAppeared?.(this);
}
}
));
}

destroy() {
this.proxySignals.forEach(signal => this.proxy.disconnect(signal));
}

get isActive() {
return this.proxy.g_name_owner !== null;
}

get isPlaying() {
return this.isActive && this.proxy.PlaybackStatus === 'Playing';
}

next() {
this.proxy.NextSync();
}

previous() {
this.proxy.PreviousSync();
}

playPause() {
this.proxy.PlayPauseSync();
}
}
const { Gio } = imports.gi;

// thanks esenliyim - https://github.com/esenliyim/sp-tray/blob/master/panelButton.js
// https://wiki.gnome.org/Gjs/Examples/DBusClient
// https://www.andyholmes.ca/articles/dbus-in-gjs.html#high-level-proxies
const path = '/org/mpris/MediaPlayer2';
const playerInterface = `
<node>
<interface name="org.mpris.MediaPlayer2.Player">
<property name="PlaybackStatus" type="s" access="read" />
<method name="Next" />
<method name="Previous" />
<method name="PlayPause" />
</interface>
</node>
`;

// Declare the proxy class based on the interface
const PlayerProxy = Gio.DBusProxy.makeProxyWrapper(playerInterface);

/**
* A Gio.DBuxProxy of the `org.mpris.MediaPlayer2.Player` interface.
* @typedef {Object} MediaPlayerProxy
* @property {string} PlaybackStatus
*/

/**
* @callback playerConsumer
* @param {MediaPlayer} player
*/

/**
* A non-player specific wrapper of a proxy for a media player.
* @todo Write the documentation.
*/
class MediaPlayer {
// this.onAppeared = onAppeared;
// this.onVanished = onVanished;
// this.onPropertiesChanged = onPropertiesChanged;


/**
* @param {playerConsumer=} onAppeared
* @param {playerConsumer=} onVanished
* @param {playerConsumer=} onPropertiesChanged
*/
constructor(onAppeared, onVanished, onPropertiesChanged) {
this.onAppeared = onAppeared;
this.onVanished = onVanished;
this.onPropertiesChanged = onPropertiesChanged;

this.proxy = null;
this.proxySignals = [];
}

/**
* @param {string} dest - The well-known name
*/
setupProxy(dest = 'org.mpris.MediaPlayer2.spotify') {
if (this.proxy)
return;

try {
// Get the MediaPlayer instance from the bus
this.proxy = new PlayerProxy(Gio.DBus.session, dest, path);
} catch (e) {
logError(e);
return;
}

this.proxySignals.push(this.proxy.connect(
"g-properties-changed",
(proxy, changed, invalidated) => {
this.onPropertiesChanged?.(this);
}
));

// https://www.andyholmes.ca/articles/dbus-in-gjs.html#low-level-proxies
this.proxySignals.push(this.proxy.connect(
"notify::g-name-owner",
(proxy, pspec) => {
if (proxy.g_name_owner === null) {
log(`${proxy.g_name} has vanished`);
this.onVanished?.(this);
} else {
log(`${proxy.g_name} has appeared`);
this.onAppeared?.(this);
}
}
));
}

destroy() {
this.proxySignals.forEach(signal => this.proxy.disconnect(signal));
}

get isActive() {
return this.proxy.g_name_owner !== null;
}

get isPlaying() {
return this.isActive && this.proxy.PlaybackStatus === 'Playing';
}

next() {
this.proxy.NextSync();
}

previous() {
this.proxy.PreviousSync();
}

playPause() {
this.proxy.PlayPauseSync();
}
}
2 changes: 1 addition & 1 deletion spotify-controller@koolskateguy89/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"settings-schema": "org.gnome.shell.extensions.spotify-controller",
"url": "https://github.com/koolskateguy89/gnome-shell-extension-spotify-controller",
"uuid": "spotify-controller@koolskateguy89",
"version": 12
"version": 13
}

0 comments on commit 2d78914

Please sign in to comment.