Skip to content

Commit

Permalink
Merge pull request #2557 from cwensley/curtis/mac-fix-drawable-clipping
Browse files Browse the repository at this point in the history
Mac: Fix Drawable clipping to bounds on macOS Sonoma
  • Loading branch information
cwensley authored Sep 28, 2023
2 parents ac36212 + 1f933e8 commit c3fc227
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/Eto.Mac/Drawing/GraphicsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,18 @@ public GraphicsHandler()
}

#if OSX
public GraphicsHandler(NSView view, NSGraphicsContext graphicsContext, float height)
public GraphicsHandler(NSView view, NSGraphicsContext graphicsContext, float height, CGRect? dirtyRect)
{
DisplayView = view;
this.height = height;
var flipped = view?.IsFlipped ?? false;
this.graphicsContext = graphicsContext.IsFlipped ? graphicsContext : NSGraphicsContext.FromCGContext(graphicsContext.CGContext, true);
Control = this.graphicsContext.CGContext;

// Clip to bounds otherwise you can draw outside the control on macOS Sonoma
if (dirtyRect != null)
Control.ClipToRect(dirtyRect.Value);

InitializeContext(!flipped);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Eto.Mac/Forms/Cells/DrawableCellHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public override void DrawInteriorWithFrame(CGRect cellFrame, NSView inView)
}

var handler = Handler;
var graphicsHandler = new GraphicsHandler(inView, nscontext, (float)cellFrame.Height);
var graphicsHandler = new GraphicsHandler(inView, nscontext, (float)cellFrame.Height, cellFrame);
using (var graphics = new Graphics(graphicsHandler))
{
var state = Highlighted ? CellStates.Selected : CellStates.None;
Expand Down Expand Up @@ -167,7 +167,7 @@ public override void DrawRect(CGRect dirtyRect)
var handler = Handler;
if (handler == null)
return;
var graphicsHandler = new GraphicsHandler(this, nscontext, (float)cellFrame.Height);
var graphicsHandler = new GraphicsHandler(this, nscontext, (float)cellFrame.Height, cellFrame);
using (var graphics = new Graphics(graphicsHandler))
{
var rowView = this.Superview as NSTableRowView;
Expand Down
12 changes: 3 additions & 9 deletions src/Eto.Mac/Forms/Controls/DrawableHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,8 @@ public override void DrawRect(CGRect dirtyRect)

if (!IsFlipped)
dirty.Y = bounds.Height - dirty.Y - dirty.Height;
if (dirty.X % 1.0f > 0f)
dirty.Width += 1;
if (dirty.Y % 1.0f > 0f)
dirty.Height += 1;
ApplicationHandler.QueueResizing = true;
drawable.DrawRegion(Rectangle.Ceiling(dirty));
drawable.DrawRegion(dirty);
ApplicationHandler.QueueResizing = false;
}

Expand Down Expand Up @@ -120,14 +116,12 @@ public override void Invalidate(Rectangle rect, bool invalidateChildren)
base.Invalidate(rect, invalidateChildren);
}

void DrawRegion(Rectangle rect)
void DrawRegion(RectangleF rect)
{
var context = NSGraphicsContext.CurrentContext;
if (context != null)
{
var handler = new GraphicsHandler(Control, context, (float)Control.Frame.Height);
// macOS Sonoma can draw outside the bounds of the NSView, so clip to the drawing rectangle
handler.Control.ClipToRect(rect.ToNS());
var handler = new GraphicsHandler(Control, context, (float)Control.Frame.Height, rect.ToNS());

using (var graphics = new Graphics(handler))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Eto.Mac/Forms/Printing/PrintDocumentHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public override void DrawRect(CGRect dirtyRect)
var height = Frame.Height;
var pageRect = RectForPage(operation.CurrentPage);

using (var graphics = new Graphics(new GraphicsHandler(this, context, (float)height)))
using (var graphics = new Graphics(new GraphicsHandler(this, context, (float)height, pageRect)))
{
graphics.TranslateTransform((float)pageRect.X, (float)(height-pageRect.Y-pageRect.Height));
Handler.Callback.OnPrintPage(Handler.Widget, new PrintPageEventArgs(graphics, operation.PrintInfo.ImageablePageBounds.Size.ToEto(), (int)operation.CurrentPage - 1));
Expand Down

0 comments on commit c3fc227

Please sign in to comment.