Last active
July 31, 2020 05:00
-
-
Save aryanshridhar/c7a7c18033aeaca328cfb146f4e37d1a to your computer and use it in GitHub Desktop.
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
const electron = require('electron') | |
const {app, BrowserWindow , Menu , MenuItem , Tray , session, ipcMain ,screen} = electron; | |
const windowState = require('electron-window-state') | |
global['myglob'] = 'A var set in main.js' | |
let tray; | |
function createTray(menu){ | |
tray = new Tray('tp.png') | |
tray.setToolTip = "Tray details" | |
tray.setContextMenu(menu); | |
} | |
function createWindow () { | |
console.log(screen.getAllDisplays()) // Shared api betwwen preocess and main.js | |
let winstate = windowState({ | |
defaultWidth : 800, | |
defaultHeight : 600, | |
}) | |
const win = new BrowserWindow({ | |
width: winstate.width, | |
height: winstate.height, | |
x : winstate.x, | |
y :winstate.y, | |
webPreferences: { | |
nodeIntegration: true, | |
// offscreen : true // used for offscreen rendering (without displaying) | |
}, | |
frame : true, | |
darkTheme : true, | |
backgroundColor : "#2C92F9" | |
}) | |
let mainmenu = new Menu(); | |
let item1 = new MenuItem({ | |
label : "Learning", | |
submenu : [ | |
{ | |
label : "Item1", | |
submenu : [ | |
{ | |
label : "Item1.1", | |
click : () => { | |
console.log("Clicked Item1,1") | |
}, | |
accelerator : "X" | |
}, | |
{ | |
label : "Item1.2", | |
role : "toggleDevTools" | |
}, | |
{ | |
label : "Item1.3" | |
} | |
] | |
}, | |
{ | |
label : "Item2" | |
}, | |
{ | |
label : "Item3" | |
} | |
] | |
}); | |
mainmenu.append(item1); | |
Menu.setApplicationMenu(mainmenu); | |
win.setProgressBar(0.25); | |
createTray(mainmenu); | |
// For context menus | |
win.webContents.on("context-menu" , () => { | |
mainmenu.popup(); | |
}) | |
// Add shortcut , can be done in webContents but by this , pressing button anytime (not neccessarily on electron screen) will log this message out | |
globalShortcut.register('F' , () => { | |
console.log("F pressed"); | |
}) | |
win.loadFile("index.html"); | |
win.webContents.on("did-finish-load" , (e) =>{ | |
dialog.showOpenDialog({ | |
buttonLabel : "Select !!!", | |
defaultPath : app.getPath("desktop"), | |
properties : ['multiSelections' , 'createDirectory' , 'openFile' , 'openDirectory'] | |
}).then(result =>{ | |
console.log(result.filePaths); | |
}) | |
dialog.showSaveDialog({}).then(results => { | |
console.log(results.filePath); | |
}) | |
const answers = ['Yes' , 'No' , 'Maybe'] | |
dialog.showMessageBox(win , { | |
title : "Message Box", | |
message : "Select an option", | |
buttons : answers | |
}).then(results => { | |
console.log(results.response); | |
}) | |
}) | |
let wc = win.webContents; | |
session.defaultSession.on("will-download" , (e,item,content) => { | |
let filename = item.getFilename(); | |
let filesize = item.getTotalBytes(); | |
item.setSavePath(app.getPath("desktop") + `/${filename}`); //downloading without prompt | |
console.log('downloaded !') | |
item.on("updated" , (e,state) =>{ | |
let recieved = item.getReceivedBytes(); | |
if(state = 'progressing' && recieved){ | |
let progress = Math.round((recieved/filesize)*100); | |
console.log('sending .....') | |
win.webContents.send('channelsend' , progress) | |
} | |
}) | |
}) | |
wc.on("context-menu" , (e,param) => { | |
console.log(param.x); | |
console.log(param.y); | |
console.log(param.frameURL); | |
}) | |
wc.on('did-finish-load' , () => { | |
console.log("Did finish load !"); | |
}) | |
wc.on('dom-ready' , () => { | |
console.log("Dom is damn ready"); | |
}) | |
wc.on('new-window' , (e ,url) => { | |
console.log(`Opening the new window for ${url}`); | |
}) | |
wc.on('before-input-event' , (e,obj) => { // Key events | |
console.log(`${obj.key}:${obj.type}`); | |
}) | |
win.loadURL('https://httpbin.org/basic-auth/user/passwrd') | |
wc.on('login' , (e,request,authinfo,callback) =>{ | |
console.log('Logging in'); | |
callback('user' , 'passwd'); | |
}) | |
wc.on('did-navigate' , (e,url,response,message) =>{ | |
console.log(`Navigating to ${url} , Response : ${response} , Message : ${message}`); | |
}) | |
winstate.manage(win) | |
win.once('ready-to-show', () => { // to load the starting screen more gracefully without flickering | |
win.show() | |
}) | |
electron.powerMonitor.on("resume" , () => { | |
console.log("Resuming the application") | |
}) | |
electron.powerMonitor.on("suspend" , () => { | |
console.log("Sleeping the desktop") | |
}) | |
} | |
ipcMain.on('channel1' , (e,msg) => { | |
console.log("Hello") | |
console.log(msg); | |
e.sender.send("channel2" , "This is new message"); | |
}) | |
// 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.whenReady().then(createWindow) | |
// Quit when all windows are closed, except on macOS. There, it's common | |
// for applications and their menu bar to stay active until the user quits | |
// explicitly with Cmd + Q. | |
app.on('window-all-closed', () => { | |
if (process.platform !== 'darwin') { | |
app.quit() | |
} | |
}) | |
app.on('activate', () => { | |
// On macOS 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 (BrowserWindow.getAllWindows().length === 0) { | |
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment