fix keyboard catch, fix loading

refactoring
This commit is contained in:
Alex Bezdieniezhnykh
2024-11-25 13:03:46 +02:00
parent b61bed3b51
commit 30b9b9aa80
18 changed files with 88 additions and 117 deletions
+1 -1
View File
@@ -76,7 +76,7 @@ public partial class Annotator
_vlcFrameExtractor = vlcFrameExtractor;
_aiDetector = aiDetector;
VideoView.Loaded += VideoView_Loaded;
Loaded += VideoView_Loaded;
Closed += OnFormClosed;
Editor.GetTimeFunc = () => TimeSpan.FromMilliseconds(_mediaPlayer.Time);
+2
View File
@@ -50,4 +50,6 @@ public class AnnotatorModule : IAzaionModule
</svg>";
public Type MainWindowType => typeof(Annotator);
public WindowEnum WindowEnum => WindowEnum.Annotator;
}
-8
View File
@@ -41,12 +41,4 @@
<SubType>Designer</SubType>
</Page>
</ItemGroup>
<ItemGroup>
<None Remove="annotator-logo.svg" />
<Resource Include="annotator-logo.svg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>
</ItemGroup>
</Project>
-38
View File
@@ -1,38 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 28.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 800 800" style="enable-background:new 0 0 800 800;" xml:space="preserve">
<style type="text/css">
.st0{fill:#FFFFFF;}
.st1{opacity:0.75;fill:#006932;stroke:#000000;stroke-width:20;stroke-miterlimit:10;}
.st2{opacity:0.75;fill:#6C0A0B;stroke:#000000;stroke-width:20;stroke-miterlimit:10;}
.st3{opacity:0.85;fill:#FFFFFF;stroke:#000000;stroke-width:20;stroke-miterlimit:10;}
</style>
<g id="SVGRepo_bgCarrier">
</g>
<g id="SVGRepo_tracerCarrier">
</g>
<g id="SVGRepo_iconCarrier">
<g transform="translate(1)">
<g>
<polygon class="st0" points="465.7,93.3 545.7,93.3 545.7,13.3 465.7,13.3 "/>
</g>
</g>
</g>
<rect x="43.3" y="53.3" class="st1" width="609.7" height="301"/>
<rect x="443.2" y="400" class="st2" width="285.8" height="363"/>
<rect x="19" y="325" class="st3" width="53" height="53"/>
<rect x="17.5" y="166" class="st3" width="53" height="53"/>
<rect x="17.5" y="27" class="st3" width="53" height="53"/>
<rect x="325.5" y="329" class="st3" width="53" height="53"/>
<rect x="624.5" y="325" class="st3" width="53" height="53"/>
<rect x="626.5" y="168" class="st3" width="53" height="53"/>
<rect x="626.5" y="27" class="st3" width="53" height="53"/>
<rect x="323.5" y="27" class="st3" width="53" height="53"/>
<rect x="419.8" y="377.3" class="st3" width="53" height="53"/>
<rect x="698.7" y="378.3" class="st3" width="53" height="53"/>
<rect x="418.5" y="733.2" class="st3" width="53" height="53"/>
<rect x="698.7" y="736.5" class="st3" width="53" height="53"/>
<rect x="415.8" y="555.7" class="st3" width="53" height="53"/>
<rect x="701.2" y="551.7" class="st3" width="53" height="53"/>
</svg>

Before

Width:  |  Height:  |  Size: 1.8 KiB

+1
View File
@@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="MediatR" Version="12.4.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
@@ -1,8 +1,8 @@
using CommandLine;
namespace Azaion.Suite.Services.DTO;
namespace Azaion.Common.DTO;
public class SuiteCommandLineOptions
public class ApiCredentials
{
[Option('e', "email", Required = true, HelpText = "The email for authorization.")]
public string Email { get; set; } = null!;
+1
View File
@@ -5,4 +5,5 @@ public interface IAzaionModule
string Name { get; }
string SvgIcon { get; }
Type MainWindowType { get; }
WindowEnum WindowEnum { get; }
}
@@ -1,20 +0,0 @@
using System.Windows;
using Azaion.Common.DTO;
namespace Azaion.Common.Extensions;
public static class WindowExtensions
{
public static WindowEnum GetParentWindow(this FrameworkElement? element)
{
while (element != null && element is not Window)
{
element = element.Parent as FrameworkElement;
}
if (element is not Window)
return WindowEnum.None;
return WindowEnum.Annotator;
}
}
+6 -6
View File
@@ -17,13 +17,13 @@ public class AzaionApiClient(HttpClient httpClient) : IDisposable
private SecureString Password { get; set; } = new();
private string JwtToken { get; set; } = null!;
public void Login(string email, string password)
public void EnterCredentials(ApiCredentials credentials)
{
if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password))
if (string.IsNullOrWhiteSpace(credentials.Email) || string.IsNullOrWhiteSpace(credentials.Password))
throw new Exception("Email or password is empty!");
Email = email;
Password = password.ToSecureString();
Email = credentials.Email;
Password = credentials.Password.ToSecureString();
}
public async Task<Stream> GetResource(string fileName, string password, HardwareInfo hardware)
@@ -38,7 +38,7 @@ public class AzaionApiClient(HttpClient httpClient) : IDisposable
private async Task<string> Authorize()
{
if (string.IsNullOrEmpty(Email) || Password.Length == 0)
throw new Exception("Email or password is empty! Please do Login first!");
throw new Exception("Email or password is empty! Please do EnterCredentials first!");
var payload = new
{
@@ -50,7 +50,7 @@ public class AzaionApiClient(HttpClient httpClient) : IDisposable
new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, JSON_MEDIA));
if (!response.IsSuccessStatusCode)
throw new Exception($"Login failed: {response.StatusCode}");
throw new Exception($"EnterCredentials failed: {response.StatusCode}");
var responseData = await response.Content.ReadAsStringAsync();
+28 -3
View File
@@ -1,20 +1,45 @@
using System.IO;
using System.Reflection;
using Azaion.Common.DTO;
namespace Azaion.Common.Services;
public interface IResourceLoader
{
Task<MemoryStream> Load(string fileName, CancellationToken cancellationToken = default);
Assembly LoadAssembly(string asmName);
}
public class ResourceLoader(string email, string password, AzaionApiClient api, IHardwareService hardwareService) : IResourceLoader
public class ResourceLoader(AzaionApiClient api, ApiCredentials credentials) : IResourceLoader
{
private static readonly List<string> EncryptedResources =
[
"Azaion.Annotator",
"Azaion.Dataset"
];
public Assembly LoadAssembly(string resourceName)
{
var assemblyName = resourceName.Split(',').First();
if (EncryptedResources.Contains(assemblyName))
{
var stream = Load(assemblyName).GetAwaiter().GetResult();
return Assembly.Load(stream.ToArray());
}
var loadedAssembly = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(a => a.GetName().Name == assemblyName);
return loadedAssembly;
}
public async Task<MemoryStream> Load(string fileName, CancellationToken cancellationToken = default)
{
var hardwareService = new HardwareService();
var hardwareInfo = await hardwareService.GetHardware();
var encryptedStream = await api.GetResource(fileName, password, hardwareInfo);
var encryptedStream = await api.GetResource(fileName, credentials.Password, hardwareInfo);
var key = Security.MakeEncryptionKey(email, password, hardwareInfo.Hash);
var key = Security.MakeEncryptionKey(credentials.Email, credentials.Password, hardwareInfo.Hash);
var stream = new MemoryStream();
await encryptedStream.DecryptTo(stream, key, cancellationToken);
return stream;
-6
View File
@@ -25,10 +25,4 @@
<ProjectReference Include="..\Azaion.Common\Azaion.Common.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Drawing.Common">
<HintPath>C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\8.0.8\System.Drawing.Common.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
-1
View File
@@ -3,7 +3,6 @@ using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using Azaion.Annotator.Extensions;
using Azaion.Common;
using Azaion.Common.DTO;
using Azaion.Common.DTO.Config;
+2
View File
@@ -20,4 +20,6 @@ public class DatasetExplorerModule : IAzaionModule
</svg>";
public Type MainWindowType => typeof(DatasetExplorer);
public WindowEnum WindowEnum => WindowEnum.DatasetExplorer;
}
+37 -27
View File
@@ -1,7 +1,7 @@
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using Azaion.Annotator;
@@ -13,7 +13,6 @@ using Azaion.Common.DTO.Config;
using Azaion.Common.Extensions;
using Azaion.Common.Services;
using Azaion.Dataset;
using Azaion.Suite.Services.DTO;
using CommandLine;
using LibVLCSharp.Shared;
using MediatR;
@@ -33,24 +32,28 @@ public partial class App
private readonly ILogger<App> _logger;
private readonly IMediator _mediator;
private static readonly List<string> EncryptedResources =
[
"Azaion.Annotator.dll",
"Azaion.Dataset.dll"
];
private static readonly IResourceLoader? ResourceLoader = new ResourceLoader("", "", null!, null!);
//Authorize to api and
private static readonly IResourceLoader ResourceLoader;
static App()
{
//Load encrypted dlls
var result = Parser.Default.ParseArguments<SuiteCommandLineOptions>(Environment.GetCommandLineArgs());
new ConfigUpdater().CheckConfig();
var result = Parser.Default.ParseArguments<ApiCredentials>(Environment.GetCommandLineArgs());
if (result.Errors.Any())
return;
return;
var apiCreds = result.Value;
ResourceLoader = new ResourceLoader(CreateApiClient(apiCreds), apiCreds);
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => ResourceLoader.LoadAssembly(args.Name);
}
private static AzaionApiClient CreateApiClient(ApiCredentials credentials)
{
ApiConfig apiConfig;
try
{
if (File.Exists(Constants.CONFIG_PATH))
if (!File.Exists(Constants.CONFIG_PATH))
throw new FileNotFoundException(Constants.CONFIG_PATH);
var configStr = File.ReadAllText(Constants.CONFIG_PATH);
apiConfig = JsonConvert.DeserializeObject<AppConfig>(configStr)!.ApiConfig;
@@ -70,19 +73,9 @@ public partial class App
BaseAddress = new Uri(apiConfig.Url),
Timeout = TimeSpan.FromSeconds(apiConfig.TimeoutSeconds)
});
var email = result.Value.Email;
var password = result.Value.Password;
api.Login(email, password);
ResourceLoader = new ResourceLoader(email, password, api, new HardwareService());
foreach (var resource in EncryptedResources)
{
var stream = ResourceLoader.Load(resource).GetAwaiter().GetResult();
Assembly.Load(stream.ToArray());
}
new ConfigUpdater().CheckConfig();
api.EnterCredentials(credentials);
return api;
}
public App()
@@ -104,7 +97,7 @@ public partial class App
{
services.AddSingleton<MainSuite>();
services.AddSingleton<IHardwareService, HardwareService>();
services.AddSingleton<IResourceLoader>(ResourceLoader!);
services.AddSingleton(ResourceLoader);
services.Configure<AppConfig>(context.Configuration);
services.ConfigureSection<ApiConfig>(context.Configuration);
@@ -170,10 +163,27 @@ public partial class App
private void GlobalClick(object sender, RoutedEventArgs e)
{
var args = (KeyEventArgs)e;
var keyEvent = new KeyEvent(sender, args, (sender as FrameworkElement).GetParentWindow());
var windowEnum = GetParentWindow(sender as FrameworkElement);
if (windowEnum == WindowEnum.None)
return;
var keyEvent = new KeyEvent(sender, args, windowEnum);
_ = ThrottleExt.Throttle(() => _mediator.Publish(keyEvent), TimeSpan.FromMilliseconds(50));
}
private WindowEnum GetParentWindow(FrameworkElement? element)
{
while (element != null && element is not TabItem)
{
element = element.Parent as FrameworkElement;
}
if (element is not TabItem || element.Tag == null)
return WindowEnum.None;
return (WindowEnum)element.Tag;
}
protected override async void OnExit(ExitEventArgs e)
{
base.OnExit(e);
+1 -1
View File
@@ -4,7 +4,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Title="MainSuite" Height="450" Width="800">
Title="Azaion Оператор" Height="450" Width="800">
<Grid Background="Black">
<TabControl Name="MainTabControl"
TabStripPlacement="Left"
+6 -1
View File
@@ -57,6 +57,10 @@ public partial class MainSuite
foreach (var azaionModule in _modules)
{
var window = (_sp.GetRequiredService(azaionModule.MainWindowType) as Window)!;
window.Height = 0;
window.Width = 0;
window.Show();
window.Hide();
_openedWindows.Add(window);
var icon = new SvgViewbox
{
@@ -78,7 +82,8 @@ public partial class MainSuite
Content = window.Content,
Background = Brushes.Black,
Foreground = Brushes.White,
Cursor = Cursors.Hand
Cursor = Cursors.Hand,
Tag = azaionModule.WindowEnum
};
MainTabControl.Items.Add(tabItem);
}
-2
View File
@@ -1,6 +1,4 @@
using Azaion.Common.Services;
using Azaion.Suite;
using Azaion.Suite.Services;
using Xunit;
namespace Azaion.Annotator.Test;