-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
236 lines (210 loc) · 6.5 KB
/
main.js
File metadata and controls
236 lines (210 loc) · 6.5 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const { app, BrowserWindow, ipcMain, Tray, TouchBar } = require("electron");
const fs = require("fs");
const path = require("path");
const {
setBearer,
setActiveApp,
getActiveApp,
saveBounds,
savePosition,
getWindowSettings,
getPosition,
getAllApps,
clearStorage,
} = require("./core/store");
require("./core/messages");
const appMenu = require("./core/menu");
let workRenderer, tray;
let allApps, touchBar;
const createTray = () => {
tray = new Tray(`${__dirname}/app/assets/images/trayTemplate@2x.png`);
tray.setToolTip("Shift + Click to quit");
tray.on("click", (e) => {
if (e.shiftKey) {
app.quit();
} else {
workRenderer.isVisible() ? workRenderer.hide() : workRenderer.show();
}
});
};
const workRendererFunction = () => {
const bounds = getWindowSettings();
const position = getPosition();
const rendererOptions = {
width: bounds[0],
height: bounds[1],
x: position[0],
y: position[1],
minWidth: 350,
minHeight: 500,
frame: false,
icon: path.join(__dirname, "./app/assets/images/logo.png"),
show: false,
titleBarStyle: "customButtonsOnHover",
webPreferences: {
// nodeIntegration: false,
// contextIsolation: true,
// sandbox: true,
// enableRemoteModule: false,
webviewTag: true,
preload: path.join(__dirname, "./app/preload.js"),
},
};
appMenu();
workRenderer = new BrowserWindow(rendererOptions);
//Start: This block of code is for xApps which are not sandboxed. Ex: Partner xApps which need permission to device camera etc.
workRenderer.webContents.session.webRequest.onHeadersReceived(
{ urls: ["*://*/*"] },
(d, c) => {
if (d.responseHeaders["X-Frame-Options"]) {
delete d.responseHeaders["X-Frame-Options"];
} else if (d.responseHeaders["x-frame-options"]) {
delete d.responseHeaders["x-frame-options"];
}
c({ cancel: false, responseHeaders: d.responseHeaders });
}
);
//End: This block of code is for xApps which are not sandboxed. Ex: Partner xApps which need permission to device camera etc.
workRenderer.loadFile(path.join(__dirname, "./app/work/index.html"));
workRenderer.once("ready-to-show", () => {
workRenderer.on("resized", () => saveBounds(workRenderer.getSize()));
workRenderer.on("moved", () => savePosition(workRenderer.getPosition()));
workRenderer.on("closed", () => {
workRenderer = null;
});
workRenderer.webContents.openDevTools({ mode: "right" });
workRenderer.show();
workRenderer.webContents.on("devtools-closed", () => {
workRenderer.webContents.send("devtool-closed");
});
});
createTray();
if (process.platform === "darwin") {
(async () => {
await TouchBarInit();
})();
}
};
ipcMain.on("from-loader", (e, args) => {
workRenderer !== undefined &&
workRenderer.webContents.send("from-main", args);
});
//shift this method to message module as it doesn't need to redirect now.
// ipcMain.on("save-bearer", (e, args) => {
// setBearer(args.data);
// });
ipcMain.on("save-active-xapp", (e, args) => {
setActiveApp(args);
const data = JSON.parse(args);
workRenderer !== undefined &&
workRenderer.setTitle("Workspace (" + data.name + ")");
});
// We can delete this title ipc call, as there is no titlebar in the UI any more!!
// Decided not to delete it, as the title still shows up when user right clicks on the app icon in taskbar for example.
ipcMain.on("title", (e, title) => {
if (workRenderer !== undefined) {
if (title === "") {
const data = JSON.parse(getActiveApp());
workRenderer.setTitle("Workspace (" + data.name + ")");
} else {
workRenderer.setTitle(title);
}
}
});
app.whenReady().then(() => {
selectConsole();
workRendererFunction();
// app.setAsDefaultProtocolClient("xappbuilder");
app.on("activate", async () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
workRendererFunction();
}
});
});
ipcMain.on("TouchBarReInit", async () => {
if (process.platform === "darwin") {
await TouchBarInit();
}
});
const TouchBarInit = async () => {
const touchBarValue = [];
touchBarValue.push(
new TouchBar.TouchBarButton({
icon: `${__dirname}/app/assets/images/refresh@2x.png`,
click: () => {
workRenderer.webContents.send("refresh");
},
})
);
allApps = await getAllApps(true);
if (!allApps.error && allApps !== 1) {
for (var i = 0; i < allApps.length; i++) {
if (allApps[i].canLaunch) {
const temp = {
uuid: allApps[i].uuid,
canLaunch: allApps[i].canLaunch,
xapp: allApps[i].xapp,
icon: allApps[i].icon,
name: allApps[i].name,
};
const obj = {};
obj.label = allApps[i].name;
obj.click = () => {
workRenderer.webContents.send("touched", temp);
};
touchBarValue.push(new TouchBar.TouchBarButton(obj));
}
}
touchBar = new TouchBar({ items: touchBarValue });
workRenderer.setTouchBar(touchBar);
}
};
ipcMain.on("logout", (e, args) => {
touchBarValue = [];
touchBar = new TouchBar({ items: touchBarValue });
workRenderer.setTouchBar(touchBar);
clearStorage();
});
ipcMain.on("devTool-open", (e, args) => {
if (process.platform === "darwin") {
workRenderer.webContents.openDevTools();
} else {
workRenderer.close();
}
});
const selectConsole = () => {
// Get app directory
// on OSX it's /Users/Yourname/Library/Application Support/AppName
try {
const userDataPath = app.getPath("userData");
const prefPath = path.join(userDataPath, "Preferences");
if (fs.existsSync(prefPath)) {
const prefs = JSON.parse(fs.readFileSync(prefPath, "utf-8"));
if (
prefs.electron.devtools.preferences["panel-selectedTab"] !==
JSON.stringify("console")
) {
const bounds = getWindowSettings();
if (bounds[0] === 1360 && bounds[1] === 780) {
prefs.electron.devtools = {
preferences: {
"InspectorView.splitViewState": JSON.stringify({
vertical: { size: 660 },
}),
},
};
}
prefs.electron.devtools = {
preferences: {
"panel-selectedTab": JSON.stringify("console"),
},
};
fs.writeFileSync(prefPath, JSON.stringify(prefs));
}
}
} catch (err) {
console.log("err ", err);
}
};