Custom NSThemeFrame

Custom NSThemeFrame

EDIT to completely change the window appearance, check out Matt Gallagher's  Drawing a custom window

After seeing Safari 4 drawing its tab bar in a custom way, I wondered how to do that. Each window has a frame view (the superview ofcontentView) that draws the window, replacing that view's drawRect: with our own will let us draw it ourselves !

Image:Custom NSThemeFrame drawing.png

First, replace the window's frame drawRect: with our own :

// Get window's frame view class id class = [[[window contentView] superview] class];  // Add our drawRect: to the frame class Method m0 = class_getInstanceMethod([self class], @selector(drawRect:)); class_addMethod(class, @selector(drawRectOriginal:), method_getImplementation(m0), method_getTypeEncoding(m0));  // Exchange methods Method m1 = class_getInstanceMethod(class, @selector(drawRect:)); Method m2 = class_getInstanceMethod(class, @selector(drawRectOriginal:)); method_exchangeImplementations(m1, m2); 

Cocoa will then call our method each time the window frame needs to be drawn. We can draw an entirely custom frame or draw over the existing one(The attached sample does the latter)

A standard Cocoa window has rounded corners that need to be accounted for by building a clipping path to clip our custom drawing to the window shape :

  • Get the rounded corner radius with roundedCornerRadius
  • Using this radius, build a rounded corner path that describes the window
  • Intersect that path with the current rect
  • Draw our stuff
- (void)drawRect:(NSRect)rect { // Call original drawing method [self drawRectOriginal:rect];  // // Build clipping path : intersection of frame clip (bezier path with rounded corners) and rect argument // NSRect windowRect = [[self window] frame]; windowRect.origin = NSMakePoint(0, 0);  float cornerRadius = [self roundedCornerRadius]; [[NSBezierPath bezierPathWithRoundedRect:windowRect xRadius:cornerRadius yRadius:cornerRadius] addClip]; [[NSBezierPath bezierPathWithRect:rect] addClip];  // Any custom drawing goes here ... } 
Sample Code  Image:iconZip.png Custom frame drawing.zip draws an  NSImageNameActionTemplate image and a custom color (in kCGBlendModeColorDodge) over the existing frame
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值