2021-03-24 17:34:04 -03:00
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2021-06-06 20:28:36 +01:00
|
|
|
using System.Xml;
|
|
|
|
|
using Avalonia.Controls;
|
|
|
|
|
using Avalonia.Input;
|
|
|
|
|
using Avalonia.Markup.Xaml;
|
2021-03-19 17:07:27 -03:00
|
|
|
|
|
|
|
|
namespace RedBookPlayer
|
|
|
|
|
{
|
|
|
|
|
public class MainWindow : Window
|
|
|
|
|
{
|
2021-06-06 20:28:36 +01:00
|
|
|
public static MainWindow Instance;
|
|
|
|
|
public ContentControl ContentControl;
|
|
|
|
|
public Window settingsWindow;
|
2021-03-19 17:07:27 -03:00
|
|
|
|
|
|
|
|
public MainWindow()
|
|
|
|
|
{
|
|
|
|
|
Instance = this;
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 21:39:05 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Apply a custom theme to the player
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="theme">Path to the theme under the themes directory</param>
|
2021-03-24 17:34:04 -03:00
|
|
|
public static void ApplyTheme(string theme)
|
|
|
|
|
{
|
2021-06-06 21:39:05 -07:00
|
|
|
// If no theme path is provided, we can ignore
|
|
|
|
|
if(string.IsNullOrWhiteSpace(theme))
|
2021-03-24 17:34:04 -03:00
|
|
|
return;
|
|
|
|
|
|
2021-06-06 21:39:05 -07:00
|
|
|
// If the theme name is "default", we assume the internal theme is used
|
|
|
|
|
if(theme.Equals("default", StringComparison.OrdinalIgnoreCase))
|
2021-03-24 17:34:04 -03:00
|
|
|
{
|
2021-06-06 20:28:36 +01:00
|
|
|
Instance.ContentControl.Content = new PlayerView();
|
2021-03-24 17:34:04 -03:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-06-06 21:39:05 -07:00
|
|
|
string themeDirectory = $"{Directory.GetCurrentDirectory()}/themes/{theme}";
|
|
|
|
|
string xamlPath = $"{themeDirectory}/view.xaml";
|
2021-03-24 17:34:04 -03:00
|
|
|
|
2021-06-06 20:28:36 +01:00
|
|
|
if(!File.Exists(xamlPath))
|
2021-03-24 17:34:04 -03:00
|
|
|
{
|
2021-06-06 20:28:36 +01:00
|
|
|
Console.WriteLine("Warning: specified theme doesn't exist, reverting to default");
|
2021-03-24 17:34:04 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-06-06 21:39:05 -07:00
|
|
|
string xaml = File.ReadAllText(xamlPath);
|
|
|
|
|
xaml = xaml.Replace("Source=\"", $"Source=\"file://{themeDirectory}/");
|
|
|
|
|
Instance.ContentControl.Content = new PlayerView(xaml);
|
2021-03-24 17:34:04 -03:00
|
|
|
}
|
2021-06-06 20:28:36 +01:00
|
|
|
catch(XmlException ex)
|
2021-03-24 17:34:04 -03:00
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Error: invalid theme XAML ({ex.Message}), reverting to default");
|
2021-06-06 20:28:36 +01:00
|
|
|
Instance.ContentControl.Content = new PlayerView();
|
2021-03-24 17:34:04 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 20:28:36 +01:00
|
|
|
Instance.Width = ((PlayerView)Instance.ContentControl.Content).Width;
|
|
|
|
|
Instance.Height = ((PlayerView)Instance.ContentControl.Content).Height;
|
2021-03-24 17:34:04 -03:00
|
|
|
}
|
|
|
|
|
|
2021-03-19 17:07:27 -03:00
|
|
|
public void OnKeyDown(object sender, KeyEventArgs e)
|
|
|
|
|
{
|
2021-06-06 20:28:36 +01:00
|
|
|
if(e.Key == Key.F1)
|
2021-03-19 17:07:27 -03:00
|
|
|
{
|
2021-03-24 17:34:04 -03:00
|
|
|
settingsWindow = new SettingsWindow(App.Settings);
|
2021-03-23 19:58:25 -03:00
|
|
|
settingsWindow.Show();
|
2021-03-19 17:07:27 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 20:28:36 +01:00
|
|
|
void InitializeComponent()
|
2021-03-19 17:07:27 -03:00
|
|
|
{
|
|
|
|
|
AvaloniaXamlLoader.Load(this);
|
|
|
|
|
|
2021-06-06 20:28:36 +01:00
|
|
|
ContentControl = this.FindControl<ContentControl>("Content");
|
2021-03-19 17:07:27 -03:00
|
|
|
ContentControl.Content = new PlayerView();
|
|
|
|
|
|
2021-06-06 20:28:36 +01:00
|
|
|
Instance.MaxWidth = ((PlayerView)Instance.ContentControl.Content).Width;
|
|
|
|
|
Instance.MaxHeight = ((PlayerView)Instance.ContentControl.Content).Height;
|
2021-03-19 17:07:27 -03:00
|
|
|
|
|
|
|
|
ContentControl.Content = new PlayerView();
|
|
|
|
|
|
2021-06-06 20:28:36 +01:00
|
|
|
CanResize = false;
|
|
|
|
|
|
|
|
|
|
KeyDown += OnKeyDown;
|
2021-03-19 17:07:27 -03:00
|
|
|
|
2021-06-06 20:28:36 +01:00
|
|
|
Closing += (s, e) =>
|
2021-03-19 17:07:27 -03:00
|
|
|
{
|
2021-03-23 19:58:25 -03:00
|
|
|
settingsWindow?.Close();
|
|
|
|
|
settingsWindow = null;
|
2021-03-19 17:07:27 -03:00
|
|
|
};
|
|
|
|
|
|
2021-06-06 20:28:36 +01:00
|
|
|
Closing += (e, f) =>
|
2021-03-19 17:07:27 -03:00
|
|
|
{
|
2021-04-06 18:28:25 -03:00
|
|
|
PlayerView.Player.Stop();
|
2021-03-19 17:07:27 -03:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|