Files
RedBookPlayer/RedBookPlayer/MainWindow.xaml.cs

102 lines
3.2 KiB
C#
Raw Normal View History

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();
}
/// <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)
{
// If no theme path is provided, we can ignore
if(string.IsNullOrWhiteSpace(theme))
2021-03-24 17:34:04 -03:00
return;
// 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
{
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
{
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);
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
{
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
};
}
}
}