Skip to content

Commit 1ed0104

Browse files
author
Ruizhe Pang
committed
Make the notification volume adjustable
1 parent 3a21348 commit 1ed0104

File tree

6 files changed

+55
-3
lines changed

6 files changed

+55
-3
lines changed

src/renderer/__mocks__/state-mocks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ const mockNotificationSettings: NotificationSettingsState = {
9393
markAsDoneOnOpen: false,
9494
markAsDoneOnUnsubscribe: false,
9595
delayNotificationState: false,
96+
notificationVolume: 20,
9697
};
9798

9899
const mockSystemSettings: SystemSettingsState = {

src/renderer/components/settings/NotificationSettings.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,26 @@ export const NotificationSettings: FC = () => {
246246
</Text>
247247
}
248248
/>
249+
250+
<Box>
251+
<Text as="label" htmlFor="notificationVolume">
252+
Notification Volume: {settings.notificationVolume}%
253+
</Text>
254+
<input
255+
id="notificationVolume"
256+
type="range"
257+
min={0}
258+
max={100}
259+
step={1}
260+
value={settings.notificationVolume}
261+
onChange={(evt) =>
262+
updateSetting(
263+
'notificationVolume',
264+
Number.parseInt(evt.target.value, 10),
265+
)
266+
}
267+
/>
268+
</Box>
249269
</Stack>
250270
</fieldset>
251271
);

src/renderer/context/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const defaultNotificationSettings: NotificationSettingsState = {
8585
markAsDoneOnOpen: false,
8686
markAsDoneOnUnsubscribe: false,
8787
delayNotificationState: false,
88+
notificationVolume: 20,
8889
};
8990

9091
const defaultSystemSettings: SystemSettingsState = {

src/renderer/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export interface NotificationSettingsState {
8181
markAsDoneOnOpen: boolean;
8282
markAsDoneOnUnsubscribe: boolean;
8383
delayNotificationState: boolean;
84+
notificationVolume: number;
8485
}
8586

8687
export interface SystemSettingsState {

src/renderer/utils/notifications/native.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,33 @@ describe('renderer/utils/notifications/native.ts', () => {
134134
expect(window.Audio.prototype.play).toHaveBeenCalledTimes(1);
135135
});
136136
});
137+
138+
describe('triggerNativeNotifications', () => {
139+
afterEach(() => {
140+
jest.clearAllMocks();
141+
});
142+
143+
it('should raise only sound notification with correct volume', () => {
144+
const settings: SettingsState = {
145+
...defaultSettings,
146+
playSound: true,
147+
showNotifications: false,
148+
notificationVolume: 80,
149+
};
150+
151+
const raiseSoundNotificationMock = jest.spyOn(
152+
native,
153+
'raiseSoundNotification',
154+
);
155+
jest.spyOn(native, 'raiseNativeNotification');
156+
157+
native.triggerNativeNotifications([], mockAccountNotifications, {
158+
auth: mockAuth,
159+
settings,
160+
});
161+
162+
expect(raiseSoundNotificationMock).toHaveBeenCalledWith(0.8);
163+
expect(native.raiseNativeNotification).not.toHaveBeenCalled();
164+
});
165+
});
137166
});

src/renderer/utils/notifications/native.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const triggerNativeNotifications = (
4848
}
4949

5050
if (state.settings.playSound) {
51-
raiseSoundNotification();
51+
raiseSoundNotification(state.settings.notificationVolume / 100);
5252
}
5353

5454
if (state.settings.showNotifications) {
@@ -86,7 +86,7 @@ export const raiseNativeNotification = (notifications: Notification[]) => {
8686
return nativeNotification;
8787
};
8888

89-
export const raiseSoundNotification = () => {
89+
export const raiseSoundNotification = (volume = 0.2) => {
9090
const audio = new Audio(
9191
path.join(
9292
__dirname,
@@ -96,6 +96,6 @@ export const raiseSoundNotification = () => {
9696
Constants.NOTIFICATION_SOUND,
9797
),
9898
);
99-
audio.volume = 0.2;
99+
audio.volume = volume;
100100
audio.play();
101101
};

0 commit comments

Comments
 (0)