My program overrides NSTextView.drawBackground(in:) to draw nested
capsules around text in an NSTextView.  You can see what I'm talking
about in this video:

https://twitter.com/defenestrately/status/1593322315709923335

Each capsule has a CALayer with non-zero shadowRadius and
shadowOpacity.  That CALayer has one sublayer
that fills one or more
rounded rectangles to produce the capsule shape.

Inside a `drawBackground(in: rect)` call, I find all of the capsules
that may intersect `rect` (allowing a large margin for the cast
shadows), add them to a master CALayer, and render that master CALayer
using the current graphics context:

        NSGraphicsContext.saveGraphicsState()
        if let ctx = NSGraphicsContext.current {
                let cgc = ctx.cgContext
                master.render(in: cgc)
        }
        NSGraphicsContext.restoreGraphicsState()

I see a funny graphic artifact appear when the default NSTextView
insertion-point caret blinks off: a sliver of shadow disappears when the
caret disappears.  I have surmised that this may have something to do
with the narrow clip path that is set while `drawBackground(in:)` is
called to erase the caret.

I find that by overriding `setNeedsDisplay(in:flags:)`, I can make the
caret-erasure clip path enclose a larger and larger area by calling
`super.setNeedsDisplay(in:flags:)` with bigger and bigger rectangles.
As the area enclosed enlarges, the region where the shadow may disappear
moves further away from the caret.  I guess that I can force the whole
view to redraw in this way, but that seems inefficient, and it seems
like there must be a better way.

Is it possible that a CGContext clip path will clip a shadow cast inside
the clip path, if the CALayer whose content cast the shadow is fully
outside of the clip path?  Any ideas why?

Dave

-- 
David Young
dyo...@pobox.com    Urbana, IL    (217) 721-9981
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to