Files
annotations/Azaion.Suite/MainSuite.xaml.cs
T
2024-11-28 18:04:34 +02:00

123 lines
4.2 KiB
C#

using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Azaion.Annotator.Extensions;
using Azaion.Common.DTO;
using Azaion.Common.DTO.Config;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using SharpVectors.Converters;
namespace Azaion.Suite;
public partial class MainSuite
{
private readonly AppConfig _appConfig;
private readonly IConfigUpdater _configUpdater;
private readonly IEnumerable<IAzaionModule> _modules;
private readonly IServiceProvider _sp;
private readonly Dictionary<WindowEnum, Window> _openedWindows = new();
public MainSuite(IOptions<AppConfig> appConfig, IConfigUpdater configUpdater, IEnumerable<IAzaionModule> modules, IServiceProvider sp )
{
_configUpdater = configUpdater;
_modules = modules;
_sp = sp;
_appConfig = appConfig.Value;
InitializeComponent();
Loaded += OnLoaded;
Closed += OnFormClosed;
SizeChanged += async (_, _) => await SaveUserSettings();
LocationChanged += async (_, _) => await SaveUserSettings();
StateChanged += async (_, _) => await SaveUserSettings();
Left = SystemParameters.WorkArea.Width - Width - 250;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
if (!Directory.Exists(_appConfig.DirectoriesConfig.LabelsDirectory))
Directory.CreateDirectory(_appConfig.DirectoriesConfig.LabelsDirectory);
if (!Directory.Exists(_appConfig.DirectoriesConfig.ImagesDirectory))
Directory.CreateDirectory(_appConfig.DirectoriesConfig.ImagesDirectory);
if (!Directory.Exists(_appConfig.DirectoriesConfig.ResultsDirectory))
Directory.CreateDirectory(_appConfig.DirectoriesConfig.ResultsDirectory);
foreach (var azaionModule in _modules)
{
var lvItem = new ListViewItem
{
Content = new StackPanel { Children =
{
new SvgViewbox
{
SvgSource = azaionModule.SvgIcon,
Width = 32,
Height = 32,
Margin = new Thickness(0, 0, 10, 0)
},
new TextBlock
{
Text = azaionModule.Name,
Foreground = Brushes.White,
HorizontalAlignment = HorizontalAlignment.Center,
Background = Brushes.Black
}
} },
Background = Brushes.Black,
Foreground = Brushes.White,
Cursor = Cursors.Hand,
Tag = azaionModule
};
lvItem.MouseUp += (lv, _) => OpenWindow((lv as ListViewItem)!);
ListView.Items.Add(lvItem);
}
//by default show first
ListView.SelectedIndex = 0;
OpenWindow((ListView.Items[0] as ListViewItem)!);
}
private void OpenWindow(ListViewItem sender)
{
var module = (sender.Tag as IAzaionModule)!;
var window = (_sp.GetRequiredService(module.MainWindowType) as Window)!;
if (_openedWindows.ContainsKey(module.WindowEnum))
window.Activate();
else
{
_openedWindows[module.WindowEnum] = window;
window.Closed += (_, _) =>
{
_openedWindows.Remove(module.WindowEnum);
if (!_openedWindows.Any())
Close();
};
window.Show();
window.Activate();
}
}
private async Task SaveUserSettings()
{
await ThrottleExt.Throttle(() =>
{
_configUpdater.Save(_appConfig);
return Task.CompletedTask;
}, TimeSpan.FromSeconds(5));
}
private void OnFormClosed(object? sender, EventArgs e)
{
_configUpdater.Save(_appConfig);
foreach (var window in _openedWindows)
window.Value.Close();
Application.Current.Shutdown();
}
private void CloseBtn_OnClick(object sender, RoutedEventArgs e) => Close();
}