Skip to content

Commit

Permalink
Merge pull request #1475 from imolorhe/add-proxy-settings
Browse files Browse the repository at this point in the history
Added new settings for proxy configuration.
  • Loading branch information
imolorhe committed Mar 19, 2021
2 parents e540e45 + db07fdf commit 32093a6
Show file tree
Hide file tree
Showing 22 changed files with 2,990 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/altair-app/package.json
Expand Up @@ -39,6 +39,7 @@
"@angular/router": "^11.2.4",
"@apollo/client": "^3.2.5",
"@ctrl/ngx-codemirror": "^3.1.3",
"@fontsource/jetbrains-mono": "^4.2.2",
"@ngneat/until-destroy": "^8.0.1",
"@ngrx/effects": "^11.0.1",
"@ngrx/store": "^11.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/altair-app/src/app/services/theme/theme.ts
Expand Up @@ -50,7 +50,7 @@ const theme = deepmerge(foundations, {
},
editor: {
fontFamily: {
default: 'inherit',
default: 'JetBrains Mono',
},
fontSize: foundations.type.fontSize.body,
colors: {
Expand Down
2 changes: 2 additions & 0 deletions packages/altair-app/src/styles.scss
@@ -1,5 +1,7 @@
@import "scss/functions";

@import "~@fontsource/jetbrains-mono/index.css";

@import "~codemirror/lib/codemirror.css";
@import "~codemirror/addon/fold/foldgutter.css";
@import "~codemirror/addon/hint/show-hint.css";
Expand Down
5 changes: 5 additions & 0 deletions packages/altair-app/yarn.lock
Expand Up @@ -1769,6 +1769,11 @@
resolved "https://registry.yarnpkg.com/@emotion/weak-memoize/-/weak-memoize-0.2.5.tgz#8eed982e2ee6f7f4e44c253e12962980791efd46"
integrity sha512-6U71C2Wp7r5XtFtQzYrW5iKFT67OixrSxjI4MptCHzdSVlgabczzqLe0ZSgnub/5Kp4hSbpDB1tMytZY9pwxxA==

"@fontsource/jetbrains-mono@^4.2.2":
version "4.2.2"
resolved "https://registry.yarnpkg.com/@fontsource/jetbrains-mono/-/jetbrains-mono-4.2.2.tgz#925cbcdb4e21f48320f8db3ccb4ca50dff2465a6"
integrity sha512-aCwLIqfZZrZjy+cIx/9hSzxyOcz8YzMbd3VQ8QRkS2MB3+XsXAnMLEkELXLCfKYUkEysRECaq5s7+Qhi1hgZAA==

"@graphql-typed-document-node/core@^3.0.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@graphql-typed-document-node/core/-/core-3.1.0.tgz#0eee6373e11418bfe0b5638f654df7a4ca6a3950"
Expand Down
4 changes: 2 additions & 2 deletions packages/altair-electron/package.json
Expand Up @@ -32,14 +32,14 @@
"@types/jest": "^26.0.3",
"devtron": "^1.4.0",
"dotenv": "^8.1.0",
"electron": "^11.2.2",
"electron": "^12.0.1",
"electron-builder": "^22.9.1",
"electron-notarize": "^1.0.0",
"electron-reloader": "^0.3.0",
"eslint": "^7.3.1",
"jest": "^26.1.0",
"jest-circus": "^26.1.0",
"spectron": "^13.0.0"
"spectron": "^14.0.0"
},
"repository": {
"type": "git",
Expand Down
20 changes: 20 additions & 0 deletions packages/altair-electron/src/app/actions.js
@@ -1,6 +1,7 @@
// @ts-check
const { importBackupData, exportBackupData } = require('../utils/backup');
const { checkForUpdates } = require('../updates');
const { BrowserWindow } = require('electron');

class ActionManager {
/**
Expand Down Expand Up @@ -57,6 +58,25 @@ class ActionManager {
checkForUpdates(menuItem) {
return checkForUpdates(menuItem);
}

showPreferences() {
const prefWindow = new BrowserWindow({
width: 600,
height: 600,
minWidth: 500,
minHeight: 200,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
}
// acceptFirstMouse: true,
// titleBarStyle: 'hidden',
});

// and load the index.html of the app.
prefWindow.loadURL(`file://${__dirname}/../settings/renderer/index.html`);
// return preferences.show();
}
}

module.exports = ActionManager;
43 changes: 41 additions & 2 deletions packages/altair-electron/src/app/index.js
@@ -1,10 +1,11 @@
// @ts-check
const { app, protocol } = require('electron');
const { app, protocol, session } = require('electron');
const { readFile } = require('fs');
const isDev = require('electron-is-dev');
const { setupAutoUpdates } = require('../updates');
const { InMemoryStore } = require('../store');
const WindowManager = require('./window');
const settingsStore = require('../settings/main/store');

class ElectronApp {

Expand All @@ -28,7 +29,45 @@ class ElectronApp {
// 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', () => {
app.on('ready', async () => {

/**
* @type any
*/
const settings = settingsStore.get('settings');
console.log(settings);
if (settings) {
/**
* @type Electron.Config
*/
const proxyConfig = {
mode: 'direct',
};

switch (settings.proxy_setting) {
case 'none':
proxyConfig.mode = 'direct';
break;
case 'autodetect':
proxyConfig.mode = 'auto_detect';
break;
case 'system':
proxyConfig.mode = 'system';
break;
case 'pac':
proxyConfig.mode = 'pac_script';
proxyConfig.pacScript = settings.pac_address;
break;
case 'proxy_server':
proxyConfig.mode = 'fixed_servers';
proxyConfig.proxyRules = `${settings.proxy_host}:${settings.proxy_port}`;
break;
default:
}
await session.defaultSession.setProxy(proxyConfig);
const proxy = await session.defaultSession.resolveProxy('http://localhost');
console.log(proxy, proxyConfig);
}
this.windowManager.createWindow();
if (!isDev) {
setupAutoUpdates();
Expand Down
4 changes: 4 additions & 0 deletions packages/altair-electron/src/app/menu.js
Expand Up @@ -28,6 +28,10 @@ class MenuManager {
accelerator: 'Cmd+,',
click: () => this.actionManager.showSettings(),
},
{
label: 'Desktop settings...',
click: () => this.actionManager.showPreferences(),
},
...(isMac ? [
{ type: 'separator' },
{ role: 'services', submenu: [] },
Expand Down
8 changes: 8 additions & 0 deletions packages/altair-electron/src/app/window.js
Expand Up @@ -4,6 +4,7 @@ const {
protocol,
ipcMain,
session,
app,
} = require('electron');
const path = require('path');
const url = require('url');
Expand All @@ -15,6 +16,7 @@ const { getDistDirectory, renderAltair } = require('altair-static');

const { checkMultipleDataVersions } = require('../utils/check-multi-data-versions');
const { initMainProcessStoreEvents } = require('../electron-store-adapter/main-store-events');
const { initSettingsStoreEvents } = require('../settings/main/events');

const MenuManager = require('./menu');
const ActionManager = require('./actions');
Expand Down Expand Up @@ -96,6 +98,7 @@ class WindowManager {
manageEvents() {

initMainProcessStoreEvents();
initSettingsStoreEvents();

// Prevent the app from navigating away from the app
this.instance.webContents.on('will-navigate', e => e.preventDefault());
Expand Down Expand Up @@ -173,6 +176,11 @@ class WindowManager {
});
});

ipcMain.on('restart-app', () => {
app.relaunch();
app.exit();
});

// Get 'set headers' instruction from app
ipcMain.on('set-headers-sync', (e, headers) => {
this.requestHeaders = {};
Expand Down
11 changes: 11 additions & 0 deletions packages/altair-electron/src/settings/constants.js
@@ -0,0 +1,11 @@
const settingsStoreFileName = 'desktop_settings';

const SETTINGS_STORE_EVENTS = {
GET_SETTINGS_DATA: 'GET_SETTINGS_DATA',
UPDATE_SETTINGS_DATA: 'UPDATE_SETTINGS_DATA',
};

module.exports = {
settingsStoreFileName,
SETTINGS_STORE_EVENTS,
};
17 changes: 17 additions & 0 deletions packages/altair-electron/src/settings/main/events.js
@@ -0,0 +1,17 @@
const { ipcMain } = require('electron');
const settingsStore = require('./store');
const { SETTINGS_STORE_EVENTS } = require('../constants');
const initSettingsStoreEvents = () => {

ipcMain.on(SETTINGS_STORE_EVENTS.GET_SETTINGS_DATA, (e) => {
e.returnValue = settingsStore.get('settings');
});

ipcMain.on(SETTINGS_STORE_EVENTS.UPDATE_SETTINGS_DATA, (e, value) => {
e.returnValue = settingsStore.set('settings', value);
});
};

module.exports = {
initSettingsStoreEvents,
};
8 changes: 8 additions & 0 deletions packages/altair-electron/src/settings/main/store.js
@@ -0,0 +1,8 @@
const ElectronStore = require('electron-store');
const { settingsStoreFileName } = require('../constants');

const store = new ElectronStore({
name: settingsStoreFileName,
});

module.exports = store;
76 changes: 76 additions & 0 deletions packages/altair-electron/src/settings/renderer/app.js
@@ -0,0 +1,76 @@
const ipc = require('electron').ipcRenderer;
const { SETTINGS_STORE_EVENTS } = require('../constants');

/**
* event delegation
* @param {string} selector
* @param {string} eventName
* @param {(e: Event) => void} handler
*/
const on = (selector, eventName, handler) => {
document.addEventListener(eventName, function(e) {
// loop parent nodes from the target to the delegation node
for (let target = e.target; target && target !== this; target = target.parentNode) {
if (target.matches(selector)) {
Reflect.apply(handler, target, [ e ]);
break;
}
}
}, false);
};

const serializeForm = (form) => {
let obj = {};
const data = new FormData(form);
for (let [key, value] of data) {
if (typeof obj[key] === 'undefined') {
obj[key] = value;
} else {
if (!Array.isArray(obj[key])) {
obj[key] = [obj[key]];
}
obj[key].push(value);
}
}
return obj;
};

const hideAllNested = () => {
document.querySelectorAll('.nested').forEach(el => {
el.classList.add('hidden');
});
document.querySelectorAll('input.js-input[type="radio"]:checked').forEach((radioEl) => {
const radioContainer = radioEl.closest('.radio');
const nested = radioContainer.querySelector('.nested');
if (nested) {
nested.classList.remove('hidden');
}
});
};

// Initialize when loaded
document.addEventListener('DOMContentLoaded', function () {
// load settings
const initialData = ipc.sendSync(SETTINGS_STORE_EVENTS.GET_SETTINGS_DATA);
// set selected settings
const networkForm = document.querySelector('.js-network-form');
Object.keys(initialData).forEach((key) => {
networkForm[key].value = initialData[key];
});
// hide nested
hideAllNested();

on('input.js-input[type="radio"]', 'click', () => {
hideAllNested();
});

on('.js-network-form', 'submit', (e) => {
e.preventDefault();
const formData = serializeForm(e.target);

console.log('submitted.', formData);
ipc.sendSync(SETTINGS_STORE_EVENTS.UPDATE_SETTINGS_DATA, formData);
ipc.sendSync('restart-app');
});
});

0 comments on commit 32093a6

Please sign in to comment.