Skip to content

Commit f88b1d1

Browse files
committed
use sergiye.Common IconFactory
1 parent c4e4eb0 commit f88b1d1

File tree

6 files changed

+30
-198
lines changed

6 files changed

+30
-198
lines changed

TrayRAMBooster/Engine/ComputerService.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010
namespace TrayRAMBooster {
1111

1212
internal class ComputerService {
13-
13+
14+
public static bool HasCombinedPageList => OperatingSystemHelper.IsWindows8OrGreater;
15+
public static bool HasModifiedPageList => OperatingSystemHelper.IsWindowsVistaOrGreater;
16+
public static bool HasProcessesWorkingSet => OperatingSystemHelper.IsWindowsXpOrGreater;
17+
public static bool HasStandbyList => OperatingSystemHelper.IsWindowsVistaOrGreater;
18+
public static bool HasSystemWorkingSet => OperatingSystemHelper.IsWindowsXpOrGreater;
19+
1420
private WindowsStructs.MemoryStatusEx memoryStatusEx;
1521

1622
public ComputerService() {
@@ -214,7 +220,7 @@ public void Optimize(Enums.MemoryAreas areas, Enums.OptimizationReason reason) {
214220
}
215221

216222
private static void OptimizeCombinedPageList() {
217-
if (!OperatingSystem.HasCombinedPageList)
223+
if (!HasCombinedPageList)
218224
throw new Exception("The Combined Page List optimization is not supported on this operating system version");
219225

220226
if (!SetIncreasePrivilege(Constants.Windows.Privilege.SeProfSingleProcessName))
@@ -243,7 +249,7 @@ private static void OptimizeCombinedPageList() {
243249
}
244250

245251
private static void OptimizeModifiedPageList() {
246-
if (!OperatingSystem.HasModifiedPageList)
252+
if (!HasModifiedPageList)
247253
throw new Exception("The Modified Page List optimization is not supported on this operating system version");
248254

249255
if (!SetIncreasePrivilege(Constants.Windows.Privilege.SeProfSingleProcessName))
@@ -271,7 +277,7 @@ private static void OptimizeModifiedPageList() {
271277
}
272278

273279
private static void OptimizeProcessesWorkingSet() {
274-
if (!OperatingSystem.HasProcessesWorkingSet)
280+
if (!HasProcessesWorkingSet)
275281
throw new Exception("The Processes Working Set optimization is not supported on this operating system version");
276282

277283
if (!SetIncreasePrivilege(Constants.Windows.Privilege.SeDebugName))
@@ -302,7 +308,7 @@ private static void OptimizeProcessesWorkingSet() {
302308
}
303309

304310
private static void OptimizeStandbyList(bool lowPriority = false) {
305-
if (!OperatingSystem.HasStandbyList)
311+
if (!HasStandbyList)
306312
throw new Exception("The Standby List optimization is not supported on this operating system version");
307313

308314
if (!SetIncreasePrivilege(Constants.Windows.Privilege.SeProfSingleProcessName))
@@ -332,7 +338,7 @@ private static void OptimizeStandbyList(bool lowPriority = false) {
332338
}
333339

334340
private static void OptimizeSystemWorkingSet() {
335-
if (!OperatingSystem.HasSystemWorkingSet)
341+
if (!HasSystemWorkingSet)
336342
throw new Exception("The System Working Set optimization is not supported on this operating system version");
337343

338344
if (!SetIncreasePrivilege(Constants.Windows.Privilege.SeIncreaseQuotaName))

TrayRAMBooster/Engine/ExtensionMethods.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Drawing;
43
using System.Linq;
54

65
namespace TrayRAMBooster {
@@ -34,20 +33,5 @@ public static string RemoveWhitespaces(this string value) {
3433
var mag = (int) Math.Log(value, 1024);
3534
return new KeyValuePair<double, Enums.MemoryUnit>(value / Math.Pow(1024, mag), (Enums.MemoryUnit) mag);
3635
}
37-
38-
public static bool IsDark(this Color color) {
39-
var brightness = (0.299 * color.R + 0.587 * color.G + 0.114 * color.B);
40-
return brightness < 186;
41-
}
42-
43-
public static string ToTrayValue(this double value) {
44-
double rounded1 = Math.Round(value, 1);
45-
return rounded1 < 10 ? rounded1.ToString("0.0") : Math.Round(value).ToString("0");
46-
}
47-
48-
public static void Destroy(this Icon icon) {
49-
NativeMethods.DestroyIcon(icon.Handle);
50-
icon.Dispose();
51-
}
5236
}
5337
}
Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using System;
2-
using System.ComponentModel;
3-
using System.Drawing;
4-
using System.Drawing.Imaging;
52
using System.Runtime.InteropServices;
63

74
namespace TrayRAMBooster {
@@ -14,10 +11,6 @@ internal static extern bool AdjustTokenPrivileges(IntPtr tokenHandle,
1411
[MarshalAs(UnmanagedType.Bool)] bool disableAllPrivileges, ref WindowsStructs.TokenPrivileges newState,
1512
int bufferLength, IntPtr previousState, IntPtr returnLength);
1613

17-
[DllImport("user32.dll", SetLastError = true)]
18-
[return: MarshalAs(UnmanagedType.Bool)]
19-
internal static extern bool DestroyIcon(IntPtr hIcon);
20-
2114
[DllImport("psapi.dll", SetLastError = true)]
2215
internal static extern bool EmptyWorkingSet(IntPtr hProcess);
2316

@@ -37,58 +30,5 @@ internal static extern bool AdjustTokenPrivileges(IntPtr tokenHandle,
3730
[return: MarshalAs(UnmanagedType.Bool)]
3831
internal static extern bool SetSystemFileCacheSize(IntPtr minimumFileCacheSize, IntPtr maximumFileCacheSize,
3932
int flags);
40-
41-
private struct APPBARDATA {
42-
public int cbSize;
43-
public IntPtr hWnd;
44-
public int uCallbackMessage;
45-
public int uEdge;
46-
public RECT rc;
47-
public IntPtr lParam;
48-
}
49-
50-
private struct RECT {
51-
public int left, top, right, bottom;
52-
}
53-
54-
private const int ABM_GETTASKBARPOS = 5;
55-
56-
[DllImport("shell32.dll")]
57-
private static extern IntPtr SHAppBarMessage(int msg, ref APPBARDATA data);
58-
59-
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
60-
private static extern int BitBlt(IntPtr hDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc,
61-
int ySrc, int dwRop);
62-
63-
private static Rectangle GetTaskbarPosition() {
64-
var data = new APPBARDATA();
65-
data.cbSize = Marshal.SizeOf(data);
66-
67-
var retval = SHAppBarMessage(ABM_GETTASKBARPOS, ref data);
68-
if (retval == IntPtr.Zero) {
69-
throw new Win32Exception("Please re-install Windows");
70-
}
71-
72-
return new Rectangle(data.rc.left, data.rc.top, data.rc.right - data.rc.left, data.rc.bottom - data.rc.top);
73-
}
74-
75-
private static Color GetColorAt(Point location) {
76-
using (var screenPixel = new Bitmap(1, 1, PixelFormat.Format32bppArgb))
77-
using (var gDst = Graphics.FromImage(screenPixel)) {
78-
using (var gSrc = Graphics.FromHwnd(IntPtr.Zero)) {
79-
var hSrcDC = gSrc.GetHdc();
80-
var hDC = gDst.GetHdc();
81-
var retVal = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, (int) CopyPixelOperation.SourceCopy);
82-
gDst.ReleaseHdc();
83-
gSrc.ReleaseHdc();
84-
}
85-
86-
return screenPixel.GetPixel(0, 0);
87-
}
88-
}
89-
90-
public static Color GetTaskbarColor() {
91-
return GetColorAt(GetTaskbarPosition().Location);
92-
}
9333
}
9434
}

TrayRAMBooster/Engine/OperatingSystem.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

TrayRAMBooster/Engine/Settings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Drawing;
44
using System.Linq;
55
using Microsoft.Win32;
6+
using sergiye.Common;
67

78
namespace TrayRAMBooster {
89

@@ -32,8 +33,7 @@ static Settings() {
3233
showVirtualMemory = true;
3334
trayIconMode = Enums.TrayIconMode.MemoryAvailable;
3435
doubleClickAction = Enums.DoubleClickAction.Optimize;
35-
var taskbarColor = NativeMethods.GetTaskbarColor();
36-
trayIconValueColor = taskbarColor.IsDark()
36+
trayIconValueColor = WinApiHelper.IsTaskbarDark()
3737
? Color.Lime //Cyan / SpringGreen
3838
: Color.DarkGreen; //Teal / Green
3939

TrayRAMBooster/TrayApplicationContext.cs

Lines changed: 16 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
using System.ComponentModel;
33
using System.Diagnostics;
44
using System.Drawing;
5-
using System.Drawing.Drawing2D;
6-
using System.Drawing.Imaging;
7-
using System.Drawing.Text;
85
using System.Linq;
96
using System.Threading;
107
using System.Threading.Tasks;
@@ -20,6 +17,7 @@ internal class TrayApplicationContext : ApplicationContext {
2017

2118
private readonly Icon imageIcon;
2219
private readonly NotifyIcon notifyIcon;
20+
private readonly IconFactory iconFactory;
2321
private readonly SynchronizationContext uiContext;
2422
private readonly ComputerService computer;
2523
private readonly StartupManager startupManager;
@@ -30,10 +28,6 @@ internal class TrayApplicationContext : ApplicationContext {
3028
private ToolStripMenuItem autoOptimizeUsageMenu;
3129
private ToolStripMenuItem updateIntervalMenu;
3230
private ToolStripMenuItem optimizationTypesMenu;
33-
private readonly Bitmap bitmap;
34-
private readonly Graphics graphics;
35-
private readonly Font font;
36-
private readonly Font smallFont;
3731
private bool isBusy;
3832
private string iconValue;
3933
private DateTimeOffset lastRun;
@@ -87,24 +81,7 @@ public TrayApplicationContext() {
8781
};
8882
notifyIcon.ContextMenuStrip.Renderer = new ThemedToolStripRenderer();
8983
uiContext = SynchronizationContext.Current;
90-
91-
float dpiX, dpiY;
92-
using (var b = new Bitmap(1, 1, PixelFormat.Format32bppArgb)) {
93-
dpiX = b.HorizontalResolution;
94-
dpiY = b.VerticalResolution;
95-
}
96-
var width = Math.Max(16, (int)Math.Round(16 * dpiX / 96));
97-
var height = Math.Max(16, (int)Math.Round(16 * dpiY / 96));
98-
bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
99-
graphics = Graphics.FromImage(bitmap);
100-
if (OperatingSystem.IsWindowsXpOrGreater) {
101-
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
102-
graphics.SmoothingMode = SmoothingMode.AntiAlias;
103-
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
104-
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
105-
}
106-
font = new Font("Arial", width == 16 ? 10.0F : 8.0F * dpiX / 96);
107-
smallFont = new Font("Arial", width == 16 ? 8.0F : 6.0F * dpiX / 96);
84+
iconFactory = new IconFactory(Settings.TrayIconValueColor);
10885

10986
Updater.Subscribe(
11087
(message, isError) => {
@@ -202,65 +179,9 @@ private void UpdateIcon(bool force = false) {
202179
}
203180
else {
204181
try {
205-
var color = Settings.TrayIconValueColor;
206-
var iconBackColor = Color.Transparent;
207-
graphics.Clear(iconBackColor);
208-
209-
if (optimizationProgressPercentage > 0) {
210-
var rect = new Rectangle(1, 1, bitmap.Width - 2, bitmap.Height - 2);
211-
// var backColor = NativeMethods.GetTaskbarColor();
212-
// using (var b = new SolidBrush(backColor))
213-
// graphics.FillEllipse(b, rect);
214-
var sweepAngle = 360f * optimizationProgressPercentage / 100f;
215-
if (sweepAngle > 0) {
216-
using (var b = new SolidBrush(color))
217-
graphics.FillPie(b, rect, -90, sweepAngle);
218-
}
219-
using (var p = new Pen(color, 1))
220-
graphics.DrawEllipse(p, rect);
221-
222-
// var text = value.ToString(); //optimizationProgressPercentage.ToString();
223-
// using (var path = new GraphicsPath()) {
224-
// var emSize = graphics.DpiY * smallFont.Size / 72;
225-
// var format = new StringFormat {Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center};
226-
// path.AddString(text, smallFont.FontFamily, (int) smallFont.Style, emSize, new RectangleF(0, 0, bitmap.Width, bitmap.Height), format);
227-
// using (var outline = new Pen(fillColor, 2)) {
228-
// outline.LineJoin = LineJoin.Round;
229-
// graphics.DrawPath(outline, path);
230-
// }
231-
// using (var brush = new SolidBrush(backColor)) graphics.FillPath(brush, path);
232-
// }
233-
}
234-
else {
235-
var small = iconValue.Length > 2;
236-
if (small) {
237-
if (iconValue[1] == '.' || iconValue[1] == ',') {
238-
var bigPart = iconValue.Substring(0, 1);
239-
var smallPart = iconValue.Substring(1);
240-
TextRenderer.DrawText(graphics, bigPart, font, new Point(-bitmap.Width / 4, bitmap.Height / 2), color,
241-
iconBackColor, TextFormatFlags.VerticalCenter);
242-
TextRenderer.DrawText(graphics, smallPart, smallFont, new Point(bitmap.Width / 4, bitmap.Height), color,
243-
iconBackColor, TextFormatFlags.Bottom);
244-
}
245-
else {
246-
var size = TextRenderer.MeasureText(iconValue, smallFont);
247-
TextRenderer.DrawText(graphics, iconValue, smallFont,
248-
new Point((bitmap.Width - size.Width) / 2, bitmap.Height / 2), color, iconBackColor,
249-
TextFormatFlags.VerticalCenter);
250-
}
251-
}
252-
else {
253-
var size = TextRenderer.MeasureText(iconValue, font);
254-
TextRenderer.DrawText(graphics, iconValue, font,
255-
new Point((bitmap.Width - size.Width) / 2, bitmap.Height / 2), color, iconBackColor,
256-
TextFormatFlags.VerticalCenter);
257-
}
258-
}
259-
260-
var handle = bitmap.GetHicon();
261-
using (var icon = Icon.FromHandle(handle))
262-
SafeSetTrayIcon((Icon) icon.Clone());
263-
NativeMethods.DestroyIcon(handle);
182+
SafeSetTrayIcon(optimizationProgressPercentage > 0
183+
? iconFactory.CreatePercentagePieIcon(optimizationProgressPercentage)
184+
: iconFactory.CreateTransparentIcon(iconValue));
264185
}
265186
catch {
266187
SafeSetTrayIcon(imageIcon);
@@ -278,17 +199,17 @@ private void OnOptimizeProgressUpdate(byte value, string step) {
278199
}
279200

280201
private static Enums.MemoryAreas GetEnabledMemoryAreas() {
281-
if (!OperatingSystem.HasCombinedPageList)
202+
if (!ComputerService.HasCombinedPageList)
282203
Settings.MemoryAreas &= ~Enums.MemoryAreas.CombinedPageList;
283-
if (!OperatingSystem.HasModifiedPageList)
204+
if (!ComputerService.HasModifiedPageList)
284205
Settings.MemoryAreas &= ~Enums.MemoryAreas.ModifiedPageList;
285-
if (!OperatingSystem.HasProcessesWorkingSet)
206+
if (!ComputerService.HasProcessesWorkingSet)
286207
Settings.MemoryAreas &= ~Enums.MemoryAreas.ProcessesWorkingSet;
287-
if (!OperatingSystem.HasStandbyList) {
208+
if (!ComputerService.HasStandbyList) {
288209
Settings.MemoryAreas &= ~Enums.MemoryAreas.StandbyList;
289210
Settings.MemoryAreas &= ~Enums.MemoryAreas.StandbyListLowPriority;
290211
}
291-
if (!OperatingSystem.HasSystemWorkingSet)
212+
if (!ComputerService.HasSystemWorkingSet)
292213
Settings.MemoryAreas &= ~Enums.MemoryAreas.SystemWorkingSet;
293214
return Settings.MemoryAreas;
294215
}
@@ -540,23 +461,23 @@ private void AddMenuItems() {
540461

541462
#region Optimization types
542463
optimizationTypesMenu = new ToolStripMenuItem("Optimization types");
543-
if (OperatingSystem.HasProcessesWorkingSet)
464+
if (ComputerService.HasProcessesWorkingSet)
544465
optimizationTypesMenu.DropDownItems.Add(new ToolStripMenuItem("Processes working set", null, (_, _) => {
545466
ToggleMemoryArea(Enums.MemoryAreas.ProcessesWorkingSet);
546467
}) { Tag = Enums.MemoryAreas.ProcessesWorkingSet });
547-
if (OperatingSystem.HasSystemWorkingSet)
468+
if (ComputerService.HasSystemWorkingSet)
548469
optimizationTypesMenu.DropDownItems.Add(new ToolStripMenuItem("System working set", null, (_, _) => {
549470
ToggleMemoryArea(Enums.MemoryAreas.SystemWorkingSet);
550471
}) { Tag = Enums.MemoryAreas.SystemWorkingSet });
551-
if (OperatingSystem.HasCombinedPageList)
472+
if (ComputerService.HasCombinedPageList)
552473
optimizationTypesMenu.DropDownItems.Add(new ToolStripMenuItem("Combined page list", null, (_, _) => {
553474
ToggleMemoryArea(Enums.MemoryAreas.CombinedPageList);
554475
}) { Tag = Enums.MemoryAreas.CombinedPageList });
555-
if (OperatingSystem.HasModifiedPageList)
476+
if (ComputerService.HasModifiedPageList)
556477
optimizationTypesMenu.DropDownItems.Add(new ToolStripMenuItem("Modified page list", null, (_, _) => {
557478
ToggleMemoryArea(Enums.MemoryAreas.ModifiedPageList);
558479
}) { Tag = Enums.MemoryAreas.ModifiedPageList });
559-
if (OperatingSystem.HasStandbyList) {
480+
if (ComputerService.HasStandbyList) {
560481
optimizationTypesMenu.DropDownItems.Add(new ToolStripMenuItem("Standby list", null, (_, _) => {
561482
ToggleMemoryArea(Enums.MemoryAreas.StandbyList);
562483
}) { Tag = Enums.MemoryAreas.StandbyList });
@@ -704,10 +625,7 @@ protected override void Dispose(bool disposing) {
704625
components.Dispose();
705626
imageIcon?.Dispose();
706627
notifyIcon?.Dispose();
707-
graphics?.Dispose();
708-
bitmap?.Dispose();
709-
font?.Dispose();
710-
smallFont?.Dispose();
628+
iconFactory?.Dispose();
711629
GC.SuppressFinalize(this);
712630
}
713631

0 commit comments

Comments
 (0)