Skip to content

Commit

Permalink
Implement macOS menu logic and streamline related features
Browse files Browse the repository at this point in the history
- Added `setupDarwinMenu` for macOS-specific menu handling.
- Removed manual `Menu.setApplicationMenu` call in favor of modular setup.
- Cleaned up unused keyboard shortcuts, including Shift + F4 for resetting Last.fm keys.
- Updated Discord RPC activity with conditional display options for small SoundCloud icon.
- Simplified Last.fm integration by removing redundant error logging.
  • Loading branch information
richardhbtz committed Nov 15, 2024
1 parent e4fdf6e commit 8b0e7f1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
engine.bin
node_modules
dist
build
tsc
**/.DS_Store
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Before installing and running this app, you must have [Node.js](https://nodejs.o
| F2 | Toggles adblocker |
| F3 | Shows the proxy config window |
| F4 | Insert last.fm api keys for scrobbling functionality |
| Shift + F4 | Reset last.fm keys in case of errors |
| Ctrl + B or Command + B | Goes back to the previous web page |
| Ctrl + F or Command + F | Goes forward a web page |

Expand Down
23 changes: 11 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
{
"name": "soundcloud-rpc",
"version": "0.0.6",
"version": "0.0.7",
"description": "🎵 A Soundcloud client with Discord Rich Presence support",
"author": {
"name": "Richard Habitzreuter",
"email": "richardhabitzreuter@icloud.com"
},
"license": "MIT",
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"lint": "eslint -c .eslintrc --ext .ts ./src",
"start": "npm run build && electron ./tsc/main.js",
"dist": "npm run build && electron-builder --dir",
"dist-win": "npm run build && electron-builder -w nsis",
"dist-mac": "npm run build && electron-builder -m dmg",
"dist-linux-deb": "npm run build && electron-builder -l deb",
"dist-linux-appimage": "npm run build && electron-builder -l appimage"
"dev": "npm run build && electron ./tsc/main.js",
"build": "tsc && electron-builder --dir",
"build-win": "tsc && electron-builder -w nsis",
"build-win-ptb": "tsc && electron-builder -w portable",
"build-mac": "tsc && electron-builder -m dmg",
"build-linux-deb": "tsc && electron-builder -l deb",
"build-linux-appimage": "tsc && electron-builder -l appimage"
},
"main": "./tsc/main.js",
"repository": "https://github.com/richardhbtz/soundcloud-rpc",
Expand All @@ -32,12 +31,12 @@
"tsc/**/*"
],
"directories": {
"output": "dist"
"output": "build"
},
"mac": {
"target": "dmg",
"icon": "assets/icons/soundcloud-mac.icns",
"artifactName": "soundcloud-${version}.installer-mac.${ext}"
"artifactName": "soundcloud-${version}.mac.${ext}"
},
"win": {
"target": "nsis",
Expand Down Expand Up @@ -66,4 +65,4 @@
"electron-store": "^8.1.0",
"electron-updater": "^6.3.4"
}
}
}
1 change: 0 additions & 1 deletion src/lastfm/lastfm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ const trackChanged = (current: any, previous: any): boolean => {
async function updateNowPlaying(trackInfo: { author: any; title: any }, store: ElectronStore): Promise<void> {
const sessionKey = store.get('lastFmSessionKey');
if (!sessionKey) {
console.error('No Last.fm session key found');
return;
}
const apiKey = store.get('lastFmApiKey') as string;
Expand Down
42 changes: 42 additions & 0 deletions src/macos/menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { app, Menu, shell, MenuItemConstructorOptions } from 'electron';

const isMac = process.platform === 'darwin';

const template: Electron.MenuItemConstructorOptions[] = [{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'selectAll' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'delete' },
]
},
{
label: 'View',
submenu: [
{ role: 'reload' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
},
{ role: 'window', submenu: [{ role: 'minimize' }, { role: 'quit' }] },
{
label: 'Help',
submenu: [{
label: 'Learn More',
click() {
require('electron').shell.openExternal('https://github.com/richardhbtz/soundcloud-rpc');
}
}]
}
];

export function setupDarwinMenu(): void {
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}

31 changes: 17 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Store = require('electron-store');

import { app, BrowserWindow, Menu, dialog } from 'electron';
import { app, BrowserWindow, dialog } from 'electron';
import { ElectronBlocker, fullLists } from '@cliqz/adblocker-electron';
import { readFileSync, writeFileSync } from 'fs';

Expand All @@ -14,6 +14,7 @@ import { setupLastFmConfig } from './lastfm/lastfm-auth';
import type { ScrobbleState } from './lastfm/lastfm';

import fetch from 'cross-fetch';
import { setupDarwinMenu } from './macos/menu';

const { autoUpdater } = require('electron-updater');
const localShortcuts = require('electron-localshortcut');
Expand All @@ -27,6 +28,7 @@ export interface Info {
autoReconnect: boolean;
}


autoUpdater.autoDownload = true;
autoUpdater.autoInstallOnAppQuit = true;

Expand All @@ -48,15 +50,15 @@ const info: Info = {

info.rpc.login().catch(console.error);

Menu.setApplicationMenu(null);
setupDarwinMenu();

let mainWindow: BrowserWindow | null;
let blocker: ElectronBlocker;
let currentScrobbleState: ScrobbleState | null = null;
let displayWhenIdling = false; // Whether to display a status message when music is paused
let displaySCSmallIcon = false; // Whether to display the small SoundCloud logo

async function createWindow() {
let displayWhenIdling = false; // Whether to display a status message when music is paused

let bounds = store.get('bounds');
let maximazed = store.get('maximazed');

Expand All @@ -81,10 +83,11 @@ async function createWindow() {

// Load the SoundCloud website
mainWindow.loadURL('https://soundcloud.com/discover');
autoUpdater.checkForUpdates();

const executeJS = (script: string) => mainWindow.webContents.executeJavaScript(script);

autoUpdater.checkForUpdates();

// Wait for the page to fully load
mainWindow.webContents.on('did-finish-load', async () => {
if (store.get('darkMode')) {
Expand Down Expand Up @@ -202,16 +205,21 @@ async function createWindow() {
currentScrobbleState.scrobbled = true;
}

if (!info.rpc.isConnected) {
if (await !info.rpc.login().catch(console.error)) {
return;
}
}

info.rpc.user?.setActivity({
type: ActivityType.Listening,
details: shortenString(currentTrack.title),
state: `by ${shortenString(trackInfo.author)}`,
state: `${shortenString(trackInfo.author)}`,
largeImageKey: artworkUrl.replace('50x50.', '500x500.'),
largeImageText: currentTrack.title,
startTimestamp: Date.now() - elapsedMilliseconds,
endTimestamp: Date.now() + (totalMilliseconds - elapsedMilliseconds),
smallImageKey: 'soundcloud-logo',
smallImageText: 'SoundCloud',
smallImageKey: displaySCSmallIcon ? 'soundcloud-logo' : '',
smallImageText: displaySCSmallIcon ? 'SoundCloud' : '',
instance: false,
});
} else if (displayWhenIdling) {
Expand Down Expand Up @@ -264,11 +272,6 @@ async function createWindow() {
}
});

// Register Shift + F4 shortcut for force resetting the last.fm api keys
localShortcuts.register(mainWindow, 'Shift+F4', async () => {
await setupLastFmConfig(mainWindow, store);
});

localShortcuts.register(mainWindow, ['CmdOrCtrl+B', 'CmdOrCtrl+P'], () => mainWindow.webContents.goBack());
localShortcuts.register(mainWindow, ['CmdOrCtrl+F', 'CmdOrCtrl+N'], () => mainWindow.webContents.goForward());
}
Expand Down

0 comments on commit 8b0e7f1

Please sign in to comment.