Files
RedBookPlayer/RedBookPlayer.GUI/Views/SettingsWindow.xaml.cs

186 lines
8.5 KiB
C#
Raw Normal View History

2021-06-30 17:22:37 -07:00
using System;
2021-03-19 17:07:27 -03:00
using System.Collections.Generic;
using System.IO;
using Avalonia.Controls;
2021-06-30 17:22:37 -07:00
using Avalonia.Input;
2021-03-19 17:07:27 -03:00
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
2021-07-12 15:49:43 -07:00
namespace RedBookPlayer.GUI.Views
2021-03-19 17:07:27 -03:00
{
public class SettingsWindow : Window
2021-03-19 17:07:27 -03:00
{
private readonly Settings _settings;
private string _selectedTheme;
private ListBox _themeList;
2021-03-19 17:07:27 -03:00
2021-06-06 20:28:36 +01:00
public SettingsWindow() {}
2021-03-24 17:34:04 -03:00
public SettingsWindow(Settings settings)
2021-03-19 17:07:27 -03:00
{
DataContext = _settings = settings;
2021-03-19 17:07:27 -03:00
InitializeComponent();
}
public void ThemeList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count == 0)
2021-03-19 17:07:27 -03:00
return;
_selectedTheme = (string)e.AddedItems[0];
2021-03-19 17:07:27 -03:00
}
2021-03-24 17:34:04 -03:00
public void ApplySettings(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrWhiteSpace(_selectedTheme))
2021-03-19 17:07:27 -03:00
{
_settings.SelectedTheme = _selectedTheme;
MainWindow.ApplyTheme(_selectedTheme);
2021-03-19 17:07:27 -03:00
}
2021-06-30 17:22:37 -07:00
SaveKeyboardList();
_settings.Save();
2021-03-19 17:07:27 -03:00
}
public void UpdateView() => this.FindControl<TextBlock>("VolumeLabel").Text = _settings.Volume.ToString();
2021-03-29 19:40:22 -03:00
2021-06-06 20:28:36 +01:00
void InitializeComponent()
2021-03-19 17:07:27 -03:00
{
AvaloniaXamlLoader.Load(this);
PopulateThemes();
2021-06-30 17:22:37 -07:00
PopulateKeyboardList();
2021-03-19 17:07:27 -03:00
this.FindControl<Button>("ApplyButton").Click += ApplySettings;
this.FindControl<Slider>("VolumeSlider").PropertyChanged += (s, e) => UpdateView();
}
/// <summary>
/// Populate the list of themes
/// </summary>
private void PopulateThemes()
{
// Get a reference to the theme list
_themeList = this.FindControl<ListBox>("ThemeList");
_themeList.SelectionChanged += ThemeList_SelectionChanged;
2021-03-19 17:07:27 -03:00
// Create a list of all found themes
2021-06-06 20:28:36 +01:00
List<string> items = new List<string>();
2021-03-19 17:07:27 -03:00
items.Add("default");
// Ensure the theme directory exists
if(!Directory.Exists("themes/"))
Directory.CreateDirectory("themes/");
// Add all theme directories if they're valid
foreach(string dir in Directory.EnumerateDirectories("themes/"))
2021-03-19 17:07:27 -03:00
{
string themeName = dir.Split('/')[1];
2021-03-19 17:07:27 -03:00
if(!File.Exists($"themes/{themeName}/view.xaml"))
continue;
2021-03-19 17:07:27 -03:00
items.Add(themeName);
2021-03-19 17:07:27 -03:00
}
_themeList.Items = items;
2021-03-19 17:07:27 -03:00
}
2021-06-30 17:22:37 -07:00
/// <summary>
/// Populate all of the keyboard bindings
/// </summary>
private void PopulateKeyboardList()
{
// Access all of the combo boxes
2021-07-01 10:19:25 -07:00
ComboBox loadImageKeyBind = this.FindControl<ComboBox>("LoadImageKeyBind");
ComboBox togglePlaybackKeyBind = this.FindControl<ComboBox>("TogglePlaybackKeyBind");
ComboBox stopPlaybackKeyBind = this.FindControl<ComboBox>("StopPlaybackKeyBind");
ComboBox nextTrackKeyBind = this.FindControl<ComboBox>("NextTrackKeyBind");
ComboBox previousTrackKeyBind = this.FindControl<ComboBox>("PreviousTrackKeyBind");
ComboBox nextIndexKeyBind = this.FindControl<ComboBox>("NextIndexKeyBind");
ComboBox previousIndexKeyBind = this.FindControl<ComboBox>("PreviousIndexKeyBind");
ComboBox fastForwardPlaybackKeyBind = this.FindControl<ComboBox>("FastForwardPlaybackKeyBind");
ComboBox rewindPlaybackKeyBind = this.FindControl<ComboBox>("RewindPlaybackKeyBind");
ComboBox volumeUpKeyBind = this.FindControl<ComboBox>("VolumeUpKeyBind");
ComboBox volumeDownKeyBind = this.FindControl<ComboBox>("VolumeDownKeyBind");
ComboBox toggleMuteKeyBind = this.FindControl<ComboBox>("ToggleMuteKeyBind");
ComboBox toggleDeEmphasisKeyBind = this.FindControl<ComboBox>("ToggleDeEmphasisKeyBind");
2021-06-30 17:22:37 -07:00
// Assign the list of values to all of them
Array keyboardList = GenerateKeyboardList();
2021-07-01 10:19:25 -07:00
loadImageKeyBind.Items = keyboardList;
togglePlaybackKeyBind.Items = keyboardList;
stopPlaybackKeyBind.Items = keyboardList;
nextTrackKeyBind.Items = keyboardList;
previousTrackKeyBind.Items = keyboardList;
nextIndexKeyBind.Items = keyboardList;
previousIndexKeyBind.Items = keyboardList;
fastForwardPlaybackKeyBind.Items = keyboardList;
rewindPlaybackKeyBind.Items = keyboardList;
volumeUpKeyBind.Items = keyboardList;
volumeDownKeyBind.Items = keyboardList;
toggleMuteKeyBind.Items = keyboardList;
toggleDeEmphasisKeyBind.Items = keyboardList;
2021-06-30 17:22:37 -07:00
// Set all of the currently selected items
2021-07-01 10:19:25 -07:00
loadImageKeyBind.SelectedItem = _settings.LoadImageKey;
togglePlaybackKeyBind.SelectedItem = _settings.TogglePlaybackKey;
stopPlaybackKeyBind.SelectedItem = _settings.StopPlaybackKey;
nextTrackKeyBind.SelectedItem = _settings.NextTrackKey;
previousTrackKeyBind.SelectedItem = _settings.PreviousTrackKey;
nextIndexKeyBind.SelectedItem = _settings.NextIndexKey;
previousIndexKeyBind.SelectedItem = _settings.PreviousIndexKey;
fastForwardPlaybackKeyBind.SelectedItem = _settings.FastForwardPlaybackKey;
rewindPlaybackKeyBind.SelectedItem = _settings.RewindPlaybackKey;
volumeUpKeyBind.SelectedItem = _settings.VolumeUpKey;
volumeDownKeyBind.SelectedItem = _settings.VolumeDownKey;
toggleMuteKeyBind.SelectedItem = _settings.ToggleMuteKey;
toggleDeEmphasisKeyBind.SelectedItem = _settings.ToggleDeEmphasisKey;
2021-06-30 17:22:37 -07:00
}
/// <summary>
/// Save back all values from keyboard bindings
/// </summary>
private void SaveKeyboardList()
{
// Access all of the combo boxes
2021-07-01 10:19:25 -07:00
ComboBox loadImageKeyBind = this.FindControl<ComboBox>("LoadImageKeyBind");
ComboBox togglePlaybackKeyBind = this.FindControl<ComboBox>("TogglePlaybackKeyBind");
ComboBox stopPlaybackKeyBind = this.FindControl<ComboBox>("StopPlaybackKeyBind");
ComboBox nextTrackKeyBind = this.FindControl<ComboBox>("NextTrackKeyBind");
ComboBox previousTrackKeyBind = this.FindControl<ComboBox>("PreviousTrackKeyBind");
ComboBox nextIndexKeyBind = this.FindControl<ComboBox>("NextIndexKeyBind");
ComboBox previousIndexKeyBind = this.FindControl<ComboBox>("PreviousIndexKeyBind");
ComboBox fastForwardPlaybackKeyBind = this.FindControl<ComboBox>("FastForwardPlaybackKeyBind");
ComboBox rewindPlaybackKeyBind = this.FindControl<ComboBox>("RewindPlaybackKeyBind");
ComboBox volumeUpKeyBind = this.FindControl<ComboBox>("VolumeUpKeyBind");
ComboBox volumeDownKeyBind = this.FindControl<ComboBox>("VolumeDownKeyBind");
ComboBox toggleMuteKeyBind = this.FindControl<ComboBox>("ToggleMuteKeyBind");
ComboBox toggleDeEmphasisKeyBind = this.FindControl<ComboBox>("ToggleDeEmphasisKeyBind");
2021-06-30 17:22:37 -07:00
// Set all of the currently selected items
2021-07-01 10:19:25 -07:00
_settings.LoadImageKey = (Key)loadImageKeyBind.SelectedItem;
_settings.TogglePlaybackKey = (Key)togglePlaybackKeyBind.SelectedItem;
_settings.StopPlaybackKey = (Key)stopPlaybackKeyBind.SelectedItem;
_settings.NextTrackKey = (Key)nextTrackKeyBind.SelectedItem;
_settings.PreviousTrackKey = (Key)previousTrackKeyBind.SelectedItem;
_settings.NextIndexKey = (Key)nextIndexKeyBind.SelectedItem;
_settings.PreviousIndexKey = (Key)previousIndexKeyBind.SelectedItem;
_settings.FastForwardPlaybackKey = (Key)fastForwardPlaybackKeyBind.SelectedItem;
_settings.RewindPlaybackKey = (Key)rewindPlaybackKeyBind.SelectedItem;
_settings.VolumeUpKey = (Key)volumeUpKeyBind.SelectedItem;
_settings.VolumeDownKey = (Key)volumeDownKeyBind.SelectedItem;
_settings.ToggleMuteKey = (Key)toggleMuteKeyBind.SelectedItem;
_settings.ToggleDeEmphasisKey = (Key)toggleDeEmphasisKeyBind.SelectedItem;
2021-06-30 17:22:37 -07:00
}
/// <summary>
/// Generate a list of keyboard keys for mapping
/// </summary>
/// <returns></returns>
private Array GenerateKeyboardList()
{
return Enum.GetValues(typeof(Key));
}
2021-03-19 17:07:27 -03:00
}
}