Files
cappuccino/Tools/nib2cib/NSCustomResource.j
T
2021-08-19 10:56:13 +02:00

161 lines
5.0 KiB
Plaintext

/*
* NSCustomResource.j
* nib2cib
*
* Portions based on NSCustomResource.m (01/08/2009) in Cocotron (http://www.cocotron.org/)
* Copyright (c) 2006-2007 Christopher J. W. Lloyd
*
* 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 <AppKit/_CPCibCustomResource.j>
@import "Nib2CibException.j"
@global CP_NSMapClassName
var fs = require("fs");
var path = require("path");
var child_process = require("child_process");
function imagesize() {
var result;
try {
result = child_process.execSync("imagesize -n " + p);
} catch(err) {
console.error("imagesize failed:" + err);
}
return result ? JSON.parse(result) : null;
}
/*
var FILE = require("file"),
imageSize = require("cappuccino/imagesize").imagesize
*/
var supportedTemplateImages = {
"NSAddTemplate": "CPAddTemplate",
"NSRemoveTemplate": "CPRemoveTemplate",
"NSActionTemplate": "CPActionTemplate",
"NSToolbarShowColors": "CPImageNameColorPanel"
};
@implementation _CPCibCustomResource (NSCoding)
- (id)NS_initWithCoder:(CPCoder)aCoder
{
self = [super init];
if (self)
{
_className = CP_NSMapClassName([aCoder decodeObjectForKey:@"NSClassName"]);
_resourceName = [aCoder decodeObjectForKey:@"NSResourceName"];
var size = CGSizeMakeZero(),
framework = @"",
bundleIdentifier = @"";
if (_resourceName == "NSSwitch" || _resourceName == "NSRadioButton")
return nil;
else if (/^NS[A-Z][A-Za-z]+$/.test(_resourceName))
{
if (supportedTemplateImages[_resourceName])
{
// Defer resolving this path until runtime.
_resourceName = supportedTemplateImages[_resourceName];
}
else
[CPException raise:Nib2CibException format:@"The built in image “%@” is not supported.", _resourceName];
}
else if (/^(.+)@MaterialIcons+$/.test(_resourceName))
{
// Catch a material icon specified by "icon-name@MaterialIcons"
// Just do nothing and leave _resourceName as is.
// It will be treated during runtime by _CPCibCustomResource
}
else
{
var match = /^(.+)@(.+)$/.exec(_resourceName);
if (match)
{
_resourceName = match[1];
framework = match[2];
}
var resourceInfo = [aCoder resourceInfoForName:_resourceName inFramework:framework];
if (!resourceInfo)
CPLog.warn("Resource \"" + _resourceName + "\" not found in the Resources directories");
else
{
size = imagesize(fs.realpathSync(resourceInfo.path)) || CGSizeMakeZero();
framework = resourceInfo.framework;
}
// Account for the fact that an extension may have been inferred.
if (resourceInfo && resourceInfo.path)
{
// Include subdirectories in the name
match = /^.+\/Resources\/(.+)$/.exec(resourceInfo.path);
_resourceName = match[1];
}
}
if (resourceInfo && resourceInfo.path && resourceInfo.framework)
{
var frameworkPath = path.dirname(path.dirname(resourceInfo.path)),
bundle = [CPBundle bundleWithPath:frameworkPath];
[bundle loadWithDelegate:nil];
bundleIdentifier = [bundle bundleIdentifier] || @"";
}
_properties = @{ @"size":size, @"bundleIdentifier":bundleIdentifier, @"framework":framework };
CPLog.debug(" Resource: %s\n Framework: %s%s\n Path: %s\n Size: %d x %d",
_resourceName,
framework ? framework : "<none>",
bundleIdentifier ? " (" + bundleIdentifier + ")" :
framework ? " (<no bundle identifier>)" : "",
resourceInfo ? fs.realpathSync(resourceInfo.path) : "",
size.width,
size.height);
}
return self;
}
@end
@implementation NSCustomResource : _CPCibCustomResource
{
}
- (id)initWithCoder:(CPCoder)aCoder
{
return [self NS_initWithCoder:aCoder];
}
- (Class)classForKeyedArchiver
{
return [_CPCibCustomResource class];
}
@end