Added node-webkit definitions

This commit is contained in:
Pedro Casaubon
2014-08-08 19:10:43 +02:00
parent 7ccc55d5f0
commit 3878a37dfa
2 changed files with 424 additions and 0 deletions

View File

@@ -0,0 +1,203 @@
///<reference path="../node/node.d.ts"/>
///<reference path="node-webkit.d.ts"/>
// Load native UI library
var gui: typeof nw.gui;
/* WINDOW */
// Get the current window
var win = gui.Window.get();
// Listen to the minimize event
win.on('minimize', function() {
console.log('Window is minimized');
});
// Minimize the window
win.minimize();
// Unlisten the minimize event
win.removeAllListeners('minimize');
// Create a new window and get it
var new_win = gui.Window.get(
window.open('https://github.com')
);
// And listen to new window's focus event
new_win.on('focus', function() {
console.log('New window is focused');
});
// Get the current window
var win = gui.Window.get();
// Create a new window and get it
var new_win = gui.Window.get(
window.open('https://github.com')
);
// png as base64string
win.capturePage(function(base64string:string){
// do something with the base64string
}, { format : 'png', datatype : 'raw'} );
// png as node buffer
win.capturePage(function(buffer:Buffer){
// do something with the buffer
}, { format : 'png', datatype : 'buffer'} );
// Open a new window.
var win = gui.Window.get(
window.open('popup.html')
);
// Release the 'win' object here after the new window is closed.
win.on('closed', function() {
win = null;
});
// Listen to main window's close event
gui.Window.get().on('close', function() {
// Hide the window to give user the feeling of closing immediately
this.hide();
// If the new window is still open then close it.
if (win != null)
win.close(true);
// After closing the new window, close the main window.
this.close(true);
});
/* MENU */
// Create an empty menu
var menu = new gui.Menu();
// Add some items
menu.append(new gui.MenuItem({ label: 'Item A' }));
menu.append(new gui.MenuItem({ label: 'Item B' }));
menu.append(new gui.MenuItem({ type: 'separator' }));
menu.append(new gui.MenuItem({ label: 'Item C' }));
// Remove one item
menu.removeAt(1);
// Popup as context menu
menu.popup(10, 10);
// Iterate menu's items
for (var i = 0; i < menu.items.length; ++i) {
console.log(menu.items[i]);
}
var win = gui.Window.get();
var nativeMenuBar = new gui.Menu({ type: "menubar" });
nativeMenuBar.createMacBuiltin("My App");
win.menu = nativeMenuBar;
nativeMenuBar.createMacBuiltin("My App", {
hideEdit: true,
hideWindow: true
});
/* MENU ITEM */
var itemc:nw.gui.MenuItem;
// Create a separator
itemc = new gui.MenuItem({ type: 'separator' });
// Create a normal item with label and icon
itemc = new gui.MenuItem({
type: "normal",
label: "I'm a menu item",
icon: "img/icon.png"
});
// Or you can omit the 'type' field for normal items
itemc = new gui.MenuItem({ label: 'Simple item' });
// Bind a callback to item
itemc = new gui.MenuItem({
label: "Click me",
click: function() {
console.log("I'm clicked");
},
key: "s",
modifiers: "ctrl-alt",
});
// You can have submenu!
var submenu = new gui.Menu();
submenu.append(new gui.MenuItem({ label: 'Item 1' }));
submenu.append(new gui.MenuItem({ label: 'Item 2' }));
submenu.append(new gui.MenuItem({ label: 'Item 3' }));
itemc.submenu = submenu;
// And everything can be changed at runtime
itemc.label = 'New label';
itemc.click = function() { console.log('New click callback'); };
/* APP */
// Print arguments
console.log(gui.App.argv);
// Quit current app
gui.App.quit();
// Get the name field in manifest
gui.App.manifest.name
gui.App.addOriginAccessWhitelistEntry('http://github.com/', 'app', 'myapp', true);
/* CLIPBOARD */
// We can not create a clipboard, we have to receive the system clipboard
var clipboard = gui.Clipboard.get();
// Read from clipboard
var text = clipboard.get('text');
console.log(text);
// Or write something
clipboard.set('I love node-webkit :)', 'text');
// And clear it!
clipboard.clear();
/* TRAY */
// Create a tray icon
var tray = new gui.Tray({ title: 'Tray', icon: 'img/icon.png' });
// Give it a menu
var menu = new gui.Menu();
menu.append(new gui.MenuItem({ type: 'checkbox', label: 'box1' }));
tray.menu = menu;
// Remove the tray
tray.remove();
tray = null;
/* SHELL */
// Open URL with default browser.
gui.Shell.openExternal('https://github.com/rogerwang/node-webkit');
// Open a text file with default text editor.
gui.Shell.openItem('test.txt');
// Open a file in file explorer.
gui.Shell.showItemInFolder('test.txt');

221
node-webkit/node-webkit.d.ts vendored Normal file
View File

@@ -0,0 +1,221 @@
// Type definitions for node-webkit
// Project: https://github.com/rogerwang/node-webkit
// Definitions by: Pedro Casaubon <https://github.com/xperiments>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module nw.gui {
interface IEventEmitter {
addListener(event: string, listener: Function): EventEmitter;
on(event: string, listener: Function): EventEmitter;
once(event: string, listener: Function): EventEmitter;
removeListener(event: string, listener: Function): EventEmitter;
removeAllListeners(event?: string): EventEmitter;
setMaxListeners(n: number): void;
listeners(event: string): Function[];
emit(event: string, ...args: any[]): boolean;
}
class EventEmitter implements IEventEmitter {
addListener(event: string, listener: Function): EventEmitter;
on(event: string, listener: Function): EventEmitter;
once(event: string, listener: Function): EventEmitter;
removeListener(event: string, listener: Function): EventEmitter;
removeAllListeners(event?: string): EventEmitter;
setMaxListeners(n: number): void;
listeners(event: string): Function[];
emit(event: string, ...args: any[]): boolean;
}
export interface MenuConfig {
type?: string;
}
export interface HideMenusOptions {
hideEdit: boolean;
hideWindow: boolean;
}
export interface MenuItemConfig {
label?: string;
click?: Function;
type?: string;
submenu?: Menu;
icon?: string;
tooltip?: string;
checked?: boolean;
enabled?: boolean;
key?: string;
modifiers?: string;
}
export class MenuItem extends EventEmitter implements MenuItemConfig {
constructor(config: MenuItemConfig);
label: string;
click: Function;
type: string;
submenu: Menu;
icon: string;
tooltip: string;
checked: boolean;
enabled: boolean;
key: string;
modifiers: string;
}
export class Menu {
constructor(config?: MenuConfig);
items: MenuItem[];
append(item: MenuItem): void;
remove(item: MenuItem): void;
insert(item: MenuItem, atPosition: number): void;
removeAt(index: number): void;
popup(x: number, y: number): void;
// since v0.10.0-rc1
createMacBuiltin(appname: string, options?: HideMenusOptions): void;
}
export interface ShortcutOption {
key: string;
active: Function;
failed: Function;
}
export class Shortcut extends EventEmitter {
constructor(option: ShortcutOption);
key: string;
active: Function;
failed: Function;
}
export interface WindowManifestOptions {
title?: string;
icon?: string;
toolbar?: boolean;
frame?: boolean;
width?: number;
height?: number;
position?: string;
min_width?: number;
min_height?: number;
max_width?: number;
max_height?: number;
}
export class Window extends EventEmitter {
static get(windowObject?: any): Window;
static open(url: string, options?: WindowManifestOptions): Window;
x: number;
y: number;
width: number;
height: number;
title: string;
menu: Menu;
isFullScreen: boolean;
isKioskMode: boolean;
zoomLevel: number;
moveTo(x: number, y: number): void;
moveBy(x: number, y: number): void;
resizeTo(width: number, height: number): void;
resizeBy(width: number, height: number): void;
focus(): void;
blur(): void;
show(): void;
hide(): void;
close(force?: boolean): void;
reload(): void;
reloadIgnoringCache(): void;
maximize(): void;
unmaximize(): void;
minimize(): void;
restore(): void;
enterFullscreen(): void;
leaveFullscreen(): void;
toggleFullscreen(): void;
enterKioskMode(): void;
leaveKioskMode(): void;
toggleKioskMode(): void;
showDevTools(id?: string, headless?: boolean): void;
showDevTools(id: HTMLIFrameElement, headless?: boolean): void;
closeDevTools(): void;
isDevToolsOpen(): boolean;
setMaximumSize(width: number, height: number): void;
setMinimumSize(width: number, height: number): void;
setResizable(resizable: boolean): void;
setAlwaysOnTop(top: boolean): void;
setPosition(position: string): void;
setShowInTaskbar(show: boolean): void;
requestAttention(attention: boolean): void;
requestAttention(attention: number): void;
capturePage(callback: Function, imageformat?: string): void;
capturePage(callback: Function, config_object: { format: string; datatype: string }): void;
setProgressBar(progress: number): void;
setBadgeLabel(label: string): void;
eval(frame: HTMLIFrameElement, script: string): void;
}
export interface App {
argv: any;
fullArgv: any;
dataPath: string;
manifest: any;
clearCache(): void;
closeAllWindows(): void;
crashBrowser(): void;
crashRenderer(): void;
getProxyForURL(url: string): void;
quit(): void;
setCrashDumpDir(dir: string): void;
addOriginAccessWhitelistEntry(
sourceOrigin: string
, destinationProtocol: string
, destinationHost: string
, allowDestinationSubdomains: boolean
): void;
removeOriginAccessWhitelistEntry(
sourceOrigin: string
, destinationProtocol: string
, destinationHost: string
, allowDestinationSubdomains: boolean
): void;
registerGlobalHotKey(shortcut: Shortcut): void;
unregisterGlobalHotKey(shortcut: Shortcut): void;
}
export class Clipboard {
static get(): Clipboard;
get(type?: string): string;
set(data: string, type?: string): void;
clear(): void;
}
export interface TrayOption {
title?: string;
tooltip?: string;
icon?: string;
alticon?: string;
menu?: Menu;
}
export class Tray implements TrayOption {
constructor(option: TrayOption);
title: string;
tooltip: string;
icon: string;
alticon: string;
menu: Menu;
remove(): void;
}
interface Shell {
openExternal(uri: string): void;
openItem(file_path: string): void;
showItemInFolder(file_path: string): void;
}
export var App: App;
export var Shell: Shell;
}