2021-02-21 14:04:24 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Controls;
|
2021-02-24 09:05:11 +01:00
|
|
|
|
using AudioSwitcher.AudioApi;
|
2021-02-24 19:35:01 +01:00
|
|
|
|
using MassiveKnob.Plugin.CoreAudio.OSD;
|
2021-02-28 11:55:23 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-02-21 14:04:24 +01:00
|
|
|
|
|
2021-02-24 19:35:01 +01:00
|
|
|
|
namespace MassiveKnob.Plugin.CoreAudio.SetVolume
|
2021-02-21 14:04:24 +01:00
|
|
|
|
{
|
2021-02-24 19:35:01 +01:00
|
|
|
|
public class DeviceSetVolumeAction : IMassiveKnobAction
|
2021-02-21 14:04:24 +01:00
|
|
|
|
{
|
|
|
|
|
public Guid ActionId { get; } = new Guid("aabd329c-8be5-4d1e-90ab-5114143b21dd");
|
|
|
|
|
public MassiveKnobActionType ActionType { get; } = MassiveKnobActionType.InputAnalog;
|
2021-02-24 19:35:01 +01:00
|
|
|
|
public string Name { get; } = Strings.SetVolumeName;
|
|
|
|
|
public string Description { get; } = Strings.SetVolumeDescription;
|
2021-02-21 14:04:24 +01:00
|
|
|
|
|
|
|
|
|
|
2021-02-28 11:55:23 +01:00
|
|
|
|
public IMassiveKnobActionInstance Create(ILogger logger)
|
2021-02-21 14:04:24 +01:00
|
|
|
|
{
|
2021-02-24 09:05:11 +01:00
|
|
|
|
return new Instance();
|
2021-02-21 14:04:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class Instance : IMassiveKnobAnalogAction
|
|
|
|
|
{
|
2021-02-24 09:05:11 +01:00
|
|
|
|
private IMassiveKnobActionContext actionContext;
|
2021-02-24 19:35:01 +01:00
|
|
|
|
private DeviceSetVolumeActionSettings settings;
|
2021-02-24 09:05:11 +01:00
|
|
|
|
private IDevice playbackDevice;
|
2021-02-21 14:04:24 +01:00
|
|
|
|
|
2021-02-28 11:55:23 +01:00
|
|
|
|
|
2021-02-24 09:05:11 +01:00
|
|
|
|
public void Initialize(IMassiveKnobActionContext context)
|
2021-02-21 14:04:24 +01:00
|
|
|
|
{
|
2021-02-24 09:05:11 +01:00
|
|
|
|
actionContext = context;
|
2021-02-24 19:35:01 +01:00
|
|
|
|
settings = context.GetSettings<DeviceSetVolumeActionSettings>();
|
2021-02-24 09:05:11 +01:00
|
|
|
|
ApplySettings();
|
2021-02-21 14:04:24 +01:00
|
|
|
|
}
|
2021-02-24 09:05:11 +01:00
|
|
|
|
|
2021-02-21 14:04:24 +01:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-24 09:05:11 +01:00
|
|
|
|
|
|
|
|
|
private void ApplySettings()
|
2021-02-21 14:04:24 +01:00
|
|
|
|
{
|
2021-02-24 09:05:11 +01:00
|
|
|
|
var coreAudioController = CoreAudioControllerInstance.Acquire();
|
|
|
|
|
playbackDevice = settings.DeviceId.HasValue ? coreAudioController.GetDevice(settings.DeviceId.Value) : null;
|
2021-02-21 14:04:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-02-24 09:05:11 +01:00
|
|
|
|
public UserControl CreateSettingsControl()
|
2021-02-21 14:04:24 +01:00
|
|
|
|
{
|
2021-02-24 19:35:01 +01:00
|
|
|
|
var viewModel = new DeviceSetVolumeActionSettingsViewModel(settings);
|
2021-02-24 09:05:11 +01:00
|
|
|
|
viewModel.PropertyChanged += (sender, args) =>
|
|
|
|
|
{
|
|
|
|
|
if (!viewModel.IsSettingsProperty(args.PropertyName))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
actionContext.SetSettings(settings);
|
|
|
|
|
ApplySettings();
|
|
|
|
|
};
|
2021-02-21 14:04:24 +01:00
|
|
|
|
|
2021-02-24 19:35:01 +01:00
|
|
|
|
return new DeviceSetVolumeActionSettingsView(viewModel);
|
2021-02-24 09:05:11 +01:00
|
|
|
|
}
|
2021-02-21 14:04:24 +01:00
|
|
|
|
|
2021-02-24 09:05:11 +01:00
|
|
|
|
|
|
|
|
|
public async ValueTask AnalogChanged(byte value)
|
|
|
|
|
{
|
|
|
|
|
if (playbackDevice == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
await playbackDevice.SetVolumeAsync(value);
|
2021-02-24 19:35:01 +01:00
|
|
|
|
|
|
|
|
|
if (settings.OSD)
|
|
|
|
|
OSDManager.Show(playbackDevice);
|
2021-02-24 09:05:11 +01:00
|
|
|
|
}
|
2021-02-21 14:04:24 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|