Communication with electron, Ref. #13

This commit is contained in:
ziajka
2018-04-06 13:15:23 +02:00
parent 37acae2697
commit 0ee66da007
5 changed files with 56 additions and 21 deletions

43
main.js
View File

@ -1,22 +1,30 @@
const electron = require('electron'); const electron = require('electron');
var fs = require('fs'); const fs = require('fs');
// Module to control application life.
const app = electron.app; const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow; const BrowserWindow = electron.BrowserWindow;
const path = require('path'); const path = require('path');
const url = require('url'); const url = require('url');
const yargs = require('yargs');
require('./sentry'); require('./sentry');
// Keep a global reference of the window object, if you don't, the window will // 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. // be closed automatically when the JavaScript object is garbage collected.
let mainWindow; let mainWindow;
let serverProc = null; let serverProc = null;
let isWin = /^win/.test(process.platform); let isWin = /^win/.test(process.platform);
let isDev = false;
const argv = yargs
.describe('m', 'Maximizes window on startup.')
.boolean('m')
.describe('e', 'Environment, `dev` for developer mode and when not specified then production mode. ')
.choices('e', ['dev', null])
.argv;
if (argv.e == 'dev') {
isDev = true;
}
const createServerProc = () => { const createServerProc = () => {
@ -64,15 +72,26 @@ function createWindow () {
mainWindow = new BrowserWindow({width: 800, height: 600}); mainWindow = new BrowserWindow({width: 800, height: 600});
// and load the index.html of the app. // and load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'dist/index.html'), if(isDev) {
protocol: 'file:', mainWindow.loadURL('http://localhost:4200/');
slashes: true mainWindow.webContents.openDevTools();
})); }
else {
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'dist/index.html'),
protocol: 'file:',
slashes: true
}));
}
// Open the DevTools. // Open the DevTools.
// mainWindow.webContents.openDevTools(); // mainWindow.webContents.openDevTools();
if(argv.m) {
mainWindow.maximize();
}
// Emitted when the window is closed. // Emitted when the window is closed.
mainWindow.on('closed', function () { mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows // Dereference the window object, usually you would store windows
@ -109,3 +128,5 @@ app.on('activate', function () {
// In this file you can include the rest of your app's specific main process // 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. // code. You can also put them in separate files and require them here.

View File

@ -16,7 +16,8 @@
"test": "ng test", "test": "ng test",
"lint": "ng lint", "lint": "ng lint",
"e2e": "ng e2e", "e2e": "ng e2e",
"electrondev": "concurrently -k \"yarn startforelectron\" \"electron .\"", "electron": "electron .",
"electrondev": "concurrently -k \"yarn startforelectron\" \"electron . -e dev\"",
"distlinux": "yarn buildforelectron && electron-builder --linux --x64", "distlinux": "yarn buildforelectron && electron-builder --linux --x64",
"distwin": "yarn buildforelectron && electron-builder --win --x64", "distwin": "yarn buildforelectron && electron-builder --win --x64",
"distmac": "yarn buildforelectron && electron-builder --mac --x64", "distmac": "yarn buildforelectron && electron-builder --mac --x64",
@ -47,6 +48,7 @@
"npm-check-updates": "^2.13.0", "npm-check-updates": "^2.13.0",
"raven-js": "^3.24.0", "raven-js": "^3.24.0",
"rxjs": "^5.4.1", "rxjs": "^5.4.1",
"yargs": "^11.0.0",
"zone.js": "^0.8.20" "zone.js": "^0.8.20"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,6 +1,8 @@
const { SentryClient } = require('@sentry/electron'); const { SentryClient } = require('@sentry/electron');
var fs = require('fs'); const fs = require('fs');
const { ipcMain } = require('electron');
let crashReportsEnabled = true;
const DSN = const DSN =
'https://cb7b474b2e874afb8e400c47d1452ecc:7876224cbff543d992cb0ac4021962f8@sentry.io/1040940'; 'https://cb7b474b2e874afb8e400c47d1452ecc:7876224cbff543d992cb0ac4021962f8@sentry.io/1040940';
@ -8,8 +10,17 @@ const isDev = () => {
return fs.existsSync('.git'); return fs.existsSync('.git');
}; };
if (!isDev()) { const shouldSendCallback = () => {
SentryClient.create({ return !isDev() && crashReportsEnabled;
dsn: DSN };
});
}
ipcMain.on('settings.changed', function (event, settings) {
crashReportsEnabled = settings.crash_reports;
});
SentryClient.create({
dsn: DSN,
shouldSendCallback: shouldSendCallback
});

View File

@ -25,6 +25,7 @@ export class AppComponent implements OnInit {
ngOnInit(): void { ngOnInit(): void {
if (this.electronService.isElectronApp) { if (this.electronService.isElectronApp) {
this.settingsService.subscribe((settings) => { this.settingsService.subscribe((settings) => {
console.log("settings changed on angular");
this.electronService.ipcRenderer.send('settings.changed', settings); this.electronService.ipcRenderer.send('settings.changed', settings);
}); });
} }

View File

@ -1,6 +1,7 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { PersistenceService, StorageType } from "angular-persistence"; import { PersistenceService, StorageType } from "angular-persistence";
import { Subject } from "rxjs/Subject"; import { Subject } from "rxjs/Subject";
import { BehaviorSubject } from "rxjs/BehaviorSubject";
export interface Settings { export interface Settings {
@ -14,11 +15,10 @@ export class SettingsService {
'crash_reports': true 'crash_reports': true
}; };
private settingsSubject: Subject<Settings>; private settingsSubject: BehaviorSubject<Settings>;
constructor(private persistenceService: PersistenceService) { constructor(private persistenceService: PersistenceService) {
this.settingsSubject = new Subject<Settings>(); this.settingsSubject = new BehaviorSubject<Settings>(this.getAll());
this.settingsSubject.next(this.getAll());
} }
get<T>(key: string) { get<T>(key: string) {