-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInputAdjustment.cs
More file actions
124 lines (97 loc) · 4.02 KB
/
InputAdjustment.cs
File metadata and controls
124 lines (97 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System.Linq;
using System.Threading.Tasks;
using Loupedeck.VolumeControlPlugin.Helpers;
namespace Loupedeck.VolumeControlPlugin.Commands
{
class InputAdjustment : PluginDynamicAdjustment
{
private VolumeControlPlugin _plugin;
private DeviceHelper _deviceHelper;
public InputAdjustment()
: base(true)
{
//
}
protected override bool OnLoad()
{
_plugin = base.Plugin as VolumeControlPlugin;
if (_plugin is null)
return false;
_deviceHelper = new DeviceHelper(_plugin);
if (_plugin.NotificationService is null)
return false;
var inputs = _plugin.NotificationService.Devices
.Where(device => device.Value.IsCaptureDevice)
.ToArray();
foreach (var input in inputs)
{
this.AddParameter(input.Key, input.Value.FullName, "Adjust Volume Inputs");
}
return true;
}
protected override void RunCommand(string actionParameter)
{
if (string.IsNullOrWhiteSpace(actionParameter))//Always null issue.
return;
var device = _deviceHelper.GetDevice(actionParameter);
if (_deviceHelper.IsDisabled(device))
{
//Update image in case the device was disconnected/disabled
base.ActionImageChanged(actionParameter);
return;
}
Task.Run(() => device.SetMuteAsync(!device.IsMuted)).GetAwaiter().GetResult();
base.ActionImageChanged(actionParameter);
}
protected override void ApplyAdjustment(string actionParameter, int ticks)
{
if (string.IsNullOrWhiteSpace(actionParameter))
return;
var device = _deviceHelper.GetDevice(actionParameter);
if (_deviceHelper.IsDisabled(device))
{
//Update image in case the device was disconnected/disabled
base.ActionImageChanged(actionParameter);
return;
}
var volume = device.Volume + ticks;
if (volume > 100)
volume = 100;
if (volume < 0)
volume = 0;
Task.Run(() => device.SetVolumeAsync(volume)).GetAwaiter().GetResult();
base.ActionImageChanged(actionParameter);
}
protected override string GetAdjustmentValue(string actionParameter)
{
if (string.IsNullOrWhiteSpace(actionParameter))
return null;
var device = _deviceHelper.GetDevice(actionParameter);
if (_deviceHelper.IsDisabled(device))
return "disabled";
if (device.IsMuted)
return "muted";
return device.Volume.ToString("###'%'");
}
protected override BitmapImage GetCommandImage(string actionParameter, PluginImageSize imageSize)
{
if (string.IsNullOrWhiteSpace(actionParameter))
return null;
var device = _deviceHelper.GetDevice(actionParameter);
if (device is null)
return null;
using (var bitmapBuilder = new BitmapBuilder(imageSize))
{
var path = "Loupedeck.VolumeControlPlugin.Resources.VolumeControl.input-active-50.png";
if (device.IsMuted)
path = "Loupedeck.VolumeControlPlugin.Resources.VolumeControl.input-muted-50.png";
if (_deviceHelper.IsDisabled(device))
path = "Loupedeck.VolumeControlPlugin.Resources.VolumeControl.input-disabled-50.png";
var background = EmbeddedResources.ReadImage(path);
bitmapBuilder.SetBackgroundImage(background);
bitmapBuilder.DrawText(device.Name, 0, 35, 50, 10, BitmapColor.White, 10, 8);
return bitmapBuilder.ToImage();
}
}
}
}