diff --git a/RedBookPlayer/App.xaml.cs b/RedBookPlayer/App.xaml.cs index efc805a..6a3a6a9 100644 --- a/RedBookPlayer/App.xaml.cs +++ b/RedBookPlayer/App.xaml.cs @@ -9,7 +9,7 @@ namespace RedBookPlayer { public class App : Application { - public static string CurrentTheme = "default"; + public static Settings Settings; static App() { @@ -32,6 +32,8 @@ namespace RedBookPlayer { desktop.MainWindow = new MainWindow(); desktop.ShutdownMode = ShutdownMode.OnMainWindowClose; + + Settings = Settings.Load("settings.json"); } base.OnFrameworkInitializationCompleted(); diff --git a/RedBookPlayer/MainWindow.xaml.cs b/RedBookPlayer/MainWindow.xaml.cs index 6224f13..057a43f 100644 --- a/RedBookPlayer/MainWindow.xaml.cs +++ b/RedBookPlayer/MainWindow.xaml.cs @@ -1,6 +1,8 @@ using Avalonia.Controls; using Avalonia.Markup.Xaml; using Avalonia.Input; +using System; +using System.IO; namespace RedBookPlayer { @@ -16,11 +18,50 @@ namespace RedBookPlayer InitializeComponent(); } + public static void ApplyTheme(string theme) + { + if ((theme ?? "") == "") + { + return; + } + + if (theme == "default") + { + MainWindow.Instance.ContentControl.Content = new PlayerView(); + } + else + { + string themeDirectory = Directory.GetCurrentDirectory() + "/themes/" + theme; + string xamlPath = themeDirectory + "/view.xaml"; + + if (!File.Exists(xamlPath)) + { + Console.WriteLine($"Warning: specified theme doesn't exist, reverting to default"); + return; + } + + try + { + MainWindow.Instance.ContentControl.Content = new PlayerView( + File.ReadAllText(xamlPath).Replace("Source=\"", $"Source=\"file://{themeDirectory}/") + ); + } + catch (System.Xml.XmlException ex) + { + Console.WriteLine($"Error: invalid theme XAML ({ex.Message}), reverting to default"); + MainWindow.Instance.ContentControl.Content = new PlayerView(); + } + } + + MainWindow.Instance.Width = ((PlayerView)MainWindow.Instance.ContentControl.Content).Width; + MainWindow.Instance.Height = ((PlayerView)MainWindow.Instance.ContentControl.Content).Height; + } + public void OnKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.F1) { - settingsWindow = new SettingsWindow(); + settingsWindow = new SettingsWindow(App.Settings); settingsWindow.Show(); } } diff --git a/RedBookPlayer/PlayerView.xaml.cs b/RedBookPlayer/PlayerView.xaml.cs index 5e705d2..b1328ec 100644 --- a/RedBookPlayer/PlayerView.xaml.cs +++ b/RedBookPlayer/PlayerView.xaml.cs @@ -168,14 +168,14 @@ namespace RedBookPlayer private Bitmap GetBitmap(char character) { - if (App.CurrentTheme == "default") + if (App.Settings.SelectedTheme == "default") { IAssetLoader assets = AvaloniaLocator.Current.GetService(); return new Bitmap(assets.Open(new Uri($"avares://RedBookPlayer/Assets/{character}.png"))); } else { - string themeDirectory = Directory.GetCurrentDirectory() + "/themes/" + App.CurrentTheme; + string themeDirectory = Directory.GetCurrentDirectory() + "/themes/" + App.Settings.SelectedTheme; Bitmap bitmap; using (FileStream stream = File.Open(themeDirectory + $"/{character}.png", FileMode.Open)) { diff --git a/RedBookPlayer/Settings.cs b/RedBookPlayer/Settings.cs new file mode 100644 index 0000000..726d489 --- /dev/null +++ b/RedBookPlayer/Settings.cs @@ -0,0 +1,57 @@ +using System; +using System.IO; +using System.Text.Json; + +namespace RedBookPlayer +{ + public class Settings + { + public bool AutoPlay { get; set; } = false; + public string SelectedTheme { get; set; } = "default"; + string filePath; + + public Settings() { } + + public Settings(string filePath) + { + this.filePath = filePath; + } + + public static Settings Load(string filePath) + { + if (File.Exists(filePath)) + { + try + { + Settings settings = JsonSerializer.Deserialize(File.ReadAllText(filePath)); + settings.filePath = filePath; + Console.WriteLine(settings.AutoPlay); + + MainWindow.ApplyTheme(settings.SelectedTheme); + + return settings; + } + catch (JsonException) + { + Console.WriteLine("Couldn't parse settings, reverting to default"); + return new Settings(filePath); + } + } + else + { + return new Settings(filePath); + } + } + + public void Save() + { + JsonSerializerOptions options = new JsonSerializerOptions() + { + WriteIndented = true + }; + + string json = JsonSerializer.Serialize(this, options); + File.WriteAllText(filePath, json); + } + } +} \ No newline at end of file diff --git a/RedBookPlayer/SettingsWindow.xaml b/RedBookPlayer/SettingsWindow.xaml index 7d563b2..b05b4ed 100644 --- a/RedBookPlayer/SettingsWindow.xaml +++ b/RedBookPlayer/SettingsWindow.xaml @@ -8,7 +8,13 @@ Width="450" Height="600"> Themes - + + + + Auto-play CD on load + + + diff --git a/RedBookPlayer/SettingsWindow.xaml.cs b/RedBookPlayer/SettingsWindow.xaml.cs index 048d912..8eb358b 100644 --- a/RedBookPlayer/SettingsWindow.xaml.cs +++ b/RedBookPlayer/SettingsWindow.xaml.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Text.Json; using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; @@ -9,11 +10,16 @@ namespace RedBookPlayer { public class SettingsWindow : Window { + Settings settings; ListBox themeList; string selectedTheme; + CheckBox autoPlay; - public SettingsWindow() + public SettingsWindow() { } + + public SettingsWindow(Settings settings) { + this.settings = settings; InitializeComponent(); } @@ -27,39 +33,21 @@ namespace RedBookPlayer selectedTheme = (string)e.AddedItems[0]; } - public void ApplyTheme(object sender, RoutedEventArgs e) + public void LoadSettings() { - if (selectedTheme == "" || selectedTheme == null) + autoPlay.IsChecked = settings.AutoPlay; + } + + public void ApplySettings(object sender, RoutedEventArgs e) + { + if ((selectedTheme ?? "") != "") { - return; + settings.SelectedTheme = selectedTheme; + MainWindow.ApplyTheme(selectedTheme); } - if (selectedTheme == "default") - { - MainWindow.Instance.ContentControl.Content = new PlayerView(); - } - else - { - string themeDirectory = Directory.GetCurrentDirectory() + "/themes/" + selectedTheme; - string xamlPath = themeDirectory + "/view.xaml"; - - try - { - MainWindow.Instance.ContentControl.Content = new PlayerView( - File.ReadAllText(xamlPath).Replace("Source=\"", $"Source=\"file://{themeDirectory}/") - ); - } - catch (System.Xml.XmlException ex) - { - Console.WriteLine($"Error: invalid theme XAML ({ex.Message}), reverting to default"); - MainWindow.Instance.ContentControl.Content = new PlayerView(); - } - } - - MainWindow.Instance.Width = ((PlayerView)MainWindow.Instance.ContentControl.Content).Width; - MainWindow.Instance.Height = ((PlayerView)MainWindow.Instance.ContentControl.Content).Height; - - App.CurrentTheme = selectedTheme; + settings.AutoPlay = autoPlay.IsChecked ?? false; + settings.Save(); } private void InitializeComponent() @@ -89,7 +77,10 @@ namespace RedBookPlayer themeList.Items = items; - this.FindControl