Skip to content

Commit 7c9c0f9

Browse files
committed
Wire up the "add" button in the channel mapping list
1 parent 2c14cab commit 7c9c0f9

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

DigiMixer/DigiMixer.Wpf/ChannelListControl.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
MinHeight="50" MaxHeight="205" Margin="5" PreviewKeyDown="HandleKeyDown" x:Name="mappingList" />
1515
</Border>
1616
<DockPanel Width="260" Margin="0,5,0,0">
17-
<Button DockPanel.Dock="Left" Content="Add" HorizontalAlignment="Left" VerticalAlignment="Top"/>
17+
<Button DockPanel.Dock="Left" Content="Add" Command="{Binding AddChannelCommand}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
1818
<TextBlock Margin="5,0,0,0" DockPanel.Dock="Right" HorizontalAlignment="Left" TextWrapping="Wrap">Use Ctrl-Up and Ctrl-Down to move the selected item, or Ctrl-Del to delete it.</TextBlock>
1919
</DockPanel>
2020
</StackPanel>
Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,58 @@
1-
using JonSkeet.WpfUtil;
1+
using DigiMixer.Controls;
2+
using JonSkeet.WpfUtil;
23
using System.Collections.ObjectModel;
4+
using System.Windows.Input;
35

46
namespace DigiMixer.Wpf;
57

68
public class ChannelListViewModel : ViewModelBase, IReorderableList
79
{
10+
private readonly
11+
string idPrefix;
812
public ObservableCollection<ChannelMappingViewModel> Mappings { get; } = new();
913

14+
public ICommand AddChannelCommand { get; }
15+
1016
private ChannelMappingViewModel selectedMapping;
1117
public ChannelMappingViewModel SelectedMapping
1218
{
1319
get => selectedMapping;
1420
set => SetProperty(ref selectedMapping, value);
1521
}
1622

23+
public ChannelListViewModel(string idPrefix)
24+
{
25+
AddChannelCommand = ActionCommand.FromAction(AddChannel);
26+
this.idPrefix = idPrefix;
27+
}
28+
1729
public void DeleteSelectedItem() => SelectedMapping = Mappings.RemoveSelected(SelectedMapping);
1830
public void MoveSelectedItemUp() => Mappings.MoveSelectedItemUp(SelectedMapping, value => SelectedMapping = value);
1931
public void MoveSelectedItemDown() => Mappings.MoveSelectedItemDown(SelectedMapping, value => SelectedMapping = value);
32+
33+
private void AddChannel()
34+
{
35+
// Generate an ID that's unused and somewhat plausible.
36+
int index = Mappings.Count + 1;
37+
while (Mappings.Any(m => m.Model.Id == $"{idPrefix}{index}"))
38+
{
39+
index++;
40+
}
41+
string id = $"{idPrefix}{index}";
42+
// Find the first unused channel number
43+
int channel = 1;
44+
while (Mappings.Any(m => m.Number == channel))
45+
{
46+
channel++;
47+
}
48+
var mapping = new ChannelMappingViewModel(new ChannelMapping
49+
{
50+
Id = $"{idPrefix}{index}",
51+
DisplayName = "Unnamed channel",
52+
Channel = channel,
53+
InitiallyVisible = true
54+
});
55+
Mappings.Add(mapping);
56+
SelectedMapping = mapping;
57+
}
2058
}

DigiMixer/DigiMixer.Wpf/ConfigurationEditorViewModel.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace DigiMixer.Wpf;
1010

1111
public class ConfigurationEditorViewModel : ViewModelBase<DigiMixerAppConfig>
1212
{
13+
private const string InputPrefix = "input-";
14+
private const string OutputPrefix = "output-";
1315
private readonly ILogger logger;
1416

1517
private static List<(string, MHT)> allMixerTypes = new()
@@ -61,8 +63,8 @@ public string IconXPlusDevice
6163
set => SetProperty(IconXPlusDevice, value, x => Model.Mixer.IconXPlusDevice = x);
6264
}
6365

64-
public ChannelListViewModel InputChannels { get; } = new();
65-
public ChannelListViewModel OutputChannels { get; } = new();
66+
public ChannelListViewModel InputChannels { get; } = new(InputPrefix);
67+
public ChannelListViewModel OutputChannels { get; } = new(OutputPrefix);
6668

6769
public List<string> AllMixerTypes => allMixerTypes.Select(pair => pair.Item1).ToList();
6870

@@ -135,7 +137,7 @@ private async Task TestConfiguration()
135137
{
136138
var inputMappings = inputs.Select((input, index) => new ChannelMappingViewModel(new ChannelMapping
137139
{
138-
Id = $"input-{index + 1}",
140+
Id = $"{InputPrefix}{index + 1}",
139141
DisplayName = input.Name,
140142
Channel = input.LeftOrMonoChannelId.Value,
141143
InitiallyVisible = true
@@ -144,7 +146,7 @@ private async Task TestConfiguration()
144146

145147
var outputMappings = outputs.Select((output, index) => new ChannelMappingViewModel(new ChannelMapping
146148
{
147-
Id = $"output-{index + 1}",
149+
Id = $"{OutputPrefix}{index + 1}",
148150
DisplayName = output.Name,
149151
Channel = output.LeftOrMonoChannelId.Value,
150152
InitiallyVisible = true

0 commit comments

Comments
 (0)