From e0c9fc1f7b2c70b8495bbe060abd297199c61afe Mon Sep 17 00:00:00 2001 From: RKrom Date: Tue, 11 Nov 2014 14:13:13 +0100 Subject: [PATCH] Previous changes created problems with the context-menu, as it was still referencing an icon that we dispose at icon size change. This change introduces a PropertyChanged event object to the CoreConfiguration so those interested can register to changes (currently only the IconSize), and apply the correct menu image. --- Greenshot/Forms/MainForm.cs | 15 ++-- Greenshot/Forms/SettingsForm.cs | 7 -- .../ExternalCommandPlugin.cs | 36 +++++++--- GreenshotPlugin/Core/CoreConfiguration.cs | 71 ++++++++++--------- GreenshotPlugin/Core/PluginUtils.cs | 33 ++++++--- 5 files changed, 96 insertions(+), 66 deletions(-) diff --git a/Greenshot/Forms/MainForm.cs b/Greenshot/Forms/MainForm.cs index 3249482d6..224d0fb49 100644 --- a/Greenshot/Forms/MainForm.cs +++ b/Greenshot/Forms/MainForm.cs @@ -388,7 +388,8 @@ namespace Greenshot { } SoundHelper.Initialize(); - MainForm.ResetImageScalingSize(); + coreConfiguration.PropertyChanged += OnIconSizeChanged; + OnIconSizeChanged(this, new PropertyChangedEventArgs("IconSize")); // Set the Greenshot icon visibility depending on the configuration. (Added for feature #3521446) // Setting it to true this late prevents Problems with the context menu @@ -552,12 +553,14 @@ namespace Greenshot { } /// - /// Reset ImageScalingSize + /// Fix icon reference /// - /// Used for fixing scaling issues - public static void ResetImageScalingSize() { - MainForm thisForm = MainForm.Instance; - thisForm.contextMenu.ImageScalingSize = coreConfiguration.IconSize; + /// + /// + private void OnIconSizeChanged(object sender, PropertyChangedEventArgs e) { + if (e.PropertyName == "IconSize") { + contextMenu.ImageScalingSize = coreConfiguration.IconSize; + } } /// diff --git a/Greenshot/Forms/SettingsForm.cs b/Greenshot/Forms/SettingsForm.cs index cbbb61b8a..c823bb143 100644 --- a/Greenshot/Forms/SettingsForm.cs +++ b/Greenshot/Forms/SettingsForm.cs @@ -368,7 +368,6 @@ namespace Greenshot { numericUpDown_daysbetweencheck.Value = coreConfiguration.UpdateCheckInterval; numericUpDown_daysbetweencheck.Enabled = !coreConfiguration.Values["UpdateCheckInterval"].IsFixed; - coreConfiguration.FixIconSize(); numericUpdownIconSize.Value = (coreConfiguration.IconSize.Width /16) * 16; CheckDestinationSettings(); } @@ -417,12 +416,6 @@ namespace Greenshot { Size previousValue = coreConfiguration.IconSize; coreConfiguration.IconSize = new Size((int)numericUpdownIconSize.Value, (int)numericUpdownIconSize.Value); - // Clear caches when changing the settings - if (previousValue != coreConfiguration.IconSize) { - PluginUtils.ClearExeIconCache(); - MainForm.ResetImageScalingSize(); - } - coreConfiguration.FixIconSize(); try { if (checkbox_autostartshortcut.Checked) { // It's checked, so we set the RunUser if the RunAll isn't set. diff --git a/GreenshotExternalCommandPlugin/ExternalCommandPlugin.cs b/GreenshotExternalCommandPlugin/ExternalCommandPlugin.cs index fd692ca33..74e5bcdf4 100644 --- a/GreenshotExternalCommandPlugin/ExternalCommandPlugin.cs +++ b/GreenshotExternalCommandPlugin/ExternalCommandPlugin.cs @@ -24,6 +24,7 @@ using Greenshot.Plugin; using GreenshotPlugin.Core; using System; using System.Collections.Generic; +using System.ComponentModel; using System.IO; using System.Windows.Forms; @@ -33,6 +34,7 @@ namespace ExternalCommand { /// public class ExternalCommandPlugin : IGreenshotPlugin { private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ExternalCommandPlugin)); + private static CoreConfiguration coreConfig = IniConfig.GetIniSection(); private static ExternalCommandConfiguration config = IniConfig.GetIniSection(); private IGreenshotHost host; private PluginAttribute myAttributes; @@ -121,24 +123,36 @@ namespace ExternalCommand { itemPlugInRoot = new ToolStripMenuItem(); - itemPlugInRoot.Text = Language.GetString("externalcommand", "contextmenu_configure"); itemPlugInRoot.Tag = host; - try { - string exePath = PluginUtils.GetExePath("cmd.exe"); - if (exePath != null && File.Exists(exePath)) { - itemPlugInRoot.Image = PluginUtils.GetCachedExeIcon(exePath, 0); - } - } catch (Exception ex) { - LOG.Warn("Couldn't get the cmd.exe image", ex); - } + OnIconSizeChanged(this, new PropertyChangedEventArgs("IconSize")); + OnLanguageChanged(this, null); itemPlugInRoot.Click += new System.EventHandler(ConfigMenuClick); PluginUtils.AddToContextMenu(host, itemPlugInRoot); - Language.LanguageChanged += new LanguageChangedHandler(OnLanguageChanged); + Language.LanguageChanged += OnLanguageChanged; + coreConfig.PropertyChanged += OnIconSizeChanged; return true; } - public void OnLanguageChanged(object sender, EventArgs e) { + /// + /// Fix icon reference + /// + /// + /// + private void OnIconSizeChanged(object sender, PropertyChangedEventArgs e) { + if (e.PropertyName == "IconSize") { + try { + string exePath = PluginUtils.GetExePath("cmd.exe"); + if (exePath != null && File.Exists(exePath)) { + itemPlugInRoot.Image = PluginUtils.GetCachedExeIcon(exePath, 0); + } + } catch (Exception ex) { + LOG.Warn("Couldn't get the cmd.exe image", ex); + } + } + } + + private void OnLanguageChanged(object sender, EventArgs e) { if (itemPlugInRoot != null) { itemPlugInRoot.Text = Language.GetString("externalcommand", "contextmenu_configure"); } diff --git a/GreenshotPlugin/Core/CoreConfiguration.cs b/GreenshotPlugin/Core/CoreConfiguration.cs index c5e911712..8a11d5837 100644 --- a/GreenshotPlugin/Core/CoreConfiguration.cs +++ b/GreenshotPlugin/Core/CoreConfiguration.cs @@ -18,14 +18,16 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -using System; -using System.Collections.Generic; -using System.Drawing; -using System.IO; -using System.Windows.Forms; + using Greenshot.IniFile; using Greenshot.Plugin; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Drawing; +using System.IO; using System.Reflection; +using System.Windows.Forms; namespace GreenshotPlugin.Core { public enum ClipboardFormat { @@ -56,7 +58,9 @@ namespace GreenshotPlugin.Core { /// Description of CoreConfiguration. /// [IniSection("Core", Description="Greenshot core configuration")] - public class CoreConfiguration : IniSection { + public class CoreConfiguration : IniSection, INotifyPropertyChanged { + public event PropertyChangedEventHandler PropertyChanged; + [IniProperty("Language", Description = "The language in IETF format (e.g. en-US)")] public string Language; @@ -256,8 +260,36 @@ namespace GreenshotPlugin.Core { [IniProperty("LastCapturedRegion", Description = "The last used region, for reuse in the capture last region")] public Rectangle LastCapturedRegion; + private Size _iconSize; [IniProperty("IconSize", Description = "Defines the size of the icons (e.g. for the buttons in the editor), default value 16,16 anything bigger will cause scaling", DefaultValue = "16,16")] - public Size IconSize; + public Size IconSize { + get { + return _iconSize; + } + set { + Size newSize = value; + if (newSize != Size.Empty) { + if (newSize.Width < 16) { + newSize.Width = 16; + } else if (newSize.Width > 256) { + newSize.Width = 256; + } + newSize.Width = (newSize.Width / 16) * 16; + if (newSize.Height < 16) { + newSize.Height = 16; + } else if (IconSize.Height > 256) { + newSize.Height = 256; + } + newSize.Height = (newSize.Height / 16) * 16; + } + if (_iconSize != newSize) { + _iconSize = value; + if (PropertyChanged != null) { + PropertyChanged(this, new PropertyChangedEventArgs("IconSize")); + } + } + } + } // Specifies what THIS build is public BuildStates BuildState = BuildStates.RELEASE_CANDIDATE; @@ -441,32 +473,7 @@ namespace GreenshotPlugin.Core { if (OutputFileReduceColorsTo > 256) { OutputFileReduceColorsTo = 256; } - FixIconSize(); } - /// - /// Validation & correction of the icon size - /// - public void FixIconSize() { - if (IconSize == Size.Empty) { - IconSize = new Size(16, 16); - } else { - if (IconSize.Width < 16) { - IconSize.Width = 16; - } - if (IconSize.Width > 256) { - IconSize.Width = 256; - } - IconSize.Width = (IconSize.Width / 16) * 16; - if (IconSize.Height < 16) { - IconSize.Height = 16; - } - if (IconSize.Height > 256) { - IconSize.Height = 256; - } - IconSize.Height = (IconSize.Height/16)*16; - } - - } } } \ No newline at end of file diff --git a/GreenshotPlugin/Core/PluginUtils.cs b/GreenshotPlugin/Core/PluginUtils.cs index 3eefcf11b..63467f59f 100644 --- a/GreenshotPlugin/Core/PluginUtils.cs +++ b/GreenshotPlugin/Core/PluginUtils.cs @@ -26,6 +26,7 @@ using log4net; using Microsoft.Win32; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Drawing; using System.IO; using System.Windows.Forms; @@ -40,6 +41,10 @@ namespace GreenshotPlugin.Core { private const string PATH_KEY = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\"; private static IDictionary exeIconCache = new Dictionary(); + static PluginUtils() { + conf.PropertyChanged += OnIconSizeChanged; + } + /// /// Simple global property to get the Greenshot host /// @@ -48,18 +53,26 @@ namespace GreenshotPlugin.Core { set; } - public static void ClearExeIconCache() { - List cachedImages = new List(); - lock (exeIconCache) { - foreach (string key in exeIconCache.Keys) { - cachedImages.Add(exeIconCache[key]); + /// + /// Clear icon cache + /// + /// + /// + private static void OnIconSizeChanged(object sender, PropertyChangedEventArgs e) { + if (e.PropertyName == "IconSize") { + List cachedImages = new List(); + lock (exeIconCache) { + foreach (string key in exeIconCache.Keys) { + cachedImages.Add(exeIconCache[key]); + } + exeIconCache.Clear(); } - exeIconCache.Clear(); - } - foreach (Image cachedImage in cachedImages) { - if (cachedImage != null) { - cachedImage.Dispose(); + foreach (Image cachedImage in cachedImages) { + if (cachedImage != null) { + cachedImage.Dispose(); + } } + } }