diff --git a/src/Greenshot.Addons/Controls/ContextMenuToolStripProfessionalRenderer.cs b/src/Greenshot.Addons/Controls/ContextMenuToolStripProfessionalRenderer.cs index 7ba5aaeee..993c6c695 100644 --- a/src/Greenshot.Addons/Controls/ContextMenuToolStripProfessionalRenderer.cs +++ b/src/Greenshot.Addons/Controls/ContextMenuToolStripProfessionalRenderer.cs @@ -39,14 +39,18 @@ namespace Greenshot.Addons.Controls /// public class ContextMenuToolStripProfessionalRenderer : ToolStripProfessionalRenderer { - // TODO: Solve, was static reference! - private static readonly ICoreConfiguration CoreConfig = new CoreConfigurationImpl(); + private readonly ICoreConfiguration _coreConfiguration = new CoreConfigurationImpl(); + + public ContextMenuToolStripProfessionalRenderer(ICoreConfiguration coreConfiguration) + { + _coreConfiguration = coreConfiguration; + } private Image _scaledCheckbox; private bool _newImage; protected override void OnRenderItemCheck(ToolStripItemImageRenderEventArgs e) { - if (_scaledCheckbox == null || (NativeSize)_scaledCheckbox.Size != CoreConfig.IconSize) + if (_scaledCheckbox == null || (NativeSize)_scaledCheckbox.Size != _coreConfiguration.IconSize) { if (_newImage) { diff --git a/src/Greenshot.Addons/Controls/SaveImageFileDialog.cs b/src/Greenshot.Addons/Controls/SaveImageFileDialog.cs index 7a6de3ad1..a7a34a887 100644 --- a/src/Greenshot.Addons/Controls/SaveImageFileDialog.cs +++ b/src/Greenshot.Addons/Controls/SaveImageFileDialog.cs @@ -43,23 +43,18 @@ namespace Greenshot.Addons.Controls public class SaveImageFileDialog : IDisposable { private static readonly LogSource Log = new LogSource(); - // TODO: Solve, was static reference! - private static readonly ICoreConfiguration CoreConfiguration = new CoreConfigurationImpl(); + private readonly ICoreConfiguration _coreConfiguration; private readonly ICaptureDetails _captureDetails; private DirectoryInfo _eagerlyCreatedDirectory; private FilterOption[] _filterOptions; protected SaveFileDialog SaveFileDialog; - public SaveImageFileDialog() + public SaveImageFileDialog(ICoreConfiguration coreConfiguration, ICaptureDetails captureDetails = null) { - Init(); - } - - public SaveImageFileDialog(ICaptureDetails captureDetails) - { - _captureDetails = captureDetails; - Init(); - } + _coreConfiguration = coreConfiguration; + _captureDetails = captureDetails; + Init(); + } /// /// filename exactly as typed in the filename field @@ -146,21 +141,21 @@ namespace Greenshot.Addons.Controls string initialDirectory = null; try { - CoreConfiguration.ValidateAndCorrect(); - initialDirectory = Path.GetDirectoryName(CoreConfiguration.OutputFileAsFullpath); + _coreConfiguration.ValidateAndCorrect(); + initialDirectory = Path.GetDirectoryName(_coreConfiguration.OutputFileAsFullpath); } catch { - Log.Warn().WriteLine("OutputFileAsFullpath was set to {0}, ignoring due to problem in path.", CoreConfiguration.OutputFileAsFullpath); + Log.Warn().WriteLine("OutputFileAsFullpath was set to {0}, ignoring due to problem in path.", _coreConfiguration.OutputFileAsFullpath); } if (!string.IsNullOrEmpty(initialDirectory) && Directory.Exists(initialDirectory)) { SaveFileDialog.InitialDirectory = initialDirectory; } - else if (Directory.Exists(CoreConfiguration.OutputFilePath)) + else if (Directory.Exists(_coreConfiguration.OutputFilePath)) { - SaveFileDialog.InitialDirectory = CoreConfiguration.OutputFilePath; + SaveFileDialog.InitialDirectory = _coreConfiguration.OutputFilePath; } // The following property fixes a problem that the directory where we save is locked (bug #2899790) SaveFileDialog.RestoreDirectory = true; @@ -175,7 +170,7 @@ namespace Greenshot.Addons.Controls PrepareFilterOptions(); var fdf = ""; var preselect = 0; - var outputFileFormatAsString = Enum.GetName(typeof(OutputFormats), CoreConfiguration.OutputFileFormat); + var outputFileFormatAsString = Enum.GetName(typeof(OutputFormats), _coreConfiguration.OutputFileFormat); for (var i = 0; i < _filterOptions.Length; i++) { var fo = _filterOptions[i]; @@ -224,7 +219,7 @@ namespace Greenshot.Addons.Controls private void ApplySuggestedValues() { // build the full path and set dialog properties - FileName = FilenameHelper.GetFilenameWithoutExtensionFromPattern(CoreConfiguration.OutputFileFilenamePattern, _captureDetails); + FileName = FilenameHelper.GetFilenameWithoutExtensionFromPattern(_coreConfiguration.OutputFileFilenamePattern, _captureDetails); } private void CleanUp() diff --git a/src/Greenshot.Addons/Controls/ThumbnailForm.cs b/src/Greenshot.Addons/Controls/ThumbnailForm.cs index 5ccc63869..919ff55d2 100644 --- a/src/Greenshot.Addons/Controls/ThumbnailForm.cs +++ b/src/Greenshot.Addons/Controls/ThumbnailForm.cs @@ -48,17 +48,14 @@ namespace Greenshot.Addons.Controls /// public sealed class ThumbnailForm : FormWithoutActivation { - // TODO: Solve, was static reference! - private static readonly ICoreConfiguration coreConfiguration = new CoreConfigurationImpl(); - private IntPtr _thumbnailHandle = IntPtr.Zero; /// /// Constructor for the Thumbnail form /// - public ThumbnailForm() + public ThumbnailForm(ICoreConfiguration coreConfiguration) { - ShowInTaskbar = false; + ShowInTaskbar = false; FormBorderStyle = FormBorderStyle.None; TopMost = false; Enabled = false; diff --git a/src/Greenshot.Addons/Core/ImageOutput.cs b/src/Greenshot.Addons/Core/ImageOutput.cs index 028088336..0857fc76c 100644 --- a/src/Greenshot.Addons/Core/ImageOutput.cs +++ b/src/Greenshot.Addons/Core/ImageOutput.cs @@ -128,7 +128,7 @@ namespace Greenshot.Addons.Core public static string SaveWithDialog(ISurface surface, ICaptureDetails captureDetails) { string returnValue = null; - using (var saveImageFileDialog = new SaveImageFileDialog(captureDetails)) + using (var saveImageFileDialog = new SaveImageFileDialog(CoreConfig, captureDetails)) { var dialogResult = saveImageFileDialog.ShowDialog(); if (dialogResult.Equals(DialogResult.OK)) diff --git a/src/Greenshot/Components/CaptureSupportInfo.cs b/src/Greenshot/Components/CaptureSupportInfo.cs index b284331c6..1d9720c54 100644 --- a/src/Greenshot/Components/CaptureSupportInfo.cs +++ b/src/Greenshot/Components/CaptureSupportInfo.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using Greenshot.Addon.InternetExplorer; +using Greenshot.Addons.Core; using Greenshot.Addons.Interfaces; namespace Greenshot.Components @@ -10,19 +11,22 @@ namespace Greenshot.Components public class CaptureSupportInfo { /// - /// Constructor + /// Constructor for DI /// /// InternetExplorerCaptureHelper /// IEnumerable with IFormEnhancer public CaptureSupportInfo( + ICoreConfiguration coreConfiguration, InternetExplorerCaptureHelper internetExplorerCaptureHelper, IEnumerable formEnhancers = null ) { + CoreConfiguration = coreConfiguration; InternetExplorerCaptureHelper = internetExplorerCaptureHelper; FormEnhancers = formEnhancers; } + public ICoreConfiguration CoreConfiguration { get; } public InternetExplorerCaptureHelper InternetExplorerCaptureHelper { get; } public IEnumerable FormEnhancers { get; } diff --git a/src/Greenshot/Forms/MainForm.Designer.cs b/src/Greenshot/Forms/MainForm.Designer.cs index 6b65ccff7..d8faa25ae 100644 --- a/src/Greenshot/Forms/MainForm.Designer.cs +++ b/src/Greenshot/Forms/MainForm.Designer.cs @@ -105,7 +105,7 @@ namespace Greenshot.Forms { this.contextMenu.Name = "contextMenu"; this.contextMenu.Closing += new System.Windows.Forms.ToolStripDropDownClosingEventHandler(this.ContextMenuClosing); this.contextMenu.Opening += new System.ComponentModel.CancelEventHandler(this.ContextMenuOpening); - this.contextMenu.Renderer = new ContextMenuToolStripProfessionalRenderer(); + this.contextMenu.Renderer = new ContextMenuToolStripProfessionalRenderer(_coreConfiguration); // // contextmenu_capturearea // diff --git a/src/Greenshot/Forms/MainForm.cs b/src/Greenshot/Forms/MainForm.cs index 7bdc6122f..30512fdd4 100644 --- a/src/Greenshot/Forms/MainForm.cs +++ b/src/Greenshot/Forms/MainForm.cs @@ -676,7 +676,7 @@ namespace Greenshot.Forms var window = captureWindowItem.Tag as IInteropWindow; if (_thumbnailForm == null) { - _thumbnailForm = new ThumbnailForm(); + _thumbnailForm = new ThumbnailForm(_coreConfiguration); } _thumbnailForm.ShowThumbnail(window, captureWindowItem.GetCurrentParent().TopLevelControl); } diff --git a/src/Greenshot/Helpers/CaptureHelper.cs b/src/Greenshot/Helpers/CaptureHelper.cs index fa0874d0d..0de2c87f6 100644 --- a/src/Greenshot/Helpers/CaptureHelper.cs +++ b/src/Greenshot/Helpers/CaptureHelper.cs @@ -61,8 +61,7 @@ namespace Greenshot.Helpers public class CaptureHelper : IDisposable { private static readonly LogSource Log = new LogSource(); - // TODO: Solve, was static reference! - private static readonly ICoreConfiguration CoreConfig = new CoreConfigurationImpl(); + private readonly ICoreConfiguration _coreConfiguration; private readonly bool _captureMouseCursor; private ICapture _capture; private readonly CaptureSupportInfo _captureSupportInfo; @@ -78,6 +77,7 @@ namespace Greenshot.Helpers public CaptureHelper(CaptureSupportInfo captureSupportInfo, CaptureMode captureMode) { _captureSupportInfo = captureSupportInfo; + _coreConfiguration = captureSupportInfo.CoreConfiguration; _captureMode = captureMode; _capture = new Capture(); _destinationHolder = DestinationHolder.Instance; @@ -134,7 +134,7 @@ namespace Greenshot.Helpers SelectedCaptureWindow = null; _capture = null; // Empty working set after capturing - if (CoreConfig.MinimizeWorkingSetSize) + if (_coreConfiguration.MinimizeWorkingSetSize) { PsApi.EmptyWorkingSet(); } @@ -248,7 +248,7 @@ namespace Greenshot.Helpers private void DoCaptureFeedback() { - if (CoreConfig.PlayCameraSound) + if (_coreConfiguration.PlayCameraSound) { SoundHelper.Play(); } @@ -282,7 +282,7 @@ namespace Greenshot.Helpers { // This fixes a problem when a balloon is still visible and a capture needs to be taken // forcefully removes the balloon! - if (!CoreConfig.HideTrayicon) + if (!_coreConfiguration.HideTrayicon) { MainForm.Instance.NotifyIcon.Visible = false; MainForm.Instance.NotifyIcon.Visible = true; @@ -313,20 +313,20 @@ namespace Greenshot.Helpers } // Delay for the Context menu - if (CoreConfig.CaptureDelay > 0) + if (_coreConfiguration.CaptureDelay > 0) { - Thread.Sleep(CoreConfig.CaptureDelay); + Thread.Sleep(_coreConfiguration.CaptureDelay); } else { - CoreConfig.CaptureDelay = 0; + _coreConfiguration.CaptureDelay = 0; } // Capture Mousecursor if we are not loading from file or clipboard, only show when needed if (_captureMode != CaptureMode.File && _captureMode != CaptureMode.Clipboard) { _capture = WindowCapture.CaptureCursor(_capture); - _capture.CursorVisible = _captureMouseCursor && CoreConfig.CaptureMousepointer; + _capture.CursorVisible = _captureMouseCursor && _coreConfiguration.CaptureMousepointer; } switch (_captureMode) @@ -382,9 +382,9 @@ namespace Greenshot.Helpers } break; case ScreenCaptureMode.Fixed: - if (CoreConfig.ScreenToCapture > 0 && CoreConfig.ScreenToCapture <= Screen.AllScreens.Length) + if (_coreConfiguration.ScreenToCapture > 0 && _coreConfiguration.ScreenToCapture <= Screen.AllScreens.Length) { - _capture = WindowCapture.CaptureRectangle(_capture, Screen.AllScreens[CoreConfig.ScreenToCapture].Bounds); + _capture = WindowCapture.CaptureRectangle(_capture, Screen.AllScreens[_coreConfiguration.ScreenToCapture].Bounds); captureTaken = true; } break; @@ -497,9 +497,9 @@ namespace Greenshot.Helpers } break; case CaptureMode.LastRegion: - if (!CoreConfig.LastCapturedRegion.IsEmpty) + if (!_coreConfiguration.LastCapturedRegion.IsEmpty) { - _capture = WindowCapture.CaptureRectangle(_capture, CoreConfig.LastCapturedRegion); + _capture = WindowCapture.CaptureRectangle(_capture, _coreConfiguration.LastCapturedRegion); // Move cursor, fixing bug #3569703 _capture.MoveMouseLocation(_capture.ScreenBounds.Location.X - _capture.Location.X, _capture.ScreenBounds.Location.Y - _capture.Location.Y); @@ -559,7 +559,7 @@ namespace Greenshot.Helpers private void AddConfiguredDestination() { - foreach (var destinationDesignation in CoreConfig.OutputDestinations) + foreach (var destinationDesignation in _coreConfiguration.OutputDestinations) { var destination = _destinationHolder.SortedActiveDestinations.Find(destinationDesignation); if (destination != null) @@ -714,8 +714,8 @@ namespace Greenshot.Helpers return false; } // Fix for Bug #3430560 - CoreConfig.LastCapturedRegion = SelectedCaptureWindow.GetInfo().Bounds; - var returnValue = CaptureWindow(SelectedCaptureWindow, _capture, CoreConfig.WindowCaptureMode) != null; + _coreConfiguration.LastCapturedRegion = SelectedCaptureWindow.GetInfo().Bounds; + var returnValue = CaptureWindow(SelectedCaptureWindow, _capture, _coreConfiguration.WindowCaptureMode) != null; return returnValue; } @@ -802,7 +802,7 @@ namespace Greenshot.Helpers // 3) Otherwise use GDI (Screen might be also okay but might lose content) if (isAutoMode) { - if (CoreConfig.IECapture && _captureSupportInfo.InternetExplorerCaptureHelper.IsIeWindow(windowToCapture)) + if (_coreConfiguration.IECapture && _captureSupportInfo.InternetExplorerCaptureHelper.IsIeWindow(windowToCapture)) { try { @@ -1032,7 +1032,7 @@ namespace Greenshot.Helpers // } //} - using (var captureForm = new CaptureForm(CoreConfig, _capture, _windows, _captureSupportInfo.FormEnhancers)) + using (var captureForm = new CaptureForm(_coreConfiguration, _capture, _windows, _captureSupportInfo.FormEnhancers)) { // Make sure the form is hidden after showing, even if an exception occurs, so all errors will be shown DialogResult result; @@ -1086,7 +1086,7 @@ namespace Greenshot.Helpers // save for re-capturing later and show recapture context menu option // Important here is that the location needs to be offsetted back to screen coordinates! var tmpRectangle = _captureRect.Offset(_capture.ScreenBounds.Location.X, _capture.ScreenBounds.Location.Y); - CoreConfig.LastCapturedRegion = tmpRectangle; + _coreConfiguration.LastCapturedRegion = tmpRectangle; HandleCapture(); } }