Files
annotations/Azaion.Suite/MainSuite.xaml.cs
T
Alex Bezdieniezhnykh d842466594 gps matcher async
put cryptography lib to fixed version
fix race condition bug in queue handler
add lock to db writing and backup to file db on each write
2025-05-29 00:35:35 +03:00

167 lines
5.6 KiB
C#

using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Azaion.Common.Database;
using Azaion.Common.DTO;
using Azaion.Common.DTO.Config;
using Azaion.Common.Extensions;
using Azaion.Common.Services;
using Azaion.CommonSecurity.Services;
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 IGalleryService _galleryService;
private readonly IDbFactory _dbFactory;
private readonly Dictionary<WindowEnum, Window> _openedWindows = new();
private readonly IInferenceClient _inferenceClient;
private readonly IGpsMatcherClient _gpsMatcherClient;
private static readonly Guid SaveConfigTaskId = Guid.NewGuid();
public MainSuite(IOptions<AppConfig> appConfig,
IConfigUpdater configUpdater,
IEnumerable<IAzaionModule> modules,
IServiceProvider sp,
IGalleryService galleryService,
IDbFactory dbFactory,
IInferenceClient inferenceClient,
IGpsMatcherClient gpsMatcherClient)
{
_configUpdater = configUpdater;
_modules = modules;
_sp = sp;
_galleryService = galleryService;
_dbFactory = dbFactory;
_inferenceClient = inferenceClient;
_gpsMatcherClient = gpsMatcherClient;
_appConfig = appConfig.Value;
InitializeComponent();
Loaded += OnLoaded;
Closed += OnFormClosed;
SizeChanged += (_, _) => SaveUserSettings();
LocationChanged += (_, _) => SaveUserSettings();
StateChanged += (_, _) => SaveUserSettings();
Left = (SystemParameters.WorkArea.Width - Width) / 2;
}
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 = 24,
Height = 24,
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);
_ = _sp.GetRequiredService(azaionModule.MainWindowType) as Window;
}
_ = Task.Run(async () => await _galleryService.RefreshThumbnails());
//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())
return;
Close();
};
window.Show();
window.Activate();
}
}
private void SaveUserSettings()
{
ThrottleExt.Throttle(() =>
{
_configUpdater.Save(_appConfig);
return Task.CompletedTask;
}, SaveConfigTaskId, TimeSpan.FromSeconds(2));
}
private void OnFormClosed(object? sender, EventArgs e)
{
_configUpdater.Save(_appConfig);
foreach (var window in _openedWindows)
window.Value.Close();
_inferenceClient.Dispose();
_gpsMatcherClient.Dispose();
Application.Current.Shutdown();
}
private void CloseBtn_OnClick(object sender, RoutedEventArgs e) => Close();
private double _startMouseMovePosition;
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (e.OriginalSource is Button || e.OriginalSource is StackPanel)
return;
if (e.LeftButton == MouseButtonState.Pressed)
Left = System.Windows.Forms.Cursor.Position.X - _startMouseMovePosition;
}
private void MainSuite_OnMouseDown(object sender, MouseButtonEventArgs e)
{
_startMouseMovePosition = e.GetPosition(this).X;
}
}