From 09b9e185e3500d98c7e3a41115d9f253dee74add Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Sat, 5 Dec 2020 17:04:30 +1300 Subject: [PATCH] Correcting audio selection loading in UI Was incorrectly loading audio volume on form load and also not correctly adjusting it when selecting a different audio device. --- .../UIForms/ShortcutForm.Designer.cs | 143 +++++---- DisplayMagician/UIForms/ShortcutForm.cs | 296 ++++++++++++------ 2 files changed, 279 insertions(+), 160 deletions(-) diff --git a/DisplayMagician/UIForms/ShortcutForm.Designer.cs b/DisplayMagician/UIForms/ShortcutForm.Designer.cs index a85a093..20a8430 100644 --- a/DisplayMagician/UIForms/ShortcutForm.Designer.cs +++ b/DisplayMagician/UIForms/ShortcutForm.Designer.cs @@ -44,6 +44,11 @@ namespace DisplayMagician.UIForms this.ilv_saved_profiles = new Manina.Windows.Forms.ImageListView(); this.dv_profile = new DisplayMagician.Shared.UserControls.DisplayView(); this.tabp_audio = new System.Windows.Forms.TabPage(); + this.gb_audio_volume = new System.Windows.Forms.GroupBox(); + this.rb_set_audio_volume = new System.Windows.Forms.RadioButton(); + this.rb_keep_audio_volume = new System.Windows.Forms.RadioButton(); + this.lbl_audio_volume = new System.Windows.Forms.Label(); + this.nud_audio_volume = new System.Windows.Forms.NumericUpDown(); this.btn_rescan_audio = new System.Windows.Forms.Button(); this.cb_audio_device = new System.Windows.Forms.ComboBox(); this.rb_change_audio = new System.Windows.Forms.RadioButton(); @@ -122,14 +127,11 @@ namespace DisplayMagician.UIForms this.lbl_title = new System.Windows.Forms.Label(); this.lbl_shortcut_name = new System.Windows.Forms.Label(); this.cb_autosuggest = new System.Windows.Forms.CheckBox(); - this.gb_audio_volume = new System.Windows.Forms.GroupBox(); - this.rb_set_audio_volume = new System.Windows.Forms.RadioButton(); - this.rb_keep_audio_volume = new System.Windows.Forms.RadioButton(); - this.lbl_audio_volume = new System.Windows.Forms.Label(); - this.nud_audio_volume = new System.Windows.Forms.NumericUpDown(); this.tabc_shortcut.SuspendLayout(); this.tabp_display.SuspendLayout(); this.tabp_audio.SuspendLayout(); + this.gb_audio_volume.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.nud_audio_volume)).BeginInit(); this.tabp_before.SuspendLayout(); this.pnl_start_program4.SuspendLayout(); this.pnl_start_program3.SuspendLayout(); @@ -143,8 +145,6 @@ namespace DisplayMagician.UIForms this.tabp_after.SuspendLayout(); this.groupBox1.SuspendLayout(); this.gb_display_after.SuspendLayout(); - this.gb_audio_volume.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)(this.nud_audio_volume)).BeginInit(); this.SuspendLayout(); // // btn_save @@ -303,6 +303,70 @@ namespace DisplayMagician.UIForms this.tabp_audio.TabIndex = 4; this.tabp_audio.Text = "2. Choose Audio"; // + // gb_audio_volume + // + this.gb_audio_volume.Controls.Add(this.rb_set_audio_volume); + this.gb_audio_volume.Controls.Add(this.rb_keep_audio_volume); + this.gb_audio_volume.Controls.Add(this.lbl_audio_volume); + this.gb_audio_volume.Controls.Add(this.nud_audio_volume); + this.gb_audio_volume.ForeColor = System.Drawing.Color.White; + this.gb_audio_volume.Location = new System.Drawing.Point(392, 240); + this.gb_audio_volume.Name = "gb_audio_volume"; + this.gb_audio_volume.Size = new System.Drawing.Size(429, 147); + this.gb_audio_volume.TabIndex = 10; + this.gb_audio_volume.TabStop = false; + this.gb_audio_volume.Text = "Audio Output Volume"; + this.gb_audio_volume.Visible = false; + // + // rb_set_audio_volume + // + this.rb_set_audio_volume.AutoSize = true; + this.rb_set_audio_volume.ForeColor = System.Drawing.Color.White; + this.rb_set_audio_volume.Location = new System.Drawing.Point(38, 84); + this.rb_set_audio_volume.Name = "rb_set_audio_volume"; + this.rb_set_audio_volume.Size = new System.Drawing.Size(167, 24); + this.rb_set_audio_volume.TabIndex = 13; + this.rb_set_audio_volume.Text = "Set audio volume at"; + this.rb_set_audio_volume.UseVisualStyleBackColor = true; + this.rb_set_audio_volume.CheckedChanged += new System.EventHandler(this.rb_set_audio_volume_CheckedChanged); + // + // rb_keep_audio_volume + // + this.rb_keep_audio_volume.AutoSize = true; + this.rb_keep_audio_volume.Checked = true; + this.rb_keep_audio_volume.ForeColor = System.Drawing.Color.White; + this.rb_keep_audio_volume.Location = new System.Drawing.Point(38, 41); + this.rb_keep_audio_volume.Name = "rb_keep_audio_volume"; + this.rb_keep_audio_volume.Size = new System.Drawing.Size(203, 24); + this.rb_keep_audio_volume.TabIndex = 12; + this.rb_keep_audio_volume.TabStop = true; + this.rb_keep_audio_volume.Text = "Leave audio volume as is"; + this.rb_keep_audio_volume.UseVisualStyleBackColor = true; + this.rb_keep_audio_volume.CheckedChanged += new System.EventHandler(this.rb_keep_audio_volume_CheckedChanged); + // + // lbl_audio_volume + // + this.lbl_audio_volume.AutoSize = true; + this.lbl_audio_volume.ForeColor = System.Drawing.Color.White; + this.lbl_audio_volume.Location = new System.Drawing.Point(275, 86); + this.lbl_audio_volume.Name = "lbl_audio_volume"; + this.lbl_audio_volume.Size = new System.Drawing.Size(63, 20); + this.lbl_audio_volume.TabIndex = 11; + this.lbl_audio_volume.Text = "percent"; + // + // nud_audio_volume + // + this.nud_audio_volume.Enabled = false; + this.nud_audio_volume.Location = new System.Drawing.Point(209, 84); + this.nud_audio_volume.Name = "nud_audio_volume"; + this.nud_audio_volume.Size = new System.Drawing.Size(60, 26); + this.nud_audio_volume.TabIndex = 10; + this.nud_audio_volume.Value = new decimal(new int[] { + 100, + 0, + 0, + 0}); + // // btn_rescan_audio // this.btn_rescan_audio.Enabled = false; @@ -1209,65 +1273,6 @@ namespace DisplayMagician.UIForms this.cb_autosuggest.UseVisualStyleBackColor = true; this.cb_autosuggest.CheckedChanged += new System.EventHandler(this.cb_autosuggest_CheckedChanged); // - // gb_audio_volume - // - this.gb_audio_volume.Controls.Add(this.rb_set_audio_volume); - this.gb_audio_volume.Controls.Add(this.rb_keep_audio_volume); - this.gb_audio_volume.Controls.Add(this.lbl_audio_volume); - this.gb_audio_volume.Controls.Add(this.nud_audio_volume); - this.gb_audio_volume.Location = new System.Drawing.Point(392, 240); - this.gb_audio_volume.Name = "gb_audio_volume"; - this.gb_audio_volume.Size = new System.Drawing.Size(506, 147); - this.gb_audio_volume.TabIndex = 10; - this.gb_audio_volume.TabStop = false; - this.gb_audio_volume.Visible = false; - // - // rb_set_audio_volume - // - this.rb_set_audio_volume.AutoSize = true; - this.rb_set_audio_volume.ForeColor = System.Drawing.Color.White; - this.rb_set_audio_volume.Location = new System.Drawing.Point(38, 84); - this.rb_set_audio_volume.Name = "rb_set_audio_volume"; - this.rb_set_audio_volume.Size = new System.Drawing.Size(167, 24); - this.rb_set_audio_volume.TabIndex = 13; - this.rb_set_audio_volume.Text = "Set audio volume at"; - this.rb_set_audio_volume.UseVisualStyleBackColor = true; - // - // rb_keep_audio_volume - // - this.rb_keep_audio_volume.AutoSize = true; - this.rb_keep_audio_volume.Checked = true; - this.rb_keep_audio_volume.ForeColor = System.Drawing.Color.White; - this.rb_keep_audio_volume.Location = new System.Drawing.Point(38, 36); - this.rb_keep_audio_volume.Name = "rb_keep_audio_volume"; - this.rb_keep_audio_volume.Size = new System.Drawing.Size(203, 24); - this.rb_keep_audio_volume.TabIndex = 12; - this.rb_keep_audio_volume.TabStop = true; - this.rb_keep_audio_volume.Text = "Leave audio volume as is"; - this.rb_keep_audio_volume.UseVisualStyleBackColor = true; - // - // lbl_audio_volume - // - this.lbl_audio_volume.AutoSize = true; - this.lbl_audio_volume.ForeColor = System.Drawing.Color.White; - this.lbl_audio_volume.Location = new System.Drawing.Point(275, 86); - this.lbl_audio_volume.Name = "lbl_audio_volume"; - this.lbl_audio_volume.Size = new System.Drawing.Size(63, 20); - this.lbl_audio_volume.TabIndex = 11; - this.lbl_audio_volume.Text = "percent"; - // - // nud_audio_volume - // - this.nud_audio_volume.Location = new System.Drawing.Point(209, 84); - this.nud_audio_volume.Name = "nud_audio_volume"; - this.nud_audio_volume.Size = new System.Drawing.Size(60, 26); - this.nud_audio_volume.TabIndex = 10; - this.nud_audio_volume.Value = new decimal(new int[] { - 100, - 0, - 0, - 0}); - // // ShortcutForm // this.AcceptButton = this.btn_save; @@ -1298,6 +1303,9 @@ namespace DisplayMagician.UIForms this.tabp_display.PerformLayout(); this.tabp_audio.ResumeLayout(false); this.tabp_audio.PerformLayout(); + this.gb_audio_volume.ResumeLayout(false); + this.gb_audio_volume.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.nud_audio_volume)).EndInit(); this.tabp_before.ResumeLayout(false); this.pnl_start_program4.ResumeLayout(false); this.pnl_start_program4.PerformLayout(); @@ -1320,9 +1328,6 @@ namespace DisplayMagician.UIForms this.groupBox1.PerformLayout(); this.gb_display_after.ResumeLayout(false); this.gb_display_after.PerformLayout(); - this.gb_audio_volume.ResumeLayout(false); - this.gb_audio_volume.PerformLayout(); - ((System.ComponentModel.ISupportInitialize)(this.nud_audio_volume)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); diff --git a/DisplayMagician/UIForms/ShortcutForm.cs b/DisplayMagician/UIForms/ShortcutForm.cs index 66503de..a33e4e8 100644 --- a/DisplayMagician/UIForms/ShortcutForm.cs +++ b/DisplayMagician/UIForms/ShortcutForm.cs @@ -40,6 +40,9 @@ namespace DisplayMagician.UIForms private uint _gameId = 0; private string _uuid = ""; private CoreAudioController audioController = new CoreAudioController(); + private List audioDevices = null; + private CoreAudioDevice selectedAudioDevice = null; + private bool audioVolumeSetToDefault = true; public ShortcutForm(ShortcutItem shortcutToEdit) { @@ -366,7 +369,11 @@ namespace DisplayMagician.UIForms _audioDevice = cb_audio_device.Text; } else + { _changeAudioDevice = false; + _audioDevice = ""; + } + if (rb_set_audio_volume.Checked) { @@ -700,18 +707,79 @@ namespace DisplayMagician.UIForms ProfileItem chosenProfile = null; // Populate all the Audio devices in the audio devices list. + // Set the Audio device to the shortcut audio device only if + // the Change Audio radiobutton is set + rb_change_audio.Checked = _shortcutToEdit.ChangeAudioDevice; cb_audio_device.Items.Clear(); - List audioDevices = audioController.GetPlaybackDevices().ToList(); - foreach (CoreAudioDevice audioDevice in audioDevices) + audioDevices = audioController.GetPlaybackDevices().ToList(); + + // If the shortcut is to change the audio device + if (_shortcutToEdit.ChangeAudioDevice) { - if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.Active) + // Then we need to populate the list + bool foundAudioDevice = false; + foreach (CoreAudioDevice audioDevice in audioDevices) { - int index = cb_audio_device.Items.Add(audioDevice.FullName); - // Set the audio device to the default device by default - if (audioDevice.IsDefaultDevice) - cb_audio_device.SelectedIndex = index; + if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.Active) + { + int index = cb_audio_device.Items.Add(audioDevice.FullName); + // Set the audio device to the default device by default + if (audioDevice.FullName.Equals(_shortcutToEdit.AudioDevice)) + { + foundAudioDevice = true; + selectedAudioDevice = audioDevice; + cb_audio_device.SelectedIndex = index; + if (_shortcutToEdit.SetAudioVolume && _shortcutToEdit.AudioVolume >= 0 && _shortcutToEdit.AudioVolume <= 100) + nud_audio_volume.Value = _shortcutToEdit.AudioVolume; + else + nud_audio_volume.Value = Convert.ToDecimal(audioDevice.Volume); + } + } } - } + + // We need to handle the edgecase where the selected audio device + // isn't currently plugged in. We don't want to break the shortcut + // as it could be plugged in when it comes time to actually run + // the shortcut, so we need to just add it to the list to not break + // the UI. + + if (!foundAudioDevice) + { + int index = cb_audio_device.Items.Add(_shortcutToEdit.AudioDevice); + cb_audio_device.SelectedIndex = index; + selectedAudioDevice = null; + if (_shortcutToEdit.SetAudioVolume && _shortcutToEdit.AudioVolume >= 0 && _shortcutToEdit.AudioVolume <= 100) + { + rb_set_audio_volume.Checked = true; + nud_audio_volume.Value = _shortcutToEdit.AudioVolume; + } + else + { + rb_keep_audio_volume.Checked = true; + nud_audio_volume.Value = 50; + } + } + } + else + { + audioVolumeSetToDefault = true; + // Then we need to populate the list + foreach (CoreAudioDevice audioDevice in audioDevices) + { + if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.Active) + { + int index = cb_audio_device.Items.Add(audioDevice.FullName); + // Set the audio device to the default device by default + if (audioDevice.IsDefaultDevice) + { + selectedAudioDevice = audioDevice; + cb_audio_device.SelectedIndex = index; + nud_audio_volume.Value = Convert.ToDecimal(audioDevice.Volume); + } + } + } + rb_keep_audio_volume.Checked = true; + } // Populate a full list of games // Start with the Steam Games @@ -852,63 +920,8 @@ namespace DisplayMagician.UIForms break; } - // Set the Audio device to the shortcut audio device only if - // the Change Audio radiobutton is set - rb_change_audio.Checked = _shortcutToEdit.ChangeAudioDevice; - if (_shortcutToEdit.ChangeAudioDevice) - { - bool foundAudioDevice = false; - for (int i = 0; i < cb_audio_device.Items.Count; i++) - { - if (cb_audio_device.Items[i].Equals(_shortcutToEdit.AudioDevice)) - { - cb_audio_device.SelectedIndex = i; - foundAudioDevice = true; - break; - } - - } - // If we have a saved Audio device which isn't plugged in - // or isn't turned on right now, we don't want to lose the - // information, as the user has specifically set that audio device - // and they may plug it in when it comes time to use the shortcut - // So we add the audiodevice as an extra selection in this case. - if (!foundAudioDevice) - { - int index = cb_audio_device.Items.Add(_shortcutToEdit.AudioDevice); - cb_audio_device.SelectedIndex = index; - } - } - - if (_shortcutToEdit.SetAudioVolume) - { - if (_shortcutToEdit.AudioVolume >= 0 && _shortcutToEdit.AudioVolume <= 100) - { - nud_audio_volume.Value = _shortcutToEdit.AudioVolume; - rb_set_audio_volume.Checked = true; - } - else - { - bool foundVolumeDevice = false; - foreach (CoreAudioDevice audioDevice in audioDevices) - { - if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.Active && - audioDevice.FullName.Equals(_shortcutToEdit.AudioDevice)) - { - nud_audio_volume.Value = Convert.ToDecimal(audioDevice.Volume); - foundVolumeDevice = true; - } - } - if (foundVolumeDevice) - nud_audio_volume.Value = _shortcutToEdit.AudioVolume; - else - nud_audio_volume.Value = 100; - rb_set_audio_volume.Checked = false; - } - - } - // Set the launcher items if we have them - txt_game_launcher.Text = _shortcutToEdit.GameLibrary.ToString(); + // Set the launcher items if we have them + txt_game_launcher.Text = _shortcutToEdit.GameLibrary.ToString(); txt_game_name.Text = _shortcutToEdit.GameName; _gameId = _shortcutToEdit.GameAppId; nud_timeout_game.Value = _shortcutToEdit.StartTimeout; @@ -1535,43 +1548,141 @@ namespace DisplayMagician.UIForms { if (_loadedShortcut) _isUnsaved = true; + + // Populate all the Audio devices in the audio devices list. + // Set the Audio device to the shortcut audio device only if + // the Change Audio radiobutton is set + audioDevices = audioController.GetPlaybackDevices().ToList(); + + // If the shortcut is to change the audio device + if (_shortcutToEdit.ChangeAudioDevice) + { + // Then we need to populate the list + bool foundAudioDevice = false; + foreach (CoreAudioDevice audioDevice in audioDevices) + { + if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.Active) + { + // Set the audio device to the default device by default + if (audioDevice.FullName.Equals(cb_audio_device.SelectedItem.ToString())) + { + foundAudioDevice = true; + selectedAudioDevice = audioDevice; + nud_audio_volume.Value = Convert.ToDecimal(audioDevice.Volume); + } + } + } + + // We need to handle the edgecase where the selected audio device + // isn't currently plugged in. We don't want to break the shortcut + // as it could be plugged in when it comes time to actually run + // the shortcut, so we need to just add it to the list to not break + // the UI. + + if (!foundAudioDevice) + { + selectedAudioDevice = null; + nud_audio_volume.Value = _shortcutToEdit.AudioVolume; + } + } + else + { + audioVolumeSetToDefault = true; + // Then we need to populate the list + foreach (CoreAudioDevice audioDevice in audioDevices) + { + if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.Active) + { + int index = cb_audio_device.Items.Add(audioDevice.FullName); + // Set the audio device to the default device by default + if (audioDevice.IsDefaultDevice) + { + selectedAudioDevice = audioDevice; + cb_audio_device.SelectedIndex = index; + nud_audio_volume.Value = Convert.ToDecimal(audioDevice.Volume); + } + } + } + rb_keep_audio_volume.Checked = true; + } } private void btn_rescan_audio_Click(object sender, EventArgs e) { // Populate all the Audio devices in the audio devices list. + // Set the Audio device to the shortcut audio device only if + // the Change Audio radiobutton is set + rb_change_audio.Checked = _shortcutToEdit.ChangeAudioDevice; cb_audio_device.Items.Clear(); - List audioDevices = audioController.GetPlaybackDevices().ToList(); - foreach (CoreAudioDevice audioDevice in audioDevices) - { - if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.Active) - { - int index = cb_audio_device.Items.Add(audioDevice.FullName); - if (audioDevice.IsDefaultDevice) - cb_audio_device.SelectedIndex = index; - } - } + audioDevices = audioController.GetPlaybackDevices().ToList(); - bool foundAudioDevice = false; - for (int i = 0; i < cb_audio_device.Items.Count; i++) + // If the shortcut is to change the audio device + if (_shortcutToEdit.ChangeAudioDevice) { - if (cb_audio_device.Items[i].Equals(_shortcutToEdit.AudioDevice)) + // Then we need to populate the list + bool foundAudioDevice = false; + foreach (CoreAudioDevice audioDevice in audioDevices) { - cb_audio_device.SelectedIndex = i; - foundAudioDevice = true; - break; + if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.Active) + { + int index = cb_audio_device.Items.Add(audioDevice.FullName); + // Set the audio device to the default device by default + if (audioDevice.FullName.Equals(_shortcutToEdit.AudioDevice)) + { + foundAudioDevice = true; + selectedAudioDevice = audioDevice; + cb_audio_device.SelectedIndex = index; + if (_shortcutToEdit.SetAudioVolume && _shortcutToEdit.AudioVolume >= 0 && _shortcutToEdit.AudioVolume <= 100) + nud_audio_volume.Value = _shortcutToEdit.AudioVolume; + else + nud_audio_volume.Value = Convert.ToDecimal(audioDevice.Volume); + } + } } + // We need to handle the edgecase where the selected audio device + // isn't currently plugged in. We don't want to break the shortcut + // as it could be plugged in when it comes time to actually run + // the shortcut, so we need to just add it to the list to not break + // the UI. + + if (!foundAudioDevice) + { + int index = cb_audio_device.Items.Add(_shortcutToEdit.AudioDevice); + cb_audio_device.SelectedIndex = index; + selectedAudioDevice = null; + if (_shortcutToEdit.SetAudioVolume && _shortcutToEdit.AudioVolume >= 0 && _shortcutToEdit.AudioVolume <= 100) + { + rb_set_audio_volume.Checked = true; + nud_audio_volume.Value = _shortcutToEdit.AudioVolume; + } + else + { + rb_keep_audio_volume.Checked = true; + nud_audio_volume.Value = 50; + } + + } } - // If we have a saved Audio device which isn't plugged in - // or isn't turned on right now, we don't want to lose the - // information, as the user has specifically set that audio device - // and they may plug it in when it comes time to use the shortcut - // So we add the audiodevice as an extra selection in this case. - if (!foundAudioDevice) + else { - int index = cb_audio_device.Items.Add(_shortcutToEdit.AudioDevice); - cb_audio_device.SelectedIndex = index; + audioVolumeSetToDefault = true; + // Then we need to populate the list + foreach (CoreAudioDevice audioDevice in audioDevices) + { + if (audioDevice.State == AudioSwitcher.AudioApi.DeviceState.Active) + { + int index = cb_audio_device.Items.Add(audioDevice.FullName); + // Set the audio device to the default device by default + if (audioDevice.IsDefaultDevice) + { + selectedAudioDevice = audioDevice; + cb_audio_device.SelectedIndex = index; + nud_audio_volume.Value = Convert.ToDecimal(audioDevice.Volume); + } + } + } + rb_keep_audio_volume.Checked = true; } } @@ -1580,7 +1691,8 @@ namespace DisplayMagician.UIForms { if (_loadedShortcut) _isUnsaved = true; - nud_audio_volume.Enabled = false; + if (rb_set_audio_volume.Checked) + nud_audio_volume.Enabled = false; } @@ -1588,7 +1700,9 @@ namespace DisplayMagician.UIForms { if (_loadedShortcut) _isUnsaved = true; - nud_audio_volume.Enabled = true; + if (rb_set_audio_volume.Checked) + nud_audio_volume.Enabled = true; } + } } \ No newline at end of file