|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +// you may not use this file except in compliance with the License. |
| 3 | +// You may obtain a copy of the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | + |
| 13 | +const { app, BrowserWindow } = require('electron') |
| 14 | +let rclnodejs = require("rclnodejs"); |
| 15 | +const { ipcMain } = require('electron'); |
| 16 | + |
| 17 | +function createWindow() { |
| 18 | + // Create the browser window. |
| 19 | + const mainWindow = new BrowserWindow({ |
| 20 | + width: 800, |
| 21 | + height: 600, |
| 22 | + webPreferences: { |
| 23 | + // Add the following two lines in order to use require() in renderer, see details |
| 24 | + // https://stackoverflow.com/questions/44391448/electron-require-is-not-defined |
| 25 | + nodeIntegration: true, |
| 26 | + contextIsolation: false, |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + mainWindow.loadFile('index.html'); |
| 31 | +} |
| 32 | + |
| 33 | +// This method will be called when Electron has finished |
| 34 | +// initialization and is ready to create browser windows. |
| 35 | +// Some APIs can only be used after this event occurs. |
| 36 | +app.whenReady().then(() => { |
| 37 | + createWindow(); |
| 38 | + rclnodejs.init().then(() => { |
| 39 | + let sender = null; |
| 40 | + const node = rclnodejs.createNode('publisher_example_node'); |
| 41 | + node.createSubscription('std_msgs/msg/String', 'topic', (msg) => { |
| 42 | + if (sender) { |
| 43 | + sender.send('topic-received', msg.data); |
| 44 | + } |
| 45 | + }); |
| 46 | + const publisher = node.createPublisher('std_msgs/msg/String', 'topic'); |
| 47 | + ipcMain.on('publish-topic', (event, topic) => { |
| 48 | + publisher.publish(topic); |
| 49 | + sender = event.sender; |
| 50 | + }); |
| 51 | + rclnodejs.spin(node); |
| 52 | + }); |
| 53 | + |
| 54 | + app.on('activate', function () { |
| 55 | + // On macOS it's common to re-create a window in the app when the |
| 56 | + // dock icon is clicked and there are no other windows open. |
| 57 | + if (BrowserWindow.getAllWindows().length === 0) createWindow(); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +// Quit when all windows are closed, except on macOS. There, it's common |
| 62 | +// for applications and their menu bar to stay active until the user quits |
| 63 | +// explicitly with Cmd + Q. |
| 64 | +app.on('window-all-closed', function () { |
| 65 | + if (process.platform !== 'darwin') app.quit() |
| 66 | +}); |
0 commit comments