Skip to content

Commit

Permalink
Merge pull request #2206 from cwensley/curtis/mac-fix-template-image-…
Browse files Browse the repository at this point in the history
…on-graphics

Mac: Fix flipping when drawing template images on Graphics
  • Loading branch information
cwensley authored Apr 29, 2022
2 parents 04b0245 + 4d1e274 commit 17abeb9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/Eto.Mac/Drawing/ImageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ public virtual void DrawTemplateImage(GraphicsHandler graphics, RectangleF sourc
var ctx = graphics.Control;
ctx.SaveState();

if (graphics.GraphicsContext.IsFlipped)
{
// draw flipped
ctx.ConcatCTM(new CGAffineTransform(1, 0, 0, -1, 0, graphics.ViewHeight));
destination.Y = graphics.ViewHeight - destination.Y - destination.Height;
}

RectangleF destMask;
if (destination.Size != source.Size)
{
Expand All @@ -87,25 +94,21 @@ public virtual void DrawTemplateImage(GraphicsHandler graphics, RectangleF sourc
// just position
destMask = new RectangleF(destination.Location - source.Location, imageSize);
}

var destRect = destination.ToNS();
var cgImage = GetImage().AsCGImage(ref destRect, graphics.GraphicsContext, null);

// clip to the image as a mask, using only alpha channel
ctx.ClipToMask(destMask.ToNS(), cgImage);

// set fill color based on current dark/light theme
// set fill color based on current dark/light theme
// this is the best approximation I can find to get it to draw the same as NSImageView
// thus far..
NSColor color;
if (MacVersion.IsAtLeast(10, 14) && graphics.DisplayView.HasDarkTheme())
{
if (graphics.DisplayView.HasDarkTheme())
color = NSColor.FromWhite(1f, .55f);
}
else
{
color = NSColor.FromWhite(0, .5f);
}
color.SetFill();

ctx.FillRect(destRect);
Expand Down
5 changes: 4 additions & 1 deletion test/Eto.Test.Mac/UnitTests/BitmapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ public void TemplateImagesShouldDrawCorrectly(string color)

g.DrawRectangle(Colors.Black, 0.5f, 0.5f, 199, 199);
g.FillRectangle(Colors.Black, 59, 59, 80, 80);


var path = new GraphicsPath();
g.DrawPolygon(Colors.Black, new PointF(100, 20), new PointF(180, 180), new PointF(20, 180));

}

var drawable = new Drawable { Size = bmp.Size };
Expand Down

0 comments on commit 17abeb9

Please sign in to comment.