Files
cappuccino/Tests/Manual/CopyAndPaste/AppController.j
T
Alexander Ljungberg 757c79d15c Refactor, restructure and simplify copy and paste handling.
This change brings nearly all copy and paste related into a single, self-contained class. This new class is still somewhat coupled to CPPlatformWindow+DOM.j in the sense that it's highly specific and meant to be called in a very specialised way. Despite this, it's still a significant decoupling and the new code is more readable and easier to follow.
2013-06-17 00:17:00 +01:00

94 lines
2.4 KiB
Plaintext

/*
* AppController.j
* CopyAndPaste
*
* Created by Alexander Ljungberg on June 13, 2013.
* Copyright 2013, SlevenBits, Ltd. All rights reserved.
*/
@import <Foundation/Foundation.j>
@import <AppKit/AppKit.j>
@import "CollectionViewItem.j"
@implementation AppController : CPObject
{
@outlet CPWindow theWindow;
@outlet CPCollectionView aCollectionView;
@outlet CPArrayController anArrayController;
}
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
// This is called when the application is done loading.
}
- (void)awakeFromCib
{
// http://www.flickr.com/photos/viamoi/2952609526/
var anImage = CPImageInBundle("2952609526_9fd245dfcd_q.jpg");
[anArrayController setContent:@[@"Cat", @"Rabbit", @"Dinosaur", anImage]];
[theWindow setFullPlatformWindow:YES];
}
- (void)copy:(id)sender
{
var selected = [anArrayController selectedObjects],
strings = [],
images = [];
for (var i = 0; i < selected.length; i++)
([selected[i] isKindOfClass:CPImage] ? images : strings).push(selected[i]);
var stringValue = strings.join(", "),
pasteboard = [CPPasteboard generalPasteboard],
types = [];
if (stringValue)
[types addObject:CPStringPboardType]
if (images)
[types addObject:CPImagesPboardType]
[pasteboard declareTypes:types owner:nil];
if (stringValue)
[pasteboard setString:stringValue forType:CPStringPboardType];
if (images)
[pasteboard setData:[CPKeyedArchiver archivedDataWithRootObject:images] forType:CPImagesPboardType];
CPLog.info("Copied %@.", (stringValue || "") + " " + (images || ""));
}
- (void)cut:(id)sender
{
[self copy:sender];
[anArrayController remove:self];
}
- (void)paste:(id)sender
{
console.log(self + "paste: " + sender);
var pasteboard = [CPPasteboard generalPasteboard],
parts = [];
if ([[pasteboard types] containsObject:CPStringPboardType])
{
var stringValue = [pasteboard stringForType:CPStringPboardType];
[parts addObjectsFromArray:[stringValue componentsSeparatedByString:@", "]];
}
if ([[pasteboard types] containsObject:CPImagesPboardType])
{
var images = [CPKeyedUnarchiver unarchiveObjectWithData:[pasteboard dataForType:CPImagesPboardType]];
[parts addObjectsFromArray:images];
}
[anArrayController addObjects:parts];
CPLog.info("Pasted %@.", parts);
}
@end