Skip to content

Commit

Permalink
Wire up the "add" button in the channel mapping list
Browse files Browse the repository at this point in the history
  • Loading branch information
jskeet committed Jan 15, 2024
1 parent 2c14cab commit 7c9c0f9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DigiMixer/DigiMixer.Wpf/ChannelListControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
MinHeight="50" MaxHeight="205" Margin="5" PreviewKeyDown="HandleKeyDown" x:Name="mappingList" />
</Border>
<DockPanel Width="260" Margin="0,5,0,0">
<Button DockPanel.Dock="Left" Content="Add" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Button DockPanel.Dock="Left" Content="Add" Command="{Binding AddChannelCommand}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<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>
</DockPanel>
</StackPanel>
Expand Down
40 changes: 39 additions & 1 deletion DigiMixer/DigiMixer.Wpf/ChannelListViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,58 @@
using JonSkeet.WpfUtil;
using DigiMixer.Controls;
using JonSkeet.WpfUtil;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace DigiMixer.Wpf;

public class ChannelListViewModel : ViewModelBase, IReorderableList
{
private readonly
string idPrefix;
public ObservableCollection<ChannelMappingViewModel> Mappings { get; } = new();

public ICommand AddChannelCommand { get; }

private ChannelMappingViewModel selectedMapping;
public ChannelMappingViewModel SelectedMapping
{
get => selectedMapping;
set => SetProperty(ref selectedMapping, value);
}

public ChannelListViewModel(string idPrefix)
{
AddChannelCommand = ActionCommand.FromAction(AddChannel);
this.idPrefix = idPrefix;
}

public void DeleteSelectedItem() => SelectedMapping = Mappings.RemoveSelected(SelectedMapping);
public void MoveSelectedItemUp() => Mappings.MoveSelectedItemUp(SelectedMapping, value => SelectedMapping = value);
public void MoveSelectedItemDown() => Mappings.MoveSelectedItemDown(SelectedMapping, value => SelectedMapping = value);

private void AddChannel()
{
// Generate an ID that's unused and somewhat plausible.
int index = Mappings.Count + 1;
while (Mappings.Any(m => m.Model.Id == $"{idPrefix}{index}"))
{
index++;
}
string id = $"{idPrefix}{index}";
// Find the first unused channel number
int channel = 1;
while (Mappings.Any(m => m.Number == channel))
{
channel++;
}
var mapping = new ChannelMappingViewModel(new ChannelMapping
{
Id = $"{idPrefix}{index}",
DisplayName = "Unnamed channel",
Channel = channel,
InitiallyVisible = true
});
Mappings.Add(mapping);
SelectedMapping = mapping;
}
}
10 changes: 6 additions & 4 deletions DigiMixer/DigiMixer.Wpf/ConfigurationEditorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace DigiMixer.Wpf;

public class ConfigurationEditorViewModel : ViewModelBase<DigiMixerAppConfig>
{
private const string InputPrefix = "input-";
private const string OutputPrefix = "output-";
private readonly ILogger logger;

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

public ChannelListViewModel InputChannels { get; } = new();
public ChannelListViewModel OutputChannels { get; } = new();
public ChannelListViewModel InputChannels { get; } = new(InputPrefix);
public ChannelListViewModel OutputChannels { get; } = new(OutputPrefix);

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

Expand Down Expand Up @@ -135,7 +137,7 @@ private async Task TestConfiguration()
{
var inputMappings = inputs.Select((input, index) => new ChannelMappingViewModel(new ChannelMapping
{
Id = $"input-{index + 1}",
Id = $"{InputPrefix}{index + 1}",
DisplayName = input.Name,
Channel = input.LeftOrMonoChannelId.Value,
InitiallyVisible = true
Expand All @@ -144,7 +146,7 @@ private async Task TestConfiguration()

var outputMappings = outputs.Select((output, index) => new ChannelMappingViewModel(new ChannelMapping
{
Id = $"output-{index + 1}",
Id = $"{OutputPrefix}{index + 1}",
DisplayName = output.Name,
Channel = output.LeftOrMonoChannelId.Value,
InitiallyVisible = true
Expand Down

0 comments on commit 7c9c0f9

Please sign in to comment.