Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Utils for electron-window-manager
# -------------------------------------------------
# Modules
#
path = require 'path'
url = require 'url'
fs = require 'fs'
Menu = require('electron').Menu
# -------------------------------------------------
# Exports
#
# Open a new BrowserWindow
module.exports.openWindow = (winmgr, win, parent = null) ->
if parent isnt null
win.setup.modal = true
win.setup.parent = parent
# Re-create the window object if necessary
win.create() if win.object is null
# If the main window is closed, exit the process
if win.name is 'main' then win.object.on 'closed', -> process.exit()
win.onReady true, -> win.object.show() # Open window as soon as site was loaded
# https://github.com/electron/electron/blob/master/docs/api/browser-window.md#showing-window-gracefully
# Window base path
winUrl = win.setup.url
basePath = path.join __dirname, '..', winUrl
menuPath = path.join basePath, "menu.coffee"
# Add menu (if defined)
if fs.existsSync menuPath
menuTemplate = require(menuPath)(winmgr) # load the menu template
menu = Menu.buildFromTemplate menuTemplate # compile the template
win.object.setMenu menu # apply the compiled menu
# Format file path
file = url.format
pathname: path.join basePath, "index.html"
protocol: 'file:'
slashes: true
# Load the file
win.loadURL file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment