electron-osx-spaces 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +77 -0
- package/binding.gyp +20 -0
- package/index.d.ts +24 -0
- package/index.js +49 -0
- package/package.json +66 -0
- package/src/spaces.mm +172 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Toru Nayuki
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# electron-osx-spaces
|
|
2
|
+
|
|
3
|
+
Save and restore Electron window positions across macOS Spaces (virtual desktops).
|
|
4
|
+
|
|
5
|
+
## Why?
|
|
6
|
+
|
|
7
|
+
Electron does not participate in macOS State Restoration, so windows always open on the current Space. This package captures AppKit's native restoration data and replays it on the next launch, putting windows back on the Space where they were saved.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install electron-osx-spaces
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Requires Xcode Command Line Tools for the native addon build.
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
const { app, BrowserWindow } = require('electron');
|
|
21
|
+
const spaces = require('electron-osx-spaces');
|
|
22
|
+
|
|
23
|
+
let win;
|
|
24
|
+
|
|
25
|
+
app.whenReady().then(() => {
|
|
26
|
+
win = new BrowserWindow({ width: 800, height: 600 });
|
|
27
|
+
|
|
28
|
+
// Restore from previously saved data
|
|
29
|
+
const saved = loadState(); // your persistence layer
|
|
30
|
+
if (saved) {
|
|
31
|
+
spaces.restoreState(win, saved, { restoreSpace: true });
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
app.on('before-quit', () => {
|
|
36
|
+
// Save the window's Space + frame state
|
|
37
|
+
const data = spaces.encodeState(win);
|
|
38
|
+
if (data) {
|
|
39
|
+
saveState(data); // store as Buffer or base64
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## API
|
|
45
|
+
|
|
46
|
+
### `spaces.encodeState(win: BrowserWindow): Buffer | null`
|
|
47
|
+
|
|
48
|
+
Encodes the window's current frame and Space information into an opaque Buffer. Returns `null` on non-macOS platforms or if encoding fails.
|
|
49
|
+
|
|
50
|
+
### `spaces.restoreState(win: BrowserWindow, data: Buffer, options?: RestoreOptions): boolean`
|
|
51
|
+
|
|
52
|
+
Restores the window's frame and Space from a previously encoded Buffer. Returns `true` if restoration succeeded.
|
|
53
|
+
|
|
54
|
+
#### Options
|
|
55
|
+
|
|
56
|
+
| Option | Type | Default | Description |
|
|
57
|
+
|--------|------|---------|-------------|
|
|
58
|
+
| `restoreSpace` | `boolean` | `true` | Whether to restore the Space (virtual desktop) |
|
|
59
|
+
|
|
60
|
+
## How it works
|
|
61
|
+
|
|
62
|
+
1. `encodeState` calls `NSWindow.encodeRestorableStateWithCoder:` to capture AppKit's native restoration data (frame + Space info) as a binary archive
|
|
63
|
+
2. Your app persists the binary (e.g. as base64 in a JSON file)
|
|
64
|
+
3. On next launch, `restoreState` calls `NSWindow.restoreStateWithCoder:` to replay the data, and macOS moves the window to the original Space
|
|
65
|
+
|
|
66
|
+
An `NSKeyedArchiverDelegate` is used to skip objects that don't support `NSSecureCoding` (such as Electron's internal NSView subclasses).
|
|
67
|
+
|
|
68
|
+
On macOS 15+, a workaround for broken `NSWindowRestoresWorkspaceAtLaunch` is applied via a private API override. This makes the package **incompatible with Mac App Store** distribution.
|
|
69
|
+
|
|
70
|
+
## Platform support
|
|
71
|
+
|
|
72
|
+
- **macOS**: Full functionality
|
|
73
|
+
- **Other platforms**: No-op (functions return `null` / `false`)
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
MIT
|
package/binding.gyp
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"targets": [
|
|
3
|
+
{
|
|
4
|
+
"target_name": "spaces",
|
|
5
|
+
"conditions": [
|
|
6
|
+
["OS=='mac'", {
|
|
7
|
+
"sources": ["src/spaces.mm"],
|
|
8
|
+
"xcode_settings": {
|
|
9
|
+
"OTHER_CPLUSPLUSFLAGS": ["-std=c++17", "-ObjC++"],
|
|
10
|
+
"OTHER_LDFLAGS": ["-framework Cocoa"]
|
|
11
|
+
},
|
|
12
|
+
"include_dirs": [
|
|
13
|
+
"<!@(node -p \"require('node-addon-api').include\")"
|
|
14
|
+
],
|
|
15
|
+
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"]
|
|
16
|
+
}]
|
|
17
|
+
]
|
|
18
|
+
}
|
|
19
|
+
]
|
|
20
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { BrowserWindow } from 'electron';
|
|
2
|
+
|
|
3
|
+
export interface RestoreOptions {
|
|
4
|
+
/** Whether to restore the Space (virtual desktop). Defaults to true. */
|
|
5
|
+
restoreSpace?: boolean;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Encode the current window state including Space (virtual desktop) info.
|
|
10
|
+
* Returns a Buffer containing the opaque AppKit restoration data,
|
|
11
|
+
* or null on non-macOS platforms or if encoding fails.
|
|
12
|
+
*/
|
|
13
|
+
export function encodeState(win: BrowserWindow): Buffer | null;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Restore the window's state (frame + Space) from previously encoded data.
|
|
17
|
+
* Returns true if restoration succeeded, false otherwise.
|
|
18
|
+
* No-op on non-macOS platforms.
|
|
19
|
+
*/
|
|
20
|
+
export function restoreState(
|
|
21
|
+
win: BrowserWindow,
|
|
22
|
+
data: Buffer,
|
|
23
|
+
options?: RestoreOptions,
|
|
24
|
+
): boolean;
|
package/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
if (process.platform !== 'darwin') {
|
|
2
|
+
module.exports = {
|
|
3
|
+
encodeState() {
|
|
4
|
+
return null;
|
|
5
|
+
},
|
|
6
|
+
restoreState() {
|
|
7
|
+
return false;
|
|
8
|
+
},
|
|
9
|
+
};
|
|
10
|
+
} else {
|
|
11
|
+
const native = require('./build/Release/spaces.node');
|
|
12
|
+
|
|
13
|
+
module.exports = {
|
|
14
|
+
/**
|
|
15
|
+
* Encode the current window state including Space (virtual desktop) info.
|
|
16
|
+
* Returns a Buffer containing the opaque AppKit restoration data,
|
|
17
|
+
* or null if encoding fails.
|
|
18
|
+
*
|
|
19
|
+
* @param {BrowserWindow} win - Electron BrowserWindow instance
|
|
20
|
+
* @returns {Buffer|null}
|
|
21
|
+
*/
|
|
22
|
+
encodeState(win) {
|
|
23
|
+
try {
|
|
24
|
+
return native.encodeState(win.getNativeWindowHandle());
|
|
25
|
+
} catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Restore the window's state (frame + Space) from previously encoded data.
|
|
32
|
+
*
|
|
33
|
+
* @param {BrowserWindow} win - Electron BrowserWindow instance
|
|
34
|
+
* @param {Buffer} data - Data previously returned by encodeState()
|
|
35
|
+
* @param {object} [options]
|
|
36
|
+
* @param {boolean} [options.restoreSpace=true] - Whether to restore the Space
|
|
37
|
+
* @returns {boolean} true if restoration succeeded
|
|
38
|
+
*/
|
|
39
|
+
restoreState(win, data, options) {
|
|
40
|
+
if (!data) return false;
|
|
41
|
+
try {
|
|
42
|
+
native.restoreState(win.getNativeWindowHandle(), data, options);
|
|
43
|
+
return true;
|
|
44
|
+
} catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "electron-osx-spaces",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Save and restore Electron window Spaces (virtual desktop) positions on macOS",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.js",
|
|
9
|
+
"index.d.ts",
|
|
10
|
+
"src/spaces.mm",
|
|
11
|
+
"binding.gyp"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"install": "node-gyp rebuild",
|
|
15
|
+
"build": "node-gyp rebuild",
|
|
16
|
+
"clean": "node-gyp clean",
|
|
17
|
+
"example": "env -u ELECTRON_RUN_AS_NODE npx electron example/main.js",
|
|
18
|
+
"test": "env -u ELECTRON_RUN_AS_NODE npx electron test/smoke.js",
|
|
19
|
+
"lint": "npx @biomejs/biome check .",
|
|
20
|
+
"lint:fix": "npx @biomejs/biome check --write .",
|
|
21
|
+
"prepare": "husky"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"electron",
|
|
25
|
+
"macos",
|
|
26
|
+
"spaces",
|
|
27
|
+
"virtual-desktop",
|
|
28
|
+
"window-state",
|
|
29
|
+
"restore"
|
|
30
|
+
],
|
|
31
|
+
"author": "Toru Nayuki",
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "https://github.com/tnayuki/electron-osx-spaces.git"
|
|
36
|
+
},
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/tnayuki/electron-osx-spaces/issues"
|
|
39
|
+
},
|
|
40
|
+
"homepage": "https://github.com/tnayuki/electron-osx-spaces#readme",
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"os": [
|
|
45
|
+
"darwin"
|
|
46
|
+
],
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=16.0.0"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"node-addon-api": "^8.0.0"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@biomejs/biome": "^2.4.10",
|
|
55
|
+
"@commitlint/cli": "^20.5.0",
|
|
56
|
+
"@commitlint/config-conventional": "^20.5.0",
|
|
57
|
+
"@electron/rebuild": "^4.0.3",
|
|
58
|
+
"electron": "^40.8.5",
|
|
59
|
+
"husky": "^9.1.7",
|
|
60
|
+
"lint-staged": "^16.4.0",
|
|
61
|
+
"node-gyp": ">=9.0.0"
|
|
62
|
+
},
|
|
63
|
+
"lint-staged": {
|
|
64
|
+
"*.{js,json}": "npx @biomejs/biome check --write --no-errors-on-unmatched"
|
|
65
|
+
}
|
|
66
|
+
}
|
package/src/spaces.mm
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
#import <Cocoa/Cocoa.h>
|
|
2
|
+
#import <napi.h>
|
|
3
|
+
|
|
4
|
+
// NSKeyedArchiverDelegate to skip objects that cannot be securely encoded
|
|
5
|
+
// (NSWindow itself, NSViews such as Electron's BridgedContentView).
|
|
6
|
+
@interface SpacesArchiverDelegate : NSObject <NSKeyedArchiverDelegate>
|
|
7
|
+
@property(nonatomic, assign) NSWindow* window;
|
|
8
|
+
@end
|
|
9
|
+
|
|
10
|
+
@implementation SpacesArchiverDelegate
|
|
11
|
+
|
|
12
|
+
- (id)archiver:(NSKeyedArchiver*)archiver willEncodeObject:(id)object {
|
|
13
|
+
if (object == self.window)
|
|
14
|
+
return nil;
|
|
15
|
+
if ([object isKindOfClass:[NSView class]])
|
|
16
|
+
return nil;
|
|
17
|
+
return object;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
@end
|
|
21
|
+
|
|
22
|
+
// macOS 15 workaround for Space restoration (FB15644170).
|
|
23
|
+
// Override _windowRestorationOptions to return a default-initialized value,
|
|
24
|
+
// which tells AppKit to restore windows to their original Space.
|
|
25
|
+
//
|
|
26
|
+
// On macOS 14 and earlier, NSWindowRestoresWorkspaceAtLaunch UserDefault
|
|
27
|
+
// controls this behavior instead.
|
|
28
|
+
@interface SpacesKeyedUnarchiver : NSKeyedUnarchiver
|
|
29
|
+
@property(nonatomic) BOOL shouldRestoreSpace;
|
|
30
|
+
@end
|
|
31
|
+
|
|
32
|
+
@implementation SpacesKeyedUnarchiver
|
|
33
|
+
|
|
34
|
+
- (id)_windowRestorationOptions {
|
|
35
|
+
if (self.shouldRestoreSpace) {
|
|
36
|
+
return [[NSClassFromString(@"NSWindowRestorationOptions") alloc] init];
|
|
37
|
+
}
|
|
38
|
+
return nil;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
@end
|
|
42
|
+
|
|
43
|
+
static NSWindow* GetNSWindow(Napi::Value handle) {
|
|
44
|
+
auto buf = handle.As<Napi::Buffer<uint8_t>>();
|
|
45
|
+
NSView* view = *reinterpret_cast<NSView* __strong*>(buf.Data());
|
|
46
|
+
return [view window];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Encode the window's restorable state (frame + Space info) into a Buffer.
|
|
50
|
+
// Uses NSKeyedArchiverDelegate to skip NSView objects that don't adopt
|
|
51
|
+
// NSSecureCoding.
|
|
52
|
+
Napi::Value EncodeState(const Napi::CallbackInfo& info) {
|
|
53
|
+
Napi::Env env = info.Env();
|
|
54
|
+
|
|
55
|
+
if (info.Length() < 1 || !info[0].IsBuffer()) {
|
|
56
|
+
Napi::TypeError::New(env, "Expected native window handle (Buffer)")
|
|
57
|
+
.ThrowAsJavaScriptException();
|
|
58
|
+
return env.Undefined();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
NSWindow* win = GetNSWindow(info[0]);
|
|
62
|
+
if (!win) {
|
|
63
|
+
Napi::Error::New(env, "Could not get NSWindow from handle")
|
|
64
|
+
.ThrowAsJavaScriptException();
|
|
65
|
+
return env.Undefined();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@try {
|
|
69
|
+
NSKeyedArchiver* encoder =
|
|
70
|
+
[[NSKeyedArchiver alloc] initRequiringSecureCoding:YES];
|
|
71
|
+
SpacesArchiverDelegate* delegate =
|
|
72
|
+
[[SpacesArchiverDelegate alloc] init];
|
|
73
|
+
delegate.window = win;
|
|
74
|
+
encoder.delegate = delegate;
|
|
75
|
+
|
|
76
|
+
[win encodeRestorableStateWithCoder:encoder];
|
|
77
|
+
[encoder finishEncoding];
|
|
78
|
+
NSData* data = encoder.encodedData;
|
|
79
|
+
|
|
80
|
+
return Napi::Buffer<uint8_t>::Copy(
|
|
81
|
+
env,
|
|
82
|
+
static_cast<const uint8_t*>(data.bytes),
|
|
83
|
+
data.length);
|
|
84
|
+
} @catch (NSException* exception) {
|
|
85
|
+
Napi::Error::New(
|
|
86
|
+
env,
|
|
87
|
+
std::string("encodeRestorableState failed: ") +
|
|
88
|
+
exception.reason.UTF8String)
|
|
89
|
+
.ThrowAsJavaScriptException();
|
|
90
|
+
return env.Undefined();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Restore the window's state (frame + Space) from a previously encoded Buffer.
|
|
95
|
+
Napi::Value RestoreState(const Napi::CallbackInfo& info) {
|
|
96
|
+
Napi::Env env = info.Env();
|
|
97
|
+
|
|
98
|
+
if (info.Length() < 2 || !info[0].IsBuffer() || !info[1].IsBuffer()) {
|
|
99
|
+
Napi::TypeError::New(
|
|
100
|
+
env, "Expected (nativeHandle: Buffer, stateData: Buffer)")
|
|
101
|
+
.ThrowAsJavaScriptException();
|
|
102
|
+
return env.Undefined();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
bool restoreSpace = true;
|
|
106
|
+
if (info.Length() >= 3 && info[2].IsObject()) {
|
|
107
|
+
auto opts = info[2].As<Napi::Object>();
|
|
108
|
+
if (opts.Has("restoreSpace")) {
|
|
109
|
+
restoreSpace = opts.Get("restoreSpace").ToBoolean().Value();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
NSWindow* win = GetNSWindow(info[0]);
|
|
114
|
+
if (!win) {
|
|
115
|
+
Napi::Error::New(env, "Could not get NSWindow from handle")
|
|
116
|
+
.ThrowAsJavaScriptException();
|
|
117
|
+
return env.Undefined();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
auto buf = info[1].As<Napi::Buffer<uint8_t>>();
|
|
121
|
+
NSData* data = [NSData dataWithBytes:buf.Data() length:buf.Length()];
|
|
122
|
+
|
|
123
|
+
@try {
|
|
124
|
+
NSError* error = nil;
|
|
125
|
+
SpacesKeyedUnarchiver* decoder =
|
|
126
|
+
[[SpacesKeyedUnarchiver alloc] initForReadingFromData:data
|
|
127
|
+
error:&error];
|
|
128
|
+
if (error) {
|
|
129
|
+
Napi::Error::New(
|
|
130
|
+
env,
|
|
131
|
+
std::string("Failed to create unarchiver: ") +
|
|
132
|
+
error.localizedDescription.UTF8String)
|
|
133
|
+
.ThrowAsJavaScriptException();
|
|
134
|
+
return env.Undefined();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
decoder.shouldRestoreSpace = restoreSpace;
|
|
138
|
+
|
|
139
|
+
// On macOS < 15, NSWindowRestoresWorkspaceAtLaunch UserDefault
|
|
140
|
+
// controls Space restoration. On macOS 15+, that UserDefault is
|
|
141
|
+
// broken (FB15644170, still unfixed as of macOS 26), so
|
|
142
|
+
// _windowRestorationOptions override is used instead.
|
|
143
|
+
if (restoreSpace) {
|
|
144
|
+
if (@available(macOS 15, *)) {
|
|
145
|
+
// _windowRestorationOptions override handles this
|
|
146
|
+
} else {
|
|
147
|
+
[NSUserDefaults.standardUserDefaults registerDefaults:@{
|
|
148
|
+
@"NSWindowRestoresWorkspaceAtLaunch" : @YES
|
|
149
|
+
}];
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
[win restoreStateWithCoder:decoder];
|
|
154
|
+
} @catch (NSException* exception) {
|
|
155
|
+
Napi::Error::New(
|
|
156
|
+
env,
|
|
157
|
+
std::string("restoreStateWithCoder failed: ") +
|
|
158
|
+
exception.reason.UTF8String)
|
|
159
|
+
.ThrowAsJavaScriptException();
|
|
160
|
+
return env.Undefined();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return env.Undefined();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
167
|
+
exports.Set("encodeState", Napi::Function::New(env, EncodeState));
|
|
168
|
+
exports.Set("restoreState", Napi::Function::New(env, RestoreState));
|
|
169
|
+
return exports;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
NODE_API_MODULE(spaces, Init)
|