Created
March 22, 2010 19:06
-
-
Save maz/340400 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var loadingImage=[[CPImage alloc] initWithContentsOfFile:[[CPBundle mainBundle] pathForResource:"spinner.gif"]]; | |
var fileImage=[[CPImage alloc] initWithContentsOfFile:[[CPBundle mainBundle] pathForResource:"file.png"]]; | |
var folderImage=[[CPImage alloc] initWithContentsOfFile:[[CPBundle mainBundle] pathForResource:"folder.png"]]; | |
var textHighlightImage=[[CPImage alloc] initWithContentsOfFile:[[CPBundle mainBundle] pathForResource:"TextHighlight.png"]]; | |
var iconSelectionImage=[[CPImage alloc] initWithContentsOfFile:[[CPBundle mainBundle] pathForResource:"IconSelectionOutline.png"]]; | |
var textHighlightColor=[CPColor colorWithPatternImage:textHighlightImage]; | |
RaptorFileDragType="RaptorFileDragType"; | |
RaptorFileDirectoryChanged="RaptorFileDirectoryChanged"; | |
RaptorFileShiftKeyDown=false; | |
(function(){ | |
var dwn=function(e){ | |
var evt=e?e:window.event; | |
if(parseInt(evt.keyCode)==16){ | |
RaptorFileShiftKeyDown=true; | |
} | |
}; | |
var up=function(e){ | |
var evt=e?e:window.event; | |
if(parseInt(evt.keyCode)==16){ | |
RaptorFileShiftKeyDown=false; | |
} | |
}; | |
if(document.addEventListener){ | |
document.addEventListener('keydown',dwn,false); | |
document.addEventListener('keyup',up,false); | |
}else{ | |
document.attachEvent('onkeydown',dwn); | |
document.attachEvent('onkeyup',up); | |
} | |
})(); | |
@implementation _FileIconBorder : CPView | |
{ | |
} | |
-(void)drawRect:(CPRect)r | |
{ | |
var ctx = [[CPGraphicsContext currentContext] graphicsPort]; | |
CGContextSetFillColor(ctx,[CPColor blueColor]); | |
var b=[self bounds]; | |
CGContextFillRect(ctx,CPMakeRect(0,0,5,CGRectGetHeight(b))); | |
CGContextFillRect(ctx,CPMakeRect(0,CGRectGetHeight(b)-5,CGRectGetWidth(b),5)); | |
CGContextFillRect(ctx,CPMakeRect(CGRectGetWidth(b)-5,0,5,CGRectGetHeight(b))); | |
CGContextFillRect(ctx,CPMakeRect(0,0,CGRectGetWidth(b),5)); | |
} | |
@end | |
@implementation FileIcon : CPView | |
{ | |
CPString path; | |
CPImageView iconField; | |
CPImageView iconSelView; | |
CPTextField nameField; | |
_FileIconBorder borderView; | |
BOOL folderFlag; | |
BOOL selected; | |
JSObject info; | |
} | |
-(id)initWithFrame:(CPRect)f{ | |
if(self=[super initWithFrame:f]){ | |
selected=NO; | |
} | |
return self; | |
} | |
-(CPString)path{ | |
return path; | |
} | |
-(void)mouseDown:(CPEvent)evt | |
{ | |
if(!RaptorFileShiftKeyDown){ | |
var obj=null; | |
var enu=[[[self superview] subviews] objectEnumerator]; | |
while((obj=[enu nextObject])){ | |
if([obj respondsToSelector:@selector(setSelected:)]){ | |
[obj setSelected:NO]; | |
} | |
} | |
} | |
[self setSelected:YES]; | |
if([evt clickCount]>1 && [[self superview] respondsToSelector:@selector(open:folder:)]){ | |
[[self superview] open:path folder:folderFlag]; | |
} | |
/*var e=[evt _DOMEvent]; | |
//Thanks to quirksmode.org for the following threee lines of code :) | |
var rightclick; | |
if (e.which) rightclick = (e.which == 3); | |
else if (e.button) rightclick = (e.button == 2); | |
if(rightclick){ | |
if(e.preventDefault){ | |
e.preventDefault(); | |
} | |
e.returnValue=false; | |
var menu=[[CPMenu alloc] init]; | |
[menu setDelegate:self]; | |
[menu addItemWithTitle:"Get Info" action:@selector(info:) keyEquivalent:nil]; | |
[CPMenu popUpContextMenu:menu withEvent:evt forView:self]; | |
}*/ | |
} | |
- (void)pasteboard:(CPPasteboard)aPasteboard provideDataForType:(CPString)aType | |
{ | |
[aPasteboard setData:[CPData dataWithString:path] forType:aType]; | |
} | |
- (void)mouseDragged:(CPEvent)anEvent | |
{ | |
[[CPPasteboard pasteboardWithName:CPDragPboard] declareTypes:[RaptorFileDragType] owner:self]; | |
var v=[[FileIcon alloc] initWithFrame:[self frame]]; | |
[v setPath:path]; | |
[v setAlphaValue:0.7]; | |
[self dragView:v at:CGPointMakeZero() offset:CGPointMakeZero() event:anEvent pasteboard:nil source:self slideBack:YES]; | |
window.FileIconDragOffset=[self convertPoint:[anEvent locationInWindow] fromView:nil]; | |
} | |
- (void)draggingEntered:(CPDraggingInfo)aSender | |
{ | |
[borderView setHidden:NO]; | |
} | |
- (void)draggingExited:(CPDraggingInfo)aSender | |
{ | |
[borderView setHidden:YES]; | |
} | |
-(void)setPath:(CPString)p{ | |
[self unregisterDraggedTypes]; | |
path=p; | |
if(!iconField){ | |
iconField=[[CPImageView alloc] initWithFrame:CPMakeRect(0,0,128,128)]; | |
[self addSubview:iconField]; | |
nameField=[CPTextField labelWithTitle:""]; | |
[nameField setFrame:CPMakeRect(0,128,128,CGRectGetHeight([nameField frame]))]; | |
[nameField setEditable:YES]; | |
[nameField setTarget:self]; | |
[nameField setAction:@selector(_nameChanged:)]; | |
[self addSubview:nameField]; | |
iconSelView=[[CPImageView alloc] initWithFrame:CPMakeRect(0,0,128,128)]; | |
[iconSelView setImage:iconSelectionImage]; | |
[iconSelView setHidden:YES]; | |
borderView=[[_FileIconBorder alloc] initWithFrame:[iconField frame]]; | |
[borderView setHidden:YES]; | |
[self addSubview:iconSelView]; | |
[self addSubview:borderView]; | |
[self setFrameSize:CPMakeSize(128,128+CGRectGetHeight([nameField frame]))]; | |
} | |
[nameField setStringValue:File.name(p)]; | |
[nameField setTextFieldBackgroundColor:textHighlightColor]; | |
[iconField setImage:loadingImage]; | |
File.info(path,function(inf){ | |
folderFlag=inf.folder; | |
if(folderFlag){ | |
[self registerForDraggedTypes:[RaptorFileDragType]]; | |
} | |
[iconField setImage:folderFlag?folderImage : fileImage]; | |
setTimeout(function(){ | |
[[CPRunLoop mainRunLoop] performSelectors]; | |
},100); | |
}); | |
} | |
- (BOOL)performDragOperation:(CPDraggingInfo)aSender | |
{ | |
if(![[aSender draggingPasteboard] availableTypeFromArray:[RaptorFileDragType]]) | |
return NO; | |
[borderView setHidden:YES]; | |
var data = [[aSender draggingPasteboard] dataForType:RaptorFileDragType]; | |
var p=[data string]; | |
if(p!=path) | |
File.mv(p,File.join(path,File.name(p)),function(x){ | |
[[CPNotificationCenter defaultCenter] postNotificationName:RaptorFileDirectoryChanged object:File.parent(p)]; | |
[[CPNotificationCenter defaultCenter] postNotificationName:RaptorFileDirectoryChanged object:path]; | |
}); | |
} | |
-(IBAction)_nameChanged:(id)sender{ | |
var np=File.join(File.parent(path),[nameField stringValue]); | |
File.mv(path,np,function(x){ | |
if(x){ | |
path=np; | |
}else{ | |
[nameField setStringValue:File.name(path)]; | |
} | |
}); | |
} | |
-(BOOL)selected{ | |
return selected; | |
} | |
-(void)setSelected:(BOOL)flag{ | |
if(selected!=flag){ | |
selected=flag; | |
[iconSelView setHidden:!selected]; | |
[nameField setDrawsBackground:selected]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment