1- using DisplayProfileManager ;
1+ using DisplayProfileManager ;
22using Microsoft . Win32 ;
3+ using Newtonsoft . Json ;
4+ using Newtonsoft . Json . Linq ;
35using UDPServerManagerForm ;
46
57namespace DAIRemote ;
@@ -8,7 +10,9 @@ public partial class DAIRemoteApplicationUI : Form
810{
911 private readonly TrayIconManager trayIconManager ;
1012 private Form profileDialog ;
13+ private Form audioDialog ;
1114 private ListBox profileListBox ;
15+ private ListBox audioListBox ;
1216 private AudioManager . AudioDeviceManager audioManager ;
1317 public static event EventHandler < NotificationEventArgs > NotificationRequested ;
1418
@@ -38,6 +42,7 @@ public DAIRemoteApplicationUI()
3842 SetStartupStatus ( ) ; // Checks onStartup default value to set
3943 InitializeDisplayProfilesLayouts ( ) ; // Initialize load and delete display profile flow layouts
4044 InitializeDisplayProfilesList ( ) ; // Initialize the form & listbox used for showing display profiles list
45+ InitializeAudioDevicesList ( ) ;
4146
4247 // Listen for display profile changes
4348 DisplayProfileWatcher . Initialize ( DisplayConfig . GetDisplayProfilesDirectory ( ) ) ;
@@ -142,6 +147,10 @@ private void ShowProfileOptionsMenu(object sender)
142147 ToolStripMenuItem setHotkeyItem = new ( "Set Hotkey" ) ;
143148 setHotkeyItem . Click += ( s , e ) => SetHotkeyProfileButton_Click ( optionsButton , e ) ;
144149
150+ // Add Set Default Audio Device option
151+ ToolStripMenuItem setDefaultAudioDevice = new ( "Set Audio" ) ;
152+ setDefaultAudioDevice . Click += ( s , e ) => SetDefaultAudioDevice_Click ( optionsButton , e ) ;
153+
145154 // Add Save option
146155 ToolStripMenuItem saveItem = new ( "Overwrite" ) ;
147156 saveItem . Click += ( s , e ) => SaveProfileButton_Click ( optionsButton , e ) ;
@@ -153,6 +162,7 @@ private void ShowProfileOptionsMenu(object sender)
153162 // Add options to the context menu
154163 _ = optionsMenu . Items . Add ( renameItem ) ;
155164 _ = optionsMenu . Items . Add ( setHotkeyItem ) ;
165+ _ = optionsMenu . Items . Add ( setDefaultAudioDevice ) ;
156166 _ = optionsMenu . Items . Add ( saveItem ) ;
157167 _ = optionsMenu . Items . Add ( deleteItem ) ;
158168
@@ -187,13 +197,30 @@ private void RenameProfileButton_Click(object sender, EventArgs e)
187197 ) ;
188198 }
189199
200+ // Set hotkey tooltip function
190201 private void SetHotkeyProfileButton_Click ( object sender , EventArgs e )
191202 {
192203 string profilePath = ( ( sender as Button ) ? . Tag ?? ( sender as Panel ) ? . Tag ) . ToString ( ) ;
193204 trayIconManager . GetHotkeyManager ( ) . ShowHotkeyInput ( Path . GetFileNameWithoutExtension ( profilePath ) , ( ) => DisplayConfig . SetDisplaySettings ( profilePath ) ) ;
194205 trayIconManager . RefreshSystemTray ( ) ;
195206 }
196207
208+ // Set hotkey main application pop up function to allow choosing a profile from a list and setting the hotkey.
209+ private void BtnSetDisplayProfileHotkey_click ( object sender , EventArgs e )
210+ {
211+ string fileName = ShowDisplayProfilesList ( DisplayConfig . GetDisplayProfilesDirectory ( ) ) ;
212+ if ( ! string . IsNullOrEmpty ( fileName ) )
213+ {
214+ trayIconManager . GetHotkeyManager ( ) . ShowHotkeyInput ( fileName , ( ) => DisplayConfig . SetDisplaySettings ( DisplayConfig . GetFullDisplayProfilePath ( fileName ) ) ) ;
215+ trayIconManager . RefreshSystemTray ( ) ;
216+ }
217+ }
218+
219+ private void SetDefaultAudioDevice_Click ( object sender , EventArgs e )
220+ {
221+ ShowAudioDevicesList ( ( ( sender as Button ) ? . Tag ?? ( sender as Panel ) ? . Tag ) . ToString ( ) ) ;
222+ }
223+
197224 private void SaveProfileButton_Click ( object sender , EventArgs e )
198225 {
199226 string profilePath = ( ( sender as Button ) ? . Tag ?? ( sender as Panel ) ? . Tag ) . ToString ( ) ;
@@ -370,6 +397,42 @@ private void InitializeDisplayProfilesList()
370397 profileDialog . Controls . Add ( actionButton ) ;
371398 }
372399
400+ private void InitializeAudioDevicesList ( )
401+ {
402+ audioDialog = new ( )
403+ {
404+ Text = "Audio Devices" ,
405+ Size = new Size ( 400 , 300 ) ,
406+ StartPosition = FormStartPosition . CenterScreen
407+ } ;
408+
409+ audioListBox = new ( )
410+ {
411+ Dock = DockStyle . Fill
412+ } ;
413+ audioDialog . Controls . Add ( audioListBox ) ;
414+
415+ System . Windows . Forms . Button actionButton = new ( )
416+ {
417+ Text = "Select Device" ,
418+ Dock = DockStyle . Bottom ,
419+ Height = 50 ,
420+ FlatStyle = FlatStyle . Flat ,
421+ } ;
422+
423+ actionButton . Click += ( s , e ) =>
424+ {
425+ if ( audioListBox . SelectedItem == null )
426+ {
427+ _ = MessageBox . Show ( "Please select a new default audio device." ) ;
428+ return ;
429+ }
430+ audioDialog . DialogResult = DialogResult . OK ;
431+ audioDialog . Close ( ) ;
432+ } ;
433+ audioDialog . Controls . Add ( actionButton ) ;
434+ }
435+
373436 private string ShowDisplayProfilesList ( string folderPath )
374437 {
375438 if ( ! Directory . Exists ( folderPath ) )
@@ -384,13 +447,32 @@ private string ShowDisplayProfilesList(string folderPath)
384447 return profileListBox . SelectedItem ? . ToString ( ) ;
385448 }
386449
387- private void BtnSetDisplayProfileHotkey_click ( object sender , EventArgs e )
450+ private void ShowAudioDevicesList ( string profilePath )
388451 {
389- string fileName = ShowDisplayProfilesList ( DisplayConfig . GetDisplayProfilesDirectory ( ) ) ;
390- if ( ! string . IsNullOrEmpty ( fileName ) )
452+ // Read current settings first
453+ string json = File . ReadAllText ( profilePath ) ;
454+ JObject displaySettings = JObject . Parse ( json ) ;
455+ string currentDefaultDevice = displaySettings [ "defaultAudioDevice" ] ? . ToString ( ) ;
456+
457+ // Populate the list
458+ audioListBox . Items . Clear ( ) ;
459+ audioListBox . Items . AddRange ( audioManager . ActiveDeviceNames . Cast < object > ( ) . ToArray ( ) ) ;
460+
461+ // Set the current default device as selected if it exists in the list
462+ if ( ! string . IsNullOrEmpty ( currentDefaultDevice ) )
391463 {
392- trayIconManager . GetHotkeyManager ( ) . ShowHotkeyInput ( fileName , ( ) => DisplayConfig . SetDisplaySettings ( DisplayConfig . GetFullDisplayProfilePath ( fileName ) ) ) ;
393- trayIconManager . RefreshSystemTray ( ) ;
464+ int index = audioListBox . Items . Cast < string > ( ) . ToList ( ) . IndexOf ( currentDefaultDevice ) ;
465+ if ( index >= 0 )
466+ {
467+ audioListBox . SelectedIndex = index ;
468+ }
469+ }
470+
471+ // Show dialog and check result
472+ if ( audioDialog . ShowDialog ( ) == DialogResult . OK && audioListBox . SelectedItem != null )
473+ {
474+ displaySettings [ "defaultAudioDevice" ] = audioListBox . SelectedItem . ToString ( ) ;
475+ File . WriteAllText ( profilePath , displaySettings . ToString ( Formatting . Indented ) ) ;
394476 }
395477 }
396478
@@ -402,4 +484,4 @@ private void DAIRemoteApplicationUI_Resize(object sender, EventArgs e)
402484 trayIconManager . minimized = true ;
403485 }
404486 }
405- }
487+ }
0 commit comments