Skip to content

Commit f90582e

Browse files
committed
Address Copilot feedback
1 parent d82463d commit f90582e

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

src/Skia/ImageCache.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,17 @@ public SKImage this[Uri uri]
2525
{
2626
lock (cacheLock)
2727
{
28-
if (images.ContainsKey(uri))
28+
if (images.TryGetValue(uri, out var image))
2929
{
30-
return images[uri];
30+
return image;
3131
}
32-
else
32+
33+
var img = CreateImage(uri);
34+
if (img != null)
3335
{
34-
var img = CreateImage(uri);
35-
if (img != null)
36-
{
37-
images.Add(uri, img);
38-
}
39-
return img;
36+
images.Add(uri, img);
4037
}
38+
return img;
4139
}
4240
}
4341
}

src/Skia/ThemePreviewRenderer.cs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@ internal class ThemePreviewRenderer
1616
private const byte OVERLAY_ALPHA = 127;
1717
private const float OPACITY_NORMAL = 0.5f;
1818
private const float OPACITY_HOVER = 1.0f;
19-
private const float OPACITY_MESSAGE = 0.8f;
19+
20+
// FontAwesome icons
21+
private const string ICON_PLAY = "\uf04b";
22+
private const string ICON_PAUSE = "\uf04c";
23+
private const string ICON_CHEVRON_LEFT = "\uf053";
24+
private const string ICON_CHEVRON_RIGHT = "\uf054";
2025

2126
private readonly SKPaint basePaint;
22-
private readonly SKColor overlayColor;
27+
private readonly SKTypeface systemFont;
28+
private readonly SKTypeface systemFontBold;
2329
private readonly SKFont titleFont;
24-
private readonly SKFont previewFont;
2530
private readonly SKFont textFont;
2631
private readonly SKFont iconFont16;
2732
private readonly SKFont iconFont20;
33+
private readonly SKColor overlayColor;
2834
private readonly SKSamplingOptions samplingOptions;
2935

3036
// Hit test regions (updated during rendering)
@@ -39,15 +45,16 @@ internal class ThemePreviewRenderer
3945

4046
private enum Side { Left, Right }
4147

42-
public ThemePreviewRenderer(SKTypeface fontAwesome)
48+
public ThemePreviewRenderer(SKTypeface fontAwesome, string systemFontName)
4349
{
4450
basePaint = new SKPaint { IsAntialias = true };
45-
overlayColor = new SKColor(0, 0, 0, OVERLAY_ALPHA);
46-
titleFont = new SKFont(SKTypeface.FromFamilyName("Segoe UI", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright), 19);
47-
previewFont = new SKFont(SKTypeface.FromFamilyName("Segoe UI", SKFontStyleWeight.Normal, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright), 16);
48-
textFont = new SKFont(SKTypeface.FromFamilyName("Segoe UI"), 16);
51+
systemFont = SKTypeface.FromFamilyName(systemFontName);
52+
systemFontBold = SKTypeface.FromFamilyName(systemFontName, SKFontStyle.Bold);
53+
titleFont = new SKFont(systemFontBold, 19);
54+
textFont = new SKFont(systemFont, 16);
4955
iconFont16 = new SKFont(fontAwesome, 16);
5056
iconFont20 = new SKFont(fontAwesome, 20);
57+
overlayColor = new SKColor(0, 0, 0, OVERLAY_ALPHA);
5158
samplingOptions = new SKSamplingOptions(SKCubicResampler.Mitchell);
5259
}
5360

@@ -64,12 +71,12 @@ public void DrawImage(SKCanvas canvas, SKImage image, SKImageInfo info, float op
6471
{
6572
// Apply opacity with color filter
6673
using (var paint = new SKPaint())
74+
using (var colorFilter = SKColorFilter.CreateBlendMode(
75+
SKColors.White.WithAlpha((byte)(255 * opacity)),
76+
SKBlendMode.DstIn))
6777
{
6878
paint.IsAntialias = true;
69-
paint.ColorFilter = SKColorFilter.CreateBlendMode(
70-
SKColors.White.WithAlpha((byte)(255 * opacity)),
71-
SKBlendMode.DstIn);
72-
79+
paint.ColorFilter = colorFilter;
7380
canvas.DrawImage(image, destRect, samplingOptions, paint);
7481
}
7582
}
@@ -87,10 +94,10 @@ public void DrawOverlay(SKCanvas canvas, SKImageInfo info, ThemePreviewerViewMod
8794
DrawArrowArea(canvas, info, Side.Right, hoveredItem == ThemePreviewer.HoveredItem.RightArrow);
8895

8996
// Title and preview text box (top left)
90-
var titleBounds = new SKRect();
97+
SKRect titleBounds;
9198
titleFont.MeasureText(viewModel.Title ?? "", out titleBounds);
92-
var previewBounds = new SKRect();
93-
previewFont.MeasureText(viewModel.PreviewText ?? "", out previewBounds);
99+
SKRect previewBounds;
100+
textFont.MeasureText(viewModel.PreviewText ?? "", out previewBounds);
94101

95102
float boxWidth = Math.Max(titleBounds.Width, previewBounds.Width) + MARGIN_STANDARD;
96103
float boxHeight = 19 + 4 + 16 + MARGIN_STANDARD;
@@ -101,7 +108,7 @@ public void DrawOverlay(SKCanvas canvas, SKImageInfo info, ThemePreviewerViewMod
101108

102109
basePaint.Color = SKColors.White;
103110
canvas.DrawText(viewModel.Title ?? "", TitleBoxRect.X + 10, TitleBoxRect.Y + 8 + 19, titleFont, basePaint);
104-
canvas.DrawText(viewModel.PreviewText ?? "", TitleBoxRect.X + 10, TitleBoxRect.Y + 8 + 19 + 5 + 16, previewFont, basePaint);
111+
canvas.DrawText(viewModel.PreviewText ?? "", TitleBoxRect.X + 10, TitleBoxRect.Y + 8 + 19 + 5 + 16, textFont, basePaint);
105112

106113
// Play/Pause button (top right)
107114
int playButtonSize = 40;
@@ -112,8 +119,8 @@ public void DrawOverlay(SKCanvas canvas, SKImageInfo info, ThemePreviewerViewMod
112119

113120
float playOpacity = hoveredItem == ThemePreviewer.HoveredItem.PlayButton ? OPACITY_HOVER : OPACITY_NORMAL;
114121
basePaint.Color = SKColors.White.WithAlpha((byte)(255 * playOpacity));
115-
string playIcon = viewModel.IsPlaying ? "\uf04c" : "\uf04b";
116-
var textBounds = new SKRect();
122+
string playIcon = viewModel.IsPlaying ? ICON_PAUSE : ICON_PLAY;
123+
SKRect textBounds;
117124
iconFont16.MeasureText(playIcon, out textBounds);
118125
float centerX = PlayButtonRect.X + PlayButtonRect.Width / 2;
119126
float centerY = PlayButtonRect.Y + PlayButtonRect.Height / 2;
@@ -128,7 +135,7 @@ public void DrawOverlay(SKCanvas canvas, SKImageInfo info, ThemePreviewerViewMod
128135
// Download message (centered bottom)
129136
if (!string.IsNullOrEmpty(viewModel.Message))
130137
{
131-
var msgBounds = new SKRect();
138+
SKRect msgBounds;
132139
textFont.MeasureText(viewModel.Message, out msgBounds);
133140
float msgWidth = msgBounds.Width + 16;
134141
float msgHeight = 6 + 16 + 6;
@@ -137,7 +144,7 @@ public void DrawOverlay(SKCanvas canvas, SKImageInfo info, ThemePreviewerViewMod
137144
basePaint.Color = overlayColor;
138145
canvas.DrawRoundRect(SKRect.Create(DownloadMessageRect.X, DownloadMessageRect.Y, DownloadMessageRect.Width, DownloadMessageRect.Height), BORDER_RADIUS, BORDER_RADIUS, basePaint);
139146

140-
float msgOpacity = hoveredItem == ThemePreviewer.HoveredItem.DownloadButton ? OPACITY_HOVER : OPACITY_MESSAGE;
147+
float msgOpacity = hoveredItem == ThemePreviewer.HoveredItem.DownloadButton ? OPACITY_HOVER : OPACITY_NORMAL;
141148
basePaint.Color = SKColors.White.WithAlpha((byte)(255 * msgOpacity));
142149
canvas.DrawText(viewModel.Message, DownloadMessageRect.X + 8, DownloadMessageRect.Y + 5 + 16, textFont, basePaint);
143150
}
@@ -166,8 +173,8 @@ private void DrawArrowArea(SKCanvas canvas, SKImageInfo info, Side side, bool is
166173

167174
basePaint.Color = SKColors.White.WithAlpha((byte)(255 * opacity));
168175

169-
string icon = side == Side.Left ? "\uf053" : "\uf054";
170-
var textBounds = new SKRect();
176+
string icon = side == Side.Left ? ICON_CHEVRON_LEFT : ICON_CHEVRON_RIGHT;
177+
SKRect textBounds;
171178
iconFont20.MeasureText(icon, out textBounds);
172179
canvas.DrawText(icon, x - textBounds.MidX, y - textBounds.MidY, iconFont20, basePaint);
173180
}
@@ -180,7 +187,7 @@ private void DrawCornerLabel(SKCanvas canvas, SKImageInfo info, string text, Sid
180187
return;
181188
}
182189

183-
var textBounds = new SKRect();
190+
SKRect textBounds;
184191
textFont.MeasureText(text, out textBounds);
185192

186193
var padding = new System.Windows.Forms.Padding(8, 4, 10, 10); // Left, Top, Right, Bottom
@@ -230,8 +237,9 @@ private void DrawCarouselIndicators(SKCanvas canvas, SKImageInfo info, int count
230237
public void Dispose()
231238
{
232239
basePaint?.Dispose();
240+
systemFont?.Dispose();
241+
systemFontBold?.Dispose();
233242
titleFont?.Dispose();
234-
previewFont?.Dispose();
235243
textFont?.Dispose();
236244
iconFont16?.Dispose();
237245
iconFont20?.Dispose();

src/Skia/ThemePreviewer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public ThemePreviewer()
5252
}
5353
}
5454

55-
renderer = new ThemePreviewRenderer(fontAwesome);
55+
renderer = new ThemePreviewRenderer(fontAwesome, Control.DefaultFont.FontFamily.Name);
5656

5757
// Timer for smooth fade animations
5858
fadeTimer = new Timer

0 commit comments

Comments
 (0)