-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathwindow.js
110 lines (89 loc) · 2.56 KB
/
window.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
define( [ "nwGui", "nwWindow", "ember" ], function( nwGui, nwWindow, Ember ) {
var get = Ember.get,
set = Ember.set;
var ismaximize = false;
function deferEvent( fn ) {
return function() {
var args = arguments;
setTimeout(function() {
fn.apply( null, args );
}, 10 );
};
}
var timeout = null;
function delegateSave() {
clearTimeout( timeout );
timeout = setTimeout( this.save.bind( this ), 100 );
}
function onResize( width, height ) {
if ( ismaximize ) { return; }
set( this, "width", width );
set( this, "height", height );
delegateSave.call( this );
}
function onMove( x, y ) {
if ( ismaximize ) { return; }
set( this, "x", x );
set( this, "y", y );
delegateSave.call( this );
}
function onMaximize() {
ismaximize = true;
setTimeout( function() {
ismaximize = false;
}, 20 );
}
function restoreWindow( nwWindow ) {
var width = get( this, "width" ),
height = get( this, "height" );
if ( width !== null && height !== null ) {
nwWindow.resizeTo( width, height );
}
var x = get( this, "x" ),
y = get( this, "y" );
if ( x !== null && y !== null ) {
nwWindow.moveTo( x, y );
}
}
function resetWindow() {
set( this, "width", null );
set( this, "height", null );
set( this, "x", null );
set( this, "y", null );
this.save();
}
Ember.Application.initializer({
name: "window",
after: [ "store", "nwjs" ],
initialize: function( container, application ) {
var store = container.lookup( "store:main" );
// wait for the promise to resolve first
application.deferReadiness();
store.find( "window" )
.then(function( records ) {
return records.content.length
? records.objectAt( 0 )
: store.createRecord( "window", { id: 1 } ).save();
})
.then(function( Window ) {
container.register( "record:window", Window, { instantiate: false } );
// reset window
if ( nwGui.App.fullArgv.indexOf( "--reset-window" ) >= 0 ) {
resetWindow.call( Window );
} else {
restoreWindow.call( Window, nwWindow );
}
// also listen for the maximize events
// we don't want to save the window size+pos after these events
nwWindow.on( "maximize", onMaximize );
nwWindow.on( "unmaximize", onMaximize );
// resize and move events need to be defered
// the maximize events are triggered afterwards
nwWindow.on( "resize", deferEvent( onResize.bind( Window ) ) );
nwWindow.on( "move", deferEvent( onMove.bind( Window ) ) );
// now we're ready
application.advanceReadiness();
});
}
});
});