Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Masaki Tojo committed Nov 21, 2017
0 parents commit 6a12572
Show file tree
Hide file tree
Showing 13 changed files with 737 additions and 0 deletions.
50 changes: 50 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
AccessModifierOffset: -2
AlignEscapedNewlinesLeft: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackParameters: true
BreakBeforeBinaryOperators: false
BreakBeforeBraces: Allman
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 80
CommentPragmas: \*
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 2
Cpp11BracedListStyle: true
DerivePointerBinding: false
ExperimentalAutoDetectBinPacking: false
#ForEachMacros:
IndentCaseLabels: true
IndentFunctionDeclarationAfterType: true
IndentWidth: 2
KeepEmptyLinesAtTheStartOfBlocks: true
#Language: Cpp
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: false
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 60
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerBindsToType: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
Standard: Cpp11
TabWidth: 8
UseTab: Never
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build/
/node_modules/
2 changes: 2 additions & 0 deletions .syntastic_cpp_config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-std=c++11
-Inode_modules/nan
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# system-icon

Get associated file/folder icon for Node.js.

## Installation

```bash
$ npm install --save system-icon
```

## Usage

Get icon for file or folder path:

```js
const {writeFileSync} = require('fs');
const {
getIconForPath,
ICON_SIZE_MEDIUM
} = require('system-icon');

getIconForPath("/path/to/file_or_folder", ICON_SIZE_MEDIUM, (err, result) => {
if (err) {
console.error(err);
} else {
writeFileSync("icon.png", result);
}
});
```

Get icon for file extension:

```js
const {writeFileSync} = require('fs');
const {
getIconForExtension,
ICON_SIZE_MEDIUM
} = require('system-icon');

getIconForExtension(".ext", ICON_SIZE_MEDIUM, (err, result) => {
if (err) {
console.error(err);
} else {
writeFileSync("icon.png", result);
}
});
```

## API

### Constants

#### Size constants

The correspondence between the size constants and the icon size actually obtainable on each platform is as follows:

| Constant | Windows | macOS |
| ----------------------- | ------- | ------- |
| `ICON_SIZE_EXTRA_SMALL` | 16x16 | 16x16 |
| `ICON_SIZE_SMALL` | 32x32 | 32x32 |
| `ICON_SIZE_MEDIUM` | 64x64 | 64x64 |
| `ICON_SIZE_LARGE` | 256x256 | 256x256 |
| `ICON_SIZE_EXTRA_LARGE` | 256x256 | 512x512 |

### Functions

#### getIconForPath(path, size, callback)

Gets associated icon for file or folder path, and returns it in the PNG format.

#### getIconForExtension(extension, size, callback)

Gets associated icon for file extension, and returns it in the PNG format.

## License

MIT
63 changes: 63 additions & 0 deletions addon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "system_icon.hpp"
#include <nan.h>

NAN_METHOD(getIconForExtension)
{
if (info.Length() < 3)
{
Nan::ThrowTypeError("Wrong number of arguments");
return;
}

if (!info[0]->IsString() || !info[1]->IsNumber() || !info[2]->IsFunction())
{
Nan::ThrowTypeError("Wrong arguments");
return;
}

v8::String::Utf8Value extension{info[0]->ToString()};
auto size = static_cast<IconSize>(info[1]->Int32Value());
auto callback = new Nan::Callback(info[2].As<v8::Function>());
Nan::AsyncQueueWorker(
new SystemIconAsyncWorker<ExtensionTag>(*extension, size, callback));
}

NAN_METHOD(getIconForPath)
{
if (info.Length() < 3)
{
Nan::ThrowTypeError("Wrong number of arguments");
return;
}

if (!info[0]->IsString() || !info[1]->IsNumber() || !info[2]->IsFunction())
{
Nan::ThrowTypeError("Wrong arguments");
return;
}

v8::String::Utf8Value path{info[0]->ToString()};
auto size = static_cast<IconSize>(info[1]->Int32Value());
auto callback = new Nan::Callback(info[2].As<v8::Function>());
Nan::AsyncQueueWorker(
new SystemIconAsyncWorker<PathTag>(*path, size, callback));
}

NAN_MODULE_INIT(init)
{
NAN_EXPORT(target, getIconForExtension);
NAN_EXPORT(target, getIconForPath);

Nan::Set(target, Nan::New("ICON_SIZE_EXTRA_SMALL").ToLocalChecked(),
Nan::New(static_cast<int>(IconSize::ExtraSmall)));
Nan::Set(target, Nan::New("ICON_SIZE_SMALL").ToLocalChecked(),
Nan::New(static_cast<int>(IconSize::Small)));
Nan::Set(target, Nan::New("ICON_SIZE_MEDIUM").ToLocalChecked(),
Nan::New(static_cast<int>(IconSize::Medium)));
Nan::Set(target, Nan::New("ICON_SIZE_LARGE").ToLocalChecked(),
Nan::New(static_cast<int>(IconSize::Large)));
Nan::Set(target, Nan::New("ICON_SIZE_EXTRA_LARGE").ToLocalChecked(),
Nan::New(static_cast<int>(IconSize::ExtraLarge)));
}

NODE_MODULE(system_icon, init);
79 changes: 79 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
'target_defaults': {
'conditions': [
['OS=="win"', {
'defines': [
'_WIN32_WINNT=0x0600',
],
}],
],
'msbuild_settings': {
'ClCompile': {
'WarningLevel': 'Level3',
'Optimization': 'Full',
'FunctionLevelLinking': 'true',
},
'Lib': {
'LinkTimeCodeGeneration': 'true',
},
},
'msvs_settings': {
'VCCLCompilerTool': {
'WarningLevel': '3',
'Optimization': '3',
'EnableFunctionLevelLinking': 'true',
},
'VCLibrarianTool': {
'LinkTimeCodeGeneration': 'true',
},
},
'xcode_settings': {
'CLANG_CXX_LANGUAGE_STANDARD': 'c++11',
'CLANG_CXX_LIBRARY': 'libc++',
'CLANG_ENABLE_OBJC_ARC': 'YES',
'GCC_OPTIMIZATION_LEVEL': '3',
'GCC_VERSION': 'com.apple.compilers.llvm.clang.1_0',
'GCC_WARN_SHADOW': 'YES',
'GCC_WARN_UNUSED_VARIABLE': 'YES',
'WARNING_CFLAGS': ['-Wall'],
},
},
'targets': [
{
'target_name': 'addon',
'include_dirs': [
'<!(node -e "require(\'nan\')")',
],
'sources': [
'addon.cpp',
],
'conditions': [
['OS=="mac"', {
'link_settings': {
'libraries': [
'-framework AppKit',
],
},
'sources': [
'system_icon_mac.mm',
],
}],
['OS=="win"', {
'link_settings': {
'libraries': [
'Gdiplus.lib',
],
},
'sources': [
'system_icon_win.cpp',
],
}],
['OS!="mac" and OS!="win"', {
'sources': [
'system_icon.cpp',
],
}],
],
},
],
}
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('bindings')('addon');
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "system-icon",
"description": "Get associated file/folder icon",
"version": "0.0.0",
"author": "Masaki Tojo",
"bugs": {
"url": "https://github.com/mtojo/node-system-icon/issues"
},
"devDependencies": {
"bindings": "~1.3.0",
"nan": "~2.8.0"
},
"engines": {
"node": ">=6.0.0"
},
"homepage": "https://github.com/mtojo/node-system-icon",
"license": "MIT",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/mtojo/node-system-icon.git"
},
"scripts": {
"install": "node-gyp rebuild"
}
}
13 changes: 13 additions & 0 deletions system_icon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "system_icon.hpp"

template <>
void SystemIconAsyncWorker<ExtensionTag>::Execute()
{
// Do nothing.
}

template <>
void SystemIconAsyncWorker<PathTag>::Execute()
{
// Do nothing.
}
Loading

0 comments on commit 6a12572

Please sign in to comment.