Cat/main.js

227 lines
6.1 KiB
JavaScript

const electron = require('electron')
const ipcMain = require('electron').ipcMain;
const settings = require('electron-settings');
// Module to control application life.
const app = electron.app
const Menu = electron.Menu
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow
const path = require('path')
const url = require('url')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
let defaults = {
width: 800,
height: 600,
maximized: false
};
function createWindow () {
// Load in settings.
for (let def in defaults) {
if (!settings.has(def)) {
settings.set(def, defaults[def]);
}
}
// Create the main menu.
createMenu()
// Create the browser window.
let options = {
backgroundColor: '2e2c29',
title: 'Cat',
width: settings.get('width'),
height: settings.get('height')
};
if (settings.has('x')) options.x = settings.get('x');
if (settings.has('y')) options.y = settings.get('y');
if (options.x !== 'undefined' && options.y === 'undefined') options.y = options.x;
if (options.y !== 'undefined' && options.x === 'undefined') options.x = options.y;
mainWindow = new BrowserWindow(options);
mainWindow.on('resize', e => {
var size = mainWindow.getSize();
settings.set('width', size[0]);
settings.set('height', size[1]);
});
mainWindow.on('move', e => {
var position = mainWindow.getPosition();
settings.set('x', position[0]);
settings.set('y', position[1]);
});
mainWindow.on('maximize', e => {
settings.set('maximized', true);
});
mainWindow.on('unmaximize', e => {
settings.set('maximized', false);
});
if (settings.get('maximized') === true) {
mainWindow.maximize();
}
// and load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'main.html'),
protocol: 'file:',
slashes: true
}))
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}
// 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)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
// Here begins the menu stuff
function createMenu() {
const template = [
{
label: 'File',
submenu: [
{label: 'New', accelerator: 'CmdOrCtrl+N', click() {
mainWindow.webContents.send('menu', {type: 'new'});
}},
{label: 'Open', accelerator: 'CmdOrCtrl+O', click() {
mainWindow.webContents.send('menu', {type: 'open'});
}},
{label: 'Open Recent', submenu: [
{label: 'A file'}
]},
{type: 'separator'},
{label: 'Save', accelerator: 'CmdOrCtrl+S', click() {
mainWindow.webContents.send('menu', {type: 'save'});
}},
{label: 'Save As', accelerator: 'Shift+CmdOrCtrl+S', click() {
mainWindow.webContents.send('menu', {type: 'save-as'});
}},
{type: 'separator'},
{role: 'quit'}
]
},
{
label: 'Edit',
submenu: [
{role: 'undo'},
{role: 'redo'},
{type: 'separator'},
{role: 'cut'},
{role: 'copy'},
{role: 'paste'},
{role: 'pasteandmatchstyle'},
{role: 'delete'},
{role: 'selectall'}
]
},
{
label: 'View',
submenu: [
{role: 'reload'},
{role: 'forcereload'},
{role: 'toggledevtools'},
{type: 'separator'},
{role: 'resetzoom'},
{role: 'zoomin'},
{role: 'zoomout'},
{type: 'separator'},
{role: 'togglefullscreen'}
]
},
{
role: 'window',
submenu: [
{role: 'minimize'},
{label: 'Close', accelerator: 'CmdOrCtrl+W', click() {
mainWindow.webContents.send('menu', {type: 'close'});
}}
]
},
{
role: 'help',
submenu: [
{
label: 'Learn More',
click () { require('electron').shell.openExternal('https://electron.atom.io') }
}
]
}
]
if (process.platform === 'darwin') {
template.unshift({
label: app.getName(),
submenu: [
{role: 'about'},
{type: 'separator'},
{role: 'services', submenu: []},
{type: 'separator'},
{role: 'hide'},
{role: 'hideothers'},
{role: 'unhide'},
{type: 'separator'},
{role: 'quit'}
]
})
// Edit menu
template[1].submenu.push(
{type: 'separator'},
{
label: 'Speech',
submenu: [
{role: 'startspeaking'},
{role: 'stopspeaking'}
]
}
)
// Window menu
template[3].submenu = [
{role: 'close'},
{role: 'minimize'},
{role: 'zoom'},
{type: 'separator'},
{role: 'front'}
]
}
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
ipcMain.on('open-file', (evt, arg) => {
});
}