header changes and open new window for double click in finder macos (#3652)
* header changes and open new window for double click in finder macos Signed-off-by: Jess Frazelle <github@jessfraz.com> * cleanup Signed-off-by: Jess Frazelle <github@jessfraz.com> * add protocols Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * extend with info.plist Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixes Signed-off-by: Jess Frazelle <github@jessfraz.com> --------- Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
85
src/main.ts
85
src/main.ts
@ -2,7 +2,7 @@
|
||||
// template that ElectronJS provides.
|
||||
|
||||
import dotenv from 'dotenv'
|
||||
import { app, BrowserWindow, ipcMain, dialog, shell } from 'electron'
|
||||
import { app, BrowserWindow, protocol, ipcMain, dialog, shell } from 'electron'
|
||||
import path from 'path'
|
||||
import { Issuer } from 'openid-client'
|
||||
import { Bonjour, Service } from 'bonjour-service'
|
||||
@ -11,6 +11,8 @@ import * as kittycad from '@kittycad/lib/import'
|
||||
import minimist from 'minimist'
|
||||
import getCurrentProjectFile from 'lib/getCurrentProjectFile'
|
||||
|
||||
let mainWindow: BrowserWindow | null = null
|
||||
|
||||
// Check the command line arguments for a project path
|
||||
const args = parseCLIArgs()
|
||||
|
||||
@ -27,12 +29,40 @@ if (require('electron-squirrel-startup')) {
|
||||
app.quit()
|
||||
}
|
||||
|
||||
// Global app listeners
|
||||
// Must be done before ready event
|
||||
registerListeners()
|
||||
const ZOO_STUDIO_PROTOCOL = 'zoo-studio'
|
||||
|
||||
const createWindow = () => {
|
||||
const mainWindow = new BrowserWindow({
|
||||
/// Register our application to handle all "electron-fiddle://" protocols.
|
||||
if (process.defaultApp) {
|
||||
if (process.argv.length >= 2) {
|
||||
app.setAsDefaultProtocolClient(ZOO_STUDIO_PROTOCOL, process.execPath, [
|
||||
path.resolve(process.argv[1]),
|
||||
])
|
||||
}
|
||||
} else {
|
||||
app.setAsDefaultProtocolClient(ZOO_STUDIO_PROTOCOL)
|
||||
}
|
||||
|
||||
// Register custom schemes with privileges.
|
||||
protocol.registerSchemesAsPrivileged([
|
||||
{
|
||||
scheme: ZOO_STUDIO_PROTOCOL,
|
||||
privileges: {
|
||||
standard: true,
|
||||
secure: true,
|
||||
supportFetchAPI: true,
|
||||
corsEnabled: true,
|
||||
allowServiceWorkers: true,
|
||||
codeCache: true,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
// Global app listeners
|
||||
// Must be done before ready event.
|
||||
registerStartupListeners()
|
||||
|
||||
const createWindow = (): BrowserWindow => {
|
||||
const newWindow = new BrowserWindow({
|
||||
autoHideMenuBar: true,
|
||||
show: false,
|
||||
width: 1800,
|
||||
@ -44,13 +74,15 @@ const createWindow = () => {
|
||||
preload: path.join(__dirname, './preload.js'),
|
||||
},
|
||||
icon: path.resolve(process.cwd(), 'assets', 'icon.png'),
|
||||
frame: false,
|
||||
titleBarStyle: 'hiddenInset',
|
||||
})
|
||||
|
||||
// and load the index.html of the app.
|
||||
if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
|
||||
mainWindow.loadURL(MAIN_WINDOW_VITE_DEV_SERVER_URL)
|
||||
newWindow.loadURL(MAIN_WINDOW_VITE_DEV_SERVER_URL)
|
||||
} else {
|
||||
mainWindow.loadFile(
|
||||
newWindow.loadFile(
|
||||
path.join(__dirname, `../renderer/${MAIN_WINDOW_VITE_NAME}/index.html`)
|
||||
)
|
||||
}
|
||||
@ -58,7 +90,9 @@ const createWindow = () => {
|
||||
// Open the DevTools.
|
||||
// mainWindow.webContents.openDevTools()
|
||||
|
||||
mainWindow.show()
|
||||
newWindow.show()
|
||||
|
||||
return newWindow
|
||||
}
|
||||
|
||||
// Quit when all windows are closed, except on macOS. There, it's common
|
||||
@ -73,7 +107,10 @@ app.on('window-all-closed', () => {
|
||||
// This method will be called when Electron has finished
|
||||
// initialization and is ready to create browser windows.
|
||||
// Some APIs can only be used after this event occurs.
|
||||
app.on('ready', createWindow)
|
||||
app.on('ready', (event, data) => {
|
||||
// Create the mainWindow
|
||||
mainWindow = createWindow()
|
||||
})
|
||||
|
||||
// For now there is no good reason to separate these out to another file(s)
|
||||
// There is just not enough code to warrant it and further abstracts everything
|
||||
@ -189,16 +226,13 @@ ipcMain.handle('loadProjectAtStartup', async () => {
|
||||
global['macOpenFiles'] = macOpenFilesEmpty
|
||||
|
||||
// macOS: open-url events that were received before the app is ready
|
||||
const getOpenUrls: string[] = ((global as any).getOpenUrls() ||
|
||||
[]) as string[]
|
||||
const getOpenUrls: string[] = (global as any).getOpenUrls
|
||||
if (getOpenUrls && getOpenUrls.length > 0) {
|
||||
projectPath = getOpenUrls[0] // We only do one project at a
|
||||
}
|
||||
// Reset this so we don't accidentally use it again.
|
||||
// @ts-ignore
|
||||
global['getOpenUrls'] = function () {
|
||||
return []
|
||||
}
|
||||
global['getOpenUrls'] = []
|
||||
|
||||
// Check if we have a project path in the command line arguments
|
||||
// If we do, we will load the project at that path
|
||||
@ -231,7 +265,7 @@ function parseCLIArgs(): minimist.ParsedArgs {
|
||||
return minimist(process.argv, {})
|
||||
}
|
||||
|
||||
function registerListeners() {
|
||||
function registerStartupListeners() {
|
||||
/**
|
||||
* macOS: when someone drops a file to the not-yet running VSCode, the open-file event fires even before
|
||||
* the app-ready event. We listen very early for open-file and remember this upon startup as path to open.
|
||||
@ -240,13 +274,21 @@ function registerListeners() {
|
||||
// @ts-ignore
|
||||
global['macOpenFiles'] = macOpenFiles
|
||||
app.on('open-file', function (event, path) {
|
||||
event.preventDefault()
|
||||
|
||||
macOpenFiles.push(path)
|
||||
// If we have a mainWindow, lets open another window.
|
||||
if (mainWindow) {
|
||||
createWindow()
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
* macOS: react to open-url requests.
|
||||
*/
|
||||
const openUrls: string[] = []
|
||||
// @ts-ignore
|
||||
global['openUrls'] = openUrls
|
||||
const onOpenUrl = function (
|
||||
event: { preventDefault: () => void },
|
||||
url: string
|
||||
@ -254,16 +296,13 @@ function registerListeners() {
|
||||
event.preventDefault()
|
||||
|
||||
openUrls.push(url)
|
||||
// If we have a mainWindow, lets open another window.
|
||||
if (mainWindow) {
|
||||
createWindow()
|
||||
}
|
||||
}
|
||||
|
||||
app.on('will-finish-launching', function () {
|
||||
app.on('open-url', onOpenUrl)
|
||||
})
|
||||
|
||||
// @ts-ignore
|
||||
global['getOpenUrls'] = function () {
|
||||
app.removeListener('open-url', onOpenUrl)
|
||||
|
||||
return openUrls
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user