Skip to content

Commit

Permalink
refactor: renderer notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
duzda committed Aug 25, 2024
1 parent 0a83260 commit c7f386f
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 21 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; connect-src 'self' https://api.deezer.com; style-src 'unsafe-inline'">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; connect-src 'self' https://api.deezer.com; style-src 'unsafe-inline'; img-src 'self' https://e-cdns-images.dzcdn.net">
<title>Deezer Enhanced</title>
</head>
<body>
Expand Down
3 changes: 3 additions & 0 deletions src/common/channels/notifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Notifications from electron don't support icons via http, therefore
// we need a channel :( https://github.com/electron/electron/pull/43377
export const NOTIFICATIONS_CREATE = 'notifications:create';
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ if (!gotTheLock) {

loadBounds(mainWindow);
initializeSettings(mainWindow, view);
initializePlayer(view);
initializePlayer(mainWindow, view);
initializeDownloads(mainWindow, view);
createHistoryHandles(view);
createKeyboardHandles(view);
Expand Down
39 changes: 21 additions & 18 deletions src/main/mpris.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-param-reassign */
import Player from 'mpris-service';
import { Episode, Song } from 'src/common/types/deezer';
import { BrowserView, ipcMain, Notification } from 'electron';
import { BrowserView, BrowserWindow, ipcMain } from 'electron';
import {
MPRIS_NEXT_SONG,
MPRIS_PAUSE,
Expand All @@ -28,6 +28,7 @@ import {
} from './utils/urls';
import { isDiscordEnabled, setDiscordActivity } from './discord';
import { getSettings } from './settings';
import { NOTIFICATIONS_CREATE } from '../common/channels/notifications';

let songId = '';

Expand Down Expand Up @@ -107,7 +108,7 @@ const updateMetadataEpisode = (player: Player, data: Episode) => {
};
};

const createMprisListeners = (player: Player) => {
const createMprisListeners = (player: Player, window: BrowserWindow) => {
ipcMain.on(MPRIS_READ_SONG, (_, data) => {
if (!data) return;
if (data.SNG_ID) {
Expand All @@ -128,14 +129,15 @@ const createMprisListeners = (player: Player) => {
}

if (getSettings().enableNotifications && songData.SNG_ID !== songId) {
new Notification({
title: songData.SNG_TITLE,
body: songData.ART_NAME,
icon:
DEEZER_SONG_ART_URL +
window.webContents.send(
NOTIFICATIONS_CREATE,
songData.SNG_TITLE,
songData.ART_NAME,
DEEZER_SONG_ART_URL +
songData.ALB_PICTURE +
DEEZER_NOTIFICATION_RESOLUTION,
}).show();
DEEZER_NOTIFICATION_RESOLUTION
);

songId = songData.SNG_ID;
}
} else if (data.EPISODE_ID) {
Expand All @@ -159,14 +161,15 @@ const createMprisListeners = (player: Player) => {
getSettings().enableNotifications &&
episodeData.EPISODE_ID !== songId
) {
new Notification({
title: episodeData.EPISODE_TITLE,
body: episodeData.SHOW_NAME,
icon:
DEEZER_SONG_ART_URL +
window.webContents.send(
NOTIFICATIONS_CREATE,
episodeData.EPISODE_TITLE,
episodeData.SHOW_NAME,
DEEZER_SONG_ART_URL +
episodeData.SHOW_ART_MD5 +
DEEZER_NOTIFICATION_RESOLUTION,
}).show();
DEEZER_NOTIFICATION_RESOLUTION
);

songId = episodeData.EPISODE_ID;
}
}
Expand All @@ -191,7 +194,7 @@ const createMprisListeners = (player: Player) => {
});
};

export const initializePlayer = (view: BrowserView) => {
export const initializePlayer = (window: BrowserWindow, view: BrowserView) => {
const player = new Player({
name: 'Deezer',
identity: 'Deezer media player',
Expand All @@ -213,6 +216,6 @@ export const initializePlayer = (view: BrowserView) => {
return player.playbackStatus === Player.PLAYBACK_STATUS_PLAYING;
};

createMprisListeners(player);
createMprisListeners(player, window);
createMprisHandles(player, view);
};
14 changes: 14 additions & 0 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { KEYBOARD_SEND_KEYPRESS } from './common/channels/keyboard';
import { SerializedKeyboardEvent } from './common/types/serializedKeyboardEvent';
import { ExecStatus } from './common/types/deemix';
import { NOTIFICATIONS_CREATE } from './common/channels/notifications';

export const historyAPI = {
goForward: () => ipcRenderer.send(HISTORY_GO_FORWARD),
Expand Down Expand Up @@ -53,9 +54,22 @@ export const keyboardAPI = {
ipcRenderer.send(KEYBOARD_SEND_KEYPRESS, event),
};

export const notificationsAPI = {
onNotificationCreate: (
callback: (title: string, body: string, icon: string) => void
) => {
ipcRenderer.on(
NOTIFICATIONS_CREATE,
(_, title: string, body: string, icon: string) =>
callback(title, body, icon)
);
},
};

contextBridge.exposeInMainWorld('renderer', {
historyAPI,
downloadsAPI,
settingsAPI,
keyboardAPI,
notificationsAPI,
});
4 changes: 4 additions & 0 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ function App(): React.JSX.Element {
metaKey: event.metaKey,
});
});

window.renderer.notificationsAPI.onNotificationCreate(
(title, body, icon) => new Notification(title, { body, icon })
);
}, []);

useEffect(() => {
Expand Down
9 changes: 8 additions & 1 deletion src/renderer/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { downloadsAPI, historyAPI, keyboardAPI, settingsAPI } from '../preload';
import {
downloadsAPI,
historyAPI,
keyboardAPI,
notificationsAPI,
settingsAPI,
} from '../preload';

declare global {
interface Window {
Expand All @@ -7,6 +13,7 @@ declare global {
downloadsAPI: typeof downloadsAPI;
settingsAPI: typeof settingsAPI;
keyboardAPI: typeof keyboardAPI;
notificationsAPI: typeof notificationsAPI;
};
}
}

0 comments on commit c7f386f

Please sign in to comment.