22using System . ComponentModel ;
33using System . Diagnostics ;
44using System . Drawing ;
5- using System . Drawing . Drawing2D ;
6- using System . Drawing . Imaging ;
7- using System . Drawing . Text ;
85using System . Linq ;
96using System . Threading ;
107using 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