Introduced a very simple "singleton" service-locator, which allowed for a removal of specific implementations which were very limited. With this it's easier to access dependencies.

This commit is contained in:
Krom, Robertus 2020-02-18 13:48:40 +01:00
commit 80d8f51fc5
53 changed files with 744 additions and 1230 deletions

View file

@ -260,7 +260,7 @@ namespace GreenshotPlugin.Core {
location.Offset(-40, -10);
}
// This prevents the problem that the context menu shows in the task-bar
User32.SetForegroundWindow(PluginUtils.Host.NotifyIcon.ContextMenuStrip.Handle);
User32.SetForegroundWindow(SimpleServiceProvider.Current.GetInstance<NotifyIcon>().ContextMenuStrip.Handle);
menu.Show(location);
menu.Focus();

View file

@ -1,29 +0,0 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GreenshotPlugin.Core
{
public delegate TResult Func<out TResult>();
public delegate TResult Func<in T, out TResult>(T arg);
public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);
public delegate TResult Func<in T1, in T2, in T3, out TResult>(T1 arg1, T2 arg2, T3 arg3);
public delegate TResult Func<in T1, in T2, in T3, in T4, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
}

View file

@ -52,16 +52,11 @@ namespace GreenshotPlugin.Core {
private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection<CoreConfiguration>();
private const int ExifOrientationId = 0x0112;
/// <summary>
/// This is a factory method to create a surface, set from the Greenshot main project
/// </summary>
public static Func<ISurface> SurfaceFactory { get; set; }
static ImageHelper()
static ImageHelper()
{
StreamConverters["greenshot"] = (stream, s) =>
{
var surface = SurfaceFactory();
var surface = SimpleServiceProvider.Current.GetInstance<Func<ISurface>>().Invoke();
return surface.GetImageForExport();
};

View file

@ -45,14 +45,6 @@ namespace GreenshotPlugin.Core {
CoreConfig.PropertyChanged += OnIconSizeChanged;
}
/// <summary>
/// Simple global property to get the Greenshot host
/// </summary>
public static IGreenshotHost Host {
get;
set;
}
/// <summary>
/// Clear icon cache
/// </summary>
@ -220,11 +212,10 @@ namespace GreenshotPlugin.Core {
/// <summary>
/// Helper method to add a plugin MenuItem to the Greenshot context menu
/// </summary>
/// <param name="host">IGreenshotHost</param>
/// <param name="item">ToolStripMenuItem</param>
public static void AddToContextMenu(IGreenshotHost host, ToolStripMenuItem item) {
// Here we can hang ourselves to the main context menu!
ContextMenuStrip contextMenu = host.MainMenu;
public static void AddToContextMenu(ToolStripMenuItem item) {
// Here we can hang ourselves to the main context menu!
var contextMenu = SimpleServiceProvider.Current.GetInstance<ContextMenuStrip>();
bool addedItem = false;
// Try to find a separator, so we insert ourselves after it

View file

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GreenshotPlugin.Interfaces;
namespace GreenshotPlugin.Core
{
/// <summary>
/// A really cheap and simple DI system
/// </summary>
public class SimpleServiceProvider : IServiceLocator
{
private readonly Dictionary<Type, List<object>> _services = new Dictionary<Type, List<object>>();
public static IServiceLocator Current { get; } = new SimpleServiceProvider();
public IEnumerable<TService> GetAllInstances<TService>()
{
var typeOfService = typeof(TService);
if (!_services.TryGetValue(typeOfService, out var results))
{
yield break;
}
foreach (TService result in results)
{
yield return result;
}
}
public TService GetInstance<TService>()
{
return GetAllInstances<TService>().SingleOrDefault();
}
public void AddService<TService>(IEnumerable<TService> services)
{
var serviceType = typeof(TService);
if (!_services.TryGetValue(serviceType, out var currentServices))
{
currentServices = new List<object>();
_services.Add(serviceType, currentServices);
}
foreach (var service in services)
{
currentServices.Add(service);
}
}
public void AddService<TService>(params TService[] services)
{
AddService(services.AsEnumerable());
}
}
}