-
Notifications
You must be signed in to change notification settings - Fork 38
/
main.js
177 lines (173 loc) · 5.36 KB
/
main.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
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
const {app, shell, BrowserWindow, ipcMain, dialog, clipboard } = require('electron')
const contextMenu = require('electron-context-menu');
const path = require("path")
const Breadmachine = require('breadmachine')
const packagejson = require('./package.json')
const is_mac = process.platform.startsWith("darwin")
contextMenu({ showSaveImageAs: true });
var mainWindow;
var root_url;
var wins = {}
var pinned = {}
var theme = "default";
const titleBarOverlay = (theme) => {
if (is_mac) {
return false
} else {
if (theme === "dark") {
return {
color: "#111",
symbolColor: "white"
}
} else if (theme === "default") {
return {
color: "white",
symbolColor: "black"
}
}
return {
color: "white",
symbolColor: "black"
}
}
}
function createWindow (port) {
mainWindow = new BrowserWindow({
titleBarStyle : "hidden",
titleBarOverlay : titleBarOverlay(),
minWidth: 190,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
},
})
root_url = `http://localhost:${port}`
mainWindow.loadURL(root_url)
mainWindow.maximize();
}
class Socket {
push(msg) {
console.log("msg", msg)
}
}
const breadmachine = new Breadmachine();
app.whenReady().then(async () => {
await breadmachine.init({
theme,
config: path.resolve(__dirname, "breadboard.yaml"),
socket: new Socket(),
version: packagejson.version,
releases: {
feed: "https://github.com/cocktailpeanut/breadboard/releases.atom",
url: "https://github.com/cocktailpeanut/breadboard/releases"
},
onconnect: (session) => {
// Request handlers for api.js
breadmachine.ipc[session].handle("theme", (_session, _theme) => {
// set the theme for ALL windows
breadmachine.ipc[_session].theme = _theme
let all = BrowserWindow.getAllWindows()
for(win of all) {
if (win.setTitleBarOverlay) {
const overlay = titleBarOverlay(breadmachine.ipc[_session].theme)
win.setTitleBarOverlay(overlay)
}
}
})
breadmachine.ipc[session].handle('debug', (_session) => {
// open debug console for only the current window
let win = wins[_session]
if (win) {
win.openDevTools()
}
})
breadmachine.ipc[session].handle('select', async (_session) => {
// only open one dialog
let res = await dialog.showOpenDialog({ properties: ['openDirectory', 'showHiddenFiles'] })
if (!res.canceled && res.filePaths && res.filePaths.length > 0) {
return res.filePaths
}
})
breadmachine.ipc[session].handle('open', async (_session, file_path) => {
// only one
await shell.showItemInFolder(file_path)
})
breadmachine.ipc[session].handle('pinned', (_session) => {
let win = wins[_session]
let ontop = win.isAlwaysOnTop()
if (win) {
return { pinned: ontop }
} else {
return { pinned: false }
}
})
breadmachine.ipc[session].handle('pin', (_session) => {
// pin the current window ONLY
let win = wins[_session]
if (win) {
let pinned = win.isAlwaysOnTop()
if (pinned) {
win.setAlwaysOnTop(false)
} else {
win.setAlwaysOnTop(true, 'pop-up-menu');
}
}
})
}
})
// Request handlers for preload.js
ipcMain.handle("ondragstart", (event, filePaths) => {
if (event && event.sender) {
event.sender.startDrag({
files: filePaths,
icon: filePaths[0]
})
}
})
app.on('web-contents-created', (event, webContents) => {
let wc = webContents
// override the user-agent header to the current breadboard tab
webContents.session.webRequest.onBeforeSendHeaders((details, callback) => {
let connection = `breadboard connection ${details.webContents.id}`
let win = details.webContents.getOwnerBrowserWindow()
wins[connection] = win
details.requestHeaders['User-Agent'] = connection
callback({ requestHeaders: details.requestHeaders })
})
webContents.setWindowOpenHandler((config) => {
let url = config.url
let features = config.features
let params = new URLSearchParams(features.split(",").join("&"))
let win = wc.getOwnerBrowserWindow()
let [width, height] = win.getSize()
let [x,y] = win.getPosition()
if (features && features.startsWith("popup")) {
return {
action: 'allow',
outlivesOpener: true,
overrideBrowserWindowOptions: {
width: (params.get("width") ? parseInt(params.get("width")) : width),
height: (params.get("height") ? parseInt(params.get("height")) : height),
x: x + 30,
y: y + 30,
parent: null,
titleBarStyle : "hidden",
titleBarOverlay : titleBarOverlay(),
webPreferences: {
preload: path.join(__dirname, 'preload.js')
},
}
}
} else {
shell.openExternal(url);
}
return { action: 'deny' };
});
})
createWindow(breadmachine.port)
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow(breadmachine.port)
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
})