Skip to content

Commit 11cf75d

Browse files
committed
Windows and Linux apps.
Auto update;
1 parent 52d577e commit 11cf75d

File tree

8 files changed

+12268
-6958
lines changed

8 files changed

+12268
-6958
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ node_modules
3535

3636
.DS_Store
3737
dist
38+
39+
.env.local
40+
.secrets

app/lib/config.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
const Config = require('electron-config')
22

3-
module.exports = new Config({
3+
const config = new Config({
44
defaults: {
55
lastWindowState: {
6-
width: 1280,
7-
height: 322
6+
width: 1024,
7+
height: 800
88
}
99
}
1010
})
11+
12+
module.exports = config

app/lib/menu.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ const templateWin = [{
9898
submenu: [{
9999
label: 'About ' + appName,
100100
click() {
101-
require('electron').shell.openExternal('https://github.com/Meadowcottage/Playcode/releases/tag/' + appVersion)
101+
require('electron').shell.openExternal('https://github.com/playcode/playcode-desktop/releases/tag/' + appVersion)
102102
}
103103
}, {
104104
label: 'Version ' + appVersion,
@@ -112,12 +112,14 @@ const templateWin = [{
112112
}
113113
}, {
114114
type: 'separator'
115-
}, {
116-
label: 'Changelog',
117-
click() {
118-
require('electron').shell.openExternal('https://github.com/Meadowcottage/Playcode/releases/tag/' + appVersion)
119-
}
120-
}]
115+
},
116+
// {
117+
// label: 'Changelog',
118+
// click() {
119+
// require('electron').shell.openExternal('https://github.com/Meadowcottage/Playcode/releases/tag/' + appVersion)
120+
// }
121+
// }
122+
]
121123
}]
122124

123125
const templateOSX = [{

app/main.js

Lines changed: 131 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ const fs = require('fs')
55
// Electron
66
const electron = require('electron')
77
const {ipcMain} = require('electron')
8+
const logger = require("electron-log")
89

910
const globalShortcut = electron.globalShortcut
1011
const menu = electron.Menu
1112

1213
// App Info
1314
const app = electron.app
15+
1416
const appTitle = app.getName()
15-
const appIsDev = require('electron-is-dev')
1617
const appConfig = require('./lib/config.js')
1718

19+
const appIsDev = require('electron-is-dev')
20+
1821
// Right Click/Context menu contents
1922
require('electron-context-menu')()
2023

@@ -24,40 +27,112 @@ let mainWindow
2427
// If the application is quitting
2528
let isQuitting = false
2629

30+
class AppUpdater {
31+
constructor() {
32+
const { autoUpdater } = require("electron-updater")
33+
34+
logger.transports.file.level = "info"
35+
logger.transports.console.level = "info"
36+
autoUpdater.logger = logger
37+
autoUpdater.disableWebInstaller = true
38+
autoUpdater.autoInstallOnAppQuit = true
39+
autoUpdater.on('update-downloaded', async (info) => {
40+
logger.info('update-downloaded', info)
41+
const isConfirmed = await mainWindow.webContents.executeJavaScript(
42+
`confirm('Update downloaded. Restart now?')`
43+
)
44+
if (isConfirmed) {
45+
autoUpdater.quitAndInstall()
46+
}
47+
})
48+
this.checkForUpdates()
49+
}
50+
async checkForUpdates() {
51+
const { autoUpdater } = require("electron-updater")
52+
53+
await autoUpdater.checkForUpdatesAndNotify({
54+
title: "Update downloaded. Restart now?",
55+
body: "",
56+
})
57+
}
58+
}
59+
2760
// Main Window
2861
function createMainWindow() {
2962

3063
const lastWindowState = appConfig.get('lastWindowState')
64+
65+
const remoteMain = require("@electron/remote/main")
66+
remoteMain.initialize()
67+
3168
const appView = new electron.BrowserWindow({
3269
title: appTitle,
3370
x: lastWindowState.x,
3471
y: lastWindowState.y,
35-
width: lastWindowState.width,
36-
height: lastWindowState.height,
37-
backgroundColor: '#213040',
72+
width: lastWindowState.width || 1024,
73+
height: lastWindowState.height || 800,
3874
// titleBarStyle: 'hidden',
39-
transparent: true,
40-
frame: false,
75+
76+
backgroundColor: '#15222e',
77+
transparent: process.platform !== 'linux',
78+
frame: process.platform === 'linux',
79+
80+
// backgroundColor: '#2ed3ea',
81+
// transparent: false,
82+
// frame: true,
83+
4184
center: true,
4285
movable: true,
4386
resizable: true,
87+
4488
fullscreenable: true,
4589
// autoHideMenuBar: true,
4690

4791
webPreferences: {
92+
webSecurity: true,
4893
nodeIntegration: true,
49-
enableRemoteModule: true,
94+
// enableRemoteModule: true,
5095
contextIsolation: false,
5196
// nativeWindowOpen: true
5297
}
5398
})
99+
100+
remoteMain.enable(appView.webContents)
101+
54102
if (appIsDev) {
103+
// let internal_url = 'https://playcode.io/new';
104+
//
105+
// appView.webContents.on('did-start-loading', function(e) {
106+
// electron.protocol.interceptBufferProtocol('https', function(request, respond) {
107+
// electron.protocol.uninterceptProtocol('https');
108+
// console.log('intercepted', request.url);
109+
//
110+
// if (request.url !== internal_url) {
111+
// console.warn('something went wrong');
112+
// } else {
113+
// let content = fs.readFileSync(__dirname + '/dist' + '/index.html');
114+
// // console.log(content.toString())
115+
// respond(content);
116+
// }
117+
// });
118+
// });
119+
//
120+
//
121+
// appView.loadURL(internal_url)
122+
// appView.loadURL(`peer:///dist/index.html`)
123+
// appView.loadURL('http://localhost:7000/new')
124+
// appView.loadURL('http://192.168.1.130:7070/new')
125+
// appView.loadURL('https://playcode.io/new')
126+
// appView.loadURL(`file://${__dirname}/dist/index.html`)
55127
appView.loadURL('http://localhost:7070/new')
128+
// appView.loadURL('https://playcode.io/new')
56129
} else {
57130
appView.loadURL('https://playcode.io/new')
58131
}
132+
133+
new AppUpdater()
59134

60-
// When window is closed, hide window
135+
// When window is closed, hide window on darwin and quit on other platforms
61136
appView.on('close', e => {
62137
if (!isQuitting) {
63138
e.preventDefault()
@@ -69,10 +144,10 @@ function createMainWindow() {
69144
}
70145
})
71146

147+
// Keep in until next release
72148
ipcMain.handle('minimize', (event, arg) => {
73-
app.hide()
149+
appView.minimize()
74150
})
75-
76151
ipcMain.handle('close', (event, arg) => {
77152
if (!isQuitting) {
78153
if (process.platform === 'darwin') {
@@ -82,31 +157,60 @@ function createMainWindow() {
82157
}
83158
}
84159
})
85-
86160
ipcMain.handle('maximize', (event, arg) => {
87-
if (mainWindow) {
161+
if (appView) {
88162
// mainWindow.maximize()
89-
mainWindow.setFullScreen(!mainWindow.isFullScreen())
163+
if (process.platform === 'darwin') {
164+
appView.setFullScreen(!mainWindow.isFullScreen())
165+
} else {
166+
if (appView.isMaximized()) {
167+
appView.unmaximize()
168+
} else {
169+
appView.maximize()
170+
}
171+
}
90172
}
91173
})
92-
93-
94-
// Enter fullscreen Playcode fullscreen method execution
174+
175+
// Enter fullscreen PlayCode fullscreen method execution
95176
appView.on('enter-full-screen', () => {
96177
appView.webContents.executeJavaScript('document.dispatchEvent( new Event("electronEnteredFullscreen") );')
97178
})
98179

99-
// Exit fullscreen Playcode fullscreen method execution
180+
// Exit fullscreen PlayCode fullscreen method execution
100181
appView.on('leave-full-screen', () => {
101182
appView.webContents.executeJavaScript('document.dispatchEvent( new Event("electronLeavedFullscreen") );')
102183
})
103184

104185
return appView
105186
}
106187

188+
function serveStatic () {
189+
electron.protocol.registerFileProtocol(
190+
'peer',
191+
function(req,callback) {
192+
// console.log(req)
193+
// var file_path = __dirname+'/'+req.url.substring('peer://'.length)
194+
// console.log(file_path)
195+
// cb({path: file_path})
196+
// cb(file_path)
197+
198+
console.log('URL', req.url)
199+
console.log(path.normalize(`${__dirname}/${req.url.substring('peer://'.length)}`))
200+
201+
if (req.url.startsWith('peer://')) {
202+
callback({ path: path.normalize(`${__dirname}/${req.url.substring('peer://'.length)}`) });
203+
} else {
204+
callback(req);
205+
}
206+
})
207+
}
208+
107209
app.on('ready', () => {
210+
// serveStatic()
211+
108212
const version = app.getVersion()
109-
213+
110214
mainWindow = createMainWindow()
111215

112216
// Setting App menu
@@ -118,8 +222,7 @@ app.on('ready', () => {
118222
}
119223

120224
const appPage = mainWindow.webContents
121-
122-
225+
123226
appPage.on('dom-ready', () => {
124227

125228
// console.log('Updated')
@@ -130,11 +233,6 @@ app.on('ready', () => {
130233
// Global Style Additions
131234
appPage.insertCSS(fs.readFileSync(path.join(__dirname, 'app.css'), 'utf8'))
132235

133-
// MacOS ONLY style fixes
134-
if (process.platform === 'darwin') {
135-
appPage.insertCSS('')
136-
}
137-
138236
// Global Code Additions
139237
appPage.executeJavaScript(fs.readFileSync(path.join(__dirname, 'renderer.js'), 'utf8'))
140238

@@ -171,12 +269,14 @@ app.on('ready', () => {
171269

172270
})
173271

174-
// Shortcut to reload the page.
175-
// globalShortcut.register('CmdOrCtrl+R', (item, focusedWindow) => {
176-
// if (focusedWindow) {
177-
// mainWindow.webContents.reload()
178-
// }
179-
// })
272+
if (appIsDev) {
273+
// Shortcut to reload the page.
274+
globalShortcut.register('CmdOrCtrl+R', (item, focusedWindow) => {
275+
if (focusedWindow) {
276+
mainWindow.webContents.reload()
277+
}
278+
})
279+
}
180280
// Shortcut to go back a page.
181281
// globalShortcut.register('Command+Left', ( item, focusedWindow ) => {
182282
// if ( focusedWindow && focusedWindow.webContents.canGoBack() ) {

0 commit comments

Comments
 (0)