Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mac: Fix regression with Drawable clipping #2558

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading