Files
annotations/Azaion.Suite/MainSuite.xaml.cs
T
Alex Bezdieniezhnykh b61bed3b51 add left menu with icons
2024-11-24 02:09:04 +02:00

103 lines
3.5 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 Azaion.Dataset;
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 List<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();
}
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);
Left = _appConfig.WindowConfig.WindowLocation.X;
Top = _appConfig.WindowConfig.WindowLocation.Y;
Width = _appConfig.WindowConfig.WindowSize.Width;
Height = _appConfig.WindowConfig.WindowSize.Height;
if (_appConfig.WindowConfig.FullScreen)
WindowState = WindowState.Maximized;
foreach (var azaionModule in _modules)
{
var window = (_sp.GetRequiredService(azaionModule.MainWindowType) as Window)!;
_openedWindows.Add(window);
var icon = new SvgViewbox
{
SvgSource = azaionModule.SvgIcon,
Width = 32,
Height = 32,
Margin = new Thickness(0, 0, 10, 0)
};
var text = new TextBlock
{
Text = azaionModule.Name,
Foreground = Brushes.White,
HorizontalAlignment = HorizontalAlignment.Center,
Background = Brushes.Black
};
var tabItem = new TabItem
{
Header = new StackPanel { Children = { icon, text } },
Content = window.Content,
Background = Brushes.Black,
Foreground = Brushes.White,
Cursor = Cursors.Hand
};
MainTabControl.Items.Add(tabItem);
}
}
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.Close();
Application.Current.Shutdown();
}
}