Skip to content

Commit e0d852a

Browse files
Implement WAV file picker for custom sounds
Co-Authored-By: SamsidParty <[email protected]>
1 parent ab30649 commit e0d852a

File tree

6 files changed

+72
-17
lines changed

6 files changed

+72
-17
lines changed

TopNotify/GUI/MainCommands.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,11 @@ public static async Task OpenAppFolder(WebWindow target)
113113
{
114114
Process.Start("explorer.exe", Settings.GetAppDataFolder());
115115
}
116+
117+
[Command("OpenSoundFolder")]
118+
public static async Task OpenSoundFolder(WebWindow target)
119+
{
120+
Process.Start("explorer.exe", SoundFinder.ImportedSoundFolder);
121+
}
116122
}
117123
}

TopNotify/GUI/SoundFinder.cs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using IgniteView.Core;
2+
using IgniteView.FileDialogs;
23
using Newtonsoft.Json;
34
using System;
45
using System.Collections.Generic;
@@ -14,6 +15,8 @@ namespace TopNotify.GUI
1415
{
1516
public class SoundFinder
1617
{
18+
public static string ImportedSoundFolder => Path.Join(Settings.GetAppDataFolder(), "NotificationSounds", "imported");
19+
1720
[Command("FindSounds")]
1821
public static string FindSounds()
1922
{
@@ -23,7 +26,7 @@ public static string FindSounds()
2326

2427
// Inject Files From Music Folder Into The JSON File
2528
dynamic packToInject = soundPacks.Where((dynamic pack) => pack.ID == "custom_sound_path").FirstOrDefault();
26-
var wavFiles = GetWAVFilesInMusicFolder();
29+
var wavFiles = GetImportedWAVFiles();
2730

2831
foreach (var wavFile in wavFiles)
2932
{
@@ -38,6 +41,33 @@ public static string FindSounds()
3841
return JsonConvert.SerializeObject(soundPacks);
3942
}
4043

44+
/// <summary>
45+
/// Opens a file dialog to select a sound file and imports it
46+
/// </summary>
47+
[Command("ImportSound")]
48+
public static string[] ImportSound()
49+
{
50+
var soundPath = FileDialog.PickFile(new FileFilter("wav"));
51+
52+
if (!string.IsNullOrEmpty(soundPath) && File.Exists(soundPath) && Path.GetExtension(soundPath).ToLower() == ".wav")
53+
{
54+
GetImportedWAVFiles(); // Makes sure ImportedSoundFolder exists
55+
var soundName = Path.GetFileNameWithoutExtension(soundPath);
56+
57+
// Prevent duplicate files
58+
while (File.Exists(Path.Join(ImportedSoundFolder, soundName + ".wav")))
59+
{
60+
soundName += "_";
61+
}
62+
63+
var copiedSoundPath = Path.Join(ImportedSoundFolder, soundName + ".wav");
64+
File.Copy(soundPath, copiedSoundPath, true);
65+
return new string[] { "custom_sound_path/" + copiedSoundPath, Path.GetFileNameWithoutExtension(soundPath) };
66+
}
67+
68+
return new string[0];
69+
}
70+
4171
/// <summary>
4272
/// Plays the provided sound ID
4373
/// </summary>
@@ -50,17 +80,23 @@ public static void PreviewSound(string soundID)
5080
/// <summary>
5181
/// Returns A List Of WAV Files In The Music Folder
5282
/// </summary>
53-
public static string[] GetWAVFilesInMusicFolder()
83+
public static string[] GetImportedWAVFiles()
5484
{
5585
try
5686
{
5787
var musicFolder = Environment.ExpandEnvironmentVariables("%USERPROFILE%\\Music");
5888

89+
if (!Directory.Exists(ImportedSoundFolder)) { Directory.CreateDirectory(ImportedSoundFolder); }
90+
91+
var importedFiles = Directory.GetFiles(ImportedSoundFolder, "*.wav", SearchOption.AllDirectories);
92+
5993
// Music folder doesn't always exist https://github.com/SamsidParty/TopNotify/issues/40#issuecomment-2692353622
6094
if (Directory.Exists(musicFolder))
6195
{
62-
return Directory.GetFiles(musicFolder, "*.wav", SearchOption.AllDirectories);
96+
return Directory.GetFiles(musicFolder, "*.wav", SearchOption.AllDirectories).Concat(importedFiles).ToArray();
6397
}
98+
99+
return importedFiles;
64100
}
65101
catch { }
66102

2.5 KB
Binary file not shown.

TopNotify/src-vite/public/Meta/SoundPacks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"Name": "Your Collection",
4-
"Description": ".WAV files from your music folder will show up here",
4+
"Description": "Use your own custom .WAV files as your notification sound",
55
"ID": "custom_sound_path",
66
"Sounds": [
77
{

TopNotify/src-vite/src/CSS/NotificationSounds.css

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,13 @@
7676
height: calc(17vw);
7777
}
7878

79-
.soundItem img {
80-
height: 60px;
79+
.soundItem img, .soundItem > .soundItemButton svg {
80+
height: 50px;
81+
width: 50px;
82+
}
83+
84+
.soundItem > .soundItemButton * {
85+
color: #757DF9;
8186
}
8287

8388
.soundItem .iconButton {

TopNotify/src-vite/src/NotificationSounds.jsx

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Button, Divider } from "@chakra-ui/react";
22

33
import { Fragment, useState } from "react";
4-
import { useFirstRender } from "./Helper.jsx";
54

65
import "./CSS/NotificationSounds.css";
76

@@ -12,7 +11,8 @@ import {
1211
DrawerFooter,
1312
DrawerHeader
1413
} from "@chakra-ui/react";
15-
import {TbAlertTriangle, TbChevronDown, TbPencil, TbVolume, TbX} from "react-icons/tb";
14+
import React from "react";
15+
import { TbAlertTriangle, TbChevronDown, TbFolder, TbMusicPlus, TbPencil, TbVolume, TbX } from "react-icons/tb";
1616

1717
export default function ManageNotificationSounds() {
1818

@@ -93,7 +93,7 @@ export default function ManageNotificationSounds() {
9393
</DrawerFooter>
9494
</DrawerContent>
9595
</Drawer>
96-
<SoundPicker applySound={applySound} setIsPickerOpen={setIsPickerOpen} isOpen={isPickerOpen}></SoundPicker>
96+
<SoundPicker applySound={applySound} setIsPickerOpen={setIsPickerOpen} key={window.soundPickerReferenceID + isPickerOpen || "soundPicker"} isOpen={isPickerOpen}></SoundPicker>
9797
</div>
9898
);
9999
}
@@ -118,14 +118,7 @@ function AppReferenceSoundItem(props) {
118118

119119
function SoundPicker(props) {
120120

121-
let [soundPacks, setSoundPacks] = useState([]);
122-
123-
if (useFirstRender()) {
124-
setTimeout(async () => {
125-
let newSounds = JSON.parse(await igniteView.commandBridge.FindSounds());
126-
setSoundPacks(newSounds);
127-
}, 0);
128-
}
121+
const soundPacks = JSON.parse(igniteView.withReact(React).useCommandResult("FindSounds") || "[]");
129122

130123
return (
131124
<Drawer
@@ -182,6 +175,21 @@ function SoundPack(props) {
182175
);
183176
})
184177
}
178+
{
179+
props.soundPack.Name == "Your Collection" && (
180+
<div className="soundItem" key={"add"}>
181+
<Button onClick={async () => {
182+
let result = await igniteView.commandBridge.ImportSound();
183+
if (result.length == 2) {
184+
props.applySound({ Path: result[0], Name: result[1] });
185+
}
186+
}} className="soundItemButton">
187+
<TbMusicPlus/>
188+
</Button>
189+
<h5>Import&nbsp;<Button onClick={() => igniteView.commandBridge.OpenSoundFolder()} className="iconButton"><TbFolder/></Button></h5>
190+
</div>
191+
)
192+
}
185193
</div>
186194
</div>
187195
);

0 commit comments

Comments
 (0)