Skip to content

Commit

Permalink
Merge pull request #2558 from cwensley/curtis/mac-fix-drawable-clippi…
Browse files Browse the repository at this point in the history
…ng-again

Mac: Fix regression with Drawable clipping
  • Loading branch information
cwensley authored Oct 6, 2023
2 parents c3fc227 + c305f5b commit 12c0a04
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/Eto.Mac/Drawing/GraphicsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public GraphicsHandler()
}

#if OSX
public GraphicsHandler(NSView view, NSGraphicsContext graphicsContext, float height, CGRect? dirtyRect)
public GraphicsHandler(NSView view, NSGraphicsContext graphicsContext, float height, CGRect? clipRect)
{
DisplayView = view;
this.height = height;
Expand All @@ -92,8 +92,8 @@ public GraphicsHandler(NSView view, NSGraphicsContext graphicsContext, float hei
Control = this.graphicsContext.CGContext;

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

InitializeContext(!flipped);
}
Expand Down
46 changes: 25 additions & 21 deletions src/Eto.Mac/Forms/Controls/DrawableHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,9 @@ public override void DrawRect(CGRect dirtyRect)
var drawable = Drawable;
if (drawable == null)
return;
// restrict drawing to the bounds of the drawable
// macOS can give us dirty rects outside this range
var bounds = Bounds.ToEto();
var dirty = dirtyRect.ToEto();
dirty.Restrict(bounds);

if (!IsFlipped)
dirty.Y = bounds.Height - dirty.Y - dirty.Height;

ApplicationHandler.QueueResizing = true;
drawable.DrawRegion(dirty);
drawable.DrawRegion(dirtyRect);
ApplicationHandler.QueueResizing = false;
}

Expand Down Expand Up @@ -116,22 +109,33 @@ public override void Invalidate(Rectangle rect, bool invalidateChildren)
base.Invalidate(rect, invalidateChildren);
}

void DrawRegion(RectangleF rect)
void DrawRegion(CGRect dirtyRect)
{
var context = NSGraphicsContext.CurrentContext;
if (context != null)
{
var handler = new GraphicsHandler(Control, context, (float)Control.Frame.Height, rect.ToNS());
if (context == null)
return;

using (var graphics = new Graphics(handler))
{
if (backgroundBrush != null)
graphics.FillRectangle(backgroundBrush, rect);
var bounds = Control.Bounds;

var widget = Widget;
if (widget != null)
Callback.OnPaint(widget, new PaintEventArgs(graphics, rect));
}
// restrict dirty rect to the bounds of the drawable
// macOS can give us dirty rects outside this range
var dirty = dirtyRect.ToEto();
dirty.Restrict(bounds.ToEto());

var handler = new GraphicsHandler(Control, context, (float)bounds.Height, dirty.ToNS());

// dirty rect should be flipped when passed to Drawabe.Paint event
if (!Control.IsFlipped)
dirty.Y = (float)(bounds.Height - dirty.Y - dirty.Height);

using (var graphics = new Graphics(handler))
{
if (backgroundBrush != null)
graphics.FillRectangle(backgroundBrush, dirty);

var widget = Widget;
if (widget != null)
Callback.OnPaint(widget, new PaintEventArgs(graphics, dirty));
}
}

Expand Down

0 comments on commit 12c0a04

Please sign in to comment.