Merge branch 'develop-mediatr' into develop

This commit is contained in:
Anton Kasyanov
2018-02-21 21:40:03 +02:00
69 changed files with 844 additions and 466 deletions

View File

@@ -17,6 +17,11 @@ namespace EveOPreview
public void SetupExceptionHandlers()
{
if (System.Diagnostics.Debugger.IsAttached)
{
return;
}
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += delegate (Object sender, ThreadExceptionEventArgs e)
{

View File

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace EveOPreview
{
@@ -8,11 +10,17 @@ namespace EveOPreview
/// </summary>
public interface IIocContainer
{
void Register<TService, TImplementation>() where TImplementation : TService;
void Register<TService, TImplementation>()
where TImplementation : TService;
void Register(Type serviceType, Assembly container);
void Register<TService>();
void RegisterInstance<T>(T instance);
TService Resolve<TService>();
bool IsRegistered<TService>();
void Register<TService>(Expression<Func<TService>> factory);
void Register<TService, TArgument>(Expression<Func<TArgument, TService>> factory);
void RegisterInstance<TService>(TService instance);
TService Resolve<TService>();
IEnumerable<TService> ResolveAll<TService>();
object Resolve(Type serviceType);
IEnumerable<object> ResolveAll(Type serviceType);
bool IsRegistered<TService>();
}
}

View File

@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using LightInject;
namespace EveOPreview
@@ -19,9 +21,38 @@ namespace EveOPreview
return this._container.CanGetInstance(typeof(TService), "");
}
public void Register(Type serviceType, Assembly container)
{
if (!serviceType.IsInterface)
{
this._container.Register(serviceType, new PerContainerLifetime());
return;
}
if (serviceType.IsInterface && serviceType.IsGenericType)
{
this._container.RegisterAssembly(container, (st, it) => st.IsConstructedGenericType && st.GetGenericTypeDefinition() == serviceType);
}
else
{
foreach (TypeInfo implementationType in container.DefinedTypes)
{
if (!implementationType.IsClass || implementationType.IsAbstract)
{
continue;
}
if (serviceType.IsAssignableFrom(implementationType))
{
this._container.Register(serviceType, implementationType, new PerContainerLifetime());
}
}
}
}
public void Register<TService>()
{
this._container.Register<TService>();
this.Register(typeof(TService), typeof(TService).Assembly);
}
public void Register<TService, TImplementation>()
@@ -30,12 +61,17 @@ namespace EveOPreview
this._container.Register<TService, TImplementation>();
}
public void Register<TService>(Expression<Func<TService>> factory)
{
this._container.Register(f => factory);
}
public void Register<TService, TArgument>(Expression<Func<TArgument, TService>> factory)
{
this._container.Register(f => factory);
}
public void RegisterInstance<T>(T instance)
public void RegisterInstance<TService>(TService instance)
{
this._container.RegisterInstance(instance);
}
@@ -44,5 +80,20 @@ namespace EveOPreview
{
return this._container.GetInstance<TService>();
}
public IEnumerable<TService> ResolveAll<TService>()
{
return this._container.GetAllInstances<TService>();
}
public object Resolve(Type serviceType)
{
return this._container.GetInstance(serviceType);
}
public IEnumerable<object> ResolveAll(Type serviceType)
{
return this._container.GetAllInstances(serviceType);
}
}
}

View File

@@ -8,12 +8,12 @@ namespace EveOPreview.Configuration
private const string ConfigurationFileName = "EVE-O Preview.json";
private readonly IAppConfig _appConfig;
private readonly IThumbnailConfig _thumbnailConfig;
private readonly IThumbnailConfiguration _thumbnailConfiguration;
public ConfigurationStorage(IAppConfig appConfig, IThumbnailConfig thumbnailConfig)
public ConfigurationStorage(IAppConfig appConfig, IThumbnailConfiguration thumbnailConfiguration)
{
this._appConfig = appConfig;
this._thumbnailConfig = thumbnailConfig;
this._thumbnailConfiguration = thumbnailConfiguration;
}
public void Load()
@@ -27,15 +27,15 @@ namespace EveOPreview.Configuration
string rawData = File.ReadAllText(filename);
JsonConvert.PopulateObject(rawData, this._thumbnailConfig);
JsonConvert.PopulateObject(rawData, this._thumbnailConfiguration);
// Validate data after loading it
this._thumbnailConfig.ApplyRestrictions();
this._thumbnailConfiguration.ApplyRestrictions();
}
public void Save()
{
string rawData = JsonConvert.SerializeObject(this._thumbnailConfig, Formatting.Indented);
string rawData = JsonConvert.SerializeObject(this._thumbnailConfiguration, Formatting.Indented);
File.WriteAllText(this.GetConfigFileName(), rawData);
}

View File

@@ -3,10 +3,7 @@ using System.Windows.Forms;
namespace EveOPreview.Configuration
{
/// <summary>
/// Thumbnails Manager configuration
/// </summary>
public interface IThumbnailConfig
public interface IThumbnailConfiguration
{
bool MinimizeToTray { get; set; }
int ThumbnailRefreshPeriod { get; set; }

View File

@@ -5,9 +5,9 @@ using Newtonsoft.Json;
namespace EveOPreview.Configuration
{
class ThumbnailConfig : IThumbnailConfig
class ThumbnailConfiguration : IThumbnailConfiguration
{
public ThumbnailConfig()
public ThumbnailConfiguration()
{
this.MinimizeToTray = false;
this.ThumbnailRefreshPeriod = 500;
@@ -173,12 +173,12 @@ namespace EveOPreview.Configuration
/// </summary>
public void ApplyRestrictions()
{
this.ThumbnailRefreshPeriod = ThumbnailConfig.ApplyRestrictions(this.ThumbnailRefreshPeriod, 300, 1000);
this.ThumbnailSize = new Size(ThumbnailConfig.ApplyRestrictions(this.ThumbnailSize.Width, this.ThumbnailMinimumSize.Width, this.ThumbnailMaximumSize.Width),
ThumbnailConfig.ApplyRestrictions(this.ThumbnailSize.Height, this.ThumbnailMinimumSize.Height, this.ThumbnailMaximumSize.Height));
this.ThumbnailOpacity = ThumbnailConfig.ApplyRestrictions((int)(this.ThumbnailOpacity * 100.00), 20, 100) / 100.00;
this.ThumbnailZoomFactor = ThumbnailConfig.ApplyRestrictions(this.ThumbnailZoomFactor, 2, 10);
this.ActiveClientHighlightThickness = ThumbnailConfig.ApplyRestrictions(this.ActiveClientHighlightThickness, 1, 6);
this.ThumbnailRefreshPeriod = ThumbnailConfiguration.ApplyRestrictions(this.ThumbnailRefreshPeriod, 300, 1000);
this.ThumbnailSize = new Size(ThumbnailConfiguration.ApplyRestrictions(this.ThumbnailSize.Width, this.ThumbnailMinimumSize.Width, this.ThumbnailMaximumSize.Width),
ThumbnailConfiguration.ApplyRestrictions(this.ThumbnailSize.Height, this.ThumbnailMinimumSize.Height, this.ThumbnailMaximumSize.Height));
this.ThumbnailOpacity = ThumbnailConfiguration.ApplyRestrictions((int)(this.ThumbnailOpacity * 100.00), 20, 100) / 100.00;
this.ThumbnailZoomFactor = ThumbnailConfiguration.ApplyRestrictions(this.ThumbnailZoomFactor, 2, 10);
this.ActiveClientHighlightThickness = ThumbnailConfiguration.ApplyRestrictions(this.ActiveClientHighlightThickness, 1, 6);
}
private static int ApplyRestrictions(int value, int minimum, int maximum)

View File

@@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>EveOPreview</RootNamespace>
<AssemblyName>Eve-O Preview</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
@@ -83,9 +83,12 @@
<HintPath>..\packages\LightInject.5.1.2\lib\net452\LightInject.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>R:\eve-o-preview\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
<Reference Include="MediatR, Version=4.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MediatR.4.0.1\lib\net45\MediatR.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.11.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Deployment" />
@@ -108,59 +111,75 @@
<Compile Include="Configuration\AppConfig.cs" />
<Compile Include="Configuration\ConfigurationStorage.cs" />
<Compile Include="Configuration\IAppConfig.cs" />
<Compile Include="Configuration\IThumbnailConfig.cs" />
<Compile Include="Configuration\IThumbnailConfiguration.cs" />
<Compile Include="Configuration\ZoomAnchor.cs" />
<Compile Include="DwmAPI\Implementation\DwmThumbnail.cs" />
<Compile Include="DwmAPI\Interface\IDwmThumbnail.cs" />
<Compile Include="DwmAPI\Interface\InteropConstants.cs" />
<Compile Include="DwmAPI\Interop\DWM_BLURBEHIND.cs" />
<Compile Include="DwmAPI\Interop\DWM_THUMBNAIL_PROPERTIES.cs" />
<Compile Include="DwmAPI\Interop\DWM_TNP_CONSTANTS.cs" />
<Compile Include="DwmAPI\Interface\IWindowManager.cs" />
<Compile Include="DwmAPI\Interop\MARGINS.cs" />
<Compile Include="DwmAPI\Interop\RECT.cs" />
<Compile Include="Mediator\Handlers\Configuration\SaveConfigurationHandler.cs" />
<Compile Include="Mediator\Handlers\Thumbnails\ThumbnailListUpdatedHandler.cs" />
<Compile Include="Mediator\Messages\Thumbnails\ThumbnailListUpdated.cs" />
<Compile Include="Mediator\Handlers\Thumbnails\ThumbnailLocationUpdatedHandler.cs" />
<Compile Include="Mediator\Handlers\Thumbnails\ThumbnailSizeUpdatedHandler.cs" />
<Compile Include="Mediator\Handlers\Services\StartServiceHandler.cs" />
<Compile Include="Mediator\Handlers\Services\StopServiceHandler.cs" />
<Compile Include="Mediator\Messages\Base\NotificationBase.cs" />
<Compile Include="Mediator\Messages\Configuration\SaveConfiguration.cs" />
<Compile Include="Mediator\Messages\Services\StartService.cs" />
<Compile Include="Mediator\Messages\Services\StopService.cs" />
<Compile Include="Mediator\Messages\Thumbnails\ThumbnailLocationUpdated.cs" />
<Compile Include="Mediator\Messages\Thumbnails\ThumbnailSizeUpdated.cs" />
<Compile Include="Presenters\Interface\IMainFormPresenter.cs" />
<Compile Include="Services\Implementation\ProcessInfo.cs" />
<Compile Include="Services\Implementation\ProcessMonitor.cs" />
<Compile Include="Services\Interface\IProcessInfo.cs" />
<Compile Include="Services\Interface\IProcessMonitor.cs" />
<Compile Include="Services\Implementation\DwmThumbnail.cs" />
<Compile Include="Services\Interface\IDwmThumbnail.cs" />
<Compile Include="Services\Interface\InteropConstants.cs" />
<Compile Include="Services\Interop\DWM_BLURBEHIND.cs" />
<Compile Include="Services\Interop\DWM_THUMBNAIL_PROPERTIES.cs" />
<Compile Include="Services\Interop\DWM_TNP_CONSTANTS.cs" />
<Compile Include="Services\Interface\IWindowManager.cs" />
<Compile Include="Services\Interop\MARGINS.cs" />
<Compile Include="Services\Interop\RECT.cs" />
<Compile Include="Configuration\ClientLayout.cs" />
<Compile Include="ApplicationBase\IPresenter.cs" />
<Compile Include="DwmAPI\Implementation\WindowManager.cs" />
<Compile Include="DwmAPI\Interop\User32NativeMethods.cs" />
<Compile Include="Presentation\MainPresenter.cs" />
<Compile Include="Presentation\ViewCloseRequest.cs" />
<Compile Include="Presentation\ViewZoomAnchorConverter.cs" />
<Compile Include="UI\Interface\IThumbnailViewFactory.cs" />
<Compile Include="Presentation\IThumbnailManager.cs" />
<Compile Include="UI\Implementation\ThumbnailDescriptionView.cs" />
<Compile Include="UI\Factory\ThumbnailDescriptionViewFactory.cs" />
<Compile Include="UI\Interface\IMainView.cs" />
<Compile Include="Services\Implementation\WindowManager.cs" />
<Compile Include="Services\Interop\User32NativeMethods.cs" />
<Compile Include="Presenters\Implementation\MainFormPresenter.cs" />
<Compile Include="Presenters\Interface\ViewCloseRequest.cs" />
<Compile Include="Presenters\Implementation\ViewZoomAnchorConverter.cs" />
<Compile Include="View\Interface\IThumbnailViewFactory.cs" />
<Compile Include="Services\Interface\IThumbnailManager.cs" />
<Compile Include="View\Implementation\ThumbnailDescription.cs" />
<Compile Include="View\Interface\IMainFormView.cs" />
<Compile Include="ApplicationBase\IView.cs" />
<Compile Include="UI\Interface\IThumbnailDescriptionView.cs" />
<Compile Include="UI\Interface\IThumbnailDescriptionViewFactory.cs" />
<Compile Include="UI\Interface\ViewZoomAnchor.cs" />
<Compile Include="Configuration\ThumbnailConfig.cs" />
<Compile Include="View\Interface\IThumbnailDescription.cs" />
<Compile Include="View\Interface\ViewZoomAnchor.cs" />
<Compile Include="Configuration\ThumbnailConfiguration.cs" />
<Compile Include="Hotkeys\HotkeyHandler.cs" />
<Compile Include="Hotkeys\HotkeyHandlerNativeMethods.cs" />
<Compile Include="UI\Implementation\MainForm.cs">
<Compile Include="View\Implementation\MainForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="UI\Implementation\MainForm.Designer.cs">
<Compile Include="View\Implementation\MainForm.Designer.cs">
<DependentUpon>MainForm.cs</DependentUpon>
</Compile>
<Compile Include="Configuration\IConfigurationStorage.cs" />
<Compile Include="UI\Interface\IThumbnailView.cs" />
<Compile Include="UI\Factory\ThumbnailViewFactory.cs" />
<Compile Include="Presentation\ThumbnailManager.cs" />
<Compile Include="UI\Implementation\ThumbnailOverlay.cs">
<Compile Include="View\Interface\IThumbnailView.cs" />
<Compile Include="View\Implementation\ThumbnailViewFactory.cs" />
<Compile Include="Services\Implementation\ThumbnailManager.cs" />
<Compile Include="View\Implementation\ThumbnailOverlay.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="UI\Implementation\ThumbnailOverlay.Designer.cs">
<Compile Include="View\Implementation\ThumbnailOverlay.Designer.cs">
<DependentUpon>ThumbnailOverlay.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="UI\Implementation\MainForm.resx">
<EmbeddedResource Include="View\Implementation\MainForm.resx">
<SubType>Designer</SubType>
<DependentUpon>MainForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="UI\Implementation\ThumbnailOverlay.resx">
<EmbeddedResource Include="View\Implementation\ThumbnailOverlay.resx">
<DependentUpon>ThumbnailOverlay.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
@@ -168,7 +187,7 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="UI\Implementation\ThumbnailView.resx">
<EmbeddedResource Include="View\Implementation\ThumbnailView.resx">
<SubType>Designer</SubType>
<DependentUpon>ThumbnailView.cs</DependentUpon>
</EmbeddedResource>
@@ -177,13 +196,13 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<Compile Include="UI\Implementation\ThumbnailView.cs">
<Compile Include="View\Implementation\ThumbnailView.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="UI\Implementation\ThumbnailView.Designer.cs">
<Compile Include="View\Implementation\ThumbnailView.Designer.cs">
<DependentUpon>ThumbnailView.cs</DependentUpon>
</Compile>
<Compile Include="DwmAPI\Interop\DwmApiNativeMethods.cs" />
<Compile Include="Services\Interop\DwmApiNativeMethods.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
@@ -203,11 +222,11 @@
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Fody.2.3.23\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'R:\eve-o-preview\packages\Fody.2.3.23\build\Fody.targets'))" />
<Error Condition="!Exists('..\packages\Costura.Fody.1.6.2\build\dotnet\Costura.Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Costura.Fody.1.6.2\build\dotnet\Costura.Fody.targets'))" />
<Error Condition="!Exists('..\packages\Fody.2.4.1\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.2.4.1\build\Fody.targets'))" />
</Target>
<Import Project="..\packages\Costura.Fody.1.6.2\build\dotnet\Costura.Fody.targets" Condition="Exists('..\packages\Costura.Fody.1.6.2\build\dotnet\Costura.Fody.targets')" />
<Import Project="..\packages\Fody.2.3.23\build\Fody.targets" Condition="Exists('..\packages\Fody.2.3.23\build\Fody.targets')" />
<Import Project="..\packages\Fody.2.4.1\build\Fody.targets" Condition="Exists('..\packages\Fody.2.4.1\build\Fody.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">

View File

@@ -2,7 +2,7 @@ using System;
using System.Windows.Forms;
using System.ComponentModel;
namespace EveOPreview.UI
namespace EveOPreview.UI.Hotkeys
{
class HotkeyHandler : IMessageFilter, IDisposable
{

View File

@@ -1,7 +1,7 @@
using System;
using System.Runtime.InteropServices;
namespace EveOPreview.UI
namespace EveOPreview.UI.Hotkeys
{
static class HotkeyHandlerNativeMethods
{

View File

@@ -0,0 +1,25 @@
using System.Threading;
using System.Threading.Tasks;
using EveOPreview.Configuration;
using EveOPreview.Mediator.Messages;
using MediatR;
namespace EveOPreview.Mediator.Handlers.Configuration
{
sealed class SaveConfigurationHandler : IRequestHandler<SaveConfiguration>
{
private readonly IConfigurationStorage _storage;
public SaveConfigurationHandler(IConfigurationStorage storage)
{
this._storage = storage;
}
public Task Handle(SaveConfiguration message, CancellationToken cancellationToken)
{
this._storage.Save();
return Task.CompletedTask;
}
}
}

View File

@@ -0,0 +1,25 @@
using System.Threading;
using System.Threading.Tasks;
using EveOPreview.Mediator.Messages;
using EveOPreview.Services;
using MediatR;
namespace EveOPreview.Mediator.Handlers.Services
{
sealed class StartServiceHandler : IRequestHandler<StartService>
{
private readonly IThumbnailManager _manager;
public StartServiceHandler(IThumbnailManager manager)
{
this._manager = manager;
}
public Task Handle(StartService message, CancellationToken cancellationToken)
{
this._manager.Start();
return Task.CompletedTask;
}
}
}

View File

@@ -0,0 +1,25 @@
using System.Threading;
using System.Threading.Tasks;
using EveOPreview.Mediator.Messages;
using EveOPreview.Services;
using MediatR;
namespace EveOPreview.Mediator.Handlers.Services
{
sealed class StopServiceHandler : IRequestHandler<StopService>
{
private readonly IThumbnailManager _manager;
public StopServiceHandler(IThumbnailManager manager)
{
this._manager = manager;
}
public Task Handle(StopService message, CancellationToken cancellationToken)
{
this._manager.Stop();
return Task.CompletedTask;
}
}
}

View File

@@ -0,0 +1,35 @@
using System.Threading;
using System.Threading.Tasks;
using EveOPreview.Mediator.Messages;
using EveOPreview.Presenters;
using MediatR;
namespace EveOPreview.Mediator.Handlers.Thumbnails
{
sealed class ThumbnailListUpdatedHandler : INotificationHandler<ThumbnailListUpdated>
{
#region Private fields
private readonly IMainFormPresenter _presenter;
#endregion
public ThumbnailListUpdatedHandler(MainFormPresenter presenter)
{
this._presenter = presenter;
}
public Task Handle(ThumbnailListUpdated notification, CancellationToken cancellationToken)
{
if (notification.Added.Count > 0)
{
this._presenter.AddThumbnails(notification.Added);
}
if (notification.Removed.Count > 0)
{
this._presenter.RemoveThumbnails(notification.Removed);
}
return Task.CompletedTask;
}
}
}

View File

@@ -0,0 +1,27 @@
using System.Threading;
using System.Threading.Tasks;
using EveOPreview.Configuration;
using EveOPreview.Mediator.Messages;
using MediatR;
namespace EveOPreview.Mediator.Handlers.Thumbnails
{
sealed class ThumbnailLocationUpdatedHandler : INotificationHandler<ThumbnailLocationUpdated>
{
private readonly IMediator _mediator;
private readonly IThumbnailConfiguration _configuration;
public ThumbnailLocationUpdatedHandler(IMediator mediator, IThumbnailConfiguration configuration)
{
this._mediator = mediator;
this._configuration = configuration;
}
public Task Handle(ThumbnailLocationUpdated notification, CancellationToken cancellationToken)
{
this._configuration.SetThumbnailLocation(notification.ThumbnailName, notification.ActiveClientName, notification.Location);
return this._mediator.Send(new SaveConfiguration(), cancellationToken);
}
}
}

View File

@@ -0,0 +1,25 @@
using System.Threading;
using System.Threading.Tasks;
using EveOPreview.Mediator.Messages;
using EveOPreview.Presenters;
using MediatR;
namespace EveOPreview.Mediator.Handlers.Thumbnails
{
sealed class ThumbnailSizeUpdatedHandler : INotificationHandler<ThumbnailSizeUpdated>
{
private readonly IMainFormPresenter _presenter;
public ThumbnailSizeUpdatedHandler(MainFormPresenter presenter)
{
this._presenter = presenter;
}
public Task Handle(ThumbnailSizeUpdated notification, CancellationToken cancellationToken)
{
this._presenter.UpdateThumbnailSize(notification.Value);
return Task.CompletedTask;
}
}
}

View File

@@ -0,0 +1,14 @@
using MediatR;
namespace EveOPreview.Mediator.Messages
{
abstract class NotificationBase<TValue> : INotification
{
protected NotificationBase(TValue value)
{
this.Value = value;
}
public TValue Value { get; }
}
}

View File

@@ -0,0 +1,8 @@
using MediatR;
namespace EveOPreview.Mediator.Messages
{
sealed class SaveConfiguration : IRequest
{
}
}

View File

@@ -0,0 +1,8 @@
using MediatR;
namespace EveOPreview.Mediator.Messages
{
sealed class StartService : IRequest
{
}
}

View File

@@ -0,0 +1,8 @@
using MediatR;
namespace EveOPreview.Mediator.Messages
{
sealed class StopService : IRequest
{
}
}

View File

@@ -0,0 +1,17 @@
using System.Collections.Generic;
using MediatR;
namespace EveOPreview.Mediator.Messages
{
sealed class ThumbnailListUpdated : INotification
{
public ThumbnailListUpdated(IList<string> addedThumbnails, IList<string> removedThumbnails)
{
this.Added = addedThumbnails;
this.Removed = removedThumbnails;
}
public IList<string> Added { get; }
public IList<string> Removed { get; }
}
}

View File

@@ -0,0 +1,21 @@
using System.Drawing;
using MediatR;
namespace EveOPreview.Mediator.Messages
{
sealed class ThumbnailLocationUpdated : INotification
{
public ThumbnailLocationUpdated(string thumbnailName, string activeClientName, Point location)
{
this.ThumbnailName = thumbnailName;
this.ActiveClientName = activeClientName;
this.Location = location;
}
public string ThumbnailName { get; }
public string ActiveClientName { get; }
public Point Location { get; }
}
}

View File

@@ -0,0 +1,12 @@
using System.Drawing;
namespace EveOPreview.Mediator.Messages
{
sealed class ThumbnailSizeUpdated : NotificationBase<Size>
{
public ThumbnailSizeUpdated(Size size)
: base(size)
{
}
}
}

View File

@@ -1,23 +0,0 @@
using System;
using System.Collections.Generic;
using System.Drawing;
namespace EveOPreview.UI
{
public interface IThumbnailManager
{
void Activate();
void Deactivate();
void SetThumbnailState(IntPtr thumbnailId, bool hideAlways);
void SetThumbnailsSize(Size size);
void SetupThumbnailFrames();
Action<IList<IThumbnailView>> ThumbnailsAdded { get; set; }
Action<IList<IThumbnailView>> ThumbnailsUpdated { get; set; }
Action<IList<IThumbnailView>> ThumbnailsRemoved { get; set; }
Action<String, String, Point> ThumbnailPositionChanged { get; set; }
Action<Size> ThumbnailSizeChanged { get; set; }
}
}

View File

@@ -3,36 +3,41 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using EveOPreview.Configuration;
using EveOPreview.Mediator.Messages;
using EveOPreview.Services;
using EveOPreview.View;
using MediatR;
namespace EveOPreview.UI
namespace EveOPreview.Presenters
{
public class MainPresenter : Presenter<IMainView>
public class MainFormPresenter : Presenter<IMainFormView>, IMainFormPresenter
{
#region Private constants
private const string ForumUrl = @"https://meta.eveonline.com/t/4202";
#endregion
#region Private fields
private readonly IThumbnailConfig _configuration;
private readonly IMediator _mediator;
private readonly IThumbnailConfiguration _configuration;
private readonly IConfigurationStorage _configurationStorage;
private readonly IThumbnailDescriptionViewFactory _thumbnailDescriptionViewFactory;
private readonly IDictionary<IntPtr, IThumbnailDescriptionView> _thumbnailDescriptionViews;
private readonly IThumbnailManager _thumbnailManager;
private readonly IDictionary<string, IThumbnailDescription> _descriptionsCache;
private bool _exitApplication;
#endregion
public MainPresenter(IApplicationController controller, IMainView view, IThumbnailConfig configuration, IConfigurationStorage configurationStorage,
IThumbnailManager thumbnailManager, IThumbnailDescriptionViewFactory thumbnailDescriptionViewFactory)
public MainFormPresenter(IApplicationController controller, IMainFormView view, IMediator mediator, IThumbnailConfiguration configuration, IConfigurationStorage configurationStorage,
IThumbnailManager thumbnailManager)
: base(controller, view)
{
this._mediator = mediator;
this._configuration = configuration;
this._configurationStorage = configurationStorage;
this._thumbnailDescriptionViewFactory = thumbnailDescriptionViewFactory;
this._thumbnailManager = thumbnailManager;
this._thumbnailDescriptionViews = new Dictionary<IntPtr, IThumbnailDescriptionView>();
this._descriptionsCache = new Dictionary<string, IThumbnailDescription>();
this._exitApplication = false;
this.View.FormActivated = this.Activate;
@@ -43,26 +48,19 @@ namespace EveOPreview.UI
this.View.ThumbnailStateChanged = this.UpdateThumbnailState;
this.View.DocumentationLinkActivated = this.OpenDocumentationLink;
this.View.ApplicationExitRequested = this.ExitApplication;
this._thumbnailManager.ThumbnailsAdded = this.ThumbnailsAdded;
this._thumbnailManager.ThumbnailsUpdated = this.ThumbnailsUpdated;
this._thumbnailManager.ThumbnailsRemoved = this.ThumbnailsRemoved;
this._thumbnailManager.ThumbnailPositionChanged = this.ThumbnailPositionChanged;
this._thumbnailManager.ThumbnailSizeChanged = this.ThumbnailSizeChanged;
}
private void Activate()
{
this.LoadApplicationSettings();
this.View.SetDocumentationUrl(MainPresenter.ForumUrl);
this.View.SetDocumentationUrl(MainFormPresenter.ForumUrl);
this.View.SetVersionInfo(this.GetApplicationVersion());
if (this._configuration.MinimizeToTray)
{
this.View.Minimize();
}
this._thumbnailManager.Activate();
this._mediator.Send(new StartService());
}
private void Minimize()
@@ -79,7 +77,9 @@ namespace EveOPreview.UI
{
if (this._exitApplication || !this.View.MinimizeToTray)
{
this._thumbnailManager.Deactivate();
this._mediator.Send(new StopService()).Wait();
this._thumbnailManager.Stop();
this._configurationStorage.Save();
request.Allow = true;
return;
@@ -152,89 +152,73 @@ namespace EveOPreview.UI
this._thumbnailManager.SetupThumbnailFrames();
}
private void ThumbnailsAdded(IList<IThumbnailView> thumbnails)
{
this.View.AddThumbnails(this.GetThumbnailViews(thumbnails, false));
}
private void ThumbnailsUpdated(IList<IThumbnailView> thumbnails)
public void AddThumbnails(IList<string> thumbnailTitles)
{
this.View.UpdateThumbnails(this.GetThumbnailViews(thumbnails, false));
}
IList<IThumbnailDescription> descriptions = new List<IThumbnailDescription>(thumbnailTitles.Count);
private void ThumbnailsRemoved(IList<IThumbnailView> thumbnails)
{
this.View.RemoveThumbnails(this.GetThumbnailViews(thumbnails, true));
}
private IList<IThumbnailDescriptionView> GetThumbnailViews(IList<IThumbnailView> thumbnails, bool removeFromCache)
{
IList<IThumbnailDescriptionView> thumbnailViews = new List<IThumbnailDescriptionView>(thumbnails.Count);
// Time for some thread safety
lock (this._thumbnailDescriptionViews)
lock (this._descriptionsCache)
{
foreach (IThumbnailView thumbnail in thumbnails)
foreach (string title in thumbnailTitles)
{
IThumbnailDescriptionView thumbnailView;
bool foundInCache = this._thumbnailDescriptionViews.TryGetValue(thumbnail.Id, out thumbnailView);
IThumbnailDescription description = this.CreateThumbnailDescription(title);
this._descriptionsCache[title] = description;
if (!foundInCache)
{
if (removeFromCache)
{
// This item was not even cached
continue;
}
thumbnailView = this._thumbnailDescriptionViewFactory.Create(thumbnail.Id, thumbnail.Title, !thumbnail.IsEnabled);
this._thumbnailDescriptionViews.Add(thumbnail.Id, thumbnailView);
}
else
{
if (removeFromCache)
{
this._thumbnailDescriptionViews.Remove(thumbnail.Id);
}
else
{
thumbnailView.Title = thumbnail.Title;
}
}
thumbnailViews.Add(thumbnailView);
descriptions.Add(description);
}
}
return thumbnailViews;
this.View.AddThumbnails(descriptions);
}
private void ThumbnailPositionChanged(String thumbnailName, String activeClientName, Point location)
public void RemoveThumbnails(IList<string> thumbnailTitles)
{
this._configuration.SetThumbnailLocation(thumbnailName, activeClientName, location);
this._configurationStorage.Save();
IList<IThumbnailDescription> descriptions = new List<IThumbnailDescription>(thumbnailTitles.Count);
lock (this._descriptionsCache)
{
foreach (string title in thumbnailTitles)
{
if (!this._descriptionsCache.TryGetValue(title, out IThumbnailDescription description))
{
continue;
}
this._descriptionsCache.Remove(title);
descriptions.Add(description);
}
}
this.View.RemoveThumbnails(descriptions);
}
private void ThumbnailSizeChanged(Size size)
private IThumbnailDescription CreateThumbnailDescription(string title)
{
// TODO Read here persisted value for the IsDisabled parameter
return new ThumbnailDescription(title, false);
}
private void UpdateThumbnailState(String title)
{
//this._thumbnailManager.SetThumbnailState(thumbnailId, this._thumbnailDescriptionViews[thumbnailId].IsDisabled);
}
public void UpdateThumbnailSize(Size size)
{
this.View.ThumbnailSize = size;
}
private void UpdateThumbnailState(IntPtr thumbnailId)
{
this._thumbnailManager.SetThumbnailState(thumbnailId, this._thumbnailDescriptionViews[thumbnailId].IsDisabled);
}
private void OpenDocumentationLink()
{
ProcessStartInfo processStartInfo = new ProcessStartInfo(new Uri(MainPresenter.ForumUrl).AbsoluteUri);
// TODO Move out
ProcessStartInfo processStartInfo = new ProcessStartInfo(new Uri(MainFormPresenter.ForumUrl).AbsoluteUri);
Process.Start(processStartInfo);
}
private string GetApplicationVersion()
{
Version version = System.Reflection.Assembly.GetEntryAssembly().GetName().Version;
return String.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Revision);
return $"{version.Major}.{version.Minor}.{version.Revision}";
}
private void ExitApplication()

View File

@@ -1,6 +1,6 @@
using EveOPreview.Configuration;
namespace EveOPreview.UI
namespace EveOPreview.View
{
static class ViewZoomAnchorConverter
{

View File

@@ -0,0 +1,13 @@
using System.Collections.Generic;
using System.Drawing;
namespace EveOPreview.Presenters
{
interface IMainFormPresenter
{
void AddThumbnails(IList<string> thumbnailTitles);
void RemoveThumbnails(IList<string> thumbnailTitles);
void UpdateThumbnailSize(Size size);
}
}

View File

@@ -1,4 +1,4 @@
namespace EveOPreview.UI
namespace EveOPreview.View
{
public class ViewCloseRequest
{

View File

@@ -2,18 +2,21 @@ using System;
using System.Threading;
using System.Windows.Forms;
using EveOPreview.Configuration;
using EveOPreview.Presenters;
using EveOPreview.Services;
using EveOPreview.UI;
using EveOPreview.View;
using MediatR;
namespace EveOPreview
{
static class Program
{
private static string MutexName = "EVE-O Preview Single Instance Mutex";
private static string ConfigParameterName = "--config:";
/// <summary>The main entry point for the application.</summary>
[STAThread]
static void Main(string[] args)
static void Main()
{
// The very usual Mutex-based single-instance screening
// 'token' variable is used to store reference to the instance Mutex
@@ -30,55 +33,23 @@ namespace EveOPreview
ExceptionHandler handler = new ExceptionHandler();
handler.SetupExceptionHandlers();
Program.InitializeWinFormsGui();
IApplicationController controller = Program.InitializeApplicationController();
Program.SetupApplicationConttroller(controller, Program.GetCustomConfigFile(args));
controller.Run<MainPresenter>();
}
private static string GetCustomConfigFile(string[] args)
{
// Parse startup parameters
// Simple approach is used because something like NParams would be an overkill here
string configFile = null;
foreach (string arg in args)
{
if ((arg.Length <= Program.ConfigParameterName.Length) || !arg.StartsWith(Program.ConfigParameterName, StringComparison.Ordinal))
{
continue;
}
configFile = arg.Substring(Program.ConfigParameterName.Length);
break;
}
if (string.IsNullOrEmpty(configFile))
{
return "";
}
// One more check to drop trailing "
if ((configFile.Length > 3) && (configFile[0] == '"') && (configFile[configFile.Length - 1] == '"'))
{
configFile = configFile.Substring(1, configFile.Length - 2);
}
return configFile;
Program.InitializeWinForms();
controller.Run<MainFormPresenter>();
}
private static object GetInstanceToken()
{
// The code might look overcomplicated here for a single Mutex operation
// Yet we had already experienced a Windows-level issue
// where .NET finalizer theread was literally paralyzed by
// where .NET finalizer thread was literally paralyzed by
// a failed Mutex operation. That did lead to weird OutOfMemory
// exceptions later
try
{
Mutex mutex = Mutex.OpenExisting(Program.MutexName);
// if that didn't fail then anotherinstance is already running
Mutex.OpenExisting(Program.MutexName);
// if that didn't fail then another instance is already running
return null;
}
catch (UnauthorizedAccessException)
@@ -92,7 +63,7 @@ namespace EveOPreview
}
}
private static void InitializeWinFormsGui()
private static void InitializeWinForms()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
@@ -102,28 +73,37 @@ namespace EveOPreview
{
IIocContainer container = new LightInjectContainer();
// UI classes
IApplicationController controller = new ApplicationController(container)
.RegisterView<IMainView, MainForm>()
.RegisterView<IThumbnailView, ThumbnailView>()
.RegisterView<IThumbnailDescriptionView, ThumbnailDescriptionView>()
.RegisterInstance(new ApplicationContext());
// Singleton registration is used for services
// Low-level services
container.Register<IWindowManager>();
container.Register<IProcessMonitor>();
// MediatR
container.Register<IMediator, MediatR.Mediator>();
container.RegisterInstance<SingleInstanceFactory>(t => container.Resolve(t));
container.RegisterInstance<MultiInstanceFactory>(t => container.ResolveAll(t));
container.Register(typeof(INotificationHandler<>), typeof(Program).Assembly);
container.Register(typeof(IRequestHandler<>), typeof(Program).Assembly);
container.Register(typeof(IRequestHandler<,>), typeof(Program).Assembly);
// Configuration services
container.Register<IConfigurationStorage>();
container.Register<IAppConfig>();
container.Register<IThumbnailConfiguration>();
// Application services
controller.RegisterService<IThumbnailManager, ThumbnailManager>()
.RegisterService<IThumbnailViewFactory, ThumbnailViewFactory>()
.RegisterService<IThumbnailDescriptionViewFactory, ThumbnailDescriptionViewFactory>()
.RegisterService<IConfigurationStorage, ConfigurationStorage>()
.RegisterService<IWindowManager, EveOPreview.DwmAPI.WindowManager>()
.RegisterInstance<IAppConfig>(new AppConfig())
.RegisterInstance<IThumbnailConfig>(new ThumbnailConfig());
container.Register<IThumbnailManager>();
container.Register<IThumbnailViewFactory>();
container.Register<IThumbnailDescription>();
IApplicationController controller = new ApplicationController(container);
// UI classes
controller.RegisterView<IMainFormView, MainForm>()
.RegisterView<IThumbnailView, ThumbnailView>()
.RegisterInstance(new ApplicationContext());
return controller;
}
private static void SetupApplicationConttroller(IApplicationController controller, string configFile)
{
controller.Create<IAppConfig>().ConfigFileName = configFile;
}
}
}

View File

@@ -15,4 +15,4 @@ using System.Runtime.InteropServices;
[assembly: AssemblyVersion("4.0.0.0")]
[assembly: AssemblyFileVersion("4.0.0.0")]
[assembly: CLSCompliant(true)]
[assembly: CLSCompliant(false)]

View File

@@ -1,7 +1,7 @@
using System;
using EveOPreview.DwmInterop;
using EveOPreview.Services.Interop;
namespace EveOPreview.DwmAPI
namespace EveOPreview.Services.Implementation
{
class DwmThumbnail : IDwmThumbnail
{

View File

@@ -0,0 +1,16 @@
using System;
namespace EveOPreview.Services.Implementation
{
sealed class ProcessInfo : IProcessInfo
{
public ProcessInfo(IntPtr handle, string title)
{
this.Handle = handle;
this.Title = title;
}
public IntPtr Handle { get; }
public string Title { get; }
}
}

View File

@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace EveOPreview.Services.Implementation
{
sealed class ProcessMonitor : IProcessMonitor
{
#region Private constants
private const string DefaultProcessName = "ExeFile";
#endregion
#region Private fields
private readonly IDictionary<IntPtr, string> _processCache;
#endregion
public ProcessMonitor()
{
this._processCache = new Dictionary<IntPtr, string>(512);
}
private bool IsMonitoredProcess(string processName)
{
// This is a possible extension point
return String.Equals(processName, ProcessMonitor.DefaultProcessName, StringComparison.OrdinalIgnoreCase);
}
public void GetUpdatedProcesses(out ICollection<IProcessInfo> addedProcesses, out ICollection<IProcessInfo> updatedProcesses, out ICollection<IProcessInfo> removedProcesses)
{
addedProcesses = new List<IProcessInfo>(16);
updatedProcesses = new List<IProcessInfo>(16);
removedProcesses = new List<IProcessInfo>(16);
IList<IntPtr> knownProcesses = new List<IntPtr>(this._processCache.Keys);
foreach (Process process in Process.GetProcesses())
{
string processName = process.ProcessName;
if (!this.IsMonitoredProcess(processName))
{
continue;
}
IntPtr mainWindowHandle = process.MainWindowHandle;
if (mainWindowHandle == IntPtr.Zero)
{
continue; // No need to monitor non-visual processes
}
string mainWindowTitle = process.MainWindowTitle;
this._processCache.TryGetValue(mainWindowHandle, out string cachedTitle);
if (cachedTitle == null)
{
// This is a new process in the list
this._processCache.Add(mainWindowHandle, mainWindowTitle);
addedProcesses.Add(new ProcessInfo(mainWindowHandle, mainWindowTitle));
}
else
{
// This is an already known process
if (cachedTitle != mainWindowTitle)
{
this._processCache[mainWindowHandle] = mainWindowTitle;
updatedProcesses.Add((IProcessInfo)new ProcessInfo(mainWindowHandle, mainWindowTitle));
}
knownProcesses.Remove(mainWindowHandle);
}
}
foreach (IntPtr index in knownProcesses)
{
string title = this._processCache[index];
removedProcesses.Add(new ProcessInfo(index, title));
this._processCache.Remove(index);
}
}
public ICollection<IProcessInfo> GetAllProcesses()
{
ICollection<IProcessInfo> result = new List<IProcessInfo>(this._processCache.Count);
// TODO Lock list here just in case
foreach (KeyValuePair<IntPtr, string> entry in this._processCache)
{
result.Add(new ProcessInfo(entry.Key, entry.Value));
}
return result;
}
}
}

View File

@@ -1,25 +1,28 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Threading;
using EveOPreview.Configuration;
using EveOPreview.Mediator.Messages;
using EveOPreview.View;
using MediatR;
namespace EveOPreview.UI
namespace EveOPreview.Services
{
public class ThumbnailManager : IThumbnailManager
class ThumbnailManager : IThumbnailManager
{
#region Private constants
private const int WindowPositionThreshold = -5000;
private const int WindowSizeThreshold = -5000;
private const string ClientProcessName = "ExeFile";
private const string DefaultClientTitle = "EVE";
#endregion
#region Private fields
private readonly IMediator _mediator;
private readonly IProcessMonitor _processMonitor;
private readonly IWindowManager _windowManager;
private readonly IThumbnailConfig _configuration;
private readonly IThumbnailConfiguration _configuration;
private readonly DispatcherTimer _thumbnailUpdateTimer;
private readonly IThumbnailViewFactory _thumbnailViewFactory;
private readonly Dictionary<IntPtr, IThumbnailView> _thumbnailViews;
@@ -31,8 +34,10 @@ namespace EveOPreview.UI
private bool _isHoverEffectActive;
#endregion
public ThumbnailManager(IWindowManager windowManager, IThumbnailConfig configuration, IThumbnailViewFactory factory)
public ThumbnailManager(IMediator mediator, IThumbnailConfiguration configuration, IProcessMonitor processMonitor, IWindowManager windowManager, IThumbnailViewFactory factory)
{
this._mediator = mediator;
this._processMonitor = processMonitor;
this._windowManager = windowManager;
this._configuration = configuration;
this._thumbnailViewFactory = factory;
@@ -51,32 +56,21 @@ namespace EveOPreview.UI
this._thumbnailUpdateTimer.Interval = new TimeSpan(0, 0, 0, 0, configuration.ThumbnailRefreshPeriod);
}
public Action<IList<IThumbnailView>> ThumbnailsAdded { get; set; }
public Action<IList<IThumbnailView>> ThumbnailsUpdated { get; set; }
public Action<IList<IThumbnailView>> ThumbnailsRemoved { get; set; }
public Action<String, String, Point> ThumbnailPositionChanged { get; set; }
public Action<Size> ThumbnailSizeChanged { get; set; }
public void Activate()
public void Start()
{
this._thumbnailUpdateTimer.Start();
this.RefreshThumbnails();
}
public void Deactivate()
public void Stop()
{
this._thumbnailUpdateTimer.Stop();
}
public void SetThumbnailState(IntPtr thumbnailId, bool hideAlways)
{
IThumbnailView thumbnail;
if (!this._thumbnailViews.TryGetValue(thumbnailId, out thumbnail))
if (!this._thumbnailViews.TryGetValue(thumbnailId, out IThumbnailView thumbnail))
{
return;
}
@@ -84,7 +78,7 @@ namespace EveOPreview.UI
thumbnail.IsEnabled = !hideAlways;
}
public void SetThumbnailsSize(Size size)
public async void SetThumbnailsSize(Size size)
{
this.DisableViewEvents();
@@ -94,7 +88,7 @@ namespace EveOPreview.UI
entry.Value.Refresh(false);
}
this.ThumbnailSizeChanged?.Invoke(size);
await this._mediator.Publish(new ThumbnailSizeUpdated(size));
this.EnableViewEvents();
}
@@ -110,7 +104,7 @@ namespace EveOPreview.UI
foreach (KeyValuePair<IntPtr, IThumbnailView> entry in this._thumbnailViews)
{
IThumbnailView view = entry.Value;
if (hideAllThumbnails || !view.IsEnabled)
{
if (view.IsActive)
@@ -188,94 +182,93 @@ namespace EveOPreview.UI
this._ignoreViewEvents = true;
}
private static Process[] GetClientProcesses()
private async void UpdateThumbnailsList()
{
return Process.GetProcessesByName(ThumbnailManager.ClientProcessName);
}
private void UpdateThumbnailsList()
{
Process[] clientProcesses = ThumbnailManager.GetClientProcesses();
List<IntPtr> processHandles = new List<IntPtr>(clientProcesses.Length);
this._processMonitor.GetUpdatedProcesses(out ICollection<IProcessInfo> addedProcesses, out ICollection<IProcessInfo> updatedProcesses, out ICollection<IProcessInfo> removedProcesses);
IntPtr foregroundWindowHandle = this._windowManager.GetForegroundWindowHandle();
List<IThumbnailView> viewsAdded = new List<IThumbnailView>();
List<IThumbnailView> viewsUpdated = new List<IThumbnailView>();
List<IThumbnailView> viewsRemoved = new List<IThumbnailView>();
List<string> viewsAdded = new List<string>();
List<string> viewsRemoved = new List<string>();
foreach (Process process in clientProcesses)
foreach (IProcessInfo process in addedProcesses)
{
IntPtr processHandle = process.MainWindowHandle;
string processTitle = process.MainWindowTitle;
processHandles.Add(processHandle);
IThumbnailView view = this._thumbnailViewFactory.Create(process.Handle, process.Title, this._configuration.ThumbnailSize);
view.IsEnabled = true;
view.IsOverlayEnabled = this._configuration.ShowThumbnailOverlays;
view.SetFrames(this._configuration.ShowThumbnailFrames);
// Max/Min size limitations should be set AFTER the frames are disabled
// Otherwise thumbnail window will be unnecessary resized
view.SetSizeLimitations(this._configuration.ThumbnailMinimumSize, this._configuration.ThumbnailMaximumSize);
view.SetTopMost(this._configuration.ShowThumbnailsAlwaysOnTop);
IThumbnailView view;
this._thumbnailViews.TryGetValue(processHandle, out view);
view.ThumbnailLocation = this.IsManageableThumbnail(view)
? this._configuration.GetThumbnailLocation(view.Title, this._activeClientTitle, view.ThumbnailLocation)
: this._configuration.GetDefaultThumbnailLocation();
if ((view == null) && (processTitle != ""))
this._thumbnailViews.Add(view.Id, view);
view.ThumbnailResized = this.ThumbnailViewResized;
view.ThumbnailMoved = this.ThumbnailViewMoved;
view.ThumbnailFocused = this.ThumbnailViewFocused;
view.ThumbnailLostFocus = this.ThumbnailViewLostFocus;
view.ThumbnailActivated = this.ThumbnailActivated;
view.ThumbnailDeactivated = this.ThumbnailDeactivated;
view.RegisterHotkey(this._configuration.GetClientHotkey(view.Title));
this.ApplyClientLayout(view.Id, view.Title);
// TODO Add extension filter here later
if (view.Title != ThumbnailManager.DefaultClientTitle)
{
view = this._thumbnailViewFactory.Create(processHandle, processTitle, this._configuration.ThumbnailSize);
view.IsEnabled = true;
view.IsOverlayEnabled = this._configuration.ShowThumbnailOverlays;
view.SetFrames(this._configuration.ShowThumbnailFrames);
// Max/Min size limitations should be set AFTER the frames are disabled
// Otherwise thumbnail window will be unnecessary resized
view.SetSizeLimitations(this._configuration.ThumbnailMinimumSize, this._configuration.ThumbnailMaximumSize);
view.SetTopMost(this._configuration.ShowThumbnailsAlwaysOnTop);
view.ThumbnailLocation = this.IsManageableThumbnail(view)
? this._configuration.GetThumbnailLocation(processTitle, this._activeClientTitle, view.ThumbnailLocation)
: this._configuration.GetDefaultThumbnailLocation();
this._thumbnailViews.Add(processHandle, view);
view.ThumbnailResized = this.ThumbnailViewResized;
view.ThumbnailMoved = this.ThumbnailViewMoved;
view.ThumbnailFocused = this.ThumbnailViewFocused;
view.ThumbnailLostFocus = this.ThumbnailViewLostFocus;
view.ThumbnailActivated = this.ThumbnailActivated;
view.ThumbnailDeactivated = this.ThumbnailDeactivated;
view.RegisterHotkey(this._configuration.GetClientHotkey(processTitle));
this.ApplyClientLayout(processHandle, processTitle);
viewsAdded.Add(view);
}
else if ((view != null) && (processTitle != view.Title)) // update thumbnail title
{
view.Title = processTitle;
view.RegisterHotkey(this._configuration.GetClientHotkey(processTitle));
this.ApplyClientLayout(processHandle, processTitle);
viewsUpdated.Add(view);
viewsAdded.Add(view.Title);
}
if (process.MainWindowHandle == foregroundWindowHandle)
if (process.Handle == foregroundWindowHandle)
{
this._activeClientHandle = process.MainWindowHandle;
this._activeClientTitle = process.MainWindowTitle;
this._activeClientHandle = view.Id;
this._activeClientTitle = view.Title;
}
}
// Cleanup
IList<IntPtr> obsoleteThumbnails = new List<IntPtr>();
foreach (IntPtr processHandle in this._thumbnailViews.Keys)
foreach (IProcessInfo process in updatedProcesses)
{
if (!processHandles.Contains(processHandle))
this._thumbnailViews.TryGetValue(process.Handle, out IThumbnailView view);
if (view == null)
{
obsoleteThumbnails.Add(processHandle);
// Something went terribly wrong
continue;
}
if (process.Title != view.Title) // update thumbnail title
{
viewsRemoved.Add(view.Title);
view.Title = process.Title;
viewsAdded.Add(view.Title);
view.RegisterHotkey(this._configuration.GetClientHotkey(process.Title));
this.ApplyClientLayout(view.Id, view.Title);
}
if (process.Handle == foregroundWindowHandle)
{
this._activeClientHandle = process.Handle;
this._activeClientTitle = process.Title;
}
}
foreach (IntPtr processHandle in obsoleteThumbnails)
foreach (IProcessInfo process in removedProcesses)
{
IThumbnailView view = this._thumbnailViews[processHandle];
IThumbnailView view = this._thumbnailViews[process.Handle];
this._thumbnailViews.Remove(processHandle);
this._thumbnailViews.Remove(view.Id);
if (view.Title != ThumbnailManager.DefaultClientTitle)
{
viewsRemoved.Add(view.Title);
}
view.UnregisterHotkey();
@@ -286,13 +279,12 @@ namespace EveOPreview.UI
view.ThumbnailActivated = null;
view.Close();
viewsRemoved.Add(view);
}
this.ThumbnailsAdded?.Invoke(viewsAdded);
this.ThumbnailsUpdated?.Invoke(viewsUpdated);
this.ThumbnailsRemoved?.Invoke(viewsRemoved);
if ((viewsAdded.Count > 0) || (viewsRemoved.Count > 0))
{
await this._mediator.Publish(new ThumbnailListUpdated(viewsAdded, viewsRemoved));
}
}
private void ThumbnailViewFocused(IntPtr id)
@@ -380,7 +372,7 @@ namespace EveOPreview.UI
view.Refresh(false);
}
private void ThumbnailViewMoved(IntPtr id)
private async void ThumbnailViewMoved(IntPtr id)
{
if (this._ignoreViewEvents)
{
@@ -391,7 +383,7 @@ namespace EveOPreview.UI
if (this.IsManageableThumbnail(view))
{
this.ThumbnailPositionChanged?.Invoke(view.Title, this._activeClientTitle, view.ThumbnailLocation);
await this._mediator.Publish(new ThumbnailLocationUpdated(view.Title, this._activeClientTitle, view.ThumbnailLocation));
}
view.Refresh(false);
@@ -452,11 +444,11 @@ namespace EveOPreview.UI
private void UpdateClientLayouts()
{
Process[] clientProcesses = ThumbnailManager.GetClientProcesses();
ICollection<IProcessInfo> processes = this._processMonitor.GetAllProcesses();
foreach (Process process in clientProcesses)
foreach (IProcessInfo process in processes)
{
this._windowManager.GetWindowCoordinates(process.MainWindowHandle, out int left, out int top, out int right, out int bottom);
this._windowManager.GetWindowCoordinates(process.Handle, out int left, out int top, out int right, out int bottom);
int width = Math.Abs(right - left);
int height = Math.Abs(bottom - top);
@@ -466,7 +458,7 @@ namespace EveOPreview.UI
continue;
}
this._configuration.SetClientLayout(process.MainWindowTitle, new ClientLayout(left, top, width, height));
this._configuration.SetClientLayout(process.Title, new ClientLayout(left, top, width, height));
}
}

View File

@@ -1,7 +1,7 @@
using System;
using EveOPreview.DwmInterop;
using EveOPreview.Services.Interop;
namespace EveOPreview.DwmAPI
namespace EveOPreview.Services.Implementation
{
class WindowManager : IWindowManager
{
@@ -48,5 +48,18 @@ namespace EveOPreview.DwmAPI
right = windowRectangle.Right;
bottom = windowRectangle.Bottom;
}
public bool IsWindowMinimized(IntPtr handle)
{
return User32NativeMethods.IsIconic(handle);
}
public IDwmThumbnail RegisterThumbnail(IntPtr destination, IntPtr source)
{
IDwmThumbnail thumbnail = new DwmThumbnail(this);
thumbnail.Register(destination, source);
return thumbnail;
}
}
}

View File

@@ -1,6 +1,6 @@
using System;
namespace EveOPreview
namespace EveOPreview.Services
{
public interface IDwmThumbnail
{

View File

@@ -0,0 +1,10 @@
using System;
namespace EveOPreview.Services
{
public interface IProcessInfo
{
IntPtr Handle { get; }
string Title { get; }
}
}

View File

@@ -0,0 +1,10 @@
using System.Collections.Generic;
namespace EveOPreview.Services
{
public interface IProcessMonitor
{
ICollection<IProcessInfo> GetAllProcesses();
void GetUpdatedProcesses(out ICollection<IProcessInfo> addedProcesses, out ICollection<IProcessInfo> updatedProcesses, out ICollection<IProcessInfo> removedProcesses);
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Drawing;
namespace EveOPreview.Services
{
public interface IThumbnailManager
{
void Start();
void Stop();
void SetThumbnailState(IntPtr thumbnailId, bool hideAlways);
void SetThumbnailsSize(Size size);
void SetupThumbnailFrames();
}
}

View File

@@ -1,6 +1,6 @@
using System;
namespace EveOPreview
namespace EveOPreview.Services
{
public interface IWindowManager
{
@@ -13,5 +13,8 @@ namespace EveOPreview
void MoveWindow(IntPtr handle, int left, int top, int width, int height);
void GetWindowCoordinates(IntPtr handle, out int left, out int top, out int right, out int bottom);
bool IsWindowMinimized(IntPtr handle);
IDwmThumbnail RegisterThumbnail(IntPtr destination, IntPtr source);
}
}

View File

@@ -1,6 +1,6 @@
using System;
namespace EveOPreview
namespace EveOPreview.Services
{
public static class InteropConstants
{

View File

@@ -1,7 +1,7 @@
using System;
using System.Runtime.InteropServices;
namespace EveOPreview.DwmInterop
namespace EveOPreview.Services.Interop
{
[StructLayout(LayoutKind.Sequential)]
class DWM_BLURBEHIND

View File

@@ -1,6 +1,6 @@
using System.Runtime.InteropServices;
namespace EveOPreview.DwmInterop
namespace EveOPreview.Services.Interop
{
[StructLayout(LayoutKind.Sequential)]
class DWM_THUMBNAIL_PROPERTIES

View File

@@ -1,4 +1,4 @@
namespace EveOPreview.DwmInterop
namespace EveOPreview.Services.Interop
{
static class DWM_TNP_CONSTANTS
{

View File

@@ -2,7 +2,7 @@ using System;
using System.Runtime.InteropServices;
using System.Drawing;
namespace EveOPreview.DwmInterop
namespace EveOPreview.Services.Interop
{
static class DwmApiNativeMethods
{

View File

@@ -1,6 +1,6 @@
using System.Runtime.InteropServices;
namespace EveOPreview.DwmInterop
namespace EveOPreview.Services.Interop
{
[StructLayout(LayoutKind.Sequential)]
class MARGINS

View File

@@ -1,6 +1,6 @@
using System.Runtime.InteropServices;
namespace EveOPreview.DwmInterop
namespace EveOPreview.Services.Interop
{
[StructLayout(LayoutKind.Sequential)]
struct RECT

View File

@@ -1,7 +1,7 @@
using System;
using System.Runtime.InteropServices;
namespace EveOPreview.DwmInterop
namespace EveOPreview.Services.Interop
{
static class User32NativeMethods
{
@@ -28,5 +28,12 @@ namespace EveOPreview.DwmInterop
[DllImport("user32.dll")]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool IsZoomed(IntPtr hWnd);
}
}

View File

@@ -1,25 +0,0 @@
using System;
namespace EveOPreview.UI
{
public class ThumbnailDescriptionViewFactory : IThumbnailDescriptionViewFactory
{
private readonly IApplicationController _controller;
public ThumbnailDescriptionViewFactory(IApplicationController controller)
{
this._controller = controller;
}
public IThumbnailDescriptionView Create(IntPtr id, string title, bool isDisabled)
{
IThumbnailDescriptionView view = this._controller.Create<IThumbnailDescriptionView>();
view.Id = id;
view.Title = title;
view.IsDisabled = isDisabled;
return view;
}
}
}

View File

@@ -1,25 +0,0 @@
using System;
namespace EveOPreview.UI
{
public class ThumbnailDescriptionView : IThumbnailDescriptionView
{
public IntPtr Id { get; set; }
public string Title { get; set; }
public bool IsDisabled { get; set; }
public void Show()
{
}
public void Hide()
{
}
public void Close()
{
}
}
}

View File

@@ -1,11 +0,0 @@
using System;
namespace EveOPreview.UI
{
public interface IThumbnailDescriptionView : IView
{
IntPtr Id { get; set; }
string Title { get; set; }
bool IsDisabled { get; set; }
}
}

View File

@@ -1,9 +0,0 @@
using System;
namespace EveOPreview.UI
{
public interface IThumbnailDescriptionViewFactory
{
IThumbnailDescriptionView Create(IntPtr id, string title, bool isDisabled);
}
}

View File

@@ -1,6 +1,6 @@
using System.Windows.Forms;
namespace EveOPreview.UI
namespace EveOPreview.View
{
partial class MainForm
{

View File

@@ -3,9 +3,9 @@ using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace EveOPreview.UI
namespace EveOPreview.View
{
public partial class MainForm : Form, IMainView
public partial class MainForm : Form, IMainFormView
{
#region Private fields
private readonly ApplicationContext _context;
@@ -205,16 +205,11 @@ namespace EveOPreview.UI
this.DocumentationLink.Text = url;
}
public void AddThumbnails(IList<IThumbnailDescriptionView> thumbnails)
public void AddThumbnails(IList<IThumbnailDescription> thumbnails)
{
if (thumbnails.Count == 0)
{
return;
}
this.ThumbnailsList.BeginUpdate();
foreach (IThumbnailDescriptionView view in thumbnails)
foreach (IThumbnailDescription view in thumbnails)
{
this.ThumbnailsList.Items.Add(view);
}
@@ -222,25 +217,11 @@ namespace EveOPreview.UI
this.ThumbnailsList.EndUpdate();
}
public void UpdateThumbnails(IList<IThumbnailDescriptionView> thumbnails)
public void RemoveThumbnails(IList<IThumbnailDescription> thumbnails)
{
// Just trigger redraw
if (thumbnails.Count > 0)
{
this.ThumbnailsList.Invalidate();
}
}
public void RemoveThumbnails(IList<IThumbnailDescriptionView> thumbnails)
{
if (thumbnails.Count == 0)
{
return;
}
this.ThumbnailsList.BeginUpdate();
foreach (IThumbnailDescriptionView view in thumbnails)
foreach (IThumbnailDescription view in thumbnails)
{
this.ThumbnailsList.Items.Remove(view);
}
@@ -267,7 +248,7 @@ namespace EveOPreview.UI
public Action ThumbnailsSizeChanged { get; set; }
public Action<IntPtr> ThumbnailStateChanged { get; set; }
public Action<string> ThumbnailStateChanged { get; set; }
public Action DocumentationLinkActivated { get; set; }
@@ -344,14 +325,14 @@ namespace EveOPreview.UI
private void ThumbnailsList_ItemCheck_Handler(object sender, ItemCheckEventArgs e)
{
IThumbnailDescriptionView selectedItem = this.ThumbnailsList.Items[e.Index] as IThumbnailDescriptionView;
if (selectedItem == null)
if (!(this.ThumbnailsList.Items[e.Index] is IThumbnailDescription selectedItem))
{
return;
}
selectedItem.IsDisabled = (e.NewValue == CheckState.Checked);
this.ThumbnailStateChanged?.Invoke(selectedItem.Id);
this.ThumbnailStateChanged?.Invoke(selectedItem.Title);
}
private void DocumentationLinkClicked_Handler(object sender, LinkLabelLinkClickedEventArgs e)

View File

@@ -0,0 +1,14 @@
namespace EveOPreview.View
{
sealed class ThumbnailDescription : IThumbnailDescription
{
public ThumbnailDescription(string title, bool isDisabled)
{
this.Title = title;
this.IsDisabled = isDisabled;
}
public string Title { get; set; }
public bool IsDisabled { get; set; }
}
}

View File

@@ -1,4 +1,4 @@
namespace EveOPreview.UI
namespace EveOPreview.View
{
partial class ThumbnailOverlay
{

View File

@@ -1,7 +1,8 @@
using System;
using System.Windows.Forms;
using EveOPreview.Services;
namespace EveOPreview.UI
namespace EveOPreview.View
{
public partial class ThumbnailOverlay : Form
{

View File

@@ -1,4 +1,4 @@
namespace EveOPreview.UI
namespace EveOPreview.View
{
partial class ThumbnailView
{

View File

@@ -2,9 +2,10 @@ using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using EveOPreview.DwmAPI;
using EveOPreview.Services;
using EveOPreview.UI.Hotkeys;
namespace EveOPreview.UI
namespace EveOPreview.View
{
public partial class ThumbnailView : Form, IThumbnailView
{
@@ -475,8 +476,7 @@ namespace EveOPreview.UI
#region Thumbnail management
private void RegisterThumbnail()
{
this._thumbnail = new DwmThumbnail(this._windowManager);
this._thumbnail.Register(this.Handle, this.Id);
this._thumbnail = this._windowManager.RegisterThumbnail(this.Handle, this.Id);
}
private void UpdateThumbnail()

View File

@@ -1,9 +1,9 @@
using System;
using System.Drawing;
namespace EveOPreview.UI
namespace EveOPreview.View
{
public class ThumbnailViewFactory : IThumbnailViewFactory
sealed class ThumbnailViewFactory : IThumbnailViewFactory
{
private readonly IApplicationController _controller;

View File

@@ -1,14 +1,15 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using EveOPreview.UI;
namespace EveOPreview.UI
namespace EveOPreview.View
{
/// <summary>
/// Main view interface
/// Presenter uses it to access GUI properties
/// </summary>
public interface IMainView : IView
public interface IMainFormView : IView
{
bool MinimizeToTray { get; set; }
@@ -38,9 +39,8 @@ namespace EveOPreview.UI
void Minimize();
void AddThumbnails(IList<IThumbnailDescriptionView> thumbnails);
void UpdateThumbnails(IList<IThumbnailDescriptionView> thumbnails);
void RemoveThumbnails(IList<IThumbnailDescriptionView> thumbnails);
void AddThumbnails(IList<IThumbnailDescription> thumbnails);
void RemoveThumbnails(IList<IThumbnailDescription> thumbnails);
void RefreshZoomSettings();
Action ApplicationExitRequested { get; set; }
@@ -49,7 +49,7 @@ namespace EveOPreview.UI
Action<ViewCloseRequest> FormCloseRequested { get; set; }
Action ApplicationSettingsChanged { get; set; }
Action ThumbnailsSizeChanged { get; set; }
Action<IntPtr> ThumbnailStateChanged { get; set; }
Action<string> ThumbnailStateChanged { get; set; }
Action DocumentationLinkActivated { get; set; }
}
}

View File

@@ -0,0 +1,8 @@
namespace EveOPreview.View
{
public interface IThumbnailDescription
{
string Title { get; set; }
bool IsDisabled { get; set; }
}
}

View File

@@ -2,7 +2,7 @@
using System.Drawing;
using System.Windows.Forms;
namespace EveOPreview.UI
namespace EveOPreview.View
{
public interface IThumbnailView : IView
{

View File

@@ -1,7 +1,7 @@
using System;
using System.Drawing;
namespace EveOPreview.UI
namespace EveOPreview.View
{
public interface IThumbnailViewFactory
{

View File

@@ -1,4 +1,4 @@
namespace EveOPreview.UI
namespace EveOPreview.View
{
public enum ViewZoomAnchor
{

View File

@@ -1,3 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/></startup></configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/></startup></configuration>

View File

@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Costura.Fody" version="1.6.2" targetFramework="net46" developmentDependency="true" />
<package id="Fody" version="2.3.23" targetFramework="net452" developmentDependency="true" />
<package id="LightInject" version="5.1.2" targetFramework="net452" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net452" />
<package id="Fody" version="2.4.1" targetFramework="net46" developmentDependency="true" />
<package id="LightInject" version="5.1.2" targetFramework="net452" requireReinstallation="true" />
<package id="MediatR" version="4.0.1" targetFramework="net452" />
<package id="Newtonsoft.Json" version="11.0.1" targetFramework="net46" />
</packages>