@atom-js-org/runtime 0.2.0-alpha.0 → 0.4.0-alpha.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.
@@ -1,147 +0,0 @@
1
- ObjC.import('Cocoa');
2
- ObjC.import('WebKit');
3
-
4
- let atomWindow = null;
5
- let atomWebView = null;
6
- let atomWindowDelegate = null;
7
- let atomNavigationDelegate = null;
8
-
9
-
10
- function emitHostEvent(payload) {
11
- try {
12
- const line = `__ATOMJS_EVENT__${JSON.stringify(payload)}\n`;
13
- const data = $(line).dataUsingEncoding($.NSUTF8StringEncoding);
14
- $.NSFileHandle.fileHandleWithStandardOutput.writeData(data);
15
- } catch (_) {}
16
- }
17
-
18
- function webViewUrl(webView) {
19
- try {
20
- if (!webView || !webView.URL) return '';
21
- return ObjC.unwrap(webView.URL.absoluteString) || '';
22
- } catch (_) {
23
- return '';
24
- }
25
- }
26
-
27
- function createNavigationDelegate() {
28
- if (!$.AtomJSNavigationDelegate) {
29
- ObjC.registerSubclass({
30
- name: 'AtomJSNavigationDelegate',
31
- protocols: ['WKNavigationDelegate'],
32
- methods: {
33
- 'webView:didStartProvisionalNavigation:'(webView) {
34
- emitHostEvent({ type: 'did-start-loading', url: webViewUrl(webView) });
35
- },
36
- 'webView:didFinishNavigation:'(webView) {
37
- emitHostEvent({ type: 'did-finish-load', url: webViewUrl(webView) });
38
- },
39
- 'webView:didFailNavigation:withError:'(webView, _navigation, error) {
40
- emitHostEvent({
41
- type: 'did-fail-load',
42
- url: webViewUrl(webView),
43
- error: error ? ObjC.unwrap(error.localizedDescription) : 'Navigation failed'
44
- });
45
- },
46
- 'webView:didFailProvisionalNavigation:withError:'(webView, _navigation, error) {
47
- emitHostEvent({
48
- type: 'did-fail-load',
49
- url: webViewUrl(webView),
50
- error: error ? ObjC.unwrap(error.localizedDescription) : 'Navigation failed'
51
- });
52
- }
53
- }
54
- });
55
- }
56
- return $.AtomJSNavigationDelegate.alloc.init;
57
- }
58
-
59
- function readUtf8(filePath) {
60
- const value = $.NSString.stringWithContentsOfFileEncodingError(
61
- $(String(filePath)),
62
- $.NSUTF8StringEncoding,
63
- null
64
- );
65
- if (!value) throw new Error(`Unable to read AtomJS window configuration: ${filePath}`);
66
- return ObjC.unwrap(value);
67
- }
68
-
69
- function createWindowDelegate() {
70
- if (!$.AtomJSWindowDelegate) {
71
- ObjC.registerSubclass({
72
- name: 'AtomJSWindowDelegate',
73
- protocols: ['NSWindowDelegate'],
74
- methods: {
75
- 'windowWillClose:'() {
76
- $.NSApplication.sharedApplication.terminate(null);
77
- }
78
- }
79
- });
80
- }
81
- return $.AtomJSWindowDelegate.alloc.init;
82
- }
83
-
84
- function makeWindow(config) {
85
- const width = Math.max(320, Number(config.width) || 800);
86
- const height = Math.max(240, Number(config.height) || 600);
87
- const rect = $.NSMakeRect(0, 0, width, height);
88
-
89
- let styleMask = $.NSTitledWindowMask |
90
- $.NSClosableWindowMask |
91
- $.NSMiniaturizableWindowMask;
92
- if (config.resizable !== false) styleMask |= $.NSResizableWindowMask;
93
-
94
- const win = $.NSWindow.alloc.initWithContentRectStyleMaskBackingDefer(
95
- rect,
96
- styleMask,
97
- $.NSBackingStoreBuffered,
98
- false
99
- );
100
- win.releasedWhenClosed = false;
101
- win.title = $(String(config.title || 'AtomJS App'));
102
- win.center;
103
-
104
- const contentController = $.WKUserContentController.alloc.init;
105
- const bridgeSource = String(config.bridgeScript || '');
106
- if (bridgeSource) {
107
- const userScript = $.WKUserScript.alloc.initWithSourceInjectionTimeForMainFrameOnly(
108
- $(bridgeSource),
109
- $.WKUserScriptInjectionTimeAtDocumentStart,
110
- true
111
- );
112
- contentController.addUserScript(userScript);
113
- }
114
-
115
- const webConfiguration = $.WKWebViewConfiguration.alloc.init;
116
- webConfiguration.userContentController = contentController;
117
-
118
- const webView = $.WKWebView.alloc.initWithFrameConfiguration(rect, webConfiguration);
119
- atomNavigationDelegate = createNavigationDelegate();
120
- webView.navigationDelegate = atomNavigationDelegate;
121
- webView.autoresizingMask = $.NSViewWidthSizable | $.NSViewHeightSizable;
122
-
123
- const url = $.NSURL.URLWithString($(String(config.url)));
124
- if (!url) throw new Error(`Invalid AtomJS URL: ${config.url}`);
125
- webView.loadRequest($.NSURLRequest.requestWithURL(url));
126
-
127
- atomWindowDelegate = createWindowDelegate();
128
- win.delegate = atomWindowDelegate;
129
- win.contentView.addSubview(webView);
130
-
131
- atomWindow = win;
132
- atomWebView = webView;
133
- return win;
134
- }
135
-
136
- function run(argv) {
137
- if (!argv || argv.length < 1) throw new Error('AtomJS macOS host expected a configuration path');
138
- const config = JSON.parse(readUtf8(argv[0]));
139
- const app = $.NSApplication.sharedApplication;
140
- app.setActivationPolicy($.NSApplicationActivationPolicyRegular);
141
-
142
- const win = makeWindow(config);
143
- win.makeKeyAndOrderFront(null);
144
- app.activateIgnoringOtherApps(true);
145
- app.run;
146
- return 0;
147
- }