From 8b0e7f163d9b16e575062d79069cf4cee44a8357 Mon Sep 17 00:00:00 2001 From: richardhbtz Date: Fri, 15 Nov 2024 20:26:00 -0300 Subject: [PATCH] Implement macOS menu logic and streamline related features - 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. --- .gitignore | 2 +- README.md | 1 - package.json | 23 +++++++++++------------ src/lastfm/lastfm.ts | 1 - src/macos/menu.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ src/main.ts | 31 +++++++++++++++++-------------- 6 files changed, 71 insertions(+), 29 deletions(-) create mode 100644 src/macos/menu.ts diff --git a/.gitignore b/.gitignore index 7e05c55..f9ecf97 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ engine.bin node_modules -dist +build tsc **/.DS_Store diff --git a/README.md b/README.md index f2367c3..fae91fc 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/package.json b/package.json index c82e738..aa4e7be 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "soundcloud-rpc", - "version": "0.0.6", + "version": "0.0.7", "description": "🎵 A Soundcloud client with Discord Rich Presence support", "author": { "name": "Richard Habitzreuter", @@ -8,15 +8,14 @@ }, "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", @@ -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", @@ -66,4 +65,4 @@ "electron-store": "^8.1.0", "electron-updater": "^6.3.4" } -} +} \ No newline at end of file diff --git a/src/lastfm/lastfm.ts b/src/lastfm/lastfm.ts index 0bef0fc..7d7221b 100644 --- a/src/lastfm/lastfm.ts +++ b/src/lastfm/lastfm.ts @@ -153,7 +153,6 @@ const trackChanged = (current: any, previous: any): boolean => { async function updateNowPlaying(trackInfo: { author: any; title: any }, store: ElectronStore): Promise { const sessionKey = store.get('lastFmSessionKey'); if (!sessionKey) { - console.error('No Last.fm session key found'); return; } const apiKey = store.get('lastFmApiKey') as string; diff --git a/src/macos/menu.ts b/src/macos/menu.ts new file mode 100644 index 0000000..6325f71 --- /dev/null +++ b/src/macos/menu.ts @@ -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); +} + diff --git a/src/main.ts b/src/main.ts index 34648d3..e740ca7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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'; @@ -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'); @@ -27,6 +28,7 @@ export interface Info { autoReconnect: boolean; } + autoUpdater.autoDownload = true; autoUpdater.autoInstallOnAppQuit = true; @@ -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'); @@ -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')) { @@ -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) { @@ -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()); }