-
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
277 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,3 @@ node_modules | |
# nextron | ||
bin | ||
workspace | ||
|
||
# examples | ||
examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,3 @@ node_modules | |
# nextron | ||
bin | ||
workspace | ||
|
||
# examples | ||
examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
import { app } from 'electron'; | ||
import serve from 'electron-serve'; | ||
import { createWindow } from './helpers'; | ||
import { app } from 'electron' | ||
import serve from 'electron-serve' | ||
import { createWindow } from './helpers' | ||
|
||
const isProd = process.env.NODE_ENV === 'production'; | ||
const isProd = process.env.NODE_ENV === 'production' | ||
|
||
if (isProd) { | ||
serve({ directory: 'app' }); | ||
serve({ directory: 'app' }) | ||
} else { | ||
app.setPath('userData', `${app.getPath('userData')} (development)`); | ||
app.setPath('userData', `${app.getPath('userData')} (development)`) | ||
} | ||
|
||
(async () => { | ||
await app.whenReady(); | ||
;(async () => { | ||
await app.whenReady() | ||
|
||
const mainWindow = createWindow('main', { | ||
width: 1000, | ||
height: 600, | ||
}); | ||
}) | ||
|
||
if (isProd) { | ||
await mainWindow.loadURL('app://./home.html'); | ||
await mainWindow.loadURL('app://./home') | ||
} else { | ||
const port = process.argv[2]; | ||
await mainWindow.loadURL(`http://localhost:${port}/home`); | ||
mainWindow.webContents.openDevTools(); | ||
const port = process.argv[2] | ||
await mainWindow.loadURL(`http://localhost:${port}/home`) | ||
mainWindow.webContents.openDevTools() | ||
} | ||
})(); | ||
})() | ||
|
||
app.on('window-all-closed', () => { | ||
app.quit(); | ||
}); | ||
app.quit() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,78 @@ | ||
import { | ||
screen, | ||
BrowserWindow, | ||
} from 'electron'; | ||
import Store from 'electron-store'; | ||
import { screen, BrowserWindow } from 'electron' | ||
import Store from 'electron-store' | ||
|
||
export default function createWindow(windowName, options) { | ||
const key = 'window-state'; | ||
const name = `window-state-${windowName}`; | ||
const store = new Store({ name }); | ||
export const createWindow = (windowName, options) => { | ||
const key = 'window-state' | ||
const name = `window-state-${windowName}` | ||
const store = new Store({ name }) | ||
const defaultSize = { | ||
width: options.width, | ||
height: options.height, | ||
}; | ||
let state = {}; | ||
let win; | ||
} | ||
let state = {} | ||
|
||
const restore = () => store.get(key, defaultSize); | ||
const restore = () => store.get(key, defaultSize) | ||
|
||
const getCurrentPosition = () => { | ||
const position = win.getPosition(); | ||
const size = win.getSize(); | ||
const position = win.getPosition() | ||
const size = win.getSize() | ||
return { | ||
x: position[0], | ||
y: position[1], | ||
width: size[0], | ||
height: size[1], | ||
}; | ||
}; | ||
} | ||
} | ||
|
||
const windowWithinBounds = (windowState, bounds) => { | ||
return ( | ||
windowState.x >= bounds.x && | ||
windowState.y >= bounds.y && | ||
windowState.x + windowState.width <= bounds.x + bounds.width && | ||
windowState.y + windowState.height <= bounds.y + bounds.height | ||
); | ||
}; | ||
) | ||
} | ||
|
||
const resetToDefaults = () => { | ||
const bounds = screen.getPrimaryDisplay().bounds; | ||
const bounds = screen.getPrimaryDisplay().bounds | ||
return Object.assign({}, defaultSize, { | ||
x: (bounds.width - defaultSize.width) / 2, | ||
y: (bounds.height - defaultSize.height) / 2 | ||
}); | ||
}; | ||
y: (bounds.height - defaultSize.height) / 2, | ||
}) | ||
} | ||
|
||
const ensureVisibleOnSomeDisplay = (windowState) => { | ||
const visible = screen.getAllDisplays().some(display => { | ||
const visible = screen.getAllDisplays().some((display) => { | ||
return windowWithinBounds(windowState, display.bounds) | ||
}); | ||
}) | ||
if (!visible) { | ||
// Window is partially or fully not visible now. | ||
// Reset it to safe defaults. | ||
return resetToDefaults(); | ||
return resetToDefaults() | ||
} | ||
return windowState; | ||
}; | ||
return windowState | ||
} | ||
|
||
const saveState = () => { | ||
if (!win.isMinimized() && !win.isMaximized()) { | ||
Object.assign(state, getCurrentPosition()); | ||
Object.assign(state, getCurrentPosition()) | ||
} | ||
store.set(key, state); | ||
}; | ||
store.set(key, state) | ||
} | ||
|
||
state = ensureVisibleOnSomeDisplay(restore()); | ||
state = ensureVisibleOnSomeDisplay(restore()) | ||
|
||
win = new BrowserWindow({ | ||
const win = new BrowserWindow({ | ||
...state, | ||
...options, | ||
webPreferences: { | ||
nodeIntegration: true, | ||
contextIsolation: false, | ||
...options.webPreferences, | ||
}, | ||
}); | ||
}) | ||
|
||
win.on('close', saveState); | ||
win.on('close', saveState) | ||
|
||
return win; | ||
}; | ||
return win | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1 @@ | ||
import createWindow from './create-window'; | ||
|
||
export { | ||
createWindow, | ||
}; | ||
export * from './create-window' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
import { app } from 'electron'; | ||
import serve from 'electron-serve'; | ||
import { createWindow } from './helpers'; | ||
import { app } from 'electron' | ||
import serve from 'electron-serve' | ||
import { createWindow } from './helpers' | ||
|
||
const isProd: boolean = process.env.NODE_ENV === 'production'; | ||
const isProd: boolean = process.env.NODE_ENV === 'production' | ||
|
||
if (isProd) { | ||
serve({ directory: 'app' }); | ||
serve({ directory: 'app' }) | ||
} else { | ||
app.setPath('userData', `${app.getPath('userData')} (development)`); | ||
app.setPath('userData', `${app.getPath('userData')} (development)`) | ||
} | ||
|
||
(async () => { | ||
await app.whenReady(); | ||
;(async () => { | ||
await app.whenReady() | ||
|
||
const mainWindow = createWindow('main', { | ||
width: 1000, | ||
height: 600, | ||
}); | ||
}) | ||
|
||
if (isProd) { | ||
await mainWindow.loadURL('app://./home.html'); | ||
await mainWindow.loadURL('app://./home') | ||
} else { | ||
const port = process.argv[2]; | ||
await mainWindow.loadURL(`http://localhost:${port}/home`); | ||
mainWindow.webContents.openDevTools(); | ||
const port = process.argv[2] | ||
await mainWindow.loadURL(`http://localhost:${port}/home`) | ||
mainWindow.webContents.openDevTools() | ||
} | ||
})(); | ||
})() | ||
|
||
app.on('window-all-closed', () => { | ||
app.quit(); | ||
}); | ||
app.quit() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.