Skip to content

Commit

Permalink
updater complete
Browse files Browse the repository at this point in the history
  • Loading branch information
codezerro committed Nov 12, 2024
1 parent 0e2356c commit 0796c29
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 151 deletions.
1 change: 1 addition & 0 deletions electron-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ buildDependenciesFromSource: true

extraResources:
- './splashscreen.html'
- './updaterScreen.html'
- 'resources/**'
- './roboticsacademy.db'
files:
Expand Down
Binary file modified roboticsacademy.db
Binary file not shown.
149 changes: 132 additions & 17 deletions src/main/appWindow.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,63 @@
import { app, BrowserWindow } from 'electron'
import { is } from '@electron-toolkit/utils'
import { app, BrowserWindow, shell } from 'electron'
import { join } from 'path'

export function createUpdaterWindow(): BrowserWindow {
export function createSplashWindow(info): BrowserWindow {
const win = new BrowserWindow({
width: 525,
height: 300,
frame: false,
transparent: true,
alwaysOnTop: true,
webPreferences: {
devTools: !app.isPackaged,
nodeIntegration: false,
webSecurity: true,
sandbox: false,
contextIsolation: true
}
})

const splashScreenSrc = app.isPackaged
? join(process.resourcesPath, 'splashscreen.html')
: join(__dirname, './../../', 'splashscreen.html')

win.loadFile(splashScreenSrc)

if (!app.isPackaged) {
// win.webContents.openDevTools()
} else win.webContents.openDevTools = () => {}

win.webContents.on('did-finish-load', () => {
win.webContents
.executeJavaScript(
`
const version = document.getElementById('version')
version.textContent = '${info.appVersion}'
`
)
.then((result) => {
console.log('Executed in renderer:', result)
})
.catch(console.error)
})

return win
}

export function createUpdaterWindow(version: string): BrowserWindow {
const win = new BrowserWindow({
width: 500,
height: 250,
frame: false,
transparent: true,
show: false,
resizable: false,
maximizable: false,
alwaysOnTop: true,
x: 0,
y: 0,
webPreferences: {
preload: join(__dirname, './../preload/index.js'),
devTools: !app.isPackaged,
nodeIntegration: false,
webSecurity: true,
sandbox: false,
Expand All @@ -25,19 +71,88 @@ export function createUpdaterWindow(): BrowserWindow {

win.loadFile(updaterScreenSrc)

// win.webContents.on('did-finish-load', () => {
// win.webContents
// .executeJavaScript(
// `
// const version = document.getElementById('version')
// version.textContent = '${appVersion}'
// `
// )
// .then((result) => {
// console.log('Executed in renderer:', result)
// })
// .catch(console.error)
// })
// dev tool
if (!app.isPackaged) {
// win.webContents.openDevTools()
} else win.webContents.openDevTools = () => {}

win.webContents.on('did-finish-load', () => {
win.webContents
.executeJavaScript(
`
const version = document.getElementById('update-version')
version.textContent = '${version}'
`
)
.then((result) => {
console.log('Executed in renderer:', result)
})
.catch(console.error)
})

return win
}

export const createWindow = async (): Promise<BrowserWindow> => {
let mainWindow: BrowserWindow | null =
new BrowserWindow({
width: 1280,
height: 720,
minWidth: 1280,
minHeight: 720,
show: false,
frame: false,
alwaysOnTop: false,
transparent: false,
focusable: true,
icon: join(__dirname, './../../resources/icons/icon.png'),
autoHideMenuBar: true,
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
devTools: !app.isPackaged,
nodeIntegration: false,
webSecurity: true,
sandbox: false,
contextIsolation: true
}
}) || null

//
mainWindow.webContents.session.webRequest.onHeadersReceived((details, callback) => {
if (details.responseHeaders === undefined) return
callback({
responseHeaders: Object.fromEntries(
Object.entries(details.responseHeaders).filter(
(header) => !/x-frame-options/i.test(header[0])
)
)
})
})
mainWindow.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url)
return { action: 'deny' }
})

// Load the remote URL for development or the local html file for production.
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
} else {
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
}

// dev tool
if (!app.isPackaged) {
// mainWindow.webContents.openDevTools()
} else mainWindow.webContents.openDevTools = () => {}

// window resize/close func
mainWindow.on('closed', (_e) => {
mainWindow = null
})
mainWindow.on('maximize', () => {})
mainWindow.on('unmaximize', () => {})
// Event listener for when the window is restored from minimized state
mainWindow.on('restore', () => {})

return mainWindow
}
138 changes: 25 additions & 113 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
updateCommands,
updateCommandUtils
} from './db'
import { createUpdaterWindow } from './appWindow'
import { createSplashWindow, createUpdaterWindow, createWindow } from './appWindow'

const isMac = process.platform === 'darwin'
let mainWindow: BrowserWindow | null = null
Expand All @@ -43,112 +43,16 @@ autoUpdater.autoDownload = false
// connected to database
export const db: Database = dbInit()

// splash screen
function createSplashWindow(): BrowserWindow {
const win = new BrowserWindow({
width: 525,
height: 300,
frame: false,
transparent: true,
alwaysOnTop: true,
webPreferences: {
nodeIntegration: false,
webSecurity: true,
sandbox: false,
contextIsolation: true
}
})

const splashScreenSrc = app.isPackaged
? join(process.resourcesPath, 'splashscreen.html')
: join(__dirname, './../../', 'splashscreen.html')

win.loadFile(splashScreenSrc)

win.webContents.on('did-finish-load', () => {
win.webContents
.executeJavaScript(
`
const version = document.getElementById('version')
version.textContent = '${appVersion}'
`
)
.then((result) => {
console.log('Executed in renderer:', result)
})
.catch(console.error)
})

return win
}

// updater window

// main window
const createWindow = async (): Promise<BrowserWindow> => {
mainWindow = new BrowserWindow({
width: 1280,
height: 720,
minWidth: 1280,
minHeight: 720,
show: false,
frame: false,
alwaysOnTop: false,
transparent: false,
focusable: true,
icon: join(__dirname, './../../resources/icons/icon.png'),
autoHideMenuBar: true,
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
nodeIntegration: false,
webSecurity: true,
sandbox: false,
contextIsolation: true
}
})

//
mainWindow.webContents.session.webRequest.onHeadersReceived((details, callback) => {
if (details.responseHeaders === undefined) return
callback({
responseHeaders: Object.fromEntries(
Object.entries(details.responseHeaders).filter(
(header) => !/x-frame-options/i.test(header[0])
)
)
})
})
mainWindow.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url)
return { action: 'deny' }
})

// Load the remote URL for development or the local html file for production.
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
} else {
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
}

// window resize/close func
mainWindow.on('closed', (_e) => {
mainWindow = null
})
mainWindow.on('maximize', () => {})
mainWindow.on('unmaximize', () => {})
// Event listener for when the window is restored from minimized state
mainWindow.on('restore', () => {})

return mainWindow
}

app.whenReady().then(async () => {
// disable http-cache
app.commandLine.appendSwitch('disable-http-cache')

// check update
autoUpdater.checkForUpdates()

try {
autoUpdater.checkForUpdates()
} catch (error) {
console.log(error)
}
//Store data
await insertCommandData(db)
await insertCommandUtilsData(db)
Expand Down Expand Up @@ -224,6 +128,8 @@ app.whenReady().then(async () => {
//* App Window Resize
// minimize the window
ipcMain.on('app_window:MINIMIZE', (_event) => {
if (mainWindow === null || mainWindow === undefined) return

if (!mainWindow?.isMinimized()) {
mainWindow?.minimize()
}
Expand Down Expand Up @@ -401,24 +307,29 @@ app.whenReady().then(async () => {
ipcMain.handle('updater:OPEN_LINK', (_event, url: string) => {
try {
shell.openExternal(url)
} catch (error) {}
} catch (error) {
console.log(error)
}
})
ipcMain.handle('updater:CLOSE_WINDOW', (_event) => {
try {
if (updaterWindow) updaterWindow.destroy()
if (updaterWindow != null) updaterWindow.destroy()
} catch (error) {}
})
//@ Disappering splash screen and show main screen after 3 seconds.
try {
const splashScreen: BrowserWindow = createSplashWindow()
updaterWindow = createUpdaterWindow()
const mainScreen: BrowserWindow = await createWindow()
const splashScreen: BrowserWindow = createSplashWindow({ appVersion })
// const mainScreen: BrowserWindow = await createWindow()

mainWindow = await createWindow()
// updaterWindow.show()

updaterWindow.show()
mainScreen.once('ready-to-show', () => {
mainWindow?.once('ready-to-show', () => {
setTimeout(
() => {
// mainScreen.show()
// updaterWindow = createUpdaterWindow('2.0.1')
// updaterWindow.show()
mainWindow?.show()
splashScreen.destroy()
},
app.isPackaged ? 3000 : 0
Expand Down Expand Up @@ -459,7 +370,8 @@ app.on('window-all-closed', (_e) => {

// Auto Updater
autoUpdater.on('update-available', (info) => {
console.log('====================================')
console.log('info ', info)
console.log('====================================')
updaterWindow = createUpdaterWindow(info.version)
setTimeout(() => {
updaterWindow?.show()
}, 10 * 1000)
})
2 changes: 1 addition & 1 deletion src/renderer/src/components/utlits/TopBar.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Dispatch, FC, SetStateAction, useState } from 'react'
import {
WindowMinIcon,
WindowMaximizeIcon,
WindowCloseIcon,
WindowUnMaximizeIcon
} from '@renderer/assets/icons/Icons'
import { Dispatch, FC, SetStateAction, useState } from 'react'
import PropTypes from 'prop-types'
import { ResponseStatus } from '@renderer/utils/enums'
import { Logo } from '@renderer/assets'
Expand Down
Loading

0 comments on commit 0796c29

Please sign in to comment.