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 _modules; private readonly IServiceProvider _sp; private readonly List _openedWindows = new(); public MainSuite(IOptions appConfig, IConfigUpdater configUpdater, IEnumerable 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 - 150; } 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 += (sender, _) => { var module = ((sender as ListViewItem)!.Tag as IAzaionModule)!; var window = (_sp.GetRequiredService(module.MainWindowType) as Window)!; _openedWindows.Add(window); window.Show(); }; ListView.Items.Add(lvItem); } } 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(); } }