Comments (12)
Yep! I've got some updated Electron videos in planning. Thanks!
from letswritecode.
I tried changing ipc to ipc.Main in your code with all the updated module although it didn't work.
main.js
const electron = require('electron');
const app = require('electron').app;
const remote = require('electron').remote;
const ipcMain = require('electron').ipcMain;
const menu = require('electron').Menu;
const menu = Menu.buildFromTemplate([
{
label: 'Electron',
submenu: [
{
label: 'Prefs',
click: function () {
ipcMain.send('toggle-prefs')
}
}
]
}
])
Menu.setApplicationMenu(menu)
app.js
const electron = require('electron');
const app = require('electron').app;
var ipcMain = require('electron').ipcMain
app.on('ready', function () {
var mainWindow = new electron.BrowserWindow({
width: 800,
height: 600
})
mainWindow.loadUrl('file://' + __dirname + '/main.html')
mainWindow.openDevTools()
var prefsWindow = new electron.BrowserWindow({
width: 400,
height: 400,
show: false
})
prefsWindow.loadUrl('file://' + __dirname + '/prefs.html')
ipcMain.on('toggle-prefs', function () {
if (prefsWindow.isVisible())
prefsWindow.hide()
else
prefsWindow.show()
})
})
Still getiing an error Can not read property send of undefined at click
What other changes I should do here?
from letswritecode.
Try this instead:
const {remote, ipcRenderer} = require('electron')
const {Menu} = remote
const menu = Menu.buildFromTemplate([
{
label: 'Electron',
submenu: [
{
label: 'Prefs',
click: function () {
ipcRenderer.send('toggle-prefs')
}
}
]
}
])
Menu.setApplicationMenu(menu)
main.js
is on the renderer process because main.html
does: <script>require('./main.js')</script>
from letswritecode.
Thanks man. It's working perfectly now. Should I send a PR with updated code?
from letswritecode.
Your code works fine in menu. But when I use this in tray it didn't work. I got this -
TypeError: Cannot match against 'undefined' or 'null'
main.js
'use strict';
const path = require('path');
const electron = require('electron');
const app = require('electron').app;
const {remote, ipcRenderer, shell} = require('electron')
const {Menu} = remote
let tray = null;
exports.create = win => {
if (process.platform === 'darwin' || tray) {
return;
}
const iconPath = path.join(__dirname, 'resources', 'Icon.png');
const toggleWin = () => {
if (win.isVisible()) {
win.hide();
} else {
win.show();
}
};
const reload = () => {
win.reload();
};
const contextMenu = electron.Menu.buildFromTemplate([
{
label: 'Toggle',
click() {
toggleWin();
}
},
{
label: 'About',
click() {
ipcRenderer.send('About');
}
}
]);
tray = new electron.Tray(iconPath);
tray.setToolTip(`${app.getName()}`);
tray.setContextMenu(contextMenu);
tray.on('click', toggleWin);
};
app.js
'use strict';
const path = require('path');
const electron = require('electron');
const app = require('electron').app;
var ipcMain = require('electron').ipcMain
const tray = require('./tray');
let mainWindow;
let aboutWindow;
function onClosed() {
mainWindow = null;
aboutWindow = null;
}
function createMainWindow() {
const win = new electron.BrowserWindow({
width: 1000,
height: 600,
icon: process.platform === 'linux' && path.join(__dirname, 'resources/Icon.png')
});
win.loadURL('file://' + __dirname + '/main.html');
win.on('closed', onClosed);
return win;
}
function createAboutWindow() {
const abouturl = new electron.BrowserWindow({
width: 400,
height: 400,
show: false
})
abouturl.loadURL('file://' + __dirname + '/about.html');
abouturl.on('closed', onClosed);
return abouturl;
}
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (!mainWindow) {
mainWindow = createMainWindow();
}
});
app.on('ready', () => {
mainWindow = createMainWindow();
tray.create(mainWindow);
aboutWindow = createAboutWindow();
ipcMain.on('About', function(){
aboutWindow.show();
});
});
from letswritecode.
Should I send a PR with updated code?
Thanks! But I'll have some updated videos coming out soon. So I'll leave the code examples for those videos for electron <= 1.0.0
.
For debugging, add win.openDevTools()
to your window and then add debugger
statements to places where the code isn't working. That will pause the app and let you inspect the state of the app at that time.
Also be careful doing const tray = require('./tray');
as <script>require('./tray')</script>
will run that file in the renderer process and require('./tray')
from app.js
will be ran in the main process.
from letswritecode.
Thanks. Waiting for your videos
from letswritecode.
I 've download and set git repo on link here https://www.youtube.com/watch?v=K-H2amwQ_pU
https://github.com/shama/letswritecode/tree/master/multi-window-electron-desktop-app
But I can't to run this code...
I have Mac OS.
from letswritecode.
Now I try t fix with : npm install electron --save-dev
from letswritecode.
App threw an error when running [SyntaxError: /Users/svetlanailina/Sites/letswritecode/multi-window-electron-desktop-app/app.js:1
(function (exports, require, module, __filename, __dirname) { const {app, BrowserWindow, ipcMain} = require('electron')
^
Unexpected token {]
from letswritecode.
Ok after fixing npm install electron --save-dev I can see a program, but i don't see menu, only
text "Main"
from letswritecode.
Mistakes
`Uncaught ReferenceError: require is not defined
at main.html:7
security-warnings.ts:179 Electron Security Warning (Insecure Content-Security-Policy) This renderer process has either no Content Security
Policy set or a policy with "unsafe-eval" enabled. This exposes users of
this app to unnecessary security risks.
For more information and help, consult
https://electronjs.org/docs/tutorial/security.
This warning will not show up
once the app is packaged.
(anonymous) @ security-warnings.ts:179`
from letswritecode.
Related Issues (20)
- cannot connect from other device HOT 1
- package.json : no more "package" in scripts HOT 1
- HTML Required HOT 1
- You may need an appropriate loader to handle this file type. HOT 1
- using-npm-on-the-client-side HOT 1
- Nested Loops
- Video streaming Issue. Cannot get Stream of the other peer HOT 4
- Creating Electron apps: screencat HOT 3
- How to make promise from web-socket API? HOT 3
- Why does my node_modules folder have so much more stuff than yours? HOT 1
- how to get id to connect ?
- client info not appearing in Text Area?
- window.URL.createObjectURL is deprecated
- why not put in seperate repos i dont want ll them files just for 1 project! HOT 1
- Electron apps outdated HOT 7
- chrome extension authentication to web app
- add gitter chat room HOT 2
- The issue for 'You may need an appropriate loader to handle this file type.' HOT 12
- An error at bundle.js
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from letswritecode.