1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Merge branch 'development'

This commit is contained in:
Robert 2023-08-27 12:18:16 +02:00
commit d605afe6cc
30 changed files with 377 additions and 147 deletions

View File

@ -2,9 +2,16 @@
namespace Artemis.Core; namespace Artemis.Core;
internal static class SKPaintExtensions /// <summary>
/// A static class providing <see cref="SKPaint" /> extensions
/// </summary>
public static class SKPaintExtensions
{ {
internal static void DisposeSelfAndProperties(this SKPaint paint) /// <summary>
/// Disposes the paint and its disposable properties such as shaders and filters.
/// </summary>
/// <param name="paint">The pain to dispose.</param>
public static void DisposeSelfAndProperties(this SKPaint paint)
{ {
paint.ImageFilter?.Dispose(); paint.ImageFilter?.Dispose();
paint.ColorFilter?.Dispose(); paint.ColorFilter?.Dispose();

View File

@ -40,15 +40,6 @@ public class ProfileConfigurationIcon : CorePropertyChanged, IStorageModel
private set => SetAndNotify(ref _iconName, value); private set => SetAndNotify(ref _iconName, value);
} }
/// <summary>
/// Gets the original file name of the icon (if applicable)
/// </summary>
public string? OriginalFileName
{
get => _originalFileName;
private set => SetAndNotify(ref _originalFileName, value);
}
/// <summary> /// <summary>
/// Gets or sets a boolean indicating whether or not this icon should be filled. /// Gets or sets a boolean indicating whether or not this icon should be filled.
/// </summary> /// </summary>
@ -69,7 +60,6 @@ public class ProfileConfigurationIcon : CorePropertyChanged, IStorageModel
_iconStream?.Dispose(); _iconStream?.Dispose();
IconName = iconName; IconName = iconName;
OriginalFileName = null;
IconType = ProfileConfigurationIconType.MaterialIcon; IconType = ProfileConfigurationIconType.MaterialIcon;
OnIconUpdated(); OnIconUpdated();
@ -78,21 +68,19 @@ public class ProfileConfigurationIcon : CorePropertyChanged, IStorageModel
/// <summary> /// <summary>
/// Updates the stream returned by <see cref="GetIconStream" /> to the provided stream /// Updates the stream returned by <see cref="GetIconStream" /> to the provided stream
/// </summary> /// </summary>
/// <param name="originalFileName">The original file name backing the stream, should include the extension</param>
/// <param name="stream">The stream to copy</param> /// <param name="stream">The stream to copy</param>
public void SetIconByStream(string originalFileName, Stream stream) public void SetIconByStream(Stream stream)
{ {
if (originalFileName == null) throw new ArgumentNullException(nameof(originalFileName));
if (stream == null) throw new ArgumentNullException(nameof(stream)); if (stream == null) throw new ArgumentNullException(nameof(stream));
_iconStream?.Dispose(); _iconStream?.Dispose();
_iconStream = new MemoryStream(); _iconStream = new MemoryStream();
stream.Seek(0, SeekOrigin.Begin); if (stream.CanSeek)
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(_iconStream); stream.CopyTo(_iconStream);
_iconStream.Seek(0, SeekOrigin.Begin); _iconStream.Seek(0, SeekOrigin.Begin);
IconName = null; IconName = null;
OriginalFileName = originalFileName;
IconType = ProfileConfigurationIconType.BitmapImage; IconType = ProfileConfigurationIconType.BitmapImage;
OnIconUpdated(); OnIconUpdated();
} }

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using System.Timers; using System.Timers;
using Artemis.Core.Modules; using Artemis.Core.Modules;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -31,8 +32,8 @@ internal class ModuleService : IModuleService
pluginManagementService.PluginFeatureEnabled += PluginManagementServiceOnPluginFeatureEnabled; pluginManagementService.PluginFeatureEnabled += PluginManagementServiceOnPluginFeatureEnabled;
pluginManagementService.PluginFeatureDisabled += PluginManagementServiceOnPluginFeatureDisabled; pluginManagementService.PluginFeatureDisabled += PluginManagementServiceOnPluginFeatureDisabled;
_modules = pluginManagementService.GetFeaturesOfType<Module>().ToList(); _modules = pluginManagementService.GetFeaturesOfType<Module>().ToList();
foreach (Module module in _modules)
ImportDefaultProfiles(module); Task.Run(ImportDefaultProfiles);
} }
protected virtual void OnModuleActivated(ModuleEventArgs e) protected virtual void OnModuleActivated(ModuleEventArgs e)
@ -96,7 +97,7 @@ internal class ModuleService : IModuleService
{ {
if (e.PluginFeature is Module module && !_modules.Contains(module)) if (e.PluginFeature is Module module && !_modules.Contains(module))
{ {
ImportDefaultProfiles(module); Task.Run(() => ImportDefaultProfiles(module));
_modules.Add(module); _modules.Add(module);
} }
} }
@ -111,24 +112,21 @@ internal class ModuleService : IModuleService
} }
} }
private void ImportDefaultProfiles(Module module) private async Task ImportDefaultProfiles()
{
foreach (Module module in _modules)
await ImportDefaultProfiles(module);
}
private async Task ImportDefaultProfiles(Module module)
{ {
try try
{ {
List<ProfileConfiguration> profileConfigurations = _profileService.ProfileCategories.SelectMany(c => c.ProfileConfigurations).ToList();
foreach ((DefaultCategoryName categoryName, string profilePath) in module.DefaultProfilePaths) foreach ((DefaultCategoryName categoryName, string profilePath) in module.DefaultProfilePaths)
{ {
ProfileConfigurationExportModel? profileConfigurationExportModel = ProfileCategory category = _profileService.ProfileCategories.FirstOrDefault(c => c.Name == categoryName.ToString()) ?? _profileService.CreateProfileCategory(categoryName.ToString());
JsonConvert.DeserializeObject<ProfileConfigurationExportModel>(File.ReadAllText(profilePath), IProfileService.ExportSettings); await using FileStream fileStream = File.OpenRead(profilePath);
if (profileConfigurationExportModel?.ProfileEntity == null) await _profileService.ImportProfile(fileStream, category, false, true, null);
throw new ArtemisCoreException($"Default profile at path {profilePath} contains no valid profile data");
if (profileConfigurations.Any(p => p.Entity.ProfileId == profileConfigurationExportModel.ProfileEntity.Id))
continue;
ProfileCategory category = _profileService.ProfileCategories.FirstOrDefault(c => c.Name == categoryName.ToString()) ??
_profileService.CreateProfileCategory(categoryName.ToString());
_profileService.ImportProfile(category, profileConfigurationExportModel, false, true, null);
} }
} }
catch (Exception e) catch (Exception e)

View File

@ -1,5 +1,7 @@
using System; using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using SkiaSharp; using SkiaSharp;
@ -110,17 +112,17 @@ public interface IProfileService : IArtemisService
void SaveProfile(Profile profile, bool includeChildren); void SaveProfile(Profile profile, bool includeChildren);
/// <summary> /// <summary>
/// Exports the profile described in the given <see cref="ProfileConfiguration" /> into an export model. /// Exports the profile described in the given <see cref="ProfileConfiguration" /> into a zip archive.
/// </summary> /// </summary>
/// <param name="profileConfiguration">The profile configuration of the profile to export.</param> /// <param name="profileConfiguration">The profile configuration of the profile to export.</param>
/// <returns>The resulting export model.</returns> /// <returns>The resulting zip archive.</returns>
ProfileConfigurationExportModel ExportProfile(ProfileConfiguration profileConfiguration); Task<Stream> ExportProfile(ProfileConfiguration profileConfiguration);
/// <summary> /// <summary>
/// Imports the provided base64 encoded GZIPed JSON as a profile configuration. /// Imports the provided base64 encoded GZIPed JSON as a profile configuration.
/// </summary> /// </summary>
/// <param name="archiveStream">The zip archive containing the profile to import.</param>
/// <param name="category">The <see cref="ProfileCategory" /> in which to import the profile.</param> /// <param name="category">The <see cref="ProfileCategory" /> in which to import the profile.</param>
/// <param name="exportModel">The model containing the profile to import.</param>
/// <param name="makeUnique">Whether or not to give the profile a new GUID, making it unique.</param> /// <param name="makeUnique">Whether or not to give the profile a new GUID, making it unique.</param>
/// <param name="markAsFreshImport"> /// <param name="markAsFreshImport">
/// Whether or not to mark the profile as a fresh import, causing it to be adapted until /// Whether or not to mark the profile as a fresh import, causing it to be adapted until
@ -128,8 +130,7 @@ public interface IProfileService : IArtemisService
/// </param> /// </param>
/// <param name="nameAffix">Text to add after the name of the profile (separated by a dash).</param> /// <param name="nameAffix">Text to add after the name of the profile (separated by a dash).</param>
/// <returns>The resulting profile configuration.</returns> /// <returns>The resulting profile configuration.</returns>
ProfileConfiguration ImportProfile(ProfileCategory category, ProfileConfigurationExportModel exportModel, bool makeUnique = true, bool markAsFreshImport = true, Task<ProfileConfiguration> ImportProfile(Stream archiveStream, ProfileCategory category, bool makeUnique, bool markAsFreshImport, string? nameAffix = "imported");
string? nameAffix = "imported");
/// <summary> /// <summary>
/// Adapts a given profile to the currently active devices. /// Adapts a given profile to the currently active devices.

View File

@ -2,7 +2,11 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.IO; using System.IO;
using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Artemis.Core.Modules; using Artemis.Core.Modules;
using Artemis.Storage.Entities.Profile; using Artemis.Storage.Entities.Profile;
using Artemis.Storage.Repositories.Interfaces; using Artemis.Storage.Repositories.Interfaces;
@ -311,7 +315,7 @@ internal class ProfileService : IProfileService
using Stream? stream = _profileCategoryRepository.GetProfileIconStream(profileConfiguration.Entity.FileIconId); using Stream? stream = _profileCategoryRepository.GetProfileIconStream(profileConfiguration.Entity.FileIconId);
if (stream != null) if (stream != null)
profileConfiguration.Icon.SetIconByStream(profileConfiguration.Entity.IconOriginalFileName, stream); profileConfiguration.Icon.SetIconByStream(stream);
} }
public void SaveProfileConfigurationIcon(ProfileConfiguration profileConfiguration) public void SaveProfileConfigurationIcon(ProfileConfiguration profileConfiguration)
@ -320,9 +324,8 @@ internal class ProfileService : IProfileService
return; return;
using Stream? stream = profileConfiguration.Icon.GetIconStream(); using Stream? stream = profileConfiguration.Icon.GetIconStream();
if (stream != null && profileConfiguration.Icon.OriginalFileName != null) if (stream != null)
{ {
profileConfiguration.Entity.IconOriginalFileName = profileConfiguration.Icon.OriginalFileName;
_profileCategoryRepository.SaveProfileIconStream(profileConfiguration.Entity, stream); _profileCategoryRepository.SaveProfileIconStream(profileConfiguration.Entity, stream);
} }
} }
@ -378,7 +381,7 @@ internal class ProfileService : IProfileService
OnProfileDeactivated(new ProfileConfigurationEventArgs(profileConfiguration)); OnProfileDeactivated(new ProfileConfigurationEventArgs(profileConfiguration));
} }
public void RequestDeactivation(ProfileConfiguration profileConfiguration) private void RequestDeactivation(ProfileConfiguration profileConfiguration)
{ {
if (profileConfiguration.IsBeingEdited) if (profileConfiguration.IsBeingEdited)
throw new ArtemisCoreException("Cannot disable a profile that is being edited, that's rude"); throw new ArtemisCoreException("Cannot disable a profile that is being edited, that's rude");
@ -480,34 +483,83 @@ internal class ProfileService : IProfileService
_profileRepository.Save(profile.ProfileEntity); _profileRepository.Save(profile.ProfileEntity);
} }
public ProfileConfigurationExportModel ExportProfile(ProfileConfiguration profileConfiguration) /// <inheritdoc />
public async Task<Stream> ExportProfile(ProfileConfiguration profileConfiguration)
{ {
// The profile may not be active and in that case lets activate it real quick ProfileEntity? profileEntity = _profileRepository.Get(profileConfiguration.Entity.ProfileId);
Profile profile = profileConfiguration.Profile ?? ActivateProfile(profileConfiguration); if (profileEntity == null)
throw new ArtemisCoreException("Could not locate profile entity");
return new ProfileConfigurationExportModel string configurationJson = JsonConvert.SerializeObject(profileConfiguration.Entity, IProfileService.ExportSettings);
string profileJson = JsonConvert.SerializeObject(profileEntity, IProfileService.ExportSettings);
MemoryStream archiveStream = new();
// Create a ZIP archive
using (ZipArchive archive = new(archiveStream, ZipArchiveMode.Create, true))
{ {
ProfileConfigurationEntity = profileConfiguration.Entity, ZipArchiveEntry configurationEntry = archive.CreateEntry("configuration.json");
ProfileEntity = profile.ProfileEntity, await using (Stream entryStream = configurationEntry.Open())
ProfileImage = profileConfiguration.Icon.GetIconStream() {
}; await entryStream.WriteAsync(Encoding.Default.GetBytes(configurationJson));
}
ZipArchiveEntry profileEntry = archive.CreateEntry("profile.json");
await using (Stream entryStream = profileEntry.Open())
{
await entryStream.WriteAsync(Encoding.Default.GetBytes(profileJson));
}
await using Stream? iconStream = profileConfiguration.Icon.GetIconStream();
if (iconStream != null)
{
ZipArchiveEntry iconEntry = archive.CreateEntry("icon.png");
await using Stream entryStream = iconEntry.Open();
await iconStream.CopyToAsync(entryStream);
}
}
archiveStream.Seek(0, SeekOrigin.Begin);
return archiveStream;
} }
public ProfileConfiguration ImportProfile(ProfileCategory category, ProfileConfigurationExportModel exportModel, /// <inheritdoc />
bool makeUnique, bool markAsFreshImport, string? nameAffix) public async Task<ProfileConfiguration> ImportProfile(Stream archiveStream, ProfileCategory category, bool makeUnique, bool markAsFreshImport, string? nameAffix)
{ {
if (exportModel.ProfileEntity == null) using ZipArchive archive = new(archiveStream, ZipArchiveMode.Read, true);
throw new ArtemisCoreException("Cannot import a profile without any data");
// Create a copy of the entity because we'll be using it from now on // There should be a configuration.json and profile.json
ProfileEntity profileEntity = JsonConvert.DeserializeObject<ProfileEntity>( ZipArchiveEntry? configurationEntry = archive.Entries.FirstOrDefault(e => e.Name.EndsWith("configuration.json"));
JsonConvert.SerializeObject(exportModel.ProfileEntity, IProfileService.ExportSettings), ZipArchiveEntry? profileEntry = archive.Entries.FirstOrDefault(e => e.Name.EndsWith("profile.json"));
IProfileService.ExportSettings ZipArchiveEntry? iconEntry = archive.Entries.FirstOrDefault(e => e.Name.EndsWith("icon.png"));
)!;
if (configurationEntry == null)
throw new ArtemisCoreException("Could not import profile, configuration.json missing");
if (profileEntry == null)
throw new ArtemisCoreException("Could not import profile, profile.json missing");
await using Stream configurationStream = configurationEntry.Open();
using StreamReader configurationReader = new(configurationStream);
ProfileConfigurationEntity? configurationEntity = JsonConvert.DeserializeObject<ProfileConfigurationEntity>(await configurationReader.ReadToEndAsync(), IProfileService.ExportSettings);
if (configurationEntity == null)
throw new ArtemisCoreException("Could not import profile, failed to deserialize configuration.json");
await using Stream profileStream = profileEntry.Open();
using StreamReader profileReader = new(profileStream);
ProfileEntity? profileEntity = JsonConvert.DeserializeObject<ProfileEntity>(await profileReader.ReadToEndAsync(), IProfileService.ExportSettings);
if (profileEntity == null)
throw new ArtemisCoreException("Could not import profile, failed to deserialize profile.json");
// Assign a new GUID to make sure it is unique in case of a previous import of the same content // Assign a new GUID to make sure it is unique in case of a previous import of the same content
if (makeUnique) if (makeUnique)
profileEntity.UpdateGuid(Guid.NewGuid()); profileEntity.UpdateGuid(Guid.NewGuid());
else
{
// If the profile already exists and this one is not to be made unique, return the existing profile
ProfileConfiguration? existing = ProfileCategories.SelectMany(c => c.ProfileConfigurations).FirstOrDefault(p => p.ProfileId == profileEntity.Id);
if (existing != null)
return existing;
}
if (nameAffix != null) if (nameAffix != null)
profileEntity.Name = $"{profileEntity.Name} - {nameAffix}"; profileEntity.Name = $"{profileEntity.Name} - {nameAffix}";
@ -519,25 +571,19 @@ internal class ProfileService : IProfileService
else else
throw new ArtemisCoreException($"Cannot import this profile without {nameof(makeUnique)} being true"); throw new ArtemisCoreException($"Cannot import this profile without {nameof(makeUnique)} being true");
ProfileConfiguration profileConfiguration; // A new GUID will be given on save
if (exportModel.ProfileConfigurationEntity != null) configurationEntity.FileIconId = Guid.Empty;
{ ProfileConfiguration profileConfiguration = new(category, configurationEntity);
ProfileConfigurationEntity profileConfigurationEntity = JsonConvert.DeserializeObject<ProfileConfigurationEntity>( if (nameAffix != null)
JsonConvert.SerializeObject(exportModel.ProfileConfigurationEntity, IProfileService.ExportSettings), IProfileService.ExportSettings profileConfiguration.Name = $"{profileConfiguration.Name} - {nameAffix}";
)!;
// A new GUID will be given on save
profileConfigurationEntity.FileIconId = Guid.Empty;
profileConfiguration = new ProfileConfiguration(category, profileConfigurationEntity);
if (nameAffix != null)
profileConfiguration.Name = $"{profileConfiguration.Name} - {nameAffix}";
}
else
{
profileConfiguration = new ProfileConfiguration(category, profileEntity.Name, "Import");
}
if (exportModel.ProfileImage != null && exportModel.ProfileConfigurationEntity?.IconOriginalFileName != null) // If an icon was provided, import that as well
profileConfiguration.Icon.SetIconByStream(exportModel.ProfileConfigurationEntity.IconOriginalFileName, exportModel.ProfileImage); if (iconEntry != null)
{
await using Stream iconStream = iconEntry.Open();
profileConfiguration.Icon.SetIconByStream(iconStream);
SaveProfileConfigurationIcon(profileConfiguration);
}
profileConfiguration.Entity.ProfileId = profileEntity.Id; profileConfiguration.Entity.ProfileId = profileEntity.Id;
category.AddProfileConfiguration(profileConfiguration, 0); category.AddProfileConfiguration(profileConfiguration, 0);

View File

@ -7,7 +7,6 @@ public class ProfileConfigurationEntity
{ {
public string Name { get; set; } public string Name { get; set; }
public string MaterialIcon { get; set; } public string MaterialIcon { get; set; }
public string IconOriginalFileName { get; set; }
public Guid FileIconId { get; set; } public Guid FileIconId { get; set; }
public int IconType { get; set; } public int IconType { get; set; }
public bool IconFill { get; set; } public bool IconFill { get; set; }

View File

@ -70,6 +70,6 @@ internal class ProfileCategoryRepository : IProfileCategoryRepository
if (stream == null && _profileIcons.Exists(profileConfigurationEntity.FileIconId)) if (stream == null && _profileIcons.Exists(profileConfigurationEntity.FileIconId))
_profileIcons.Delete(profileConfigurationEntity.FileIconId); _profileIcons.Delete(profileConfigurationEntity.FileIconId);
_profileIcons.Upload(profileConfigurationEntity.FileIconId, profileConfigurationEntity.IconOriginalFileName, stream); _profileIcons.Upload(profileConfigurationEntity.FileIconId, profileConfigurationEntity.FileIconId + ".png", stream);
} }
} }

View File

@ -14,7 +14,7 @@
<TrayIcon.Icons> <TrayIcon.Icons>
<TrayIcons> <TrayIcons>
<TrayIcon Icon="avares://Artemis.UI/Assets/Images/Logo/application.ico" ToolTipText="Artemis" Command="{CompiledBinding OpenScreen}" CommandParameter="Home"> <TrayIcon Icon="avares://Artemis.UI/Assets/Images/Logo/application.ico" ToolTipText="Artemis" Command="{CompiledBinding OpenScreen}">
<TrayIcon.Menu> <TrayIcon.Menu>
<NativeMenu> <NativeMenu>
<NativeMenuItem Header="Home" Command="{CompiledBinding OpenScreen}" CommandParameter="home" /> <NativeMenuItem Header="Home" Command="{CompiledBinding OpenScreen}" CommandParameter="home" />

View File

@ -14,7 +14,7 @@
<TrayIcon.Icons> <TrayIcon.Icons>
<TrayIcons> <TrayIcons>
<TrayIcon Icon="avares://Artemis.UI/Assets/Images/Logo/application.ico" ToolTipText="Artemis" Command="{CompiledBinding OpenScreen}" CommandParameter="Home"> <TrayIcon Icon="avares://Artemis.UI/Assets/Images/Logo/application.ico" ToolTipText="Artemis" Command="{CompiledBinding OpenScreen}">
<TrayIcon.Menu> <TrayIcon.Menu>
<NativeMenu> <NativeMenu>
<NativeMenuItem Header="Home" Command="{CompiledBinding OpenScreen}" CommandParameter="home" /> <NativeMenuItem Header="Home" Command="{CompiledBinding OpenScreen}" CommandParameter="home" />

View File

@ -59,4 +59,9 @@ public interface IRouter
/// <typeparam name="TScreen">The type of the root screen. It must be a class.</typeparam> /// <typeparam name="TScreen">The type of the root screen. It must be a class.</typeparam>
/// <typeparam name="TParam">The type of the parameters for the root screen. It must have a parameterless constructor.</typeparam> /// <typeparam name="TParam">The type of the parameters for the root screen. It must have a parameterless constructor.</typeparam>
void SetRoot<TScreen, TParam>(RoutableScreen<TScreen, TParam> root) where TScreen : class where TParam : new(); void SetRoot<TScreen, TParam>(RoutableScreen<TScreen, TParam> root) where TScreen : class where TParam : new();
/// <summary>
/// Clears the route used by the previous window, so that it is not restored when the main window opens.
/// </summary>
void ClearPreviousWindowRoute();
} }

View File

@ -35,11 +35,12 @@ internal class Navigation
public async Task Navigate(NavigationArguments args) public async Task Navigate(NavigationArguments args)
{ {
_logger.Information("Navigating to {Path}", _resolution.Path); if (_options.EnableLogging)
_logger.Information("Navigating to {Path}", _resolution.Path);
_cts = new CancellationTokenSource(); _cts = new CancellationTokenSource();
await NavigateResolution(_resolution, args, _root); await NavigateResolution(_resolution, args, _root);
if (!Cancelled) if (!Cancelled && _options.EnableLogging)
_logger.Information("Navigated to {Path}", _resolution.Path); _logger.Information("Navigated to {Path}", _resolution.Path);
} }
@ -48,7 +49,8 @@ internal class Navigation
if (Cancelled || Completed) if (Cancelled || Completed)
return; return;
_logger.Information("Cancelled navigation to {Path}", _resolution.Path); if (_options.EnableLogging)
_logger.Information("Cancelled navigation to {Path}", _resolution.Path);
_cts.Cancel(); _cts.Cancel();
} }

View File

@ -3,27 +3,34 @@ using System.Collections.Generic;
using System.Reactive.Subjects; using System.Reactive.Subjects;
using System.Threading.Tasks; using System.Threading.Tasks;
using Artemis.Core; using Artemis.Core;
using Artemis.UI.Shared.Services.MainWindow;
using Avalonia.Threading; using Avalonia.Threading;
using Serilog; using Serilog;
namespace Artemis.UI.Shared.Routing; namespace Artemis.UI.Shared.Routing;
internal class Router : CorePropertyChanged, IRouter internal class Router : CorePropertyChanged, IRouter, IDisposable
{ {
private readonly Stack<string> _backStack = new(); private readonly Stack<string> _backStack = new();
private readonly BehaviorSubject<string?> _currentRouteSubject; private readonly BehaviorSubject<string?> _currentRouteSubject;
private readonly Stack<string> _forwardStack = new(); private readonly Stack<string> _forwardStack = new();
private readonly Func<IRoutableScreen, RouteResolution, RouterNavigationOptions, Navigation> _getNavigation; private readonly Func<IRoutableScreen, RouteResolution, RouterNavigationOptions, Navigation> _getNavigation;
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IMainWindowService _mainWindowService;
private Navigation? _currentNavigation; private Navigation? _currentNavigation;
private IRoutableScreen? _root; private IRoutableScreen? _root;
private string? _previousWindowRoute;
public Router(ILogger logger, Func<IRoutableScreen, RouteResolution, RouterNavigationOptions, Navigation> getNavigation) public Router(ILogger logger, IMainWindowService mainWindowService, Func<IRoutableScreen, RouteResolution, RouterNavigationOptions, Navigation> getNavigation)
{ {
_logger = logger; _logger = logger;
_mainWindowService = mainWindowService;
_getNavigation = getNavigation; _getNavigation = getNavigation;
_currentRouteSubject = new BehaviorSubject<string?>(null); _currentRouteSubject = new BehaviorSubject<string?>(null);
mainWindowService.MainWindowOpened += MainWindowServiceOnMainWindowOpened;
mainWindowService.MainWindowClosed += MainWindowServiceOnMainWindowClosed;
} }
private RouteResolution Resolve(string path) private RouteResolution Resolve(string path)
@ -128,7 +135,7 @@ internal class Router : CorePropertyChanged, IRouter
await Navigate(path, new RouterNavigationOptions {AddToHistory = false}); await Navigate(path, new RouterNavigationOptions {AddToHistory = false});
if (previousPath != null) if (previousPath != null)
_forwardStack.Push(previousPath); _forwardStack.Push(previousPath);
return true; return true;
} }
@ -142,7 +149,7 @@ internal class Router : CorePropertyChanged, IRouter
await Navigate(path, new RouterNavigationOptions {AddToHistory = false}); await Navigate(path, new RouterNavigationOptions {AddToHistory = false});
if (previousPath != null) if (previousPath != null)
_backStack.Push(previousPath); _backStack.Push(previousPath);
return true; return true;
} }
@ -164,4 +171,29 @@ internal class Router : CorePropertyChanged, IRouter
{ {
_root = root; _root = root;
} }
/// <inheritdoc />
public void ClearPreviousWindowRoute()
{
_previousWindowRoute = null;
}
public void Dispose()
{
_currentRouteSubject.Dispose();
_mainWindowService.MainWindowOpened -= MainWindowServiceOnMainWindowOpened;
_mainWindowService.MainWindowClosed -= MainWindowServiceOnMainWindowClosed;
}
private void MainWindowServiceOnMainWindowOpened(object? sender, EventArgs e)
{
if (_previousWindowRoute != null && _currentRouteSubject.Value == "blank")
Dispatcher.UIThread.InvokeAsync(async () => await Navigate(_previousWindowRoute, new RouterNavigationOptions {AddToHistory = false, EnableLogging = false}));
}
private void MainWindowServiceOnMainWindowClosed(object? sender, EventArgs e)
{
_previousWindowRoute = _currentRouteSubject.Value;
Dispatcher.UIThread.InvokeAsync(async () => await Navigate("blank", new RouterNavigationOptions {AddToHistory = false, EnableLogging = false}));
}
} }

View File

@ -20,4 +20,10 @@ public class RouterNavigationOptions
/// </summary> /// </summary>
/// <example>If set to true, a route change from <c>page/subpage1/subpage2</c> to <c>page/subpage1</c> will be ignored.</example> /// <example>If set to true, a route change from <c>page/subpage1/subpage2</c> to <c>page/subpage1</c> will be ignored.</example>
public bool IgnoreOnPartialMatch { get; set; } = false; public bool IgnoreOnPartialMatch { get; set; } = false;
/// <summary>
/// Gets or sets a boolean value indicating whether logging should be enabled.
/// <remarks>Errors and warnings are always logged.</remarks>
/// </summary>
public bool EnableLogging { get; set; } = true;
} }

View File

@ -14,13 +14,13 @@
<TrayIcon.Icons> <TrayIcon.Icons>
<TrayIcons> <TrayIcons>
<TrayIcon Icon="avares://Artemis.UI/Assets/Images/Logo/application.ico" ToolTipText="Artemis" Command="{CompiledBinding OpenScreen}" CommandParameter="Home"> <TrayIcon Icon="avares://Artemis.UI/Assets/Images/Logo/application.ico" ToolTipText="Artemis" Command="{CompiledBinding OpenScreen}">
<TrayIcon.Menu> <TrayIcon.Menu>
<NativeMenu> <NativeMenu>
<NativeMenuItem Header="Home" Command="{CompiledBinding OpenScreen}" CommandParameter="home" /> <NativeMenuItem Header="Home" Command="{CompiledBinding OpenScreen}" CommandParameter="home" />
<!-- <NativeMenuItem Header="Workshop" Command="{CompiledBinding OpenScreen}" CommandParameter="workshop" /> --> <!-- <NativeMenuItem Header="Workshop" Command="{CompiledBinding OpenScreen}" CommandParameter="workshop" /> -->
<NativeMenuItem Header="Surface Editor" Command="{CompiledBinding OpenScreen}" CommandParameter="surface-editor" /> <NativeMenuItem Header="Surface Editor" Command="{CompiledBinding OpenScreen}" CommandParameter="surface-editor" />
<NativeMenuItem Header="Settings" Command="{CompiledBinding OpenScreen}" CommandParameter="settings/releases" /> <NativeMenuItem Header="Settings" Command="{CompiledBinding OpenScreen}" CommandParameter="settings" />
<NativeMenuItemSeparator /> <NativeMenuItemSeparator />
<NativeMenuItem Header="Debugger" Command="{CompiledBinding OpenDebugger}" /> <NativeMenuItem Header="Debugger" Command="{CompiledBinding OpenDebugger}" />
<NativeMenuItem Header="Exit" Command="{CompiledBinding Exit}" /> <NativeMenuItem Header="Exit" Command="{CompiledBinding Exit}" />

View File

@ -16,6 +16,7 @@ using Artemis.WebClient.Workshop.DryIoc;
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Logging;
using Avalonia.Styling; using Avalonia.Styling;
using DryIoc; using DryIoc;
using ReactiveUI; using ReactiveUI;
@ -32,9 +33,9 @@ public static class ArtemisBootstrapper
{ {
if (_application != null || _container != null) if (_application != null || _container != null)
throw new ArtemisUIException("UI already bootstrapped"); throw new ArtemisUIException("UI already bootstrapped");
Utilities.PrepareFirstLaunch(); Utilities.PrepareFirstLaunch();
application.RequestedThemeVariant = ThemeVariant.Dark; application.RequestedThemeVariant = ThemeVariant.Dark;
_application = application; _application = application;
_container = new Container(rules => rules _container = new Container(rules => rules
@ -51,6 +52,8 @@ public static class ArtemisBootstrapper
configureServices?.Invoke(_container); configureServices?.Invoke(_container);
_container.UseDryIocDependencyResolver(); _container.UseDryIocDependencyResolver();
Logger.Sink = _container.Resolve<SerilogAvaloniaSink>();
return _container; return _container;
} }

View File

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using Artemis.UI.Screens.Home; using Artemis.UI.Screens.Home;
using Artemis.UI.Screens.ProfileEditor; using Artemis.UI.Screens.ProfileEditor;
using Artemis.UI.Screens.Root;
using Artemis.UI.Screens.Settings; using Artemis.UI.Screens.Settings;
using Artemis.UI.Screens.Settings.Updating; using Artemis.UI.Screens.Settings.Updating;
using Artemis.UI.Screens.SurfaceEditor; using Artemis.UI.Screens.SurfaceEditor;
@ -15,6 +16,7 @@ public static class Routes
{ {
public static List<IRouterRegistration> ArtemisRoutes = new() public static List<IRouterRegistration> ArtemisRoutes = new()
{ {
new RouteRegistration<BlankViewModel>("blank"),
new RouteRegistration<HomeViewModel>("home"), new RouteRegistration<HomeViewModel>("home"),
#if DEBUG #if DEBUG
new RouteRegistration<WorkshopViewModel>("workshop") new RouteRegistration<WorkshopViewModel>("workshop")

View File

@ -70,7 +70,7 @@ public class MenuBarViewModel : ActivatableViewModelBase
ToggleSuspended = ReactiveCommand.Create(ExecuteToggleSuspended, this.WhenAnyValue(vm => vm.ProfileConfiguration).Select(c => c != null)); ToggleSuspended = ReactiveCommand.Create(ExecuteToggleSuspended, this.WhenAnyValue(vm => vm.ProfileConfiguration).Select(c => c != null));
DeleteProfile = ReactiveCommand.CreateFromTask(ExecuteDeleteProfile, this.WhenAnyValue(vm => vm.ProfileConfiguration).Select(c => c != null)); DeleteProfile = ReactiveCommand.CreateFromTask(ExecuteDeleteProfile, this.WhenAnyValue(vm => vm.ProfileConfiguration).Select(c => c != null));
ExportProfile = ReactiveCommand.CreateFromTask(ExecuteExportProfile, this.WhenAnyValue(vm => vm.ProfileConfiguration).Select(c => c != null)); ExportProfile = ReactiveCommand.CreateFromTask(ExecuteExportProfile, this.WhenAnyValue(vm => vm.ProfileConfiguration).Select(c => c != null));
DuplicateProfile = ReactiveCommand.Create(ExecuteDuplicateProfile, this.WhenAnyValue(vm => vm.ProfileConfiguration).Select(c => c != null)); DuplicateProfile = ReactiveCommand.CreateFromTask(ExecuteDuplicateProfile, this.WhenAnyValue(vm => vm.ProfileConfiguration).Select(c => c != null));
ToggleSuspendedEditing = ReactiveCommand.Create(ExecuteToggleSuspendedEditing); ToggleSuspendedEditing = ReactiveCommand.Create(ExecuteToggleSuspendedEditing);
OpenUri = ReactiveCommand.Create<string>(s => Process.Start(new ProcessStartInfo(s) {UseShellExecute = true, Verb = "open"})); OpenUri = ReactiveCommand.Create<string>(s => Process.Start(new ProcessStartInfo(s) {UseShellExecute = true, Verb = "open"}));
ToggleBooleanSetting = ReactiveCommand.Create<PluginSetting<bool>>(ExecuteToggleBooleanSetting); ToggleBooleanSetting = ReactiveCommand.Create<PluginSetting<bool>>(ExecuteToggleBooleanSetting);
@ -195,32 +195,31 @@ public class MenuBarViewModel : ActivatableViewModelBase
// Might not cover everything but then the dialog will complain and that's good enough // Might not cover everything but then the dialog will complain and that's good enough
string fileName = Path.GetInvalidFileNameChars().Aggregate(ProfileConfiguration.Name, (current, c) => current.Replace(c, '-')); string fileName = Path.GetInvalidFileNameChars().Aggregate(ProfileConfiguration.Name, (current, c) => current.Replace(c, '-'));
string? result = await _windowService.CreateSaveFileDialog() string? result = await _windowService.CreateSaveFileDialog()
.HavingFilter(f => f.WithExtension("json").WithName("Artemis profile")) .HavingFilter(f => f.WithExtension("zip").WithName("Artemis profile"))
.WithInitialFileName(fileName) .WithInitialFileName(fileName)
.ShowAsync(); .ShowAsync();
if (result == null) if (result == null)
return; return;
ProfileConfigurationExportModel export = _profileService.ExportProfile(ProfileConfiguration);
string json = JsonConvert.SerializeObject(export, IProfileService.ExportSettings);
try try
{ {
await File.WriteAllTextAsync(result, json); await using Stream stream = await _profileService.ExportProfile(ProfileConfiguration);
await using FileStream fileStream = File.OpenWrite(result);
await stream.CopyToAsync(fileStream);
} }
catch (Exception e) catch (Exception e)
{ {
_windowService.ShowExceptionDialog("Failed to export profile", e); _windowService.ShowExceptionDialog("Failed to export profile", e);
} }
} }
private void ExecuteDuplicateProfile() private async Task ExecuteDuplicateProfile()
{ {
if (ProfileConfiguration == null) if (ProfileConfiguration == null)
return; return;
await using Stream export = await _profileService.ExportProfile(ProfileConfiguration);
ProfileConfigurationExportModel export = _profileService.ExportProfile(ProfileConfiguration); await _profileService.ImportProfile(export, ProfileConfiguration.Category, true, false, "copy");
_profileService.ImportProfile(ProfileConfiguration.Category, export, true, false, "copy");
} }
private void ExecuteToggleSuspendedEditing() private void ExecuteToggleSuspendedEditing()

View File

@ -0,0 +1,7 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Artemis.UI.Screens.Root.BlankView">
</UserControl>

View File

@ -0,0 +1,13 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace Artemis.UI.Screens.Root;
public partial class BlankView : UserControl
{
public BlankView()
{
InitializeComponent();
}
}

View File

@ -0,0 +1,9 @@
using Artemis.UI.Shared;
namespace Artemis.UI.Screens.Root;
public class BlankViewModel : ViewModelBase, IMainScreenViewModel
{
/// <inheritdoc />
public ViewModelBase? TitleBarViewModel => null;
}

View File

@ -58,7 +58,7 @@ public class RootViewModel : RoutableScreen<IMainScreenViewModel>, IMainWindowPr
mainWindowService.ConfigureMainWindowProvider(this); mainWindowService.ConfigureMainWindowProvider(this);
DisplayAccordingToSettings(); DisplayAccordingToSettings();
OpenScreen = ReactiveCommand.Create<string>(ExecuteOpenScreen); OpenScreen = ReactiveCommand.Create<string?>(ExecuteOpenScreen);
OpenDebugger = ReactiveCommand.CreateFromTask(ExecuteOpenDebugger); OpenDebugger = ReactiveCommand.CreateFromTask(ExecuteOpenDebugger);
Exit = ReactiveCommand.CreateFromTask(ExecuteExit); Exit = ReactiveCommand.CreateFromTask(ExecuteExit);
this.WhenAnyValue(vm => vm.Screen).Subscribe(UpdateTitleBarViewModel); this.WhenAnyValue(vm => vm.Screen).Subscribe(UpdateTitleBarViewModel);
@ -72,11 +72,13 @@ public class RootViewModel : RoutableScreen<IMainScreenViewModel>, IMainWindowPr
registrationService.RegisterBuiltInDataModelDisplays(); registrationService.RegisterBuiltInDataModelDisplays();
registrationService.RegisterBuiltInDataModelInputs(); registrationService.RegisterBuiltInDataModelInputs();
registrationService.RegisterBuiltInPropertyEditors(); registrationService.RegisterBuiltInPropertyEditors();
_router.Navigate("home");
}); });
} }
public SidebarViewModel SidebarViewModel { get; } public SidebarViewModel SidebarViewModel { get; }
public ReactiveCommand<string, Unit> OpenScreen { get; } public ReactiveCommand<string?, Unit> OpenScreen { get; }
public ReactiveCommand<Unit, Unit> OpenDebugger { get; } public ReactiveCommand<Unit, Unit> OpenDebugger { get; }
public ReactiveCommand<Unit, Unit> Exit { get; } public ReactiveCommand<Unit, Unit> Exit { get; }
@ -133,8 +135,11 @@ public class RootViewModel : RoutableScreen<IMainScreenViewModel>, IMainWindowPr
#region Tray commands #region Tray commands
private void ExecuteOpenScreen(string path) private void ExecuteOpenScreen(string? path)
{ {
if (path != null)
_router.ClearPreviousWindowRoute();
// The window will open on the UI thread at some point, respond to that to select the chosen screen // The window will open on the UI thread at some point, respond to that to select the chosen screen
MainWindowOpened += OnEventHandler; MainWindowOpened += OnEventHandler;
OpenMainWindow(); OpenMainWindow();
@ -143,7 +148,8 @@ public class RootViewModel : RoutableScreen<IMainScreenViewModel>, IMainWindowPr
{ {
MainWindowOpened -= OnEventHandler; MainWindowOpened -= OnEventHandler;
// Avoid threading issues by running this on the UI thread // Avoid threading issues by running this on the UI thread
Dispatcher.UIThread.InvokeAsync(async () => await _router.Navigate(path)); if (path != null)
Dispatcher.UIThread.InvokeAsync(async () => await _router.Navigate(path));
} }
} }

View File

@ -231,7 +231,7 @@ public class ProfileConfigurationEditViewModel : DialogViewModelBase<ProfileConf
else if (_selectedIconPath != null) else if (_selectedIconPath != null)
{ {
await using FileStream fileStream = File.OpenRead(_selectedIconPath); await using FileStream fileStream = File.OpenRead(_selectedIconPath);
ProfileConfiguration.Icon.SetIconByStream(Path.GetFileName(_selectedIconPath), fileStream); ProfileConfiguration.Icon.SetIconByStream(fileStream);
} }
} }

View File

@ -1,11 +1,13 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.IO; using System.IO;
using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Reactive; using System.Reactive;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Artemis.Core; using Artemis.Core;
using Artemis.Core.Services; using Artemis.Core.Services;
@ -77,7 +79,13 @@ public class SidebarCategoryViewModel : ActivatableViewModelBase
Observable.FromEventPattern<ProfileConfigurationEventArgs>(x => profileCategory.ProfileConfigurationRemoved += x, x => profileCategory.ProfileConfigurationRemoved -= x) Observable.FromEventPattern<ProfileConfigurationEventArgs>(x => profileCategory.ProfileConfigurationRemoved += x, x => profileCategory.ProfileConfigurationRemoved -= x)
.Subscribe(e => profileConfigurations.RemoveMany(profileConfigurations.Items.Where(c => c == e.EventArgs.ProfileConfiguration))) .Subscribe(e => profileConfigurations.RemoveMany(profileConfigurations.Items.Where(c => c == e.EventArgs.ProfileConfiguration)))
.DisposeWith(d); .DisposeWith(d);
profileConfigurations.Edit(updater =>
{
updater.Clear();
updater.AddRange(profileCategory.ProfileConfigurations);
});
_isCollapsed = ProfileCategory.WhenAnyValue(vm => vm.IsCollapsed).ToProperty(this, vm => vm.IsCollapsed).DisposeWith(d); _isCollapsed = ProfileCategory.WhenAnyValue(vm => vm.IsCollapsed).ToProperty(this, vm => vm.IsCollapsed).DisposeWith(d);
_isSuspended = ProfileCategory.WhenAnyValue(vm => vm.IsSuspended).ToProperty(this, vm => vm.IsSuspended).DisposeWith(d); _isSuspended = ProfileCategory.WhenAnyValue(vm => vm.IsSuspended).ToProperty(this, vm => vm.IsSuspended).DisposeWith(d);
}); });
@ -155,32 +163,32 @@ public class SidebarCategoryViewModel : ActivatableViewModelBase
private async Task ExecuteImportProfile() private async Task ExecuteImportProfile()
{ {
string[]? result = await _windowService.CreateOpenFileDialog() string[]? result = await _windowService.CreateOpenFileDialog()
.HavingFilter(f => f.WithExtension("json").WithName("Artemis profile")) .HavingFilter(f => f.WithExtension("zip").WithExtension("json").WithName("Artemis profile"))
.ShowAsync(); .ShowAsync();
if (result == null) if (result == null)
return; return;
string json = await File.ReadAllTextAsync(result[0]);
ProfileConfigurationExportModel? profileConfigurationExportModel = null;
try try
{ {
profileConfigurationExportModel = JsonConvert.DeserializeObject<ProfileConfigurationExportModel>(json, IProfileService.ExportSettings); // Removing this at some point in the future
} if (result[0].EndsWith("json"))
catch (JsonException e) {
{ ProfileConfigurationExportModel? exportModel = JsonConvert.DeserializeObject<ProfileConfigurationExportModel>(await File.ReadAllTextAsync(result[0]), IProfileService.ExportSettings);
_windowService.ShowExceptionDialog("Import profile failed", e); if (exportModel == null)
} {
await _windowService.ShowConfirmContentDialog("Import profile", "Failed to import this profile, make sure it is a valid Artemis profile.", "Confirm", null);
return;
}
if (profileConfigurationExportModel == null) await using Stream convertedFileStream = await ConvertLegacyExport(exportModel);
{ await _profileService.ImportProfile(convertedFileStream, ProfileCategory, true, true);
await _windowService.ShowConfirmContentDialog("Import profile", "Failed to import this profile, make sure it is a valid Artemis profile.", "Confirm", null); }
return; else
} {
await using FileStream fileStream = File.OpenRead(result[0]);
try await _profileService.ImportProfile(fileStream, ProfileCategory, true, true);
{ }
_profileService.ImportProfile(ProfileCategory, profileConfigurationExportModel);
} }
catch (Exception e) catch (Exception e)
{ {
@ -234,4 +242,38 @@ public class SidebarCategoryViewModel : ActivatableViewModelBase
_profileService.SaveProfileCategory(categories[i]); _profileService.SaveProfileCategory(categories[i]);
} }
} }
private async Task<Stream> ConvertLegacyExport(ProfileConfigurationExportModel exportModel)
{
MemoryStream archiveStream = new();
string configurationJson = JsonConvert.SerializeObject(exportModel.ProfileConfigurationEntity, IProfileService.ExportSettings);
string profileJson = JsonConvert.SerializeObject(exportModel.ProfileEntity, IProfileService.ExportSettings);
// Create a ZIP archive
using (ZipArchive archive = new(archiveStream, ZipArchiveMode.Create, true))
{
ZipArchiveEntry configurationEntry = archive.CreateEntry("configuration.json");
await using (Stream entryStream = configurationEntry.Open())
{
await entryStream.WriteAsync(Encoding.Default.GetBytes(configurationJson));
}
ZipArchiveEntry profileEntry = archive.CreateEntry("profile.json");
await using (Stream entryStream = profileEntry.Open())
{
await entryStream.WriteAsync(Encoding.Default.GetBytes(profileJson));
}
if (exportModel.ProfileImage != null)
{
ZipArchiveEntry iconEntry = archive.CreateEntry("icon.png");
await using Stream entryStream = iconEntry.Open();
await exportModel.ProfileImage.CopyToAsync(entryStream);
}
}
archiveStream.Seek(0, SeekOrigin.Begin);
return archiveStream;
}
} }

View File

@ -10,8 +10,6 @@ using Artemis.Core.Services;
using Artemis.UI.Shared; using Artemis.UI.Shared;
using Artemis.UI.Shared.Routing; using Artemis.UI.Shared.Routing;
using Artemis.UI.Shared.Services; using Artemis.UI.Shared.Services;
using Artemis.UI.Shared.Services.ProfileEditor;
using Newtonsoft.Json;
using ReactiveUI; using ReactiveUI;
namespace Artemis.UI.Screens.Sidebar; namespace Artemis.UI.Screens.Sidebar;
@ -36,7 +34,7 @@ public class SidebarProfileConfigurationViewModel : ActivatableViewModelBase
SuspendAll = ReactiveCommand.Create<string>(ExecuteSuspendAll); SuspendAll = ReactiveCommand.Create<string>(ExecuteSuspendAll);
DeleteProfile = ReactiveCommand.CreateFromTask(ExecuteDeleteProfile); DeleteProfile = ReactiveCommand.CreateFromTask(ExecuteDeleteProfile);
ExportProfile = ReactiveCommand.CreateFromTask(ExecuteExportProfile); ExportProfile = ReactiveCommand.CreateFromTask(ExecuteExportProfile);
DuplicateProfile = ReactiveCommand.Create(ExecuteDuplicateProfile); DuplicateProfile = ReactiveCommand.CreateFromTask(ExecuteDuplicateProfile);
this.WhenActivated(d => _isDisabled = ProfileConfiguration.WhenAnyValue(c => c.Profile) this.WhenActivated(d => _isDisabled = ProfileConfiguration.WhenAnyValue(c => c.Profile)
.Select(p => p == null) .Select(p => p == null)
@ -116,18 +114,18 @@ public class SidebarProfileConfigurationViewModel : ActivatableViewModelBase
// Might not cover everything but then the dialog will complain and that's good enough // Might not cover everything but then the dialog will complain and that's good enough
string fileName = Path.GetInvalidFileNameChars().Aggregate(ProfileConfiguration.Name, (current, c) => current.Replace(c, '-')); string fileName = Path.GetInvalidFileNameChars().Aggregate(ProfileConfiguration.Name, (current, c) => current.Replace(c, '-'));
string? result = await _windowService.CreateSaveFileDialog() string? result = await _windowService.CreateSaveFileDialog()
.HavingFilter(f => f.WithExtension("json").WithName("Artemis profile")) .HavingFilter(f => f.WithExtension("zip").WithName("Artemis profile"))
.WithInitialFileName(fileName) .WithInitialFileName(fileName)
.ShowAsync(); .ShowAsync();
if (result == null) if (result == null)
return; return;
ProfileConfigurationExportModel export = _profileService.ExportProfile(ProfileConfiguration);
string json = JsonConvert.SerializeObject(export, IProfileService.ExportSettings);
try try
{ {
await File.WriteAllTextAsync(result, json); await using Stream stream = await _profileService.ExportProfile(ProfileConfiguration);
await using FileStream fileStream = File.OpenWrite(result);
await stream.CopyToAsync(fileStream);
} }
catch (Exception e) catch (Exception e)
{ {
@ -135,10 +133,10 @@ public class SidebarProfileConfigurationViewModel : ActivatableViewModelBase
} }
} }
private void ExecuteDuplicateProfile() private async Task ExecuteDuplicateProfile()
{ {
ProfileConfigurationExportModel export = _profileService.ExportProfile(ProfileConfiguration); await using Stream export = await _profileService.ExportProfile(ProfileConfiguration);
_profileService.ImportProfile(ProfileConfiguration.Category, export, true, false, "copy"); await _profileService.ImportProfile(export, ProfileConfiguration.Category, true, false, "copy");
} }
public bool Matches(string s) public bool Matches(string s)

View File

@ -24,7 +24,8 @@
Margin="10 2" Margin="10 2"
ItemsSource="{CompiledBinding SidebarScreen.Screens}" ItemsSource="{CompiledBinding SidebarScreen.Screens}"
SelectedItem="{CompiledBinding SelectedScreen}" SelectedItem="{CompiledBinding SelectedScreen}"
ItemContainerTheme="{StaticResource MenuTreeViewItem}"> ItemContainerTheme="{StaticResource MenuTreeViewItem}"
PointerReleased="InputElement_OnPointerReleased">
<TreeView.Styles> <TreeView.Styles>
<Style Selector="TreeViewItem"> <Style Selector="TreeViewItem">
<Setter Property="IsExpanded" Value="{CompiledBinding IsExpanded, Mode=TwoWay, DataType=sidebar:SidebarScreenViewModel}" /> <Setter Property="IsExpanded" Value="{CompiledBinding IsExpanded, Mode=TwoWay, DataType=sidebar:SidebarScreenViewModel}" />

View File

@ -1,4 +1,7 @@
using Avalonia.Markup.Xaml; using System;
using Avalonia;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI; using Avalonia.ReactiveUI;
namespace Artemis.UI.Screens.Sidebar; namespace Artemis.UI.Screens.Sidebar;
@ -10,4 +13,9 @@ public partial class SidebarView : ReactiveUserControl<SidebarViewModel>
InitializeComponent(); InitializeComponent();
} }
private void InputElement_OnPointerReleased(object? sender, PointerReleasedEventArgs e)
{
if (e.Source is IDataContextProvider dataContextProvider && dataContextProvider.DataContext is SidebarScreenViewModel sidebarScreenViewModel)
ViewModel?.NavigateToScreen(sidebarScreenViewModel);
}
} }

View File

@ -1,7 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive; using System.Reactive;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Reactive.Linq; using System.Reactive.Linq;
@ -52,7 +50,6 @@ public class SidebarViewModel : ActivatableViewModelBase
}); });
AddCategory = ReactiveCommand.CreateFromTask(ExecuteAddCategory); AddCategory = ReactiveCommand.CreateFromTask(ExecuteAddCategory);
this.WhenAnyValue(vm => vm.SelectedScreen).WhereNotNull().Subscribe(NavigateToScreen);
this.WhenAnyValue(vm => vm.SelectedScreen).WhereNotNull().Subscribe(s => SidebarScreen.ExpandIfRequired(s)); this.WhenAnyValue(vm => vm.SelectedScreen).WhereNotNull().Subscribe(s => SidebarScreen.ExpandIfRequired(s));
SourceList<ProfileCategory> profileCategories = new(); SourceList<ProfileCategory> profileCategories = new();
@ -89,7 +86,6 @@ public class SidebarViewModel : ActivatableViewModelBase
SidebarCategories = categoryViewModels; SidebarCategories = categoryViewModels;
}); });
SelectedScreen = SidebarScreen.Screens.First();
} }
public SidebarScreenViewModel SidebarScreen { get; } public SidebarScreenViewModel SidebarScreen { get; }
@ -119,7 +115,7 @@ public class SidebarViewModel : ActivatableViewModelBase
.ShowAsync(); .ShowAsync();
} }
private void NavigateToScreen(SidebarScreenViewModel sidebarScreenViewModel) public void NavigateToScreen(SidebarScreenViewModel sidebarScreenViewModel)
{ {
if (_updating) if (_updating)
return; return;

View File

@ -0,0 +1,56 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using Avalonia.Logging;
using Serilog;
using AvaloniaLogLevel = Avalonia.Logging.LogEventLevel;
using SerilogLogLevel = Serilog.Events.LogEventLevel;
namespace Artemis.UI;
[SuppressMessage("ReSharper", "TemplateIsNotCompileTimeConstantProblem")]
public class SerilogAvaloniaSink : ILogSink
{
private readonly ILogger _logger;
public SerilogAvaloniaSink(ILogger logger)
{
_logger = logger;
}
/// <inheritdoc />
public bool IsEnabled(AvaloniaLogLevel level, string area)
{
SerilogLogLevel logLevel = GetSerilogLogLevel(level, area);
// Except with binding errors, ignore anything that is information or lower
return (area == "Binding" || logLevel > SerilogLogLevel.Information) && _logger.IsEnabled(logLevel);
}
/// <inheritdoc />
public void Log(AvaloniaLogLevel level, string area, object? source, string messageTemplate)
{
SerilogLogLevel logLevel = GetSerilogLogLevel(level, area);
ILogger logger = source != null ? _logger.ForContext(source.GetType()) : _logger;
logger.Write(logLevel, messageTemplate);
}
/// <inheritdoc />
public void Log(AvaloniaLogLevel level, string area, object? source, string messageTemplate, params object?[] propertyValues)
{
SerilogLogLevel logLevel = GetSerilogLogLevel(level, area);
ILogger logger = source != null ? _logger.ForContext(source.GetType()) : _logger;
logger.Write(logLevel, messageTemplate, propertyValues);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static SerilogLogLevel GetSerilogLogLevel(AvaloniaLogLevel level, string area)
{
// Avalonia considers binding errors warnings, we'll treat them Verbose as to not spam people's logs
// And yes we should fix them instead but we can't always: https://github.com/AvaloniaUI/Avalonia/issues/5762
if (area == "Binding")
return SerilogLogLevel.Verbose;
return (SerilogLogLevel) level;
}
}

View File

@ -2,6 +2,7 @@ using System;
using System.Diagnostics; using System.Diagnostics;
using Artemis.Core; using Artemis.Core;
using Artemis.UI.Exceptions; using Artemis.UI.Exceptions;
using Artemis.UI.Screens.Root;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Templates; using Avalonia.Controls.Templates;
using Avalonia.ReactiveUI; using Avalonia.ReactiveUI;
@ -11,9 +12,13 @@ namespace Artemis.UI;
public class ViewLocator : IDataTemplate public class ViewLocator : IDataTemplate
{ {
public Control Build(object data) public Control Build(object? data)
{ {
if (data == null)
return new TextBlock {Text = "No data provided"};
Type dataType = data.GetType(); Type dataType = data.GetType();
string name = dataType.FullName!.Split('`')[0].Replace("ViewModel", "View"); string name = dataType.FullName!.Split('`')[0].Replace("ViewModel", "View");
Type? type = dataType.Assembly.GetType(name); Type? type = dataType.Assembly.GetType(name);
@ -27,7 +32,7 @@ public class ViewLocator : IDataTemplate
return new TextBlock {Text = "Not Found: " + name}; return new TextBlock {Text = "Not Found: " + name};
} }
public bool Match(object data) public bool Match(object? data)
{ {
return data is ReactiveObject; return data is ReactiveObject;
} }

View File

@ -243,6 +243,7 @@
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAlwaysTreatStructAsNotReorderableMigration/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAlwaysTreatStructAsNotReorderableMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=activatable/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=activatable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Avalonia/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Hotkey/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=Hotkey/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=luma/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=luma/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=pixmap/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/UserDictionary/Words/=pixmap/@EntryIndexedValue">True</s:Boolean>