mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-08-25 04:57:03 +00:00
Previously, Cappuccino was using preprocessor macros internally for the CGPoint/Size/Rect/Inset/Affine functions, as well as for CPRange. These macros had the same name as the corresponding function, but began with _. The functions were actually defined using the macros.
The motivation behind using macros was to increase performance by reducing function calls. However, there were a number of problems with this approach:
- There was an artificial dichotomy between _CG macros and the corresponding CG functions. We never completely replaced CG function calls with _CG macros. In fact, they were often mixed up in the same file. There was an extra burden on the programmer to remember to use the macro instead of the function.
- If a method call was passed as an argument to a macro, performance could actually be significantly *worse* than a function call. For example, _CGGetRectMakeCopy([view frame]) would expand to `{ origin:{ x:[view frame].origin.x, y:[view frame].origin.y }, size:{ width:[view frame].size.width, height:[view frame].size.height } }`. So instead of a single objj_msgSend and a single simple function call, we ended up with 4 objj_msgSend calls, which are way more expensive than simple function calls.
- Because of this expansion problem, to use macros efficiently required us to remember to use variables for all macro parameters. This didn't happen, and shouldn't have to happen.
- Finally, with modern Javascript engines, function call overhead is so small that it really isn't worth using the macros.
This commit eliminates the _CGGeometry, CGAffineTransformation and CPRange macros and replaces them with function calls.
BREAKING CHANGE:
The macros are no longer available. They could only be used with compiled code, but if there is any user code that used them, they will have to be replaced with the corresponding functions.
177 lines
4.4 KiB
Plaintext
177 lines
4.4 KiB
Plaintext
/*
|
|
* CPShadowView.j
|
|
* AppKit
|
|
*
|
|
* Created by Francisco Tolmasky.
|
|
* Copyright 2008, 280 North, Inc.
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
|
|
@import <Foundation/CPBundle.j>
|
|
|
|
@import "CGGeometry.j"
|
|
@import "CPImage.j"
|
|
@import "CPView.j"
|
|
|
|
|
|
CPLightShadow = 0;
|
|
CPHeavyShadow = 1;
|
|
|
|
CPThemeStateShadowViewLight = CPThemeState("shadowview-style-light");
|
|
CPThemeStateShadowViewHeavy = CPThemeState("shadowview-style-heavy");
|
|
|
|
|
|
/*!
|
|
@ingroup appkit
|
|
*/
|
|
@implementation CPShadowView : CPView
|
|
{
|
|
CPShadowWeight _weight;
|
|
}
|
|
|
|
+ (CPString)defaultThemeClass
|
|
{
|
|
return "shadow-view";
|
|
}
|
|
|
|
+ (id)themeAttributes
|
|
{
|
|
return @{
|
|
@"bezel-color": [CPNull null],
|
|
@"content-inset": CGInsetMakeZero()
|
|
};
|
|
}
|
|
|
|
+ (CGRect)frameForContentFrame:(CGRect)aFrame withWeight:(CPShadowWeight)aWeight
|
|
{
|
|
var shadowView = [CPShadowView new],
|
|
inset = [shadowView valueForThemeAttribute:@"content-inset" inState:(aWeight == CPLightShadow) ? CPThemeStateShadowViewLight : CPThemeStateShadowViewHeavy];
|
|
|
|
return CGRectMake(CGRectGetMinX(aFrame) - inset.left, CGRectGetMinY(aFrame) - inset.top, CGRectGetWidth(aFrame) + inset.left + inset.right, CGRectGetHeight(aFrame) + inset.top + inset.bottom);
|
|
}
|
|
|
|
+ (id)shadowViewEnclosingView:(CPView)aView
|
|
{
|
|
return [self shadowViewEnclosingView:aView withWeight:CPLightShadow];
|
|
}
|
|
|
|
+ (id)shadowViewEnclosingView:(CPView)aView withWeight:(CPShadowWeight)aWeight
|
|
{
|
|
var shadowView = [[self alloc] initWithFrame:[aView frame]];
|
|
|
|
if (shadowView)
|
|
{
|
|
[shadowView setWeight:aWeight];
|
|
|
|
var size = [shadowView frame].size,
|
|
inset = [shadowView currentValueForThemeAttribute:@"content-inset"],
|
|
width = size.width - inset.left - inset.right,
|
|
height = size.height - inset.top - inset.bottom,
|
|
enclosingView = [aView superview];
|
|
|
|
[shadowView setHitTests:[aView hitTests]];
|
|
[shadowView setAutoresizingMask:[aView autoresizingMask]];
|
|
[aView removeFromSuperview];
|
|
[shadowView addSubview:aView];
|
|
[aView setFrame:CGRectMake(inset.left, inset.top, width, height)];
|
|
[enclosingView addSubview:shadowView];
|
|
}
|
|
|
|
return shadowView;
|
|
}
|
|
|
|
- (id)initWithFrame:(CGRect)aFrame
|
|
{
|
|
self = [super initWithFrame:aFrame];
|
|
|
|
if (self)
|
|
{
|
|
[self setWeight:CPLightShadow];
|
|
|
|
[self setHitTests:NO];
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (void)setWeight:(CPShadowWeight)aWeight
|
|
{
|
|
if (_weight == aWeight)
|
|
return;
|
|
|
|
_weight = aWeight;
|
|
|
|
if (_weight == CPLightShadow)
|
|
[self setThemeState:CPThemeStateShadowViewLight];
|
|
else
|
|
[self setThemeState:CPThemeStateShadowViewHeavy];
|
|
|
|
[self setNeedsLayout];
|
|
}
|
|
|
|
- (float)leftInset
|
|
{
|
|
return [self currentValueForThemeAttribute:@"content-inset"].left;
|
|
}
|
|
|
|
- (float)rightInset
|
|
{
|
|
return [self currentValueForThemeAttribute:@"content-inset"].right;
|
|
}
|
|
|
|
- (float)topInset
|
|
{
|
|
return [self currentValueForThemeAttribute:@"content-inset"].top;
|
|
}
|
|
|
|
- (float)bottomInset
|
|
{
|
|
return [self currentValueForThemeAttribute:@"content-inset"].bottom;
|
|
}
|
|
|
|
- (float)horizontalInset
|
|
{
|
|
var currentContentInset = [self currentValueForThemeAttribute:@"content-inset"];
|
|
|
|
return currentContentInset.left + currentContentInset.right;
|
|
}
|
|
|
|
- (float)verticalInset
|
|
{
|
|
var currentContentInset = [self currentValueForThemeAttribute:@"content-inset"];
|
|
|
|
return currentContentInset.top + currentContentInset.bottom;
|
|
}
|
|
|
|
- (CGRect)frameForContentFrame:(CGRect)aFrame
|
|
{
|
|
return [[self class] frameForContentFrame:aFrame withWeight:_weight];
|
|
}
|
|
|
|
- (void)setFrameForContentFrame:(CGRect)aFrame
|
|
{
|
|
[self setFrame:[self frameForContentFrame:aFrame]];
|
|
}
|
|
|
|
- (void)layoutSubviews
|
|
{
|
|
[super layoutSubviews];
|
|
|
|
[self setBackgroundColor:[self currentValueForThemeAttribute:@"bezel-color"]];
|
|
}
|
|
|
|
@end
|