Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add playback commands #36

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ if (process.env.NODE_ENV === "development") {
);
}

let isTrackPlaying: boolean;

export default class SoundscapesPlugin extends Plugin {
settings: SoundscapesPluginSettings;
settingsObservable: Observable;
Expand Down Expand Up @@ -60,6 +62,7 @@ export default class SoundscapesPlugin extends Plugin {

async onload() {
await this.loadSettings();
isTrackPlaying = this.settings.autoplay;

this.currentTrackIndex = this.settings.currentTrackIndex; // Persist the current track when closing and opening
this.settingsObservable = new Observable(this.settings);
Expand Down Expand Up @@ -103,6 +106,37 @@ export default class SoundscapesPlugin extends Plugin {
this.indexMusicLibrary();
}, 1000);
}

// adding commands to be run from command palette or bind a hotkey to them
this.addCommand({
id: "go-to-next-track",
name: "Go to next track",
callback: () => {
this.next();
},
});

this.addCommand({
id: "go-to-previous-track",
name: "Go to previous track",
callback: () => {
this.previous();
},
});

this.addCommand({
id: "Play/Pause",
name: "Play/Pause current track",
callback: () => {
if (isTrackPlaying) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of introducing a new variable, you should be able to use:

if(this.localPlayerStateObservable.getValue().playerState === PLAYER_STATE.PLAYING)

this.pause();
isTrackPlaying = false;
} else {
this.play();
isTrackPlaying = true;
}
},
});
}

onunload() {
Expand Down Expand Up @@ -349,11 +383,17 @@ export default class SoundscapesPlugin extends Plugin {

this.playButton = this.statusBarItem.createEl("button", {});
setIcon(this.playButton, "play");
this.playButton.onclick = () => this.play();
this.playButton.onclick = () => {
this.play();
isTrackPlaying = true;
};

this.pauseButton = this.statusBarItem.createEl("button", {});
setIcon(this.pauseButton, "pause");
this.pauseButton.onclick = () => this.pause();
this.pauseButton.onclick = () => {
this.pause();
isTrackPlaying = false;
};

this.nextButton = this.statusBarItem.createEl("button", {
cls: "soundscapesroot-nextbutton",
Expand Down
Loading