mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-08-25 13:07:02 +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.
182 lines
5.2 KiB
Plaintext
182 lines
5.2 KiB
Plaintext
/*
|
|
* CPDOMWindowLayer.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/CPArray.j>
|
|
@import <Foundation/CPObject.j>
|
|
|
|
|
|
@implementation CPDOMWindowLayer : CPObject
|
|
{
|
|
int _level;
|
|
CPArray _windows;
|
|
DOMElement _DOMElement;
|
|
}
|
|
|
|
- (id)initWithLevel:(int)aLevel
|
|
{
|
|
self = [super init];
|
|
|
|
if (self)
|
|
{
|
|
_level = aLevel;
|
|
|
|
_windows = [];
|
|
|
|
_DOMElement = document.createElement("div");
|
|
_DOMElement.style.position = "absolute";
|
|
_DOMElement.style.top = "0px";
|
|
_DOMElement.style.left = "0px";
|
|
_DOMElement.style.width = "1px";
|
|
_DOMElement.style.height = "1px";
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (int)level
|
|
{
|
|
return _level;
|
|
}
|
|
|
|
- (void)removeWindow:(CPWindow)aWindow
|
|
{
|
|
if (!aWindow._isVisible)
|
|
return;
|
|
|
|
var index = aWindow._index,
|
|
count = _windows.length - 1;
|
|
|
|
CPDOMDisplayServerRemoveChild(_DOMElement, aWindow._DOMElement);
|
|
|
|
[_windows removeObjectAtIndex:aWindow._index];
|
|
|
|
for (; index < count; ++index)
|
|
{
|
|
_windows[index]._index = index;
|
|
_windows[index]._DOMElement.style.zIndex = index;
|
|
}
|
|
|
|
aWindow._isVisible = NO;
|
|
}
|
|
|
|
- (void)insertWindow:(CPWindow)aWindow atIndex:(unsigned)anIndex
|
|
{
|
|
// We will have to adjust the z-index of all windows starting at this index.
|
|
var count = [_windows count],
|
|
zIndex = (anIndex === CPNotFound ? count : anIndex),
|
|
isVisible = aWindow._isVisible;
|
|
|
|
// If the window is already a resident of this layer, remove it.
|
|
if (isVisible)
|
|
{
|
|
// Adjust the z-index to start at the window being inserted
|
|
zIndex = MIN(zIndex, aWindow._index);
|
|
|
|
// If the window being inserted is below the insertion index,
|
|
// the index will be one less after we remove the window below.
|
|
if (aWindow._index < anIndex)
|
|
--anIndex;
|
|
|
|
[_windows removeObjectAtIndex:aWindow._index];
|
|
}
|
|
else
|
|
++count;
|
|
|
|
if (anIndex === CPNotFound || anIndex >= count)
|
|
[_windows addObject:aWindow];
|
|
else
|
|
[_windows insertObject:aWindow atIndex:anIndex];
|
|
|
|
// Adjust all the affected z-indexes.
|
|
for (; zIndex < count; ++zIndex)
|
|
{
|
|
_windows[zIndex]._index = zIndex;
|
|
_windows[zIndex]._DOMElement.style.zIndex = zIndex;
|
|
}
|
|
|
|
// If the window is not already a resident of this layer, add it.
|
|
if (aWindow._DOMElement.parentNode !== _DOMElement)
|
|
{
|
|
CPDOMDisplayServerAppendChild(_DOMElement, aWindow._DOMElement);
|
|
|
|
aWindow._isVisible = YES;
|
|
|
|
if ([aWindow isFullPlatformWindow])
|
|
[aWindow setFrame:[aWindow._platformWindow usableContentFrame]];
|
|
}
|
|
}
|
|
|
|
- (CPArray)orderedWindows
|
|
{
|
|
return _windows;
|
|
}
|
|
|
|
/*!
|
|
Places \c aWindow within an element that clips it to the global rect \c clipRect.
|
|
|
|
NOTE: This is only meant for temporary usage during animation and should be balanced
|
|
with a call to removeClipForWindow:. No attempt is made to make the clipping element follow
|
|
changes to the window.
|
|
*/
|
|
- (void)clipWindow:(CPWindow)aWindow toRect:(CGRect)clipRect
|
|
{
|
|
// First check to see if the window is already clipped.
|
|
// If so, just update its rect.
|
|
var windowElement = aWindow._DOMElement,
|
|
clip = document.createElement("div"),
|
|
style = clip.style;
|
|
|
|
style = clip.style;
|
|
style.className = "cpwindowclip";
|
|
style.position = "absolute";
|
|
style.overflow = "hidden";
|
|
|
|
style.left = clipRect.origin.x + "px";
|
|
style.top = clipRect.origin.y + "px";
|
|
style.width = clipRect.size.width + "px";
|
|
style.height = clipRect.size.height + "px";
|
|
|
|
// Replace the window with the clip element, then put it inside the clip
|
|
var parent = windowElement.parentNode;
|
|
CPDOMDisplayServerInsertBefore(parent, clip, windowElement);
|
|
CPDOMDisplayServerRemoveChild(parent, windowElement);
|
|
CPDOMDisplayServerAppendChild(clip, windowElement);
|
|
}
|
|
|
|
/*!
|
|
Unclips a window that was previously clipped with clipWindow:toWindow:.
|
|
If the window was not clipped, a warning is logged.
|
|
*/
|
|
- (void)removeClipForWindow:(CPWindow)aWindow
|
|
{
|
|
var windowElement = aWindow._DOMElement,
|
|
clip = windowElement.parentNode,
|
|
parent = clip.parentNode;
|
|
|
|
CPDOMDisplayServerRemoveChild(clip, windowElement);
|
|
[aWindow setFrameOrigin:CGPointMake([aWindow frame].origin.x, clip.offsetTop)];
|
|
CPDOMDisplayServerInsertBefore(parent, windowElement, clip);
|
|
CPDOMDisplayServerRemoveChild(parent, clip);
|
|
}
|
|
|
|
@end
|