Skip to content

Commit a31167e

Browse files
committed
Merge pull request #981 from wise0704/ImageManager
Fixes to the bug of image colors not adjusting according to the theme.
2 parents 79dea0f + 91afcdd commit a31167e

File tree

8 files changed

+257
-109
lines changed

8 files changed

+257
-109
lines changed

External/Plugins/CodeRefactor/RefactorItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public RefactorItem(ToolStripItem item)
2525
this.item = item;
2626
label = Regex.Replace(item.Text, "[&.]", string.Empty);
2727
description = TextHelper.GetString("Label.Refactor").Replace("&", string.Empty);
28-
icon = new Bitmap(item.Image ?? PluginBase.MainForm.FindImage("452")); //452 or 473
28+
icon = (Bitmap) (item.Image ?? PluginBase.MainForm.FindImage("452")); //452 or 473
2929
}
3030

3131
public string Description

External/Plugins/ProjectManager/Controls/Icons.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public static FDImage GetResource(string resourceID)
208208
catch {
209209
image = new Bitmap(16, 16);
210210
}
211-
image = (Bitmap)PluginBase.MainForm.ImageSetAdjust(image);
211+
image = (Bitmap)PluginBase.MainForm.GetAutoAdjustedImage(image);
212212
imageList.Images.Add(ScaleHelper.Scale(image));
213213
return new FDImage(image,imageList.Images.Count-1);
214214
}
@@ -255,7 +255,7 @@ public static FDImage ExtractIconIfNecessary(string file)
255255
{
256256
Icon icon = IconExtractor.GetFileIcon(file, true);
257257
Image image = ScaleHelper.Scale(icon.ToBitmap());
258-
image = (Bitmap)PluginBase.MainForm.ImageSetAdjust(image);
258+
image = (Bitmap)PluginBase.MainForm.GetAutoAdjustedImage(image);
259259
icon.Dispose(); imageList.Images.Add(image);
260260
int index = imageList.Images.Count - 1; // of the icon we just added
261261
FDImage fdImage = new FDImage(image, index);

External/Plugins/SourceControl/Managers/OverlayManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static public void Init()
174174

175175
private static Image GetSkin()
176176
{
177-
return PluginBase.MainForm.ImageSetAdjust(ProjectWatcher.Skin); //can be changed by external SC-Plugin
177+
return PluginBase.MainForm.GetAutoAdjustedImage(ProjectWatcher.Skin); //can be changed by external SC-Plugin
178178
}
179179

180180
static public void Reset()

FlashDevelop/MainForm.cs

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,11 +1681,58 @@ public IPlugin FindPlugin(String guid)
16811681
}
16821682

16831683
/// <summary>
1684-
/// Adjusts the image for different themes
1684+
/// Finds the specified composed/ready image that is automatically adjusted according to the theme.
1685+
/// <para/>
1686+
/// If you make a copy of the image returned by this method, the copy will not be automatically adjusted.
1687+
/// </summary>
1688+
public Image FindImage(String data)
1689+
{
1690+
return FindImage(data, true);
1691+
}
1692+
1693+
/// <summary>
1694+
/// Finds the specified composed/ready image.
1695+
/// <para/>
1696+
/// If you make a copy of the image returned by this method, the copy will not be automatically adjusted, even if <code>autoAdjusted</code> is <code>true</code>.
1697+
/// </summary>
1698+
public Image FindImage(String data, Boolean autoAdjusted)
1699+
{
1700+
try
1701+
{
1702+
lock (this)
1703+
{
1704+
return ImageManager.GetComposedBitmap(data, autoAdjusted);
1705+
}
1706+
}
1707+
catch (Exception ex)
1708+
{
1709+
ErrorManager.ShowError(ex);
1710+
return null;
1711+
}
1712+
}
1713+
1714+
/// <summary>
1715+
/// Returns a copy of the specified image that has its color adjusted.
16851716
/// </summary>
16861717
public Image ImageSetAdjust(Image image)
16871718
{
1688-
return ImageManager.AdjustImage(image);
1719+
return ImageManager.SetImageAdjustment(image);
1720+
}
1721+
1722+
/// <summary>
1723+
/// Gets a copy of the image that gets automatically adjusted according to the theme.
1724+
/// </summary>
1725+
public Image GetAutoAdjustedImage(Image image)
1726+
{
1727+
return ImageManager.GetAutoAdjustedImage(image);
1728+
}
1729+
1730+
/// <summary>
1731+
/// Adjusts all images for different themes.
1732+
/// </summary>
1733+
public void AdjustAllImages()
1734+
{
1735+
ImageManager.AdjustAllImages();
16891736
}
16901737

16911738
/// <summary>
@@ -1831,25 +1878,6 @@ public void ApplySecondaryShortcut(ToolStripItem item)
18311878
ShortcutManager.ApplySecondaryShortcut(item);
18321879
}
18331880

1834-
/// <summary>
1835-
/// Finds the specified composed/ready image
1836-
/// </summary>
1837-
public Image FindImage(String data)
1838-
{
1839-
try
1840-
{
1841-
lock (this)
1842-
{
1843-
return ImageManager.GetComposedBitmap(data);
1844-
}
1845-
}
1846-
catch (Exception ex)
1847-
{
1848-
ErrorManager.ShowError(ex);
1849-
return null;
1850-
}
1851-
}
1852-
18531881
/// <summary>
18541882
/// Shows the settings dialog
18551883
/// </summary>

FlashDevelop/Managers/ImageManager.cs

Lines changed: 159 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,23 @@
77

88
namespace FlashDevelop.Managers
99
{
10-
class ImageManager
10+
static class ImageManager
1111
{
12-
public static Int32 X;
13-
public static Int32 Y;
14-
public static Int32 Size;
15-
public static Int32 Icon;
16-
public static Int32 Bullet;
17-
public static Int32 Padding;
18-
public static Bitmap Source;
19-
public static Dictionary<String, Bitmap> Cache;
12+
static readonly int Size;
13+
static readonly int Padding;
14+
static readonly Bitmap Source;
15+
static readonly Dictionary<string, ImagePair> Cache;
16+
static readonly List<ImagePair> AutoAdjusted;
2017

2118
/// <summary>
2219
/// Static constructor
2320
/// </summary>
2421
static ImageManager()
2522
{
26-
Double scale = ScaleHelper.GetScale();
27-
Cache = new Dictionary<String, Bitmap>();
23+
double scale = ScaleHelper.GetScale();
24+
Cache = new Dictionary<string, ImagePair>();
25+
AutoAdjusted = new List<ImagePair>();
26+
2827
if (scale >= 1.5)
2928
{
3029
Size = 32;
@@ -37,80 +36,180 @@ static ImageManager()
3736
Padding = 0;
3837
Source = new Bitmap(FileNameHelper.Images);
3938
}
40-
Source = (Bitmap)AdjustImage(Source);
4139
}
4240

4341
/// <summary>
44-
/// Adjusts the image for different themes
42+
/// Composes an icon from image data.
4543
/// </summary>
46-
public static Image AdjustImage(Image image)
44+
public static Image GetComposedBitmap(string data, bool autoAdjusted)
4745
{
48-
String style = Globals.MainForm.GetThemeValue("ImageManager.ImageSet");
49-
if (style == "Bright") return ImageKonverter.ImageAdjust(image, 20, 0);
50-
else if (style == "Dim") return ImageKonverter.ImageAdjust(image, -5, -2);
51-
else if (style == "Dark") return ImageKonverter.ImageAdjust(image, -5, -10);
52-
else if (style == "Darker") return ImageKonverter.ImageAdjust(image, -20, -20);
53-
else if (style == "Black") return ImageKonverter.ImageAdjust(image, -50, -25);
54-
else return image;
46+
if (!Cache.ContainsKey(data))
47+
{
48+
int x, y, icon, bullet, rx, ry;
49+
var original = new Bitmap(Size, Size);
50+
var graphics = Graphics.FromImage(original);
51+
var destRect = new Rectangle(Padding, Padding, Size - (Padding * 2), Size - (Padding * 2));
52+
53+
ProcessImageData(data, out x, out y, out icon, out bullet);
54+
graphics.Clear(Color.Transparent);
55+
56+
if (icon >= 0)
57+
{
58+
rx = (icon % 16) * Size;
59+
ry = (icon / 16) * Size;
60+
graphics.DrawImage(Source, destRect, new Rectangle(rx, ry, Size, Size), GraphicsUnit.Pixel);
61+
}
62+
if (bullet >= 0)
63+
{
64+
rx = (bullet % 16) * Size;
65+
ry = (bullet / 16) * Size;
66+
destRect.X += (Size == 32) ? x * 2 : x;
67+
destRect.Y += (Size == 32) ? y * 2 : y;
68+
graphics.DrawImage(Source, destRect, new Rectangle(rx, ry, Size, Size), GraphicsUnit.Pixel);
69+
}
70+
71+
graphics.Dispose();
72+
original = ScaleHelper.Scale(original);
73+
Cache[data] = new ImagePair(original);
74+
}
75+
76+
77+
if (autoAdjusted)
78+
{
79+
var imagePair = Cache[data];
80+
return imagePair.Adjusted ?? AddAutoAdjustImage(imagePair);
81+
}
82+
return Cache[data].Original;
5583
}
5684

5785
/// <summary>
58-
/// Composes an icon from Image data
86+
/// Gets an adjusted copy of the specified image.
5987
/// </summary>
60-
public static Bitmap GetComposedBitmap(String data)
88+
public static Image SetImageAdjustment(Image original)
6189
{
62-
if (Cache.ContainsKey(data))
90+
int saturation, brightness;
91+
if (GetImageAdjustments(out saturation, out brightness))
6392
{
64-
return Cache[data];
93+
return ImageKonverter.ImageAdjust(original, saturation, brightness);
6594
}
66-
ProcessImageData(data);
67-
Bitmap composed = new Bitmap(Size, Size);
68-
Graphics destination = Graphics.FromImage(composed);
69-
destination.Clear(Color.Transparent);
70-
Int32 rx; Int32 ry;
71-
if (Icon >= 0)
72-
{
73-
rx = (Icon % 16) * Size;
74-
ry = (Icon / 16) * Size;
75-
destination.DrawImage(Source, new Rectangle(Padding, Padding, Size - (Padding * 2), Size - (Padding * 2)), new Rectangle(rx, ry, Size, Size), GraphicsUnit.Pixel);
76-
}
77-
if (Bullet >= 0)
95+
return new Bitmap(original);
96+
}
97+
98+
/// <summary>
99+
/// Gets a copy of the image that changes color according to the theme.
100+
/// </summary>
101+
public static Image GetAutoAdjustedImage(Image image)
102+
{
103+
return AddAutoAdjustImage(new ImagePair(image));
104+
}
105+
106+
/// <summary>
107+
/// Adjust colors of all cached images.
108+
/// </summary>
109+
public static void AdjustAllImages()
110+
{
111+
int saturation, brightness;
112+
GetImageAdjustments(out saturation, out brightness);
113+
114+
for (int i = 0, length = AutoAdjusted.Count; i < length; i++)
78115
{
79-
rx = (Bullet % 16) * Size;
80-
ry = (Bullet / 16) * Size;
81-
X = (Size == 32) ? X * 2 : X;
82-
Y = (Size == 32) ? Y * 2 : Y;
83-
destination.DrawImage(Source, new Rectangle(X + Padding, Y + Padding, Size - (Padding * 2), Size - (Padding * 2)), new Rectangle(rx, ry, Size, Size), GraphicsUnit.Pixel);
116+
var imagePair = AutoAdjusted[i];
117+
var adjusted = imagePair.Adjusted;
118+
if (adjusted == null)
119+
{
120+
AutoAdjusted.RemoveAt(i--);
121+
length--;
122+
}
123+
else ImageKonverter.ImageAdjust(imagePair.Original, adjusted, saturation, brightness);
84124
}
85-
composed = ScaleHelper.Scale(composed);
86-
Cache[data] = composed;
87-
return composed;
88125
}
89126

90127
/// <summary>
91128
/// Processes data from "icon|bullet|x|y" or just index
92129
/// </summary>
93-
private static void ProcessImageData(String data)
130+
static void ProcessImageData(string data, out int x, out int y, out int icon, out int bullet)
94131
{
95-
X = Y = 0;
96-
Icon = Bullet = -1;
132+
x = y = 0;
133+
icon = bullet = -1;
97134
if (string.IsNullOrEmpty(data)) return;
98-
String[] par = data.Split('|');
99-
if (par.Length > 0)
135+
136+
string[] args = data.Split('|');
137+
if (args.Length == 0 || !int.TryParse(args[0], out icon)) return;
138+
if (args.Length == 1 || !int.TryParse(args[1], out bullet)) return;
139+
if (bullet < 0 || args.Length < 4) return;
140+
int.TryParse(args[2], out x);
141+
int.TryParse(args[3], out y);
142+
}
143+
144+
/// <summary>
145+
/// Adds a pair to the update list.
146+
/// </summary>
147+
static Image AddAutoAdjustImage(ImagePair pair)
148+
{
149+
AutoAdjusted.Add(pair);
150+
return pair.Adjusted = SetImageAdjustment(pair.Original);
151+
}
152+
153+
/// <summary>
154+
/// Gets the appropriate color adjustment components.
155+
/// </summary>
156+
static bool GetImageAdjustments(out int saturation, out int brightness)
157+
{
158+
switch (Globals.MainForm.GetThemeValue("ImageManager.ImageSet"))
100159
{
101-
Int32.TryParse(par[0], out Icon);
102-
if (par.Length > 1)
103-
{
104-
Int32.TryParse(par[1], out Bullet);
105-
if (Bullet >= 0 && par.Length == 4)
106-
{
107-
Int32.TryParse(par[2], out X);
108-
Int32.TryParse(par[3], out Y);
109-
}
110-
}
160+
case "Bright": saturation = 20; brightness = 0; return true;
161+
case "Dim": saturation = -5; brightness = -2; return true;
162+
case "Dark": saturation = -5; brightness = -10; return true;
163+
case "Darker": saturation = -20; brightness = -20; return true;
164+
case "Black": saturation = -50; brightness = -25; return true;
165+
default: saturation = 0; brightness = 0; return false;
111166
}
112167
}
113168

169+
/// <summary>
170+
/// A pair of images used for tracking original and adjusted.
171+
/// </summary>
172+
class ImagePair
173+
{
174+
Image original;
175+
WeakReference adjusted;
176+
177+
/// <summary>
178+
/// The original image.
179+
/// </summary>
180+
public Image Original
181+
{
182+
get { return original; }
183+
}
184+
185+
/// <summary>
186+
/// The copy of <see cref="Original"/> that changes color according to the theme.
187+
/// </summary>
188+
public Image Adjusted
189+
{
190+
get { return adjusted.Target as Image; }
191+
set { adjusted.Target = value; }
192+
}
193+
194+
/// <summary>
195+
/// Creates an instance of <see cref="ImagePair"/>.
196+
/// </summary>
197+
/// <param name="original"><see cref="Original"/></param>
198+
public ImagePair(Image original) : this(original, null)
199+
{
200+
}
201+
202+
/// <summary>
203+
/// Creates an instance of <see cref="ImagePair"/>.
204+
/// </summary>
205+
/// <param name="original"><see cref="Original"/></param>
206+
/// <param name="adjusted"><see cref="Adjusted"/></param>
207+
public ImagePair(Image original, Image adjusted)
208+
{
209+
this.original = original;
210+
this.adjusted = new WeakReference(adjusted);
211+
}
212+
}
114213
}
115214

116215
}

FlashDevelop/Managers/ThemeManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public static void WalkControls(Object obj)
117117
{
118118
NotifyEvent ne = new NotifyEvent(EventType.ApplyTheme);
119119
EventManager.DispatchEvent(Globals.MainForm, ne);
120+
Globals.MainForm.AdjustAllImages();
120121
Globals.MainForm.Refresh();
121122
}
122123
}

0 commit comments

Comments
 (0)