Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📃 Remove LayoutManagerFactory #252

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions FinalEngine.Editor.Desktop/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace FinalEngine.Editor.Desktop;
using FinalEngine.Editor.Common.Services.Environment;
using FinalEngine.Editor.Common.Services.Scenes;
using FinalEngine.Editor.Desktop.Services.Actions;
using FinalEngine.Editor.Desktop.Services.Factories.Layout;
using FinalEngine.Editor.Desktop.Services.Layout;
using FinalEngine.Editor.Desktop.Views;
using FinalEngine.Editor.Desktop.Views.Dialogs.Layout;
using FinalEngine.Editor.ViewModels;
Expand All @@ -26,7 +26,7 @@ namespace FinalEngine.Editor.Desktop;
using FinalEngine.Editor.ViewModels.Docking.Tools.Scenes;
using FinalEngine.Editor.ViewModels.Interactions;
using FinalEngine.Editor.ViewModels.Services.Actions;
using FinalEngine.Editor.ViewModels.Services.Factories.Layout;
using FinalEngine.Editor.ViewModels.Services.Layout;
using FinalEngine.Rendering;
using FinalEngine.Rendering.OpenGL;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -111,7 +111,7 @@ private static void ConfigureServices(HostBuilderContext context, IServiceCollec

services.AddSingleton<IFileSystem, FileSystem>();

services.AddTransient<Scene>();
services.AddTransient<IScene, Scene>();

services.AddSingleton<IApplicationContext, ApplicationContext>();
services.AddSingleton<IEnvironmentContext, EnvironmentContext>();
Expand All @@ -132,8 +132,8 @@ private static void ConfigureServices(HostBuilderContext context, IServiceCollec
services.AddTransient<IViewable<ISaveWindowLayoutViewModel>, SaveWindowLayoutView>();
services.AddTransient<IViewable<IManageWindowLayoutsViewModel>, ManageWindowLayoutsView>();

services.AddSingleton<ILayoutManagerFactory, LayoutManagerFactory>();
services.AddSingleton<IUserActionRequester, UserActionRequester>();
services.AddSingleton<ILayoutManager, LayoutManager>();

services.AddSingleton<IViewPresenter>(x =>
{
Expand Down

This file was deleted.

41 changes: 20 additions & 21 deletions FinalEngine.Editor.Desktop/Services/Layout/LayoutManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ namespace FinalEngine.Editor.Desktop.Services.Layout;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Windows;
using AvalonDock;
using AvalonDock.Layout.Serialization;
using FinalEngine.Editor.Common.Services.Application;
using FinalEngine.Editor.Desktop.Exceptions.Layout;
using FinalEngine.Editor.Desktop.Views.Docking;
using FinalEngine.Editor.ViewModels.Docking.Tools;
using FinalEngine.Editor.ViewModels.Services.Layout;
using MahApps.Metro.Controls;
using Microsoft.Extensions.Logging;

/// <summary>
Expand All @@ -24,14 +27,17 @@ namespace FinalEngine.Editor.Desktop.Services.Layout;
public sealed class LayoutManager : ILayoutManager
{
/// <summary>
/// The application, used to resolve a directory where the window layouts are saved.
/// The cached instanced to the docking manager.
/// </summary>
private readonly IApplicationContext application;
/// <remarks>
/// The instance is cached to avoid a <see cref="NullReferenceException"/> when unloading the <see cref="DockView"/>.
/// </remarks>
private static readonly DockingManager Instance = Application.Current.MainWindow.FindChild<DockView>().DockManager;

/// <summary>
/// The docking manager, used to manage the current window layout.
/// The application, used to resolve a directory where the window layouts are saved.
/// </summary>
private readonly DockingManager dockManager;
private readonly IApplicationContext application;

/// <summary>
/// The file system, used to create the <see cref="LayoutDirectory"/> if one does not already exist.
Expand All @@ -43,41 +49,29 @@ public sealed class LayoutManager : ILayoutManager
/// </summary>
private readonly ILogger<LayoutManager> logger;

/// <summary>
/// The layout serializer, used to serialize and deserialize the window layouts where required.
/// </summary>
private readonly XmlLayoutSerializer serializer;

/// <summary>
/// Initializes a new instance of the <see cref="LayoutManager"/> class.
/// </summary>
/// <param name="logger">
/// The logger.
/// </param>
/// <param name="dockManager">
/// The dock manager, used to manage the current window layout.
/// </param>
/// <param name="application">
/// The application context, used to resolve a directory where window layouts are stored.
/// </param>
/// <param name="fileSystem">
/// The file system, used to create the <see cref="LayoutDirectory"/> if one does not already exist.
/// </param>
/// <exception cref="ArgumentNullException">
/// The specified <paramref name="dockManager"/>, <paramref name="application"/> or <paramref name="fileSystem"/> parameter cannot be null.
/// The specified <paramref name="application"/> or <paramref name="fileSystem"/> parameter cannot be null.
/// </exception>
public LayoutManager(
ILogger<LayoutManager> logger,
DockingManager dockManager,
IApplicationContext application,
IFileSystem fileSystem)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
this.dockManager = dockManager ?? throw new ArgumentNullException(nameof(dockManager));
this.application = application ?? throw new ArgumentNullException(nameof(application));
this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));

this.serializer = new XmlLayoutSerializer(this.dockManager);
}

/// <summary>
Expand Down Expand Up @@ -165,7 +159,8 @@ public void LoadLayout(string layoutName)

this.logger.LogInformation($"Loading window layout: '{layoutName}'.");

this.serializer.Deserialize(this.GetLayoutPath(layoutName));
var serializer = new XmlLayoutSerializer(Instance);
serializer.Deserialize(this.GetLayoutPath(layoutName));

this.logger.LogInformation("Layout loaded.");
}
Expand All @@ -188,7 +183,10 @@ public void ResetLayout()
const string defaultLayoutPath = "Resources\\Layouts\\default.config";

this.logger.LogInformation("Resting window layout to default layout...");
this.serializer.Deserialize(defaultLayoutPath);

var serializer = new XmlLayoutSerializer(Instance);
serializer.Deserialize(defaultLayoutPath);

this.logger.LogInformation("Layout reset.");
}

Expand All @@ -205,7 +203,8 @@ public void SaveLayout(string layoutName)

this.logger.LogInformation($"Saving window layout: '{layoutName}'...");

this.serializer.Serialize(this.GetLayoutPath(layoutName));
var serializer = new XmlLayoutSerializer(Instance);
serializer.Serialize(this.GetLayoutPath(layoutName));

this.logger.LogInformation("Layout saved.");
}
Expand All @@ -224,7 +223,7 @@ public void ToggleToolWindow(string contentID)
throw new ArgumentException($"'{nameof(contentID)}' cannot be null or whitespace.", nameof(contentID));
}

var tool = this.dockManager.AnchorablesSource.Cast<IToolViewModel>().FirstOrDefault(x =>
var tool = Instance.AnchorablesSource.Cast<IToolViewModel>().FirstOrDefault(x =>
{
return x.ContentID == contentID;
}) ?? throw new ToolPaneNotFoundException(contentID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace FinalEngine.Editor.ViewModels.Dialogs.Layout;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FinalEngine.Editor.ViewModels.Services.Actions;
using FinalEngine.Editor.ViewModels.Services.Factories.Layout;
using FinalEngine.Editor.ViewModels.Services.Layout;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -63,26 +62,21 @@ public sealed class ManageWindowLayoutsViewModel : ObservableObject, IManageWind
/// </param>
/// <param name="userActionRequester">
/// The user action requester, used to query the user and determine whether they wish to delete the currently selected window layout.</param>
/// <param name="layoutManagerFactory">
/// The layout manager factory, used to apply and delete window layouts.
/// <param name="layoutManager">
/// The layout manager, used to apply and delete window layouts.
/// </param>
/// <exception cref="ArgumentNullException">
/// The specified <paramref name="userActionRequester"/> or <paramref name="layoutManagerFactory"/> parameter cannot be null.
/// The specified <paramref name="logger"/>, <paramref name="userActionRequester"/> or <paramref name="layoutManager"/> parameter cannot be null.
/// </exception>
public ManageWindowLayoutsViewModel(
ILogger<ManageWindowLayoutsViewModel> logger,
IUserActionRequester userActionRequester,
ILayoutManagerFactory layoutManagerFactory)
ILayoutManager layoutManager)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
this.userActionRequester = userActionRequester ?? throw new ArgumentNullException(nameof(userActionRequester));
this.layoutManager = layoutManager ?? throw new ArgumentNullException(nameof(layoutManager));

if (layoutManagerFactory == null)
{
throw new ArgumentNullException(nameof(layoutManagerFactory));
}

this.layoutManager = layoutManagerFactory.CreateManager();
this.LayoutNames = this.layoutManager.LoadLayoutNames();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace FinalEngine.Editor.ViewModels.Dialogs.Layout;
using CommunityToolkit.Mvvm.Input;
using FinalEngine.Editor.ViewModels.Interactions;
using FinalEngine.Editor.ViewModels.Services.Actions;
using FinalEngine.Editor.ViewModels.Services.Factories.Layout;
using FinalEngine.Editor.ViewModels.Services.Layout;
using FinalEngine.Editor.ViewModels.Validation;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -52,29 +51,24 @@ public sealed class SaveWindowLayoutViewModel : ObservableValidator, ISaveWindow
/// <param name="logger">
/// The logger.
/// </param>
/// <param name="layoutManagerFactory">
/// The layout manager factory, used to create an <see cref="ILayoutManager"/> to handle saving the current window layout.
/// <param name="layoutManager">
/// The layout manager, used to handle saving the current window layout.
/// </param>
/// <param name="userActionRequester">
/// The user action requester, used to query the user and determine whether they wish to overwrite a layout that already exists.
/// </param>
/// <exception cref="ArgumentNullException">
/// The specified <paramref name="layoutManagerFactory"/> or <paramref name="userActionRequester"/> parameter cannot be null.
/// The specified <paramref name="layoutManager"/> or <paramref name="userActionRequester"/> parameter cannot be null.
/// </exception>
public SaveWindowLayoutViewModel(
ILogger<SaveWindowLayoutViewModel> logger,
ILayoutManagerFactory layoutManagerFactory,
ILayoutManager layoutManager,
IUserActionRequester userActionRequester)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
this.userActionRequester = userActionRequester ?? throw new ArgumentNullException(nameof(userActionRequester));
this.layoutManager = layoutManager ?? throw new ArgumentNullException(nameof(layoutManager));

if (layoutManagerFactory == null)
{
throw new ArgumentNullException(nameof(layoutManagerFactory));
}

this.layoutManager = layoutManagerFactory.CreateManager();
this.LayoutName = "Layout Name";
}

Expand Down
Loading
Loading