Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 45 additions & 30 deletions MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AudioSwitcher.AudioApi;
using AudioSwitcher.AudioApi;
using AudioSwitcher.AudioApi.CoreAudio;
using Microsoft.Win32;
using Shortcut;
Expand All @@ -9,7 +9,6 @@
using System.Reactive;
using System.Windows.Forms;


namespace MicMute
{
public partial class MainForm : Form
Expand Down Expand Up @@ -91,7 +90,7 @@ private void MainForm_Load(object sender, EventArgs e)
{
var converter = new Shortcut.Forms.HotkeyConverter();
hotkey = (Hotkey)converter.ConvertFromString(hotkeyValue.ToString());
if (!hotkeyBinder.IsHotkeyAlreadyBound(hotkey)) hotkeyBinder.Bind(hotkey).To(ToggleMicStatus);
BindWildcard(hotkey, ToggleMicStatus);
}

// mute
Expand All @@ -100,7 +99,7 @@ private void MainForm_Load(object sender, EventArgs e)
{
var converter = new Shortcut.Forms.HotkeyConverter();
muteHotkey = (Hotkey)converter.ConvertFromString(hotkeyValue.ToString());
if (!hotkeyBinder.IsHotkeyAlreadyBound(muteHotkey)) hotkeyBinder.Bind(muteHotkey).To(MuteMicStatus);
BindWildcard(muteHotkey, MuteMicStatus);
}

// unmute
Expand All @@ -109,27 +108,25 @@ private void MainForm_Load(object sender, EventArgs e)
{
var converter = new Shortcut.Forms.HotkeyConverter();
unMuteHotkey = (Hotkey)converter.ConvertFromString(hotkeyValue.ToString());
if (!hotkeyBinder.IsHotkeyAlreadyBound(unMuteHotkey)) hotkeyBinder.Bind(unMuteHotkey).To(UnMuteMicStatus);
BindWildcard(unMuteHotkey, UnMuteMicStatus);
}

//AudioController.AudioDeviceChanged.Subscribe(x =>
//{
// Debug.WriteLine("{0} - {1}", x.Device.Name, x.ChangedType.ToString());
//});
}

private void OnMuteChanged(DeviceMuteChangedArgs next)
{
UpdateStatus(next.Device);
}

// ИСПРАВЛЕНИЕ ЗДЕСЬ: Был IDevice, стал IDisposable
IDisposable muteChangedSubscription;

public void UpdateDevice(IDevice device)
{
muteChangedSubscription?.Dispose();
muteChangedSubscription = device?.MuteChanged.Subscribe(OnMuteChanged);
UpdateStatus(device);
}

public IDevice getSelectedDevice()
{
return selectedDeviceId == "" ? AudioController.DefaultCaptureDevice : AudioController.GetDevice(new Guid(selectedDeviceId), DeviceState.Active);
Expand Down Expand Up @@ -196,6 +193,35 @@ public async void UnMuteMicStatus()
await getSelectedDevice()?.SetMuteAsync(false);
}


private void BindWildcard(Hotkey hotkey, Action action)
{
if (hotkey == null) return;

Keys k = hotkey.Key;

Modifiers[] allModifiers = new Modifiers[] {
Modifiers.None,
Modifiers.Shift,
Modifiers.Control,
Modifiers.Alt,
Modifiers.Shift | Modifiers.Control,
Modifiers.Shift | Modifiers.Alt,
Modifiers.Control | Modifiers.Alt,
Modifiers.Shift | Modifiers.Control | Modifiers.Alt
};

foreach (var mod in allModifiers)
{
Hotkey combo = new Hotkey(mod, k);
if (!hotkeyBinder.IsHotkeyAlreadyBound(combo))
{
hotkeyBinder.Bind(combo).To(action);
}
}
}
// === КОНЕЦ НОВОЙ ФУНКЦИИ ===

private void Icon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
Expand Down Expand Up @@ -237,52 +263,41 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
MyHide();
e.Cancel = true;

// TOGGLE
hotkey = hotkeyTextBox.Hotkey;

if (hotkey == null)
{
registryKey.DeleteValue(registryKeyName, false);
}
else
{
if (!hotkeyBinder.IsHotkeyAlreadyBound(hotkey))
{
registryKey.SetValue(registryKeyName, hotkey);
if (!hotkeyBinder.IsHotkeyAlreadyBound(hotkey)) hotkeyBinder.Bind(hotkey).To(ToggleMicStatus);
}
registryKey.SetValue(registryKeyName, hotkey);
BindWildcard(hotkey, ToggleMicStatus);
}

// MUTE
muteHotkey = muteTextBox.Hotkey;

if (muteHotkey == null)
{
registryKey.DeleteValue(registryKeyMute, false);
}
else
{
if (!hotkeyBinder.IsHotkeyAlreadyBound(muteHotkey))
{
registryKey.SetValue(registryKeyMute, muteHotkey);
if (!hotkeyBinder.IsHotkeyAlreadyBound(muteHotkey)) hotkeyBinder.Bind(muteHotkey).To(MuteMicStatus);
}
registryKey.SetValue(registryKeyMute, muteHotkey);
BindWildcard(muteHotkey, MuteMicStatus);
}


// UNMUTE
unMuteHotkey = unmuteTextBox.Hotkey;

if (unMuteHotkey == null)
{
registryKey.DeleteValue(registryKeyUnmute, false);
}
else
{
if (!hotkeyBinder.IsHotkeyAlreadyBound(unMuteHotkey))
{
registryKey.SetValue(registryKeyUnmute, unMuteHotkey);
if (!hotkeyBinder.IsHotkeyAlreadyBound(unMuteHotkey)) hotkeyBinder.Bind(unMuteHotkey).To(UnMuteMicStatus);
}
registryKey.SetValue(registryKeyUnmute, unMuteHotkey);
BindWildcard(unMuteHotkey, UnMuteMicStatus);
}

}
}

Expand Down